void read(std::basic_string<CharT>& buf, path_t const& path) { std::basic_string<char> buf_; std::basic_ifstream<char> ifs(path.string()); ifs.imbue(std::locale("")); if (!ifs) { throw file_error("failed to open file " + path.string()); } // get length ifs.seekg(0, std::ios::end); auto const len = ifs.tellg(); ifs.seekg(0, std::ios::beg); buf_.resize(len); ifs.read(&buf_[0], len); buf = to<std::basic_string<CharT>>(buf_); }
uint64_t file_manager::stat_filesize(path_t const& p) const { std::ifstream fp(p.string().c_str(), std::ios_base::binary); if (!fp.is_open() || !fp.good()) return 0; uint64_t size = stat_filesize(fp); fp.close(); return size; }
bool downloader::fetch(string_t const& url, path_t const& path, string_t const& checksum, int* const retry_tally) const { // TODO: rethink about this, this really sounds like an external concern for (int i = 0; i < retry_count_ + 1; ++i) { bool fetch_successful; if (!file_manager_.is_writable(path)) { error() << "Download destination is un-writable: " << path; return false; } std::ofstream fp(path.string().c_str(), std::ios_base::trunc | std::ios_base::binary); if (retry_tally != nullptr) { (*retry_tally) = i; } fetch_successful = fetch(url, fp); fp.close(); if (fetch_successful) { if (!file_manager_.is_readable(path)) { return false; // this really shouldn't happen, but oh well } // validate integrity hasher::digest_rc rc = config_.hasher->hex_digest(path); if (rc == checksum) { return true; } else { warn() << "Downloaded file integrity mismatch: " << rc.digest << " vs " << checksum; } } notice() << "Retry #" << i+1; } return false; }
bool file_manager::load_file(path_t const& path, string_t& out_buf) const { return load_file(path.string(), out_buf); }