Explorar o código

improve umask/fchmod code readability

Michael Vogt %!s(int64=12) %!d(string=hai) anos
pai
achega
db5bf949ed
Modificáronse 1 ficheiros con 8 adicións e 5 borrados
  1. 8 5
      apt-pkg/contrib/fileutl.cc

+ 8 - 5
apt-pkg/contrib/fileutl.cc

@@ -1067,9 +1067,12 @@ bool FileFd::Open(string FileName,unsigned int const Mode,APT::Configuration::Co
    if_FLAGGED_SET(Exclusive, O_EXCL);
    if_FLAGGED_SET(Exclusive, O_EXCL);
    #undef if_FLAGGED_SET
    #undef if_FLAGGED_SET
 
 
-   // there is no getumask() so we read it by setting it and reset
-   mode_t current_umask = umask(0);
-   umask(current_umask);
+   // 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)
    if ((Mode & Atomic) == Atomic)
    {
    {
@@ -1084,11 +1087,11 @@ bool FileFd::Open(string FileName,unsigned int const Mode,APT::Configuration::Co
       TemporaryFileName = string(name);
       TemporaryFileName = string(name);
       free(name);
       free(name);
 
 
-      if(AccessMode != 600 && fchmod(iFd, AccessMode & ~current_umask) == -1)
+      if(FilePermissions != 600 && fchmod(iFd, FilePermissions) == -1)
           return FileFdErrno("fchmod", "Could not change permissions for temporary file %s", TemporaryFileName.c_str());
           return FileFdErrno("fchmod", "Could not change permissions for temporary file %s", TemporaryFileName.c_str());
    }
    }
    else
    else
-      iFd = open(FileName.c_str(), fileflags, AccessMode & ~current_umask);
+      iFd = open(FileName.c_str(), fileflags, FilePermissions);
 
 
    this->FileName = FileName;
    this->FileName = FileName;
    if (iFd == -1 || OpenInternDescriptor(Mode, compressor) == false)
    if (iFd == -1 || OpenInternDescriptor(Mode, compressor) == false)