* apt-pkg/contrib/fileutl.{h,cc}:
[ntk/apt.git] / apt-pkg / contrib / fileutl.cc
index 95058cb..83b68e7 100644 (file)
@@ -24,6 +24,7 @@
 #include <apt-pkg/strutl.h>
 #include <apt-pkg/error.h>
 #include <apt-pkg/sptr.h>
+#include <apt-pkg/aptconfiguration.h>
 #include <apt-pkg/configuration.h>
 
 #include <cstdlib>
@@ -718,84 +719,181 @@ bool ExecWait(pid_t Pid,const char *Name,bool Reap)
 // FileFd::Open - Open a file                                          /*{{{*/
 // ---------------------------------------------------------------------
 /* The most commonly used open mode combinations are given with Mode */
-bool FileFd::Open(string FileName,OpenMode Mode, unsigned long Perms)
+bool FileFd::Open(string FileName,OpenMode Mode,CompressMode Compress, unsigned long Perms)
 {
+   if (Mode == ReadOnlyGzip)
+      return Open(FileName, ReadOnly, Gzip, Perms);
    Close();
    Flags = AutoClose;
-   switch (Mode)
-   {
-      case ReadOnly:
-      iFd = open(FileName.c_str(),O_RDONLY);
-      break;
 
-      case ReadOnlyGzip:
-      iFd = open(FileName.c_str(),O_RDONLY);
-      if (iFd > 0) {
-        gz = gzdopen (iFd, "r");
-        if (gz == NULL) {
-            close (iFd);
-            iFd = -1;
-        }
-      }
-      break;
-      
-      case WriteAtomic:
+   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)
       {
-        Flags |= Replace;
-        char *name = strdup((FileName + ".XXXXXX").c_str());
-        TemporaryFileName = string(mktemp(name));
-        iFd = open(TemporaryFileName.c_str(),O_RDWR | O_CREAT | O_EXCL,Perms);
-        free(name);
+        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;
       }
-
-      case WriteEmpty:
+   }
+   else if (Compress == Extension)
+   {
+      Compress = None;
+      std::string ext = flExtension(FileName);
+      if (ext != FileName)
       {
-        struct stat Buf;
-        if (lstat(FileName.c_str(),&Buf) == 0 && S_ISLNK(Buf.st_mode))
-           unlink(FileName.c_str());
-        iFd = open(FileName.c_str(),O_RDWR | O_CREAT | O_TRUNC,Perms);
-        break;
+        ext = "." + ext;
+        for (; compressor != compressors.end(); ++compressor)
+           if (ext == compressor->Extension)
+              break;
       }
-      
-      case WriteExists:
-      iFd = open(FileName.c_str(),O_RDWR);
-      break;
+   }
+   else if (Compress != None)
+   {
+      std::string name;
+      switch (Compress)
+      {
+      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());
+      }
+      for (; compressor != compressors.end(); ++compressor)
+        if (compressor->Name == name)
+           break;
+      if (compressor == compressors.end() && name != "gzip")
+        return _error->Error("Can't find a configured compressor %s for file %s", name.c_str(), FileName.c_str());
+   }
 
-      case WriteAny:
-      iFd = open(FileName.c_str(),O_RDWR | O_CREAT,Perms);
-      break;      
+   // 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();
+      }
+   }
 
-      case WriteTemp:
+   if ((Mode & Atomic) == Atomic)
+   {
+      Flags |= Replace;
+      char *name = strdup((FileName + ".XXXXXX").c_str());
+      TemporaryFileName = string(mktemp(name));
+      free(name);
+   }
+   else if ((Mode & (Exclusive | Create)) == (Exclusive | Create))
+   {
+      // for atomic, this will be done by rename in Close()
       unlink(FileName.c_str());
-      iFd = open(FileName.c_str(),O_RDWR | O_CREAT | O_EXCL,Perms);
-      break;
-   }  
+   }
+   if ((Mode & Empty) == Empty)
+   {
+      struct stat Buf;
+      if (lstat(FileName.c_str(),&Buf) == 0 && S_ISLNK(Buf.st_mode))
+        unlink(FileName.c_str());
+   }
+
+   if (compressor != compressors.end())
+   {
+      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());
+
+      _error->Error("Forking external compressor %s is not implemented for %s", compressor->Name.c_str(), FileName.c_str());
+   }
+   else
+   {
+      if (TemporaryFileName.empty() == false)
+        iFd = open(TemporaryFileName.c_str(), fileflags, Perms);
+      else
+        iFd = open(FileName.c_str(), fileflags, Perms);
+
+      if (iFd != -1)
+      {
+        if (OpenInternDescriptor(Mode, Compress) == false)
+        {
+           close (iFd);
+           iFd = -1;
+        }
+      }
+   }
 
-   if (iFd < 0)
+   if (iFd == -1)
       return _error->Errno("open",_("Could not open file %s"),FileName.c_str());
-   
+
    this->FileName = FileName;
    SetCloseExec(iFd,true);
    return true;
 }
-
-bool FileFd::OpenDescriptor(int Fd, OpenMode Mode, bool AutoClose)
+                                                                       /*}}}*/
+// FileFd::OpenDescriptor - Open a filedescriptor                      /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+bool FileFd::OpenDescriptor(int Fd, OpenMode Mode, CompressMode Compress, bool AutoClose)
 {
    Close();
    Flags = (AutoClose) ? FileFd::AutoClose : 0;
    iFd = Fd;
-   if (Mode == ReadOnlyGzip) {
-      gz = gzdopen (iFd, "r");
-      if (gz == NULL) {
-        if (AutoClose)
-           close (iFd);
-        return _error->Errno("gzdopen",_("Could not open file descriptor %d"),
-                             Fd);
-      }
+   if (OpenInternDescriptor(Mode, Compress) == false)
+   {
+      if (AutoClose)
+        close (iFd);
+      return _error->Errno("gzdopen",_("Could not open file descriptor %d"), Fd);
    }
    this->FileName = "";
    return true;
+}
+bool FileFd::OpenInternDescriptor(OpenMode Mode, CompressMode Compress)
+{
+   if (Compress == None)
+      return true;
+   else if (Compress == Gzip)
+   {
+      if ((Mode & ReadWrite) == ReadWrite)
+        gz = gzdopen(iFd, "r+");
+      else if ((Mode & WriteOnly) == WriteOnly)
+        gz = gzdopen(iFd, "w");
+      else
+        gz = gzdopen (iFd, "r");
+      if (gz == NULL)
+        return false;
+   }
+   else
+      return false;
+   return true;
 }
                                                                        /*}}}*/
 // FileFd::~File - Closes the file                                     /*{{{*/
@@ -1008,6 +1106,20 @@ unsigned long long FileFd::Size()
    return size;
 }
                                                                        /*}}}*/
+// FileFd::ModificationTime - Return the time of last touch            /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+time_t FileFd::ModificationTime()
+{
+   struct stat Buf;
+   if (fstat(iFd,&Buf) != 0)
+   {
+      _error->Errno("fstat","Unable to determine the modification time of file %s", FileName.c_str());
+      return 0;
+   }
+   return Buf.st_mtime;
+}
+                                                                       /*}}}*/
 // FileFd::Close - Close the file if the close flag is set             /*{{{*/
 // ---------------------------------------------------------------------
 /* */
@@ -1031,6 +1143,7 @@ bool FileFd::Close()
         Res &= _error->Errno("rename",_("Problem renaming the file %s to %s"), TemporaryFileName.c_str(), FileName.c_str());
 
       FileName = TemporaryFileName; // for the unlink() below.
+      TemporaryFileName.clear();
    }
 
    iFd = -1;