]> git.saurik.com Git - apt.git/blobdiff - apt-pkg/contrib/fileutl.cc
use FileFd instead of forking the compression childs by hand
[apt.git] / apt-pkg / contrib / fileutl.cc
index 83b68e796f310ed21386d394a8707e11b3abfbe1..60396fc3dd771f54a5e93baf41ffd2f89bfc682e 100644 (file)
 #include <set>
 #include <algorithm>
 
+// FIXME: Compressor Fds have some speed disadvantages and are a bit buggy currently,
+// so while the current implementation satisfies the testcases it is not a real option
+// to disable it for now
+#define APT_USE_ZLIB 1
+#if APT_USE_ZLIB
+#include <zlib.h>
+#else
+#warning "Usage of zlib is DISABLED!"
+#endif
+
 #ifdef WORDS_BIGENDIAN
 #include <inttypes.h>
 #endif
 
 using namespace std;
 
+class FileFdPrivate {
+       public:
+#if APT_USE_ZLIB
+       gzFile gz;
+#else
+       void* gz;
+#endif
+       pid_t compressor_pid;
+       bool pipe;
+       APT::Configuration::Compressor compressor;
+       unsigned int openmode;
+       FileFdPrivate() : gz(NULL), compressor_pid(-1), pipe(false) {};
+};
+
 // RunScripts - Run a set of scripts from a configuration subtree      /*{{{*/
 // ---------------------------------------------------------------------
 /* */
@@ -716,97 +740,274 @@ bool ExecWait(pid_t Pid,const char *Name,bool Reap)
 }
                                                                        /*}}}*/
 
+// ExecCompressor - Open a de/compressor pipe                          /*{{{*/
+// ---------------------------------------------------------------------
+/* This opens the compressor, either in compress mode or decompress
+   mode. FileFd is always the compressor input/output file,
+   OutFd is the created pipe, Input for Compress, Output for Decompress. */
+bool ExecCompressor(APT::Configuration::Compressor const &Prog,
+                   pid_t *Pid, int const FileFd, int &OutFd, bool const Comp)
+{
+   if (Pid != NULL)
+      *Pid = -1;
+
+   // No compression
+   if (Prog.Binary.empty() == true)
+   {
+      OutFd = dup(FileFd);
+      return true;
+   }
+
+   // Handle 'decompression' of empty files
+   if (Comp == false)
+   {
+      struct stat Buf;
+      fstat(FileFd, &Buf);
+      if (Buf.st_size == 0 && S_ISFIFO(Buf.st_mode) == false)
+      {
+        OutFd = FileFd;
+        return true;
+      }
+   }
+
+   // Create a data pipe
+   int Pipe[2] = {-1,-1};
+   if (pipe(Pipe) != 0)
+      return _error->Errno("pipe",_("Failed to create subprocess IPC"));
+   for (int J = 0; J != 2; J++)
+      SetCloseExec(Pipe[J],true);
+
+   if (Comp == true)
+      OutFd = Pipe[1];
+   else
+      OutFd = Pipe[0];
+
+   // The child..
+   pid_t child = ExecFork();
+   if (Pid != NULL)
+      *Pid = child;
+   if (child == 0)
+   {
+      if (Comp == true)
+      {
+        dup2(FileFd,STDOUT_FILENO);
+        dup2(Pipe[0],STDIN_FILENO);
+      }
+      else
+      {
+        dup2(FileFd,STDIN_FILENO);
+        dup2(Pipe[1],STDOUT_FILENO);
+      }
+
+      SetCloseExec(STDOUT_FILENO,false);
+      SetCloseExec(STDIN_FILENO,false);
+
+      std::vector<char const*> Args;
+      Args.push_back(Prog.Binary.c_str());
+      std::vector<std::string> const * const addArgs =
+               (Comp == true) ? &(Prog.CompressArgs) : &(Prog.UncompressArgs);
+      for (std::vector<std::string>::const_iterator a = addArgs->begin();
+          a != addArgs->end(); ++a)
+        Args.push_back(a->c_str());
+      Args.push_back(NULL);
+
+      execvp(Args[0],(char **)&Args[0]);
+      cerr << _("Failed to exec compressor ") << Args[0] << endl;
+      _exit(100);
+   }
+   if (Comp == true)
+      close(Pipe[0]);
+   else
+      close(Pipe[1]);
+
+   if (Pid == NULL)
+      ExecWait(child, Prog.Binary.c_str(), true);
+
+   return true;
+}
+bool ExecCompressor(APT::Configuration::Compressor const &Prog,
+                   pid_t *Pid, std::string const &FileName, int &OutFd, bool const Comp)
+{
+   if (Pid != NULL)
+      *Pid = -1;
+
+   // No compression
+   if (Prog.Binary.empty() == true)
+   {
+      if (Comp == true)
+        OutFd = open(FileName.c_str(), O_WRONLY, 0666);
+      else
+        OutFd = open(FileName.c_str(), O_RDONLY);
+      return true;
+   }
+
+   // Handle 'decompression' of empty files
+   if (Comp == false)
+   {
+      struct stat Buf;
+      stat(FileName.c_str(), &Buf);
+      if (Buf.st_size == 0)
+      {
+        OutFd = open(FileName.c_str(), O_RDONLY);
+        return true;
+      }
+   }
+
+   // Create a data pipe
+   int Pipe[2] = {-1,-1};
+   if (pipe(Pipe) != 0)
+      return _error->Errno("pipe",_("Failed to create subprocess IPC"));
+   for (int J = 0; J != 2; J++)
+      SetCloseExec(Pipe[J],true);
+
+   int FileFd = -1;
+   if (Comp == true)
+   {
+      OutFd = Pipe[1];
+      // FIXME: we should handle openmode and permission from Open() here
+      FileFd = open(FileName.c_str(), O_WRONLY, 0666);
+   }
+   else
+      OutFd = Pipe[0];
+
+   // The child..
+   pid_t child = ExecFork();
+   if (Pid != NULL)
+      *Pid = child;
+   if (child == 0)
+   {
+      if (Comp == true)
+      {
+        dup2(Pipe[0],STDIN_FILENO);
+        dup2(FileFd,STDOUT_FILENO);
+        SetCloseExec(STDIN_FILENO,false);
+      }
+      else
+      {
+        dup2(Pipe[1],STDOUT_FILENO);
+      }
+      SetCloseExec(STDOUT_FILENO,false);
+
+      std::vector<char const*> Args;
+      Args.push_back(Prog.Binary.c_str());
+      std::vector<std::string> const * const addArgs =
+               (Comp == true) ? &(Prog.CompressArgs) : &(Prog.UncompressArgs);
+      for (std::vector<std::string>::const_iterator a = addArgs->begin();
+          a != addArgs->end(); ++a)
+        Args.push_back(a->c_str());
+      if (Comp == false)
+      {
+        Args.push_back("--stdout");
+        Args.push_back(FileName.c_str());
+      }
+      Args.push_back(NULL);
+
+      execvp(Args[0],(char **)&Args[0]);
+      cerr << _("Failed to exec compressor ") << Args[0] << endl;
+      _exit(100);
+   }
+   if (Comp == true)
+   {
+      close(Pipe[0]);
+      close(FileFd);
+   }
+   else
+      close(Pipe[1]);
+
+   if (Pid == NULL)
+      ExecWait(child, Prog.Binary.c_str(), false);
+
+   return true;
+}
+                                                                       /*}}}*/
+
 // FileFd::Open - Open a file                                          /*{{{*/
 // ---------------------------------------------------------------------
 /* The most commonly used open mode combinations are given with Mode */
-bool FileFd::Open(string FileName,OpenMode Mode,CompressMode Compress, unsigned long Perms)
+bool FileFd::Open(string FileName,unsigned int const Mode,CompressMode Compress, unsigned long const Perms)
 {
    if (Mode == ReadOnlyGzip)
       return Open(FileName, ReadOnly, Gzip, Perms);
-   Close();
-   Flags = AutoClose;
 
    if (Compress == Auto && (Mode & WriteOnly) == WriteOnly)
       return _error->Error("Autodetection on %s only works in ReadOnly openmode!", FileName.c_str());
-   if ((Mode & WriteOnly) != WriteOnly && (Mode & (Atomic | Create | Empty | Exclusive)) != 0)
-      return _error->Error("ReadOnly mode for %s doesn't accept additional flags!", FileName.c_str());
-
-   int fileflags = 0;
-#define if_FLAGGED_SET(FLAG, MODE) if ((Mode & FLAG) == FLAG) fileflags |= MODE
-   if_FLAGGED_SET(ReadWrite, O_RDWR);
-   else if_FLAGGED_SET(ReadOnly, O_RDONLY);
-   else if_FLAGGED_SET(WriteOnly, O_WRONLY);
-   else return _error->Error("No openmode provided in FileFd::Open for %s", FileName.c_str());
-
-   if_FLAGGED_SET(Create, O_CREAT);
-   if_FLAGGED_SET(Exclusive, O_EXCL);
-   else if_FLAGGED_SET(Atomic, O_EXCL);
-   if_FLAGGED_SET(Empty, O_TRUNC);
-#undef if_FLAGGED_SET
 
    // FIXME: Denote inbuilt compressors somehow - as we don't need to have the binaries for them
    std::vector<APT::Configuration::Compressor> const compressors = APT::Configuration::getCompressors();
    std::vector<APT::Configuration::Compressor>::const_iterator compressor = compressors.begin();
    if (Compress == Auto)
    {
-      Compress = None;
       for (; compressor != compressors.end(); ++compressor)
       {
         std::string file = std::string(FileName).append(compressor->Extension);
         if (FileExists(file) == false)
            continue;
         FileName = file;
-        if (compressor->Binary == ".")
-           Compress = None;
-        else
-           Compress = Extension;
         break;
       }
    }
    else if (Compress == Extension)
    {
-      Compress = None;
-      std::string ext = flExtension(FileName);
-      if (ext != FileName)
+      std::string::size_type const found = FileName.find_last_of('.');
+      std::string ext;
+      if (found != std::string::npos)
       {
-        ext = "." + ext;
-        for (; compressor != compressors.end(); ++compressor)
-           if (ext == compressor->Extension)
-              break;
+        ext = FileName.substr(found);
+        if (ext == ".new" || ext == ".bak")
+        {
+           std::string::size_type const found2 = FileName.find_last_of('.', found - 1);
+           if (found2 != std::string::npos)
+              ext = FileName.substr(found2, found - found2);
+           else
+              ext.clear();
+        }
       }
+      for (; compressor != compressors.end(); ++compressor)
+        if (ext == compressor->Extension)
+           break;
+      // no matching extension - assume uncompressed (imagine files like 'example.org_Packages')
+      if (compressor == compressors.end())
+        for (compressor = compressors.begin(); compressor != compressors.end(); ++compressor)
+           if (compressor->Name == ".")
+              break;
    }
-   else if (Compress != None)
+   else
    {
       std::string name;
       switch (Compress)
       {
+      case None: name = "."; break;
       case Gzip: name = "gzip"; break;
       case Bzip2: name = "bzip2"; break;
       case Lzma: name = "lzma"; break;
       case Xz: name = "xz"; break;
-      default: return _error->Error("Can't find a match for specified compressor mode for file %s", FileName.c_str());
+      case Auto:
+      case Extension:
+        // Unreachable
+        return _error->Error("Opening File %s in None, Auto or Extension should be already handled?!?", FileName.c_str());
       }
       for (; compressor != compressors.end(); ++compressor)
         if (compressor->Name == name)
            break;
-      if (compressor == compressors.end() && name != "gzip")
+      if (compressor == compressors.end())
         return _error->Error("Can't find a configured compressor %s for file %s", name.c_str(), FileName.c_str());
    }
 
-   // if we have them, use inbuilt compressors instead of forking
-   if (compressor != compressors.end())
-   {
-      if (compressor->Name == "gzip")
-      {
-        Compress = Gzip;
-        compressor = compressors.end();
-      }
-      else if (compressor->Name == "." || Compress == None)
-      {
-        Compress = None;
-        compressor = compressors.end();
-      }
-   }
+   if (compressor == compressors.end())
+      return _error->Error("Can't find a match for specified compressor mode for file %s", FileName.c_str());
+   return Open(FileName, Mode, *compressor, Perms);
+}
+bool FileFd::Open(string FileName,unsigned int const Mode,APT::Configuration::Compressor const &compressor, unsigned long const Perms)
+{
+   Close();
+   d = new FileFdPrivate;
+   d->openmode = Mode;
+   Flags = AutoClose;
+
+   if ((Mode & WriteOnly) != WriteOnly && (Mode & (Atomic | Create | Empty | Exclusive)) != 0)
+      return _error->Error("ReadOnly mode for %s doesn't accept additional flags!", FileName.c_str());
+   if ((Mode & ReadWrite) == 0)
+      return _error->Error("No openmode provided in FileFd::Open for %s", FileName.c_str());
 
    if ((Mode & Atomic) == Atomic)
    {
@@ -827,15 +1028,62 @@ bool FileFd::Open(string FileName,OpenMode Mode,CompressMode Compress, unsigned
         unlink(FileName.c_str());
    }
 
-   if (compressor != compressors.end())
+   // if we have them, use inbuilt compressors instead of forking
+   if (compressor.Name != "."
+#if APT_USE_ZLIB
+       && compressor.Name != "gzip"
+#endif
+      )
    {
       if ((Mode & ReadWrite) == ReadWrite)
-        _error->Error("External compressors like %s do not support readwrite mode for file %s", compressor->Name.c_str(), FileName.c_str());
+        return _error->Error("External compressors like %s do not support readwrite mode for file %s", compressor.Name.c_str(), FileName.c_str());
 
-      _error->Error("Forking external compressor %s is not implemented for %s", compressor->Name.c_str(), FileName.c_str());
+      if ((Mode & (WriteOnly | Create)) == (WriteOnly | Create))
+      {
+        if (TemporaryFileName.empty() == false)
+        {
+           if (RealFileExists(TemporaryFileName) == false)
+           {
+              iFd = open(TemporaryFileName.c_str(), O_WRONLY | O_CREAT, Perms);
+              close(iFd);
+              iFd = -1;
+           }
+        }
+        else if (RealFileExists(FileName) == false)
+        {
+           iFd = open(FileName.c_str(), O_WRONLY | O_CREAT, Perms);
+           close(iFd);
+           iFd = -1;
+        }
+      }
+
+      if (TemporaryFileName.empty() == false)
+      {
+        if (ExecCompressor(compressor, &(d->compressor_pid), TemporaryFileName, iFd, ((Mode & ReadOnly) != ReadOnly)) == false)
+           return _error->Error("Forking external compressor %s is not implemented for %s", compressor.Name.c_str(), TemporaryFileName.c_str());
+      }
+      else
+      {
+        if (ExecCompressor(compressor, &(d->compressor_pid), FileName, iFd, ((Mode & ReadOnly) != ReadOnly)) == false)
+           return _error->Error("Forking external compressor %s is not implemented for %s", compressor.Name.c_str(), FileName.c_str());
+      }
+      d->pipe = true;
+      d->compressor = compressor;
    }
    else
    {
+      int fileflags = 0;
+      #define if_FLAGGED_SET(FLAG, MODE) if ((Mode & FLAG) == FLAG) fileflags |= MODE
+      if_FLAGGED_SET(ReadWrite, O_RDWR);
+      else if_FLAGGED_SET(ReadOnly, O_RDONLY);
+      else if_FLAGGED_SET(WriteOnly, O_WRONLY);
+
+      if_FLAGGED_SET(Create, O_CREAT);
+      if_FLAGGED_SET(Exclusive, O_EXCL);
+      else if_FLAGGED_SET(Atomic, O_EXCL);
+      if_FLAGGED_SET(Empty, O_TRUNC);
+      #undef if_FLAGGED_SET
+
       if (TemporaryFileName.empty() == false)
         iFd = open(TemporaryFileName.c_str(), fileflags, Perms);
       else
@@ -843,7 +1091,7 @@ bool FileFd::Open(string FileName,OpenMode Mode,CompressMode Compress, unsigned
 
       if (iFd != -1)
       {
-        if (OpenInternDescriptor(Mode, Compress) == false)
+        if (OpenInternDescriptor(Mode, compressor) == false)
         {
            close (iFd);
            iFd = -1;
@@ -862,12 +1110,38 @@ bool FileFd::Open(string FileName,OpenMode Mode,CompressMode Compress, unsigned
 // FileFd::OpenDescriptor - Open a filedescriptor                      /*{{{*/
 // ---------------------------------------------------------------------
 /* */
-bool FileFd::OpenDescriptor(int Fd, OpenMode Mode, CompressMode Compress, bool AutoClose)
+bool FileFd::OpenDescriptor(int Fd, unsigned int const Mode, CompressMode Compress, bool AutoClose)
+{
+   std::vector<APT::Configuration::Compressor> const compressors = APT::Configuration::getCompressors();
+   std::vector<APT::Configuration::Compressor>::const_iterator compressor = compressors.begin();
+   std::string name;
+   switch (Compress)
+   {
+   case None: name = "."; break;
+   case Gzip: name = "gzip"; break;
+   case Bzip2: name = "bzip2"; break;
+   case Lzma: name = "lzma"; break;
+   case Xz: name = "xz"; break;
+   case Auto:
+   case Extension:
+      return _error->Error("Opening Fd %d in Auto or Extension compression mode is not supported", Fd);
+   }
+   for (; compressor != compressors.end(); ++compressor)
+      if (compressor->Name == name)
+        break;
+   if (compressor == compressors.end())
+      return _error->Error("Can't find a configured compressor %s for file %s", name.c_str(), FileName.c_str());
+
+   return OpenDescriptor(Fd, Mode, *compressor, AutoClose);
+}
+bool FileFd::OpenDescriptor(int Fd, unsigned int const Mode, APT::Configuration::Compressor const &compressor, bool AutoClose)
 {
    Close();
+   d = new FileFdPrivate;
+   d->openmode = Mode;
    Flags = (AutoClose) ? FileFd::AutoClose : 0;
    iFd = Fd;
-   if (OpenInternDescriptor(Mode, Compress) == false)
+   if (OpenInternDescriptor(Mode, compressor) == false)
    {
       if (AutoClose)
         close (iFd);
@@ -876,23 +1150,26 @@ bool FileFd::OpenDescriptor(int Fd, OpenMode Mode, CompressMode Compress, bool A
    this->FileName = "";
    return true;
 }
-bool FileFd::OpenInternDescriptor(OpenMode Mode, CompressMode Compress)
+bool FileFd::OpenInternDescriptor(unsigned int const Mode, APT::Configuration::Compressor const &compressor)
 {
-   if (Compress == None)
+   if (compressor.Name == ".")
       return true;
-   else if (Compress == Gzip)
+#if APT_USE_ZLIB
+   else if (compressor.Name == "gzip")
    {
       if ((Mode & ReadWrite) == ReadWrite)
-        gz = gzdopen(iFd, "r+");
+        d->gz = gzdopen(iFd, "r+");
       else if ((Mode & WriteOnly) == WriteOnly)
-        gz = gzdopen(iFd, "w");
+        d->gz = gzdopen(iFd, "w");
       else
-        gz = gzdopen (iFd, "r");
-      if (gz == NULL)
+        d->gz = gzdopen (iFd, "r");
+      if (d->gz == NULL)
         return false;
+      Flags |= Compressed;
    }
+#endif
    else
-      return false;
+      return _error->Error("Can't find a match for specified compressor %s for file %s", compressor.Name.c_str(), FileName.c_str());
    return true;
 }
                                                                        /*}}}*/
@@ -915,12 +1192,14 @@ bool FileFd::Read(void *To,unsigned long long Size,unsigned long long *Actual)
    errno = 0;
    if (Actual != 0)
       *Actual = 0;
-   
+   *((char *)To) = '\0';
    do
    {
-      if (gz != NULL)
-         Res = gzread(gz,To,Size);
+#if APT_USE_ZLIB
+      if (d->gz != NULL)
+         Res = gzread(d->gz,To,Size);
       else
+#endif
          Res = read(iFd,To,Size);
       if (Res < 0 && errno == EINTR)
         continue;
@@ -951,6 +1230,31 @@ bool FileFd::Read(void *To,unsigned long long Size,unsigned long long *Actual)
    return _error->Error(_("read, still have %llu to read but none left"), Size);
 }
                                                                        /*}}}*/
+// FileFd::ReadLine - Read a complete line from the file               /*{{{*/
+// ---------------------------------------------------------------------
+/* Beware: This method can be quiet slow for big buffers on UNcompressed
+   files because of the naive implementation! */
+char* FileFd::ReadLine(char *To, unsigned long long const Size)
+{
+   *To = '\0';
+#if APT_USE_ZLIB
+   if (d->gz != NULL)
+      return gzgets(d->gz, To, Size);
+#endif
+
+   unsigned long long read = 0;
+   if (Read(To, Size, &read) == false)
+      return NULL;
+   char* c = To;
+   for (; *c != '\n' && *c != '\0' && read != 0; --read, ++c)
+      ; // find the end of the line
+   if (*c != '\0')
+      *c = '\0';
+   if (read != 0)
+      Seek(Tell() - read);
+   return To;
+}
+                                                                       /*}}}*/
 // FileFd::Write - Write to the file                                   /*{{{*/
 // ---------------------------------------------------------------------
 /* */
@@ -960,9 +1264,11 @@ bool FileFd::Write(const void *From,unsigned long long Size)
    errno = 0;
    do
    {
-      if (gz != NULL)
-         Res = gzwrite(gz,From,Size);
+#if APT_USE_ZLIB
+      if (d->gz != NULL)
+         Res = gzwrite(d->gz,From,Size);
       else
+#endif
          Res = write(iFd,From,Size);
       if (Res < 0 && errno == EINTR)
         continue;
@@ -989,10 +1295,21 @@ bool FileFd::Write(const void *From,unsigned long long Size)
 /* */
 bool FileFd::Seek(unsigned long long To)
 {
+   if (d->pipe == true)
+   {
+      // FIXME: What about OpenDescriptor() stuff here?
+      close(iFd);
+      bool result = ExecCompressor(d->compressor, NULL, FileName, iFd, (d->openmode & ReadOnly) != ReadOnly);
+      if (result == true && To != 0)
+        result &= Skip(To);
+      return result;
+   }
    int res;
-   if (gz)
-      res = gzseek(gz,To,SEEK_SET);
+#if APT_USE_ZLIB
+   if (d->gz)
+      res = gzseek(d->gz,To,SEEK_SET);
    else
+#endif
       res = lseek(iFd,To,SEEK_SET);
    if (res != (signed)To)
    {
@@ -1009,9 +1326,11 @@ bool FileFd::Seek(unsigned long long To)
 bool FileFd::Skip(unsigned long long Over)
 {
    int res;
-   if (gz)
-      res = gzseek(gz,Over,SEEK_CUR);
+#if APT_USE_ZLIB
+   if (d->gz != NULL)
+      res = gzseek(d->gz,Over,SEEK_CUR);
    else
+#endif
       res = lseek(iFd,Over,SEEK_CUR);
    if (res < 0)
    {
@@ -1027,7 +1346,7 @@ bool FileFd::Skip(unsigned long long Over)
 /* */
 bool FileFd::Truncate(unsigned long long To)
 {
-   if (gz)
+   if (d->gz != NULL)
    {
       Flags |= Fail;
       return _error->Error("Truncating gzipped files is not implemented (%s)", FileName.c_str());
@@ -1047,9 +1366,11 @@ bool FileFd::Truncate(unsigned long long To)
 unsigned long long FileFd::Tell()
 {
    off_t Res;
-   if (gz)
-     Res = gztell(gz);
+#if APT_USE_ZLIB
+   if (d->gz != NULL)
+     Res = gztell(d->gz);
    else
+#endif
      Res = lseek(iFd,0,SEEK_CUR);
    if (Res == (off_t)-1)
       _error->Errno("lseek","Failed to determine the current file position");
@@ -1062,9 +1383,19 @@ unsigned long long FileFd::Tell()
 unsigned long long FileFd::FileSize()
 {
    struct stat Buf;
-
-   if (fstat(iFd,&Buf) != 0)
+   if (d->pipe == false && fstat(iFd,&Buf) != 0)
       return _error->Errno("fstat","Unable to determine the file size");
+
+   // for compressor pipes st_size is undefined and at 'best' zero
+   if (d->pipe == true || S_ISFIFO(Buf.st_mode))
+   {
+      // we set it here, too, as we get the info here for free
+      // in theory the Open-methods should take care of it already
+      d->pipe = true;
+      if (stat(FileName.c_str(), &Buf) != 0)
+        return _error->Errno("stat","Unable to determine the file size");
+   }
+
    return Buf.st_size;
 }
                                                                        /*}}}*/
@@ -1075,10 +1406,25 @@ unsigned long long FileFd::Size()
 {
    unsigned long long size = FileSize();
 
+   // for compressor pipes st_size is undefined and at 'best' zero,
+   // so we 'read' the content and 'seek' back - see there
+   if (d->pipe == true)
+   {
+      // FIXME: If we have read first and then FileSize() the report is wrong
+      size = 0;
+      char ignore[1000];
+      unsigned long long read = 0;
+      do {
+        Read(ignore, sizeof(ignore), &read);
+        size += read;
+      } while(read != 0);
+      Seek(0);
+   }
+#if APT_USE_ZLIB
    // only check gzsize if we are actually a gzip file, just checking for
-   // "gz" is not sufficient as uncompressed files will be opened with
+   // "gz" is not sufficient as uncompressed files could be opened with
    // gzopen in "direct" mode as well
-   if (gz && !gzdirect(gz) && size > 0)
+   else if (d->gz && !gzdirect(d->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
@@ -1102,6 +1448,7 @@ unsigned long long FileFd::Size()
           return _error->Errno("lseek","Unable to seek in gzipped file");
        return size;
    }
+#endif
 
    return size;
 }
@@ -1112,11 +1459,25 @@ unsigned long long FileFd::Size()
 time_t FileFd::ModificationTime()
 {
    struct stat Buf;
-   if (fstat(iFd,&Buf) != 0)
+   if (d->pipe == false && fstat(iFd,&Buf) != 0)
    {
       _error->Errno("fstat","Unable to determine the modification time of file %s", FileName.c_str());
       return 0;
    }
+
+   // for compressor pipes st_size is undefined and at 'best' zero
+   if (d->pipe == true || S_ISFIFO(Buf.st_mode))
+   {
+      // we set it here, too, as we get the info here for free
+      // in theory the Open-methods should take care of it already
+      d->pipe = true;
+      if (stat(FileName.c_str(), &Buf) != 0)
+      {
+        _error->Errno("fstat","Unable to determine the modification time of file %s", FileName.c_str());
+        return 0;
+      }
+   }
+
    return Buf.st_mtime;
 }
                                                                        /*}}}*/
@@ -1125,15 +1486,20 @@ time_t FileFd::ModificationTime()
 /* */
 bool FileFd::Close()
 {
+   if (iFd == -1)
+      return true;
+
    bool Res = true;
    if ((Flags & AutoClose) == AutoClose)
    {
-      if (gz != NULL) {
-        int const e = gzclose(gz);
+#if APT_USE_ZLIB
+      if (d != NULL && d->gz != NULL) {
+        int const e = gzclose(d->gz);
         // gzdopen() on empty files always fails with "buffer error" here, ignore that
         if (e != 0 && e != Z_BUF_ERROR)
            Res &= _error->Errno("close",_("Problem closing the gzip file %s"), FileName.c_str());
       } else
+#endif
         if (iFd > 0 && close(iFd) != 0)
            Res &= _error->Errno("close",_("Problem closing the file %s"), FileName.c_str());
    }
@@ -1147,13 +1513,19 @@ bool FileFd::Close()
    }
 
    iFd = -1;
-   gz = NULL;
 
    if ((Flags & Fail) == Fail && (Flags & DelOnFail) == DelOnFail &&
        FileName.empty() == false)
       if (unlink(FileName.c_str()) != 0)
         Res &= _error->WarningE("unlnk",_("Problem unlinking the file %s"), FileName.c_str());
 
+   if (d != NULL)
+   {
+      if (d->compressor_pid != -1)
+        ExecWait(d->compressor_pid, "FileFdCompressor", true);
+      delete d;
+      d = NULL;
+   }
 
    return Res;
 }
@@ -1170,3 +1542,5 @@ bool FileFd::Sync()
    return true;
 }
                                                                        /*}}}*/
+
+gzFile FileFd::gzFd() { return (gzFile) d->gz; }