bool IArchive::GetFile(const std::string& name, std::vector<std::uint8_t>& buffer) { const unsigned int fid = FindFile(name); if (!IsFileId(fid)) return false; GetFile(fid, buffer); return true; }
bool CBufferedArchive::GetFile(unsigned int fid, std::vector<boost::uint8_t>& buffer) { boost::mutex::scoped_lock lck(archiveLock); assert(IsFileId(fid)); if (fid >= cache.size()) { cache.resize(fid + 1); } if (!cache[fid].populated) { cache[fid].exists = GetFileImpl(fid, cache[fid].data); cache[fid].populated = true; } buffer = cache[fid].data; return cache[fid].exists; }
bool CPoolArchive::GetFileImpl(unsigned int fid, std::vector<boost::uint8_t>& buffer) { assert(IsFileId(fid)); FileData* f = files[fid]; char table[] = "0123456789abcdef"; char c_hex[32]; for (int i = 0; i < 16; ++i) { c_hex[2 * i] = table[(f->md5[i] >> 4) & 0xf]; c_hex[2 * i + 1] = table[ f->md5[i] & 0xf]; } std::string prefix(c_hex, 2); std::string postfix(c_hex + 2, 30); std::ostringstream accu; accu << "pool/" << prefix << "/" << postfix << ".gz"; std::string rpath = accu.str(); FileSystem::FixSlashes(rpath); std::string path = dataDirsAccess.LocateFile(rpath); gzFile in = gzopen(path.c_str(), "rb"); if (in == NULL){ LOG_L(L_ERROR, "couldn't open %s", path.c_str()); return false; } unsigned int len = f->size; buffer.resize(len); int bytesread = (len == 0) ? 0 : gzread(in, (char *)&buffer[0], len); gzclose(in); if (bytesread != len) { LOG_L(L_ERROR, "couldn't read %s", path.c_str()); buffer.clear(); return false; } return true; }
unsigned int CPoolArchive::GetCrc32(unsigned int fid) { assert(IsFileId(fid)); return files[fid]->crc32; }
void CPoolArchive::FileInfo(unsigned int fid, std::string& name, int& size) const { assert(IsFileId(fid)); name = files[fid]->name; size = files[fid]->size; }