]> 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 c7f78cdfba787fecd21251c3fd3f7672c69cba05..6e13b91d9580e3c55a6760d9a215ca3d268c9c54 100644 (file)
@@ -41,6 +41,8 @@
 #include <dirent.h>
 #include <signal.h>
 #include <errno.h>
+#include <glob.h>
+
 #include <set>
 #include <algorithm>
 
@@ -387,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;
@@ -413,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());
@@ -501,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;
@@ -526,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;
@@ -852,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 */
@@ -1024,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)
    {
@@ -1183,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;
 }
@@ -1712,7 +1745,7 @@ bool FileFd::Close()
       }
    }
 
-   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());
 
@@ -1737,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;
+}
+                                                                       /*}}}*/