]> git.saurik.com Git - apt.git/blobdiff - apt-pkg/contrib/fileutl.cc
apt: Minor typo in 'apt' man page (closes: #743657)
[apt.git] / apt-pkg / contrib / fileutl.cc
index 26945c183f00ef446dfdd2ececc10c1243b4c188..69a675648f2551a6b129dc201904a2069b666235 100644 (file)
@@ -104,7 +104,11 @@ bool RunScripts(const char *Cnf)
       {
         if (Opts->Value.empty() == true)
            continue;
-        
+
+         if(_config->FindB("Debug::RunScripts", false) == true)
+            std::clog << "Running external script: '"
+                      << Opts->Value << "'" << std::endl;
+
         if (system(Opts->Value.c_str()) != 0)
            _exit(100+Count);
       }
@@ -954,10 +958,10 @@ class FileFdPrivate {                                                     /*{{{*/
 // FileFd::Open - Open a file                                          /*{{{*/
 // ---------------------------------------------------------------------
 /* The most commonly used open mode combinations are given with Mode */
-bool FileFd::Open(string FileName,unsigned int const Mode,CompressMode Compress, unsigned long const Perms)
+bool FileFd::Open(string FileName,unsigned int const Mode,CompressMode Compress, unsigned long const AccessMode)
 {
    if (Mode == ReadOnlyGzip)
-      return Open(FileName, ReadOnly, Gzip, Perms);
+      return Open(FileName, ReadOnly, Gzip, AccessMode);
 
    if (Compress == Auto && (Mode & WriteOnly) == WriteOnly)
       return FileFdError("Autodetection on %s only works in ReadOnly openmode!", FileName.c_str());
@@ -1024,9 +1028,9 @@ bool FileFd::Open(string FileName,unsigned int const Mode,CompressMode Compress,
 
    if (compressor == compressors.end())
       return FileFdError("Can't find a match for specified compressor mode for file %s", FileName.c_str());
-   return Open(FileName, Mode, *compressor, Perms);
+   return Open(FileName, Mode, *compressor, AccessMode);
 }
-bool FileFd::Open(string FileName,unsigned int const Mode,APT::Configuration::Compressor const &compressor, unsigned long const Perms)
+bool FileFd::Open(string FileName,unsigned int const Mode,APT::Configuration::Compressor const &compressor, unsigned long const AccessMode)
 {
    Close();
    Flags = AutoClose;
@@ -1063,6 +1067,13 @@ bool FileFd::Open(string FileName,unsigned int const Mode,APT::Configuration::Co
    if_FLAGGED_SET(Exclusive, O_EXCL);
    #undef if_FLAGGED_SET
 
+   // umask() will always set the umask and return the previous value, so
+   // we first set the umask and then reset it to the old value
+   mode_t CurrentUmask = umask(0);
+   umask(CurrentUmask);
+   // calculate the actual file permissions (just like open/creat)
+   mode_t FilePermissions = (AccessMode & ~CurrentUmask);
+
    if ((Mode & Atomic) == Atomic)
    {
       char *name = strdup((FileName + ".XXXXXX").c_str());
@@ -1076,11 +1087,11 @@ bool FileFd::Open(string FileName,unsigned int const Mode,APT::Configuration::Co
       TemporaryFileName = string(name);
       free(name);
 
-      if(Perms != 600 && fchmod(iFd, Perms) == -1)
+      if(FilePermissions != 600 && fchmod(iFd, FilePermissions) == -1)
           return FileFdErrno("fchmod", "Could not change permissions for temporary file %s", TemporaryFileName.c_str());
    }
    else
-      iFd = open(FileName.c_str(), fileflags, Perms);
+      iFd = open(FileName.c_str(), fileflags, FilePermissions);
 
    this->FileName = FileName;
    if (iFd == -1 || OpenInternDescriptor(Mode, compressor) == false)
@@ -1191,7 +1202,7 @@ bool FileFd::OpenInternDescriptor(unsigned int const Mode, APT::Configuration::C
       d->openmode = Mode;
       d->compressor = compressor;
 #if defined HAVE_ZLIB || defined HAVE_BZ2 || defined HAVE_LZMA
-      if (AutoClose == false && compress_open != NULL)
+      if ((Flags & AutoClose) != AutoClose && compress_open != NULL)
       {
         // Need to duplicate fd here or gz/bz2 close for cleanup will close the fd as well
         int const internFd = dup(iFd);
@@ -1430,7 +1441,15 @@ bool FileFd::Read(void *To,unsigned long long Size,unsigned long long *Actual)
            errno = 0;
         }
         else
+        {
            Res = Size - d->lzma->stream.avail_out;
+           if (Res == 0)
+           {
+              // lzma run was okay, but produced no output…
+              Res = -1;
+              errno = EINTR;
+           }
+        }
       }
 #endif
       else
@@ -1439,7 +1458,12 @@ bool FileFd::Read(void *To,unsigned long long Size,unsigned long long *Actual)
       if (Res < 0)
       {
         if (errno == EINTR)
+        {
+           // trick the while-loop into running again
+           Res = 1;
+           errno = 0;
            continue;
+        }
         if (false)
            /* dummy so that the rest can be 'else if's */;
 #ifdef HAVE_ZLIB
@@ -2018,3 +2042,14 @@ std::string GetTempDir()
 
    return string(tmpdir);
 }
+
+bool Rename(std::string From, std::string To)
+{
+   if (rename(From.c_str(),To.c_str()) != 0)
+   {
+      _error->Error(_("rename failed, %s (%s -> %s)."),strerror(errno),
+                    From.c_str(),To.c_str());
+      return false;
+   }   
+   return true;
+}