]> git.saurik.com Git - apt.git/blobdiff - apt-pkg/contrib/fileutl.cc
replace usage of potential dangerous mktemp with mkstemp
[apt.git] / apt-pkg / contrib / fileutl.cc
index 0f88923cfacfe9f3408e191f417252faf0313329..3eeef58cf7731d549171100427ae73fae34a6d9e 100644 (file)
@@ -41,6 +41,8 @@
 #include <dirent.h>
 #include <signal.h>
 #include <errno.h>
 #include <dirent.h>
 #include <signal.h>
 #include <errno.h>
+#include <glob.h>
+
 #include <set>
 #include <algorithm>
 
 #include <set>
 #include <algorithm>
 
@@ -244,17 +246,20 @@ int GetLock(string File,bool Errors)
    fl.l_len = 0;
    if (fcntl(FD,F_SETLK,&fl) == -1)
    {
    fl.l_len = 0;
    if (fcntl(FD,F_SETLK,&fl) == -1)
    {
+      // always close to not leak resources
+      int Tmp = errno;
+      close(FD);
+      errno = Tmp;
+
       if (errno == ENOLCK)
       {
         _error->Warning(_("Not using locking for nfs mounted lock file %s"),File.c_str());
         return dup(0);       // Need something for the caller to close  
       if (errno == ENOLCK)
       {
         _error->Warning(_("Not using locking for nfs mounted lock file %s"),File.c_str());
         return dup(0);       // Need something for the caller to close  
-      }      
+      }
+  
       if (Errors == true)
         _error->Errno("open",_("Could not get lock %s"),File.c_str());
       
       if (Errors == true)
         _error->Errno("open",_("Could not get lock %s"),File.c_str());
       
-      int Tmp = errno;
-      close(FD);
-      errno = Tmp;
       return -1;
    }
 
       return -1;
    }
 
@@ -941,9 +946,6 @@ bool FileFd::Open(string FileName,unsigned int const Mode,APT::Configuration::Co
    if ((Mode & Atomic) == Atomic)
    {
       Flags |= Replace;
    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))
    {
    }
    else if ((Mode & (Exclusive | Create)) == (Exclusive | Create))
    {
@@ -969,8 +971,25 @@ bool FileFd::Open(string FileName,unsigned int const Mode,APT::Configuration::Co
    else if_FLAGGED_SET(Atomic, O_EXCL);
    #undef if_FLAGGED_SET
 
    else if_FLAGGED_SET(Atomic, O_EXCL);
    #undef if_FLAGGED_SET
 
-   if (TemporaryFileName.empty() == false)
-      iFd = open(TemporaryFileName.c_str(), fileflags, Perms);
+   if ((Mode & Atomic) == Atomic)
+   {
+      char *name = strdup((FileName + ".XXXXXX").c_str());
+
+      if((iFd = mkostemp(name, fileflags)) == -1)
+      {
+          free(name);
+          return FileFdErrno("mkostemp", "Could not create temporary file for %s", FileName.c_str());
+      }
+
+      TemporaryFileName = string(name);
+
+      if(fchmod(iFd, Perms) == -1)
+      {
+          free(name);
+          return FileFdErrno("fchmod", "Could not assign permissions to temporary file %s with error %s", FileName.c_str(), strerror(errno));
+      }
+      free(name);
+   }
    else
       iFd = open(FileName.c_str(), fileflags, Perms);
 
    else
       iFd = open(FileName.c_str(), fileflags, Perms);
 
@@ -1218,11 +1237,9 @@ FileFd::~FileFd()
 {
    Close();
    if (d != NULL)
 {
    Close();
    if (d != NULL)
-   {
       d->CloseDown(FileName);
       d->CloseDown(FileName);
-      delete d;
-      d = NULL;
-   }
+   delete d;
+   d = NULL;
 }
                                                                        /*}}}*/
 // FileFd::Read - Read a bit of the file                               /*{{{*/
 }
                                                                        /*}}}*/
 // FileFd::Read - Read a bit of the file                               /*{{{*/
@@ -1598,7 +1615,11 @@ unsigned long long FileFd::Size()
       char ignore[1000];
       unsigned long long read = 0;
       do {
       char ignore[1000];
       unsigned long long read = 0;
       do {
-        Read(ignore, sizeof(ignore), &read);
+        if (Read(ignore, sizeof(ignore), &read) == false)
+        {
+           Seek(oldSeek);
+           return 0;
+        }
       } while(read != 0);
       size = Tell();
       Seek(oldSeek);
       } while(read != 0);
       size = Tell();
       Seek(oldSeek);
@@ -1615,10 +1636,16 @@ unsigned long long FileFd::Size()
        * bits of the file */
        // FIXME: Size for gz-files is limited by 32bit… no largefile support
        if (lseek(iFd, -4, SEEK_END) < 0)
        * bits of the file */
        // FIXME: Size for gz-files is limited by 32bit… no largefile support
        if (lseek(iFd, -4, SEEK_END) < 0)
-         return FileFdErrno("lseek","Unable to seek to end of gzipped file");
-       size = 0L;
+       {
+         FileFdErrno("lseek","Unable to seek to end of gzipped file");
+         return 0;
+       }
+       size = 0;
        if (read(iFd, &size, 4) != 4)
        if (read(iFd, &size, 4) != 4)
-         return FileFdErrno("read","Unable to read original size of gzipped file");
+       {
+         FileFdErrno("read","Unable to read original size of gzipped file");
+         return 0;
+       }
 
 #ifdef WORDS_BIGENDIAN
        uint32_t tmp_size = size;
 
 #ifdef WORDS_BIGENDIAN
        uint32_t tmp_size = size;
@@ -1628,7 +1655,10 @@ unsigned long long FileFd::Size()
 #endif
 
        if (lseek(iFd, oldPos, SEEK_SET) < 0)
 #endif
 
        if (lseek(iFd, oldPos, SEEK_SET) < 0)
-         return FileFdErrno("lseek","Unable to seek in gzipped file");
+       {
+         FileFdErrno("lseek","Unable to seek in gzipped file");
+         return 0;
+       }
 
        return size;
    }
 
        return size;
    }
@@ -1752,3 +1782,33 @@ bool FileFd::FileFdError(const char *Description,...) {
                                                                        /*}}}*/
 
 gzFile FileFd::gzFd() { return (gzFile) d->gz; }
                                                                        /*}}}*/
 
 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;
+   unsigned int 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;
+}
+                                                                       /*}}}*/