void operator()(const boost::filesystem::path & data) { not_cmd = true; if (exe.empty()) exe = data.native(); else args.push_back(data.native()); }
void ZipArchiveImpl::AddFolder(const bfs::path& path, const bfs::path& archive_path) { ++m_Info.folderCount; zip_fileinfo fileinfo; fileinfo.internal_fa = 0; #ifdef _WIN32 fileinfo.external_fa = ::GetFileAttributesW(path.native().c_str()); #else { struct stat path_stat; if (::stat(path.native().c_str(), &path_stat) == 0) { fileinfo.external_fa = path_stat.st_mode; } } #endif fileinfo.dosDate = 0; // Read the time from the filesystem and convert it auto fsTime = bfs::last_write_time(path); auto posixTime = boost::posix_time::from_time_t(fsTime); auto tm = boost::posix_time::to_tm(posixTime); /* TODO: this is how to get the time for a physfs file boost::posix_time::ptime posixTime; auto milisPastEpoc = PHYSFS_getLastModTime(path.generic_string().c_str()); if (milisPastEpoc >= 0) time = boost::posix_time::ptime(boost::gregorian::date(1970, 1, 1), boost::posix_time::milliseconds(milisPastEpoc)); else time = boost::posix_time::second_clock::local_time(); */ fileinfo.tmz_date.tm_hour = tm.tm_hour; fileinfo.tmz_date.tm_min = tm.tm_min; fileinfo.tmz_date.tm_sec = tm.tm_sec; fileinfo.tmz_date.tm_year = tm.tm_year; fileinfo.tmz_date.tm_mon = tm.tm_mon; fileinfo.tmz_date.tm_mday = tm.tm_mday; auto r = zipOpenNewFileInZip(m_File, (archive_path.generic_string() + "/").c_str(), &fileinfo, nullptr, 0, nullptr, 0, nullptr, Z_DEFLATED, Z_BEST_SPEED); zipCloseFileInZip(m_File); for (bfs::directory_iterator it(path), end = bfs::directory_iterator(); it != end; ++it) { AddPath(it->path(), archive_path / it->path().filename()); } }
void read_slice_channel( T view, boost::filesystem::path aff_x_file) { vigra::ImageImportInfo info(aff_x_file.native().c_str()); vigra::MultiArray<2, float> data(info.shape()); vigra::importImage(aff_x_file.native().c_str(), data); for (int x = 0; x < info.shape()[0]; x++) for (int y = 0; y < info.shape()[1]; y++) view[x][y] = data(x, y); }
void throwIfExists(const boost::filesystem::path& file) { if (boost::filesystem::exists(file)) { std::stringstream ss; ss << "File exists: " << file.native(); throw std::runtime_error(ss.str()); } }
void load_library() { if (handle_ != NULL) unload_library(); handle_ = LoadLibrary(module_.native().c_str()); if (handle_ == NULL) throw dll_exception("Could not load library: " + utf8::cvt<std::string>(error::lookup::last_error()) + ": " + module_.filename().string()); }
FS::path ConfdUnittestHarness::make_rooted_path( FS::path filename) { try { // Can handle $RIFT_ROOT std::string raw_fullpath = filename.native(); const char RIFT_ROOT[] = "$RIFT_ROOT"; if (raw_fullpath.substr(0, sizeof(RIFT_ROOT)-1) == RIFT_ROOT) { raw_fullpath.replace(0, sizeof(RIFT_ROOT)-1, rift_root_.c_str()); } FS::path fullpath = raw_fullpath; if (!fullpath.is_absolute()) { fullpath = cwd_/fullpath; } return fullpath; } catch (const FS::filesystem_error& fe) { cerr << "Filesystem error: " << fe.what() << ": " << fe.path1() << ", " << fe.path2() << std::endl; throw std::exception(); // don't rethrow, to prevent being recaught by harness } }
void try_run_file(const boost::filesystem::path & cur) { if(cur.extension().native().compare(".py")) { return; } script_file_parser script_file(cur.native()); if(script_file.is_valid()) { boost::filesystem::path work, out; create_directories(cur, work, out); auto parent = cur.parent_path(); auto start = parent.begin(); for(auto p = config::tests.begin(); p != config::tests.end() && *p == *start; ++p, ++start) ; for(; start != parent.end(); ++start) { script_file.add_keyword((*start).native()); } script_file.add_title(cur.stem().native()); if(config::query->evaluate(script_file)) { script_scheduler()->schedule_file(cur, work, out); } } }
/** * Creates a new log_writer instance which will emit output to the specified file. * @param output_file Path identifying file to write XML to. * @param create If output_file doesn't exist, indicates whether to create it on the fly. Defaults to true. * @param write_header indicates if the XML preamble should be written on startup * @param write_footer indicates if the XML closure tags should be written on shutdown. * @param specific_pid specify the PID to create log for * @param specific_application_name specify the application name to create log for * @returns shared pointer to newly instanced log_writer. */ std::shared_ptr<log_writer> log_writer::create_from_file_path( const boost::filesystem::path& output_file, const bool& create, const bool& write_header, const bool& write_footer, const pid_type& specific_pid, const std::string& specific_application_name) { try { using namespace boost::filesystem; // determine if the file exists if (!exists(output_file)) { // the file doesn't exist. // check to see if we should create it... if (create) { // // ideally - I would like to check the validity of the requested file here but // boost::filesystem::native() is having some problems with relative paths such // as "./log.xml" (incl. the leading ./); which is apparently not a valid file path. // instead we'll have to push on and rely on the resulting exception to indicate bad files. // // determine the parent directory path (required for relative paths) auto parent_directory = absolute(output_file).branch_path(); boost::system::error_code filesystem_error; // ensure parent directory exists, or create it if (!exists(parent_directory) && !create_directories( parent_directory, filesystem_error)) { using namespace inglenook::core::exceptions; // the parent path doesn't exist and we failed to create it. BOOST_THROW_EXCEPTION(failed_to_create_log_exception() << boost_filesystem_error(filesystem_error) << inglenook_error_number(log_exception_bad_file_path)); } } else // the file does not exist, and we were not instructed to create it. { using namespace inglenook::core::exceptions; BOOST_THROW_EXCEPTION(log_not_found_exception() << inglenook_error_number(log_exception_bad_file_path)); } } // at this point either the log exists, or we are good to create it // so attempt to open the specified file path for appending data. return std::shared_ptr<log_writer>(new log_writer(std::shared_ptr<std::ofstream>( new std::ofstream(output_file.native(), std::ios::app)), write_header, write_footer, specific_pid, specific_application_name)); } catch(boost::exception& ex) { // if anything goes wrong augment exception with the file path we were working with. ex << log_file_name(output_file); throw; } }
void read_image( boost::filesystem::path const& pathname, DiskImage<PixelType>& output_image ){ // make sure the file exists if( boost::filesystem::exists( pathname ) == false ){ throw std::runtime_error(std::string(std::string("error: File \"") + pathname.native() + std::string("\" does not exist.")).c_str()); } // compute the required driver GEO::ImageDriverType driver = compute_driver( pathname, DriverOptions::READ_ONLY ); // create the resource DiskResource<PixelType> resource; boost::shared_ptr<GEO::IO::ImageDriverBase> image_driver(nullptr); /** * Iterate through potential drivers */ if( driver == ImageDriverType::GDAL ){ image_driver = boost::shared_ptr<GEO::IO::ImageDriverBase>(new GEO::IO::GDAL::ImageDriverGDAL(pathname)); image_driver->open(); } else{ throw std::runtime_error("Unknown driver type."); } // set the resource resource.setDriver( image_driver ); // set the driver output_image.setResource(resource); }
bool IsPluginMaster(boost::filesystem::path filename) { char buffer[MAXLENGTH]; char* bufptr = buffer; ModHeader modHeader; if (filename.empty()) return false; ifstream file(filename.native().c_str(), ios_base::binary | ios_base::in); if (file.bad()) //throw boss_error(BOSS_ERROR_FILE_READ_FAIL, filename.string()); return false; // Reads the first MAXLENGTH bytes into the buffer file.read(&buffer[0], sizeof(buffer)); // Check for the 'magic' marker at start if (Read<uint>(bufptr) != Record::TES4){ return false; } // Next field is the total header size /*uint headerSize =*/ Read<uint>(bufptr); // Next comes the header record Flags uint flags = Read<uint>(bufptr); // LSb of this record's flags is used to indicate if the // mod is a master or a plugin return ((flags & 0x1) != 0); }
void mack::options::configuration_file::parse( boost::filesystem::path const& file_path, mack::options::program_options* options) { if (options == NULL) { BOOST_THROW_EXCEPTION(mack::core::null_pointer_error()); } try { parse(mack::core::files::read_file(file_path), options); } catch (mack::options::no_such_option_error e) { if (boost::get_error_info<mack::core::files::errinfo_file>(e) == NULL) { // this is the configuration file with the erroneous line e << mack::core::files::errinfo_file(file_path.native()); } // else error from within a further configuration file throw; } catch (mack::options::no_such_namespace_error e) { if (boost::get_error_info<mack::core::files::errinfo_file>(e) == NULL) { e << mack::core::files::errinfo_file(file_path.native()); } throw; } catch (mack::options::invalid_value_error e) { if (boost::get_error_info<mack::core::files::errinfo_file>(e) == NULL) { e << mack::core::files::errinfo_file(file_path.native()); } throw; } catch (mack::options::no_value_error e) { if (boost::get_error_info<mack::core::files::errinfo_file>(e) == NULL) { e << mack::core::files::errinfo_file(file_path.native()); } throw; } }
inline std::string pathstr(const filesystem::path& p) { #ifdef _WIN32 return Strutil::utf16_to_utf8(p.native()); #else return p.string(); #endif }
std::string PathString(const fs::path& path) { #ifndef FREEORION_WIN32 return path.string(); #else fs::path::string_type native_string = path.native(); std::string retval; utf8::utf16to8(native_string.begin(), native_string.end(), std::back_inserter(retval)); return retval; #endif }
size_t IOFactory::loadFile( std::list<Chunk> &ret, const boost::filesystem::path &filename, util::istring suffix_override, util::istring dialect ) { FileFormatList formatReader; formatReader = getFileFormatList( filename.string(), suffix_override, dialect ); const util::istring with_dialect = dialect.empty() ? util::istring( "" ) : util::istring( " with dialect \"" ) + dialect + "\""; if ( formatReader.empty() ) { if( !boost::filesystem::exists( filename ) ) { LOG( Runtime, error ) << util::MSubject( filename ) << " does not exist as file, and no suitable plugin was found to generate data from " << ( suffix_override.empty() ? util::istring( "that name" ) : util::istring( "the suffix \"" ) + suffix_override + "\"" ); } else if( suffix_override.empty() ) { LOG( Runtime, error ) << "No plugin found to read " << filename << with_dialect; } else { LOG( Runtime, error ) << "No plugin supporting the requested suffix " << suffix_override << with_dialect << " was found"; } } else { BOOST_FOREACH( FileFormatList::const_reference it, formatReader ) { LOG( ImageIoDebug, info ) << "plugin to load file" << with_dialect << " " << util::MSubject( filename ) << ": " << it->getName(); try { int loaded=it->load( ret, filename.native(), dialect, m_feedback ); BOOST_FOREACH( Chunk & ref, ret ) { if ( ! ref.hasProperty( "source" ) ) ref.setPropertyAs( "source", filename.native() ); } return loaded; } catch ( std::runtime_error &e ) { if( suffix_override.empty() ) { LOG( Runtime, formatReader.size() > 1 ? warning : error ) << "Failed to load " << filename << " using " << it->getName() << with_dialect << " ( " << e.what() << " )"; } else { LOG( Runtime, warning ) << "The enforced format " << it->getName() << " failed to read " << filename << with_dialect << " ( " << e.what() << " ), maybe it just wasn't the right format"; } } } LOG_IF( boost::filesystem::exists( filename ) && formatReader.size() > 1, Runtime, error ) << "No plugin was able to load: " << util::MSubject( filename ) << with_dialect; }
inline void png_read_and_convert_image(const boost::filesystem::path& path,Image& im) { #if defined (_WIN32) boost::filesystem::path::string_type path_native = path.native(); FILE* file = _wfopen(path_native.c_str(), L"rb"); gil::png_read_and_convert_image(file,im); fclose(file); #else std::string filename = path.generic_string(); png_read_and_convert_image(filename.c_str(),im); #endif }
void EnOpenNLPChunker::Process(std::istream &in, std::ostream &out, const vector<string> &filterList) { const boost::filesystem::path inPath = boost::filesystem::unique_path(), outPath = boost::filesystem::unique_path(); // read all input to a temp file ofstream inFile(inPath.c_str()); string line; while (getline(in, line)) { Unescape(line); inFile << line << endl; } inFile.close(); // execute chunker string cmd = "cat " + inPath.native() + " | " + m_openNLPPath + "/bin/opennlp POSTagger " + m_openNLPPath + "/models/en-pos-maxent.bin | " + m_openNLPPath + "/bin/opennlp ChunkerME " + m_openNLPPath + "/models/en-chunker.bin > " + outPath.native(); //g << "Executing:" << cmd << endl; int ret = system(cmd.c_str()); // read result of chunker and output as Moses xml trees ifstream outFile(outPath.c_str()); size_t lineNum = 0; while (getline(outFile, line)) { //cerr << line << endl; MosesReformat(line, out, filterList); out << endl; ++lineNum; } outFile.close(); // clean up temporary files remove(inPath.c_str()); remove(outPath.c_str()); }
SerialPort::SerialPort(const boost::filesystem::path &devPath, const int dir): fd_( open(devPath.native().c_str(), dirMode(dir)|O_NOCTTY|O_NDELAY) ) { if(fd_.get()==-1) throw ExceptionDevice{devPath}; configure(); // this is a little trick - it appears that when RS-232 is started // it needs a little while to warm up. so we wait 10 20ms cycles // and cleanup any trashes that may have arrived. usleep(10*20*1000); if(dir&READ) discardContent(); }
int main(int argc, char** argv) { if (argc != 4) { std::cerr << "Usage: pp_unlock CHUNKFILE BLOCK_HASH PUBKEY" << std::endl; return -1; } const std::string chunk_filename = argv[1]; const data_chunk hash = decode_hex(argv[2]); if (hash.empty() || hash.size() != hash_size) { std::cerr << "pp_unlock: not a valid BLOCK_HASH." << std::endl; return -1; } const ec_point pubkey = decode_hex(argv[3]); if (pubkey.empty() || pubkey.size() != ec_compressed_size) { std::cerr << "pp_unlock: not a valid PUBKEY." << std::endl; return -1; } std::ifstream infile(chunk_filename, std::ifstream::binary); infile.seekg(0, std::ifstream::end); size_t file_size = infile.tellg(); BITCOIN_ASSERT(file_size % 16 == 0); infile.seekg(0, std::ifstream::beg); // Read entire file in. data_chunk cipher(file_size); // Copy chunk to public chunk file. char* data = reinterpret_cast<char*>(cipher.data()); infile.read(data, file_size); infile.close(); // Get seed. payment_address bid_addr = bidding_address(pubkey); hash_digest seed = derive_seed(pubkey); // Decrypt chunk. aes256_context ctx; BITCOIN_ASSERT(seed.size() == 32); aes256_init(&ctx, seed.data()); BITCOIN_ASSERT(cipher.size() % 16 == 0); for (size_t i = 0; i < cipher.size(); i += 16) aes256_decrypt_ecb(&ctx, cipher.data() + i); aes256_done(&ctx); // Write out. const fs::path new_chunk_filename = (chunk_filename + ".decrypted"); std::ofstream outfile(new_chunk_filename.native(), std::ifstream::binary); char* dec_data = reinterpret_cast<char*>(cipher.data()); outfile.write(dec_data, cipher.size()); return 0; }
bool PatchExtractor::createPathsAndWriteFile(const boost::filesystem::path & subdirectory, const std::string & fileName, const cv::Mat & patch) { const std::string patchFileName = fileName + ".pgm"; for (;currentOutputDir != endOutputDir; ++currentOutputDir) { const boost::filesystem::path dir = (*currentOutputDir) / subdirectory; if ( !boost::filesystem::exists(dir) ) { try { boost::filesystem::create_directories(dir); } catch (boost::filesystem::filesystem_error & e) { std::cerr << "Failed to create directories " << dir.native() << ". Will try another.\n"; continue; } } const boost::filesystem::path patchPath = dir / patchFileName; if ( cv::imwrite(patchPath.native(), patch) ) { return true; } else { std::cerr << "Failed to write patch at path " << patchPath.native() << ". Will try another.\n"; continue; } } std::cerr << "No more output directories to try to write the patch.\n"; return false; }
uint64_t AssetStore::assetDiskUsage(const boost::filesystem::path& path) const { uint64_t res=0; struct stat res_stat; if (stat(path.c_str(), &res_stat) != 0) { BOOST_LOG_SEV(bithorded::storeLog, warning) << "failed stat:ing asset " << path.native(); return 0; } if (S_ISREG(res_stat.st_mode)) { res += res_stat.st_blocks * 512; } else if (S_ISDIR(res_stat.st_mode)) { fs::directory_iterator end; for (fs::directory_iterator iter(path); iter != end; iter++) { if (stat(iter->path().c_str(), &res_stat) == 0) { res += res_stat.st_blocks * 512; } else { BOOST_LOG_SEV(bithorded::storeLog, warning) << "failed stat:ing asset-part " << iter->path().native(); } } } else { BOOST_LOG_SEV(bithorded::storeLog, warning) << "unknown type for asset " << path.native(); } return res; }
void CPlaylist::Read(const boost::filesystem::path& playlistPath) { Clear(); std::unique_ptr<Framework::Xml::CNode> document; { auto stream(Framework::CreateInputStdStream(playlistPath.native())); document = std::unique_ptr<Framework::Xml::CNode>(Framework::Xml::CParser::ParseDocument(stream)); } if(!document) { throw std::runtime_error("Couldn't parse document."); } auto parentPath = playlistPath.parent_path(); auto items = document->SelectNodes(PLAYLIST_NODE_TAG "/" PLAYLIST_ITEM_NODE_TAG); for(auto nodeIterator(std::begin(items)); nodeIterator != std::end(items); nodeIterator++) { auto itemNode = (*nodeIterator); boost::filesystem::path itemPath = Framework::Utf8::ConvertFrom(itemNode->GetAttribute(PLAYLIST_ITEM_PATH_ATTRIBUTE)); const char* title = itemNode->GetAttribute(PLAYLIST_ITEM_TITLE_ATTRIBUTE); const char* length = itemNode->GetAttribute(PLAYLIST_ITEM_LENGTH_ATTRIBUTE); if(!itemPath.is_complete()) { itemPath = parentPath / itemPath; } ITEM item; item.path = itemPath.wstring(); if(title != NULL) { item.title = Framework::Utf8::ConvertFrom(title); } if(length != NULL) { item.length = atoi(length); } else { item.length = 0; } InsertItem(item); } }
void run_file(const boost::filesystem::path & cur) { if(cur.extension().native().compare(".py")) { return; } script_file_parser script_file(cur.native()); if(script_file.is_valid()) { boost::filesystem::path work, out; create_directories(cur, work, out); script_scheduler()->schedule_file(cur, work, out); } }
void CPsfZipArchive::Open(const boost::filesystem::path& filePath) { assert(m_inputFile.IsEmpty()); assert(!m_archive); m_inputFile = Framework::CreateInputStdStream(filePath.native()); m_archive = std::make_unique<Framework::CZipArchiveReader>(m_inputFile); for(const auto& fileHeaderPair : m_archive->GetFileHeaders()) { const auto& fileHeader(fileHeaderPair.second); if(fileHeader.uncompressedSize == 0) continue; FILEINFO fileInfo; fileInfo.name = fileHeaderPair.first; fileInfo.length = fileHeader.uncompressedSize; m_files.push_back(fileInfo); } }
uint64_t remove_file_recursive(const fs::path& path) { uint64_t size_freed = 0; boost::system::error_code err; struct stat res_stat; if (lstat(path.c_str(), &res_stat) == 0) { size_freed += res_stat.st_blocks * 512; if (S_ISDIR(res_stat.st_mode)) { for (fs::directory_iterator itr(path); itr != end_dir_itr; ++itr) size_freed += remove_file_recursive(itr->path()); } fs::remove(path, err); if (err) { BOOST_LOG_SEV(bithorded::storeLog, warning) << "error removing file " << path << "; " << err; } } else { BOOST_LOG_SEV(bithorded::storeLog, warning) << "failed stat:ing file " << path.native(); } return size_freed; }
uint32 Utils::ComputeFileCrc32(const boost::filesystem::path& filePath) { auto inputStream = Framework::CreateInputStdStream(filePath.native()); auto crc = crc32(0, Z_NULL, 0); while(1) { const uint32 bufferSize = 0x4000; uint8 buffer[bufferSize]; auto actualRead = inputStream.Read(buffer, bufferSize); if(actualRead == 0) { break; } crc = crc32(crc, buffer, static_cast<uInt>(actualRead)); if(inputStream.IsEOF()) { break; } } return crc; }
Framework::CStdStream CPatchFile::CreateOutputStdStreamWithRetry(const boost::filesystem::path& filePath) { int retryCount = 0; while(1) { try { auto result = Framework::CreateOutputStdStream(filePath.native()); return result; } catch(...) { if(retryCount == 5) { throw; } std::this_thread::sleep_for(std::chrono::seconds(1)); retryCount++; } } }
void mack::options::doc_parser::file_handler::check_update_file( std::string const& file_name, std::string const& content) { const boost::filesystem::path file_path = boost::filesystem::absolute(_types_directory_path / file_name); const std::set<std::string>::iterator found = _old_type_files.find(file_path.native()); if (found == _old_type_files.end()) { // new file mack::core::files::write_file(content, file_path); } else { // already exists update_file(file_path, content); _old_type_files.erase(found); } }
void read_image( boost::filesystem::path const& pathname, Image<PixelType>& output_image ){ /// make sure the file exists if( boost::filesystem::exists( pathname ) == false ){ throw std::runtime_error(std::string(std::string("error: File \"") + pathname.native() + std::string("\" does not exist.")).c_str()); } /// decide which driver to use depending on the filename GEO::ImageDriverType driver = compute_driver(pathname); /** * Since we are just loading pixel data, call the appropriate load_image_data function */ if( driver == GEO::ImageDriverType::GDAL ){ int rowSize; output_image.setResource( GEO::IO::GDAL::load_image<PixelType>(pathname)); } else{ throw std::runtime_error("Unknown driver."); } }
void CPlaylist::Write(const boost::filesystem::path& playlistPath) { std::unique_ptr<Framework::Xml::CNode> document(new Framework::Xml::CNode()); auto playlistNode = document->InsertNode(new Framework::Xml::CNode(PLAYLIST_NODE_TAG, true)); auto parentPath = playlistPath.parent_path(); for(auto itemIterator(std::begin(m_items)); itemIterator != std::end(m_items); itemIterator++) { const auto& item(*itemIterator); boost::filesystem::path itemPath(item.path); auto itemRelativePath(naive_uncomplete(itemPath, parentPath)); auto itemNode = playlistNode->InsertNode(new Framework::Xml::CNode(PLAYLIST_ITEM_NODE_TAG, true)); itemNode->InsertAttribute(PLAYLIST_ITEM_PATH_ATTRIBUTE, Framework::Utf8::ConvertTo(itemRelativePath.wstring()).c_str()); itemNode->InsertAttribute(PLAYLIST_ITEM_TITLE_ATTRIBUTE, Framework::Utf8::ConvertTo(item.title).c_str()); itemNode->InsertAttribute(Framework::Xml::CreateAttributeIntValue(PLAYLIST_ITEM_LENGTH_ATTRIBUTE, item.length)); } auto stream(Framework::CreateOutputStdStream(playlistPath.native())); Framework::Xml::CWriter::WriteDocument(stream, document.get()); }
/** \brief create error message. * \param devPath path to device, that caused an error. */ explicit ExceptionDevice(const boost::filesystem::path &devPath): Exception{ Strm{}<<"error while opening device: "<<devPath.native() } { }