Пример #1
0
void parseMaterials(GeometryConverter *cvt, std::ostream &os, const fs::path &texturesDir, 
		const fs::path &mtlFileName, std::set<std::string> &mtlList) {
	SLog(EInfo, "Loading OBJ materials from \"%s\" ..", mtlFileName.file_string().c_str());
	fs::ifstream is(mtlFileName);
	if (is.bad() || is.fail())
		SLog(EError, "Unexpected I/O error while accessing material file '%s'!", 
			mtlFileName.file_string().c_str());
	std::string buf, line;
	std::string mtlName;
	Spectrum diffuse(0.0f);
	std::string diffuseMap, maskMap;

	while (is >> buf) {
		if (buf == "newmtl") {
			mtlList.insert(mtlName);
			addMaterial(cvt, os, mtlName, texturesDir, diffuse, diffuseMap, maskMap);
			std::getline(is, line);
			mtlName = trim(line.substr(1, line.length()-1));
			diffuse = Spectrum(0.0f);
			diffuseMap = "";
			maskMap = "";
		} else if (buf == "Kd") {
			Float r, g, b;
			is >> r >> g >> b;
			if (cvt->m_srgb)
				diffuse.fromSRGB(r, g, b);
			else
				diffuse.fromLinearRGB(r, g, b);
		} else if (buf == "map_Kd") {
Пример #2
0
void
FileCollectionTest::setUp()
{
    fs::remove_all(testFolder);

    fs::create_directory(folderTest);
    fs::create_directory(folderTest/testDirectory);
    {
        ofstream test0(fileTest0.file_string().c_str());
        ofstream test1(fileTest1.file_string().c_str());
        ofstream test2(fileTest2.file_string().c_str());
        ofstream test3(fileTest3.file_string().c_str());
    }

    struct ::utimbuf times;

    times.actime = time(NULL);
    times.modtime = times.actime - 8;
    utime( fileTest0.file_string().c_str(), &times );

    times.actime -= 1;
    times.modtime += 1;
    utime( fileTest1.file_string().c_str(), &times );

    times.actime -= 1;
    times.modtime += 1;
    utime( fileTest2.file_string().c_str(), &times );

    times.actime -= 1;
    times.modtime += 1;
    utime( fileTest3.file_string().c_str(), &times );
}
Пример #3
0
void
FileOperationMetaTest::testFileOperationSubNone(
        auto_ptr<FileOperationInterface> & oper, const fs::path & fileMeta,
        const fs::path & fileEntity,
        const string & tapeName)
{
    string tape;
    char buffer[1024] = "hello,world!\n";
    char bufferRead[sizeof(buffer)];
    size_t size;
    struct stat stat;

    errno = 0;
    CPPUNIT_ASSERT( false == oper->GetStat(stat) );
    CPPUNIT_ASSERT_ERRNO_( ENOENT == errno);
    errno = 0;
    CPPUNIT_ASSERT( false == oper->Read(0,bufferRead,sizeof(bufferRead),size) );
    CPPUNIT_ASSERT_ERRNO_( EBADF == errno);
    errno = 0;
    CPPUNIT_ASSERT( false == oper->Write(0,buffer,sizeof(buffer),size) );
    CPPUNIT_ASSERT_ERRNO_( EBADF == errno);
    CPPUNIT_ASSERT( ! fs::exists(fileMeta) );
    CPPUNIT_ASSERT( ! fs::exists(fileEntity) );

    errno = 0;
    CPPUNIT_ASSERT( false == oper->OpenFile(O_RDWR) );
    CPPUNIT_ASSERT_ERRNO_( ENOENT == errno);
    CPPUNIT_ASSERT_MESSAGE( fileEntity.file_string(),
            true == oper->CreateFile(O_RDWR,0644,false) );
    CPPUNIT_ASSERT( fs::exists(fileMeta) );
    CPPUNIT_ASSERT( fs::exists(fileEntity) );
    CPPUNIT_ASSERT( true == oper->Delete() );
    CPPUNIT_ASSERT( ! fs::exists(fileMeta) );
    CPPUNIT_ASSERT( ! fs::exists(fileEntity) );

    CPPUNIT_ASSERT( true == oper->GetTape(tape) );
    CPPUNIT_ASSERT( tape == tapeName );
}
Пример #4
0
bool md5sum(const fs::path &path, md5digest &digest)
{
    try {
        Exiv2::FileIo io(path.file_string());
        if (io.open() != 0)
            return false;
        Exiv2::IoCloser closer(io);

        Exiv2::byte buff[4096];
        MD5_CTX context;
        MD5Init(&context);

        long read_count = io.read(buff, 4096);
        while(read_count) {
            MD5Update(&context, buff, read_count);
            read_count = io.read(buff, 4096);
        }
        MD5Final(digest, &context);
        return true;
    }
    catch (std::exception& ) {
        return false;
    }
}
Пример #5
0
void
FileOperationMetaTest::testFileOperationSubCreateTruncate(
        auto_ptr<MetaManager> & meta,
        const fs::path & path,
        auto_ptr<FileOperationInterface> & oper,
        const fs::path & fileMeta,
        const fs::path & fileEntity,
        const string & tapeName)
{
    string tape;
    auto_ptr<Inode> inode;
    char buffer[1024] = "hello,world!\n";
    char bufferRead[sizeof(buffer)];
    size_t size;
    struct stat stat;

    CPPUNIT_ASSERT( ! fs::exists(fileMeta) );
    CPPUNIT_ASSERT( ! fs::exists(fileEntity) );

    errno = 0;
    CPPUNIT_ASSERT( false == oper->GetStat(stat) );
    CPPUNIT_ASSERT_ERRNO_( ENOENT == errno);
    errno = 0;
    CPPUNIT_ASSERT( false == oper->Read(0,bufferRead,sizeof(bufferRead),size) );
    CPPUNIT_ASSERT_ERRNO_( EBADF == errno);
    errno = 0;
    CPPUNIT_ASSERT( false == oper->Write(0,buffer,sizeof(buffer),size) );
    CPPUNIT_ASSERT_ERRNO_( EBADF == errno);
    CPPUNIT_ASSERT( ! fs::exists(fileMeta) );
    CPPUNIT_ASSERT( ! fs::exists(fileEntity) );

    errno = 0;
    CPPUNIT_ASSERT( false == oper->OpenFile(O_RDWR) );
    CPPUNIT_ASSERT_ERRNO_( ENOENT == errno);
    CPPUNIT_ASSERT( ! fs::exists(fileMeta) );
    CPPUNIT_ASSERT( ! fs::exists(fileEntity) );

    CPPUNIT_ASSERT( true == oper->CreateFile(O_RDWR,0644,false) );
    CPPUNIT_ASSERT( fs::exists(fileMeta) );
    CPPUNIT_ASSERT_MESSAGE( fileEntity.file_string(), fs::exists(fileEntity) );
    inode.reset(meta->GetInode(path));
    errno = 0;
    CPPUNIT_ASSERT( false == oper->CreateFile(O_RDWR,0644,false) );
    CPPUNIT_ASSERT_ERRNO_( EEXIST == errno);

    tape.clear();
    CPPUNIT_ASSERT( true == oper->GetTape(tape) );
    CPPUNIT_ASSERT( tapeName == tape );
    tape.clear();
    CPPUNIT_ASSERT( true == inode->GetTape(tape) );
    CPPUNIT_ASSERT( tapeName == tape );

    CPPUNIT_ASSERT( true == oper->Write(0,buffer,sizeof(buffer),size) );
    CPPUNIT_ASSERT( fs::exists(fileMeta) );
    CPPUNIT_ASSERT( fs::exists(fileEntity) );
    CPPUNIT_ASSERT( true == oper->GetStat(stat) );
    CPPUNIT_ASSERT( sizeof(buffer) == stat.st_size );
    CPPUNIT_ASSERT( true == oper->Truncate( sizeof(buffer) - 5 ) );
    CPPUNIT_ASSERT( true == oper->GetStat(stat) );
    CPPUNIT_ASSERT( sizeof(buffer) - 5 == stat.st_size );
    CPPUNIT_ASSERT( true == oper->Truncate( sizeof(buffer) + 5 ) );
    CPPUNIT_ASSERT( true == oper->GetStat(stat) );
    CPPUNIT_ASSERT( sizeof(buffer) + 5 == stat.st_size );
    CPPUNIT_ASSERT( true == oper->Truncate( sizeof(buffer) ) );
    CPPUNIT_ASSERT( true == oper->GetStat(stat) );
    CPPUNIT_ASSERT( sizeof(buffer) == stat.st_size );

    tape.clear();
    CPPUNIT_ASSERT( true == oper->GetTape(tape) );
    CPPUNIT_ASSERT( tapeName == tape );
    tape.clear();
    CPPUNIT_ASSERT( true == inode->GetTape(tape) );
    CPPUNIT_ASSERT( tape == tapeName );
}
Пример #6
0
void process_directory(const fs::path &directory, const long depth, 
    ProcessParams &params)
{
    // Exclude entire directories
    bool exclude = false;
    std::vector<std::string>::const_iterator x_iter = params.excludes.begin();
    std::vector<std::string>::const_iterator x_end = params.excludes.end();
    for( ; x_iter != x_end; ++x_iter ) {
        if(boost::contains(directory.file_string(), *x_iter)) {
            exclude = true;
            break;
        }
    }
    if(exclude) {
        info(std::string("excluding directory: ") + directory.file_string() +
            " matched: " + *x_iter);
        ++params.dir_ex_count;
        return;
    }

    try {
        fs::directory_iterator p_iter(directory), p_end; 
        for( ; p_iter != p_end; ++p_iter) {
            if( is_directory(*p_iter) ) {
                // recurse if we haven't hit the limit
                if(depth < params.limit_depth)
                    process_directory(p_iter->path(), depth + 1, params);
                else {
                    info(std::string("depth reached, skipping: ") +
                        p_iter->path().file_string());
                }
            }
            else if( is_regular_file(*p_iter) ) {
        
                // Check again for excluding file names
                exclude = false;
                x_iter = params.excludes.begin();
                for( ; x_iter != x_end; ++x_iter ) {
                    if(boost::contains(p_iter->path().file_string(), *x_iter)) {
                        exclude = true;
                        break;
                    }
                }
                if(exclude) {
                    info(std::string("excluding file: ") + p_iter->path().file_string() +
                        " matched: " + *x_iter);
                    ++params.file_ex_count;
                    continue;
                }
            
                try {
                    const fs::path dest_subdir = build_dest(*p_iter);
                    fs::path dest_file;
                    if(!dest_subdir.empty())
                        dest_file = params.dest_dir / dest_subdir;
                    else if(params.ignore_unsorted) {
                        info(std::string("ignoring unsorted: ") + p_iter->path().file_string());
                        ++params.unsorted_ignored_count;
                        continue;
                    }
                    else {
                        info(std::string("unsorted file (missing metadata): ") + p_iter->path().file_string());
                        dest_file = params.unsorted_dir;
                        ++params.unsorted_count;
                    }
            
                    dest_file /= p_iter->filename();
                
                    if(fs::exists(dest_file)) {
                        if(params.ignore_dups) {
                            info(std::string("ignoring: ") + p_iter->path().file_string() +
                                " duplicates: " +  dest_file.file_string());
                            ++params.dups_ignored_count;
                            continue;
                        }
                        else {
                            if(params.force) {
                                info(std::string("force removing: ") + dest_file.file_string() + " for: "
                                    + p_iter->path().file_string());
                                if(!params.dry_run)
                                    fs::remove(dest_file);
                            }
                            else if(params.rename) {
                                info(std::string("renaming: ") + p_iter->path().file_string() +
                                    " duplicates: " +  dest_file.file_string());
                                dest_file = uniquify(dest_file);
                            }
                            else {
                                info(std::string("duplicate file: ") + p_iter->path().file_string() +
                                    " of: " +  dest_file.file_string());
                                dest_file = params.dups_dir / dest_subdir / p_iter->filename();
                                // Ugh, more dup possibilities
                                if(fs::exists(dest_file)) {
                                    info(std::string("renaming: ") + p_iter->path().file_string() +
                                        " duplicates: " +  dest_file.file_string());
                                    dest_file = uniquify(dest_file);
                                }
                            }
                            ++params.dups_count;
                        }
                    }
                
                    if(!params.dry_run)
                        fs::create_directories(dest_file.parent_path());
                
                    if(params.symlink) {
                        info(std::string("linking from: ") + p_iter->path().file_string() + 
                            " to: " + dest_file.file_string());
                        if(!params.dry_run) {
                            // The target of a symlink must be either absolute (aka complete) or
                            // relative to the location of the link. Easiest solution is to make
                            // a complete path.
                            fs::path target;
                            if(p_iter->path().is_complete()) 
                                target = p_iter->path();
                            else 
                                target = fs::initial_path() / p_iter->path();
                            fs::create_symlink(target, dest_file);
                        }
                    }
                    else {
                        info(std::string("copying from: ") + p_iter->path().file_string() +
                            " to: " + dest_file.file_string());
                        if(!params.dry_run) {
                            // Copy the file and restore its write time (needed for posix)
                            std::time_t time = fs::last_write_time(*p_iter);
                            fs::copy_file(*p_iter, dest_file);
                            fs::last_write_time(dest_file, time);
                            if(params.verify) {
                                md5digest src_digest, dst_digest;
                                bool ok = md5sum(p_iter->path(), src_digest);
                                if(ok)
                                    ok = md5sum(dest_file, dst_digest);
                                if(ok)
                                    ok = (memcmp(src_digest,dst_digest, sizeof(md5digest))==0);
                                if(!ok) {
                                    // Should probably find a more appropriate exception for this
                                    throw std::runtime_error(std::string("File verification failed: '") 
                                        + p_iter->path().file_string() + "' differs from '" + 
                                        dest_file.file_string() + "'");
                                } 
                                else {
                                    info(std::string("verification passed"));
                                }
                            }
                        }
                    }
                    if(params.move) {
                        info(std::string("removing: ") + p_iter->path().file_string());
                        if(!params.dry_run)
                            fs::remove(*p_iter);
                    }
                
                    if(!g_verbose && (params.ok_count % DOT_EVERY)==0) {
                        std::cout << "." << std::flush;
                        g_neednewline = true;
                    }
                    ++params.ok_count;
                }
                catch(fs::filesystem_error& e) {
                    error(e, std::string("skipping file: " + p_iter->path().file_string()));
                    ++params.file_err_count;
                }
            }
        }
    }
    catch(fs::filesystem_error& e) {
        error(e, std::string("skipping directory: " + directory.file_string()));
        ++params.dir_err_count;
    }
}