]> git.saurik.com Git - apt.git/blobdiff - apt-pkg/contrib/fileutl.cc
merged debian-sid branch and resolved conflicts
[apt.git] / apt-pkg / contrib / fileutl.cc
index 7a24b6bb0f23e31a29284536eb27ea5121c47849..6e13b91d9580e3c55a6760d9a215ca3d268c9c54 100644 (file)
@@ -41,6 +41,8 @@
 #include <dirent.h>
 #include <signal.h>
 #include <errno.h>
+#include <glob.h>
+
 #include <set>
 #include <algorithm>
 
@@ -81,6 +83,31 @@ class FileFdPrivate {
        FileFdPrivate() : gz(NULL), bz2(NULL),
                          compressed_fd(-1), compressor_pid(-1), pipe(false),
                          openmode(0), seekpos(0) {};
+       bool CloseDown(std::string const &FileName)
+       {
+          bool Res = true;
+#ifdef HAVE_ZLIB
+          if (gz != NULL) {
+             int const e = gzclose(gz);
+             gz = NULL;
+             // gzdclose() 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());
+          }
+#endif
+#ifdef HAVE_BZ2
+          if (bz2 != NULL) {
+             BZ2_bzclose(bz2);
+             bz2 = NULL;
+          }
+#endif
+          if (compressor_pid > 0)
+             ExecWait(compressor_pid, "FileFdCompressor", true);
+          compressor_pid = -1;
+
+          return Res;
+       }
+       ~FileFdPrivate() { CloseDown(""); }
 };
 
 // RunScripts - Run a set of scripts from a configuration subtree      /*{{{*/
@@ -362,7 +389,7 @@ std::vector<string> GetListOfFilesInDir(string const &Dir, std::vector<string> c
 
    std::vector<string> List;
 
-   if (DirectoryExists(Dir.c_str()) == false)
+   if (DirectoryExists(Dir) == false)
    {
       _error->Error(_("List of files can't be created as '%s' is not a directory"), Dir.c_str());
       return List;
@@ -388,14 +415,14 @@ std::vector<string> GetListOfFilesInDir(string const &Dir, std::vector<string> c
       if (Ent->d_type != DT_REG)
 #endif
       {
-        if (RealFileExists(File.c_str()) == false)
+        if (RealFileExists(File) == false)
         {
            // do not show ignoration warnings for directories
            if (
 #ifdef _DIRENT_HAVE_D_TYPE
                Ent->d_type == DT_DIR ||
 #endif
-               DirectoryExists(File.c_str()) == true)
+               DirectoryExists(File) == true)
               continue;
            if (SilentIgnore.Match(Ent->d_name) == false)
               _error->Notice(_("Ignoring '%s' in directory '%s' as it is not a regular file"), Ent->d_name, Dir.c_str());
@@ -476,7 +503,7 @@ std::vector<string> GetListOfFilesInDir(string const &Dir, bool SortList)
 
    std::vector<string> List;
 
-   if (DirectoryExists(Dir.c_str()) == false)
+   if (DirectoryExists(Dir) == false)
    {
       _error->Error(_("List of files can't be created as '%s' is not a directory"), Dir.c_str());
       return List;
@@ -501,7 +528,7 @@ std::vector<string> GetListOfFilesInDir(string const &Dir, bool SortList)
       if (Ent->d_type != DT_REG)
 #endif
       {
-        if (RealFileExists(File.c_str()) == false)
+        if (RealFileExists(File) == false)
         {
            if (Debug == true)
               std::clog << "Bad file: " << Ent->d_name << " → it is not a real file" << std::endl;
@@ -827,6 +854,26 @@ bool ExecWait(pid_t Pid,const char *Name,bool Reap)
 }
                                                                        /*}}}*/
 
+// StartsWithGPGClearTextSignature - Check if a file is Pgp/GPG clearsigned     /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+bool StartsWithGPGClearTextSignature(string const &FileName)
+{
+   static const char* SIGMSG = "-----BEGIN PGP SIGNED MESSAGE-----\n";
+   char buffer[strlen(SIGMSG)+1];
+   FILE* gpg = fopen(FileName.c_str(), "r");
+   if (gpg == NULL)
+      return false;
+
+   char const * const test = fgets(buffer, sizeof(buffer), gpg);
+   fclose(gpg);
+   if (test == NULL || strcmp(buffer, SIGMSG) != 0)
+      return false;
+
+   return true;
+}
+
+
 // FileFd::Open - Open a file                                          /*{{{*/
 // ---------------------------------------------------------------------
 /* The most commonly used open mode combinations are given with Mode */
@@ -999,7 +1046,20 @@ bool FileFd::OpenDescriptor(int Fd, unsigned int const Mode, APT::Configuration:
 {
    Close();
    Flags = (AutoClose) ? FileFd::AutoClose : 0;
-   iFd = Fd;
+   if (AutoClose == false && (
+#ifdef HAVE_ZLIB
+       compressor.Name == "gzip" ||
+#endif
+#ifdef HAVE_BZ2
+       compressor.Name == "bzip2" ||
+#endif
+       false))
+   {
+      // Need to duplicate fd here or gzclose for cleanup will close the fd as well
+      iFd = dup(Fd);
+   }
+   else
+      iFd = Fd;
    this->FileName = "";
    if (OpenInternDescriptor(Mode, compressor) == false)
    {
@@ -1011,16 +1071,18 @@ bool FileFd::OpenDescriptor(int Fd, unsigned int const Mode, APT::Configuration:
 }
 bool FileFd::OpenInternDescriptor(unsigned int const Mode, APT::Configuration::Compressor const &compressor)
 {
+   if (compressor.Name == "." || compressor.Binary.empty() == true)
+      return true;
+
    if (d == NULL)
    {
       d = new FileFdPrivate();
       d->openmode = Mode;
       d->compressor = compressor;
    }
-   if (compressor.Name == "." || compressor.Binary.empty() == true)
-      return true;
+
 #ifdef HAVE_ZLIB
-   else if (compressor.Name == "gzip")
+   if (compressor.Name == "gzip")
    {
       if (d->gz != NULL)
       {
@@ -1040,7 +1102,7 @@ bool FileFd::OpenInternDescriptor(unsigned int const Mode, APT::Configuration::C
    }
 #endif
 #ifdef HAVE_BZ2
-   else if (compressor.Name == "bzip2")
+   if (compressor.Name == "bzip2")
    {
       if (d->bz2 != NULL)
       {
@@ -1156,8 +1218,6 @@ bool FileFd::OpenInternDescriptor(unsigned int const Mode, APT::Configuration::C
       close(Pipe[0]);
    else
       close(Pipe[1]);
-   if ((Comp == true || FileName.empty() == true) && d->compressed_fd != -1)
-      close(d->compressed_fd);
 
    return true;
 }
@@ -1169,6 +1229,12 @@ bool FileFd::OpenInternDescriptor(unsigned int const Mode, APT::Configuration::C
 FileFd::~FileFd()
 {
    Close();
+   if (d != NULL)
+   {
+      d->CloseDown(FileName);
+      delete d;
+      d = NULL;
+   }
 }
                                                                        /*}}}*/
 // FileFd::Read - Read a bit of the file                               /*{{{*/
@@ -1185,12 +1251,12 @@ bool FileFd::Read(void *To,unsigned long long Size,unsigned long long *Actual)
    do
    {
 #ifdef HAVE_ZLIB
-      if (d->gz != NULL)
+      if (d != NULL && d->gz != NULL)
         Res = gzread(d->gz,To,Size);
       else
 #endif
 #ifdef HAVE_BZ2
-      if (d->bz2 != NULL)
+      if (d != NULL && d->bz2 != NULL)
         Res = BZ2_bzread(d->bz2,To,Size);
       else
 #endif
@@ -1202,7 +1268,7 @@ bool FileFd::Read(void *To,unsigned long long Size,unsigned long long *Actual)
            continue;
         Flags |= Fail;
 #ifdef HAVE_ZLIB
-        if (d->gz != NULL)
+        if (d != NULL && d->gz != NULL)
         {
            int err;
            char const * const errmsg = gzerror(d->gz, &err);
@@ -1211,7 +1277,7 @@ bool FileFd::Read(void *To,unsigned long long Size,unsigned long long *Actual)
         }
 #endif
 #ifdef HAVE_BZ2
-        if (d->bz2 != NULL)
+        if (d != NULL && d->bz2 != NULL)
         {
            int err;
            char const * const errmsg = BZ2_bzerror(d->bz2, &err);
@@ -1224,7 +1290,8 @@ bool FileFd::Read(void *To,unsigned long long Size,unsigned long long *Actual)
       
       To = (char *)To + Res;
       Size -= Res;
-      d->seekpos += Res;
+      if (d != NULL)
+        d->seekpos += Res;
       if (Actual != 0)
         *Actual += Res;
    }
@@ -1252,7 +1319,7 @@ char* FileFd::ReadLine(char *To, unsigned long long const Size)
 {
    *To = '\0';
 #ifdef HAVE_ZLIB
-   if (d->gz != NULL)
+   if (d != NULL && d->gz != NULL)
       return gzgets(d->gz, To, Size);
 #endif
 
@@ -1283,12 +1350,12 @@ bool FileFd::Write(const void *From,unsigned long long Size)
    do
    {
 #ifdef HAVE_ZLIB
-      if (d->gz != NULL)
+      if (d != NULL && d->gz != NULL)
          Res = gzwrite(d->gz,From,Size);
       else
 #endif
 #ifdef HAVE_BZ2
-      if (d->bz2 != NULL)
+      if (d != NULL && d->bz2 != NULL)
          Res = BZ2_bzwrite(d->bz2,(void*)From,Size);
       else
 #endif
@@ -1299,7 +1366,7 @@ bool FileFd::Write(const void *From,unsigned long long Size)
       {
         Flags |= Fail;
 #ifdef HAVE_ZLIB
-        if (d->gz != NULL)
+        if (d != NULL && d->gz != NULL)
         {
            int err;
            char const * const errmsg = gzerror(d->gz, &err);
@@ -1308,7 +1375,7 @@ bool FileFd::Write(const void *From,unsigned long long Size)
         }
 #endif
 #ifdef HAVE_BZ2
-        if (d->bz2 != NULL)
+        if (d != NULL && d->bz2 != NULL)
         {
            int err;
            char const * const errmsg = BZ2_bzerror(d->bz2, &err);
@@ -1321,7 +1388,8 @@ bool FileFd::Write(const void *From,unsigned long long Size)
       
       From = (char *)From + Res;
       Size -= Res;
-      d->seekpos += Res;
+      if (d != NULL)
+        d->seekpos += Res;
    }
    while (Res > 0 && Size > 0);
    
@@ -1359,11 +1427,11 @@ bool FileFd::Write(int Fd, const void *From, unsigned long long Size)
 /* */
 bool FileFd::Seek(unsigned long long To)
 {
-   if (d->pipe == true
+   if (d != NULL && (d->pipe == true
 #ifdef HAVE_BZ2
-       || d->bz2 != NULL
+                       || d->bz2 != NULL
 #endif
-       )
+       ))
    {
       // Our poor man seeking in pipes is costly, so try to avoid it
       unsigned long long seekpos = Tell();
@@ -1393,7 +1461,7 @@ bool FileFd::Seek(unsigned long long To)
         if (d->compressed_fd > 0)
            if (lseek(d->compressed_fd, 0, SEEK_SET) != 0)
               iFd = d->compressed_fd;
-        if (iFd <= 0)
+        if (iFd < 0)
         {
            Flags |= Fail;
            return _error->Error("Reopen is not implemented for pipes opened with FileFd::OpenDescriptor()!");
@@ -1414,7 +1482,7 @@ bool FileFd::Seek(unsigned long long To)
    }
    int res;
 #ifdef HAVE_ZLIB
-   if (d->gz)
+   if (d != NULL && d->gz)
       res = gzseek(d->gz,To,SEEK_SET);
    else
 #endif
@@ -1425,7 +1493,8 @@ bool FileFd::Seek(unsigned long long To)
       return _error->Error("Unable to seek to %llu", To);
    }
 
-   d->seekpos = To;
+   if (d != NULL)
+      d->seekpos = To;
    return true;
 }
                                                                        /*}}}*/
@@ -1434,11 +1503,11 @@ bool FileFd::Seek(unsigned long long To)
 /* */
 bool FileFd::Skip(unsigned long long Over)
 {
-   if (d->pipe == true
+   if (d != NULL && (d->pipe == true
 #ifdef HAVE_BZ2
-       || d->bz2 != NULL
+                       || d->bz2 != NULL
 #endif
-       )
+       ))
    {
       d->seekpos += Over;
       char buffer[1024];
@@ -1457,7 +1526,7 @@ bool FileFd::Skip(unsigned long long Over)
 
    int res;
 #ifdef HAVE_ZLIB
-   if (d->gz != NULL)
+   if (d != NULL && d->gz != NULL)
       res = gzseek(d->gz,Over,SEEK_CUR);
    else
 #endif
@@ -1467,7 +1536,8 @@ bool FileFd::Skip(unsigned long long Over)
       Flags |= Fail;
       return _error->Error("Unable to seek ahead %llu",Over);
    }
-   d->seekpos = res;
+   if (d != NULL)
+      d->seekpos = res;
 
    return true;
 }
@@ -1478,7 +1548,7 @@ bool FileFd::Skip(unsigned long long Over)
 bool FileFd::Truncate(unsigned long long To)
 {
 #if defined HAVE_ZLIB || defined HAVE_BZ2
-   if (d->gz != NULL || d->bz2 != NULL)
+   if (d != NULL && (d->gz != NULL || d->bz2 != NULL))
    {
       Flags |= Fail;
       return _error->Error("Truncating compressed files is not implemented (%s)", FileName.c_str());
@@ -1502,16 +1572,16 @@ unsigned long long FileFd::Tell()
    // seeking around, but not all users of FileFd use always Seek() and co
    // so d->seekpos isn't always true and we can just use it as a hint if
    // we have nothing else, but not always as an authority…
-   if (d->pipe == true
+   if (d != NULL && (d->pipe == true
 #ifdef HAVE_BZ2
-       || d->bz2 != NULL
+                       || d->bz2 != NULL
 #endif
-       )
+       ))
       return d->seekpos;
 
    off_t Res;
 #ifdef HAVE_ZLIB
-   if (d->gz != NULL)
+   if (d != NULL && d->gz != NULL)
      Res = gztell(d->gz);
    else
 #endif
@@ -1521,7 +1591,8 @@ unsigned long long FileFd::Tell()
       Flags |= Fail;
       _error->Errno("lseek","Failed to determine the current file position");
    }
-   d->seekpos = Res;
+   if (d != NULL)
+      d->seekpos = Res;
    return Res;
 }
                                                                        /*}}}*/
@@ -1531,18 +1602,19 @@ unsigned long long FileFd::Tell()
 unsigned long long FileFd::FileSize()
 {
    struct stat Buf;
-   if (d->pipe == false && fstat(iFd,&Buf) != 0)
+   if ((d == NULL || d->pipe == false) && fstat(iFd,&Buf) != 0)
    {
       Flags |= Fail;
       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))
+   if ((d != NULL && 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 (d != NULL)
+        d->pipe = true;
       if (stat(FileName.c_str(), &Buf) != 0)
       {
         Flags |= Fail;
@@ -1562,11 +1634,11 @@ unsigned long long FileFd::Size()
 
    // 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
+   if (d != NULL && (d->pipe == true
 #ifdef HAVE_BZ2
-       || (d->bz2 && size > 0)
+                       || (d->bz2 && size > 0)
 #endif
-       )
+       ))
    {
       unsigned long long const oldSeek = Tell();
       char ignore[1000];
@@ -1581,7 +1653,7 @@ unsigned long long FileFd::Size()
    // only check gzsize if we are actually a gzip file, just checking for
    // "gz" is not sufficient as uncompressed files could be opened with
    // gzopen in "direct" mode as well
-   else if (d->gz && !gzdirect(d->gz) && size > 0)
+   else if (d != NULL && d->gz && !gzdirect(d->gz) && size > 0)
    {
        off_t const oldPos = lseek(iFd,0,SEEK_CUR);
        /* unfortunately zlib.h doesn't provide a gzsize(), so we have to do
@@ -1626,7 +1698,7 @@ unsigned long long FileFd::Size()
 time_t FileFd::ModificationTime()
 {
    struct stat Buf;
-   if (d->pipe == false && fstat(iFd,&Buf) != 0)
+   if ((d == NULL || d->pipe == false) && fstat(iFd,&Buf) != 0)
    {
       Flags |= Fail;
       _error->Errno("fstat","Unable to determine the modification time of file %s", FileName.c_str());
@@ -1634,11 +1706,12 @@ time_t FileFd::ModificationTime()
    }
 
    // for compressor pipes st_size is undefined and at 'best' zero
-   if (d->pipe == true || S_ISFIFO(Buf.st_mode))
+   if ((d != NULL && 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 (d != NULL)
+        d->pipe = true;
       if (stat(FileName.c_str(), &Buf) != 0)
       {
         Flags |= Fail;
@@ -1661,24 +1734,18 @@ bool FileFd::Close()
    bool Res = true;
    if ((Flags & AutoClose) == AutoClose)
    {
-#ifdef HAVE_ZLIB
-      if (d != NULL && d->gz != NULL) {
-        int const e = gzclose(d->gz);
-        // gzdclose() 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
-#ifdef HAVE_BZ2
-      if (d != NULL && d->bz2 != NULL)
-        BZ2_bzclose(d->bz2);
-      else
-#endif
-        if (iFd > 0 && close(iFd) != 0)
-           Res &= _error->Errno("close",_("Problem closing the file %s"), FileName.c_str());
+      if ((Flags & Compressed) != Compressed && iFd > 0 && close(iFd) != 0)
+        Res &= _error->Errno("close",_("Problem closing the file %s"), FileName.c_str());
+
+      if (d != NULL)
+      {
+        Res &= d->CloseDown(FileName);
+        delete d;
+        d = NULL;
+      }
    }
 
-   if ((Flags & Replace) == Replace && iFd >= 0) {
+   if ((Flags & Replace) == Replace) {
       if (rename(TemporaryFileName.c_str(), FileName.c_str()) != 0)
         Res &= _error->Errno("rename",_("Problem renaming the file %s to %s"), TemporaryFileName.c_str(), FileName.c_str());
 
@@ -1693,14 +1760,6 @@ bool FileFd::Close()
       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 > 0)
-        ExecWait(d->compressor_pid, "FileFdCompressor", true);
-      delete d;
-      d = NULL;
-   }
-
    if (Res == false)
       Flags |= Fail;
    return Res;
@@ -1711,15 +1770,42 @@ bool FileFd::Close()
 /* */
 bool FileFd::Sync()
 {
-#ifdef _POSIX_SYNCHRONIZED_IO
    if (fsync(iFd) != 0)
    {
       Flags |= Fail;
       return _error->Errno("sync",_("Problem syncing the file"));
    }
-#endif
    return true;
 }
                                                                        /*}}}*/
 
 gzFile FileFd::gzFd() { return (gzFile) d->gz; }
+
+
+// Glob - wrapper around "glob()"                                      /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+std::vector<std::string> Glob(std::string const &pattern, int flags)
+{
+   std::vector<std::string> result;
+   glob_t globbuf;
+   int glob_res, i;
+
+   glob_res = glob(pattern.c_str(),  flags, NULL, &globbuf);
+
+   if (glob_res != 0)
+   {
+      if(glob_res != GLOB_NOMATCH) {
+         _error->Errno("glob", "Problem with glob");
+         return result;
+      }
+   }
+
+   // append results
+   for(i=0;i<globbuf.gl_pathc;i++)
+      result.push_back(string(globbuf.gl_pathv[i]));
+
+   globfree(&globbuf);
+   return result;
+}
+                                                                       /*}}}*/