Browse Source

file(WRITE): Avoid toggling permissions between 644 and 664

On systems with umask 022, this function would set permissions
to 664 and restore them to 644 at the end, every single time it was
called (which is many times on e.g. install_manifest.txt).

The intent of the code was to make non-writable files temporarily
writable and to restore permissions in the end, but really, if it's
already user-writable there's no point in toggling this back and forth.
David Faure 7 years ago
parent
commit
bdd0174df1
2 changed files with 18 additions and 10 deletions
  1. 9 5
      Source/cmFileCommand.cxx
  2. 9 5
      Source/cmWriteFileCommand.cxx

+ 9 - 5
Source/cmFileCommand.cxx

@@ -208,16 +208,20 @@ bool cmFileCommand::HandleWriteCommand(std::vector<std::string> const& args,
   cmSystemTools::MakeDirectory(dir);
 
   mode_t mode = 0;
+  bool writable = false;
 
   // Set permissions to writable
   if (cmSystemTools::GetPermissions(fileName.c_str(), mode)) {
-    cmSystemTools::SetPermissions(fileName.c_str(),
 #if defined(_MSC_VER) || defined(__MINGW32__)
-                                  mode | S_IWRITE
+    writable = mode & S_IWRITE;
+    mode_t newMode = mode | S_IWRITE;
 #else
-                                  mode | S_IWUSR | S_IWGRP
+    writable = mode & S_IWUSR;
+    mode_t newMode = mode | S_IWUSR | S_IWGRP;
 #endif
-    );
+    if (!writable) {
+      cmSystemTools::SetPermissions(fileName.c_str(), newMode);
+    }
   }
   // If GetPermissions fails, pretend like it is ok. File open will fail if
   // the file is not writable
@@ -242,7 +246,7 @@ bool cmFileCommand::HandleWriteCommand(std::vector<std::string> const& args,
     return false;
   }
   file.close();
-  if (mode) {
+  if (mode && !writable) {
     cmSystemTools::SetPermissions(fileName.c_str(), mode);
   }
   return true;

+ 9 - 5
Source/cmWriteFileCommand.cxx

@@ -45,16 +45,20 @@ bool cmWriteFileCommand::InitialPass(std::vector<std::string> const& args,
   cmSystemTools::MakeDirectory(dir);
 
   mode_t mode = 0;
+  bool writable = false;
 
   // Set permissions to writable
   if (cmSystemTools::GetPermissions(fileName.c_str(), mode)) {
-    cmSystemTools::SetPermissions(fileName.c_str(),
 #if defined(_MSC_VER) || defined(__MINGW32__)
-                                  mode | S_IWRITE
+    writable = mode & S_IWRITE;
+    mode_t newMode = mode | S_IWRITE;
 #else
-                                  mode | S_IWUSR | S_IWGRP
+    writable = mode & S_IWUSR;
+    mode_t newMode = mode | S_IWUSR | S_IWGRP;
 #endif
-    );
+    if (!writable) {
+      cmSystemTools::SetPermissions(fileName.c_str(), newMode);
+    }
   }
   // If GetPermissions fails, pretend like it is ok. File open will fail if
   // the file is not writable
@@ -69,7 +73,7 @@ bool cmWriteFileCommand::InitialPass(std::vector<std::string> const& args,
   }
   file << message << std::endl;
   file.close();
-  if (mode) {
+  if (mode && !writable) {
     cmSystemTools::SetPermissions(fileName.c_str(), mode);
   }