X-Git-Url: https://git.saurik.com/apt.git/blobdiff_plain/7753e4684e2b80abbed296b90f47be12b4fce8fc..0ff1a4556c0a0fb76ce48e88031eff2b4d613c5e:/apt-pkg/contrib/fileutl.cc diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index eabaadf90..266d480a4 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -42,6 +42,11 @@ #include #include #include + +#include +#ifdef WORDS_BIGENDIAN +#include +#endif /*}}}*/ using namespace std; @@ -910,18 +915,55 @@ unsigned long FileFd::Tell() return Res; } /*}}}*/ -// FileFd::Size - Return the size of the file /*{{{*/ +// FileFd::FileSize - Return the size of the file /*{{{*/ // --------------------------------------------------------------------- /* */ -unsigned long FileFd::Size() +unsigned long FileFd::FileSize() { - //TODO: For gz, do we need the actual file size here or the uncompressed length? struct stat Buf; + if (fstat(iFd,&Buf) != 0) return _error->Errno("fstat","Unable to determine the file size"); return Buf.st_size; } /*}}}*/ +// FileFd::Size - Return the size of the content in the file /*{{{*/ +// --------------------------------------------------------------------- +/* */ +unsigned long FileFd::Size() +{ + unsigned long size = FileSize(); + + // only check gzsize if we are actually a gzip file, just checking for + // "gz" is not sufficient as uncompressed files will be opened with + // gzopen in "direct" mode as well + if (gz && !gzdirect(gz) && size > 0) + { + /* unfortunately zlib.h doesn't provide a gzsize(), so we have to do + * this ourselves; the original (uncompressed) file size is the last 32 + * bits of the file */ + off_t orig_pos = lseek(iFd, 0, SEEK_CUR); + if (lseek(iFd, -4, SEEK_END) < 0) + return _error->Errno("lseek","Unable to seek to end of gzipped file"); + size = 0L; + if (read(iFd, &size, 4) != 4) + return _error->Errno("read","Unable to read original size of gzipped file"); + +#ifdef WORDS_BIGENDIAN + uint32_t tmp_size = size; + uint8_t const * const p = (uint8_t const * const) &tmp_size; + tmp_size = (p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]; + size = tmp_size; +#endif + + if (lseek(iFd, orig_pos, SEEK_SET) < 0) + return _error->Errno("lseek","Unable to seek in gzipped file"); + return size; + } + + return size; +} + /*}}}*/ // FileFd::Close - Close the file if the close flag is set /*{{{*/ // --------------------------------------------------------------------- /* */