]> git.saurik.com Git - apt.git/blobdiff - apt-pkg/contrib/fileutl.cc
* apt-pkg/contrib/fileutl.cc:
[apt.git] / apt-pkg / contrib / fileutl.cc
index eabaadf908ee7f29c637d87cb88b855d96c056df..266d480a4b17ab5183074483a5cb28adffa7821a 100644 (file)
 #include <errno.h>
 #include <set>
 #include <algorithm>
 #include <errno.h>
 #include <set>
 #include <algorithm>
+
+#include <config.h>
+#ifdef WORDS_BIGENDIAN
+#include <inttypes.h>
+#endif
                                                                        /*}}}*/
 
 using namespace std;
                                                                        /*}}}*/
 
 using namespace std;
@@ -910,18 +915,55 @@ unsigned long FileFd::Tell()
    return Res;
 }
                                                                        /*}}}*/
    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;
    struct stat Buf;
+
    if (fstat(iFd,&Buf) != 0)
       return _error->Errno("fstat","Unable to determine the file size");
    return Buf.st_size;
 }
                                                                        /*}}}*/
    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             /*{{{*/
 // ---------------------------------------------------------------------
 /* */
 // FileFd::Close - Close the file if the close flag is set             /*{{{*/
 // ---------------------------------------------------------------------
 /* */