Browse Source

Merge topic 'MoveIfDifferent'

5b96fd5b81 use cmSystemTools::MoveFileIfDifferent()
5eaf1e1be2 cmSystemTools: introduce MoveFileIfDifferent()

Acked-by: Kitware Robot <[email protected]>
Merge-request: !3794
Brad King 6 years ago
parent
commit
054d626c9c

+ 2 - 4
Source/cmGeneratorTarget.cxx

@@ -3417,8 +3417,7 @@ std::string cmGeneratorTarget::GetPchHeader(const std::string& config,
         file << pchEpilogue << "\n";
       }
     }
-    cmSystemTools::CopyFileIfDifferent(filename_tmp, filename);
-    cmSystemTools::RemoveFile(filename_tmp);
+    cmSystemTools::MoveFileIfDifferent(filename_tmp, filename);
   }
   return inserted.first->second;
 }
@@ -3451,8 +3450,7 @@ std::string cmGeneratorTarget::GetPchSource(const std::string& config,
       cmGeneratedFileStream file(filename_tmp);
       file << "/* generated by CMake */\n";
     }
-    cmSystemTools::CopyFileIfDifferent(filename_tmp, filename);
-    cmSystemTools::RemoveFile(filename_tmp);
+    cmSystemTools::MoveFileIfDifferent(filename_tmp, filename);
   }
   return inserted.first->second;
 }

+ 1 - 2
Source/cmLocalGenerator.cxx

@@ -2281,8 +2281,7 @@ void cmLocalGenerator::AddUnityBuild(cmGeneratorTarget* target,
           }
         }
       }
-      cmSystemTools::CopyFileIfDifferent(filename_tmp, filename);
-      cmSystemTools::RemoveFile(filename_tmp);
+      cmSystemTools::MoveFileIfDifferent(filename_tmp, filename);
 
       target->AddSource(filename, true);
 

+ 12 - 0
Source/cmSystemTools.cxx

@@ -855,6 +855,18 @@ bool cmSystemTools::RenameFile(const std::string& oldname,
 #endif
 }
 
+void cmSystemTools::MoveFileIfDifferent(const std::string& source,
+                                        const std::string& destination)
+{
+  if (FilesDiffer(source, destination)) {
+    if (RenameFile(source, destination)) {
+      return;
+    }
+    CopyFileAlways(source, destination);
+  }
+  RemoveFile(source);
+}
+
 std::string cmSystemTools::ComputeFileHash(const std::string& source,
                                            cmCryptoHash::Algo algo)
 {

+ 4 - 0
Source/cmSystemTools.h

@@ -131,6 +131,10 @@ public:
   static bool RenameFile(const std::string& oldname,
                          const std::string& newname);
 
+  //! Rename a file if contents are different, delete the source otherwise
+  static void MoveFileIfDifferent(const std::string& source,
+                                  const std::string& destination);
+
   //! Compute the hash of a file
   static std::string ComputeFileHash(const std::string& source,
                                      cmCryptoHash::Algo algo);

+ 1 - 2
Source/cmUseMangledMesaCommand.cxx

@@ -98,7 +98,6 @@ void CopyAndFullPathMesaHeader(const std::string& source,
   // close the files before attempting to copy
   fin.close();
   fout.close();
-  cmSystemTools::CopyFileIfDifferent(tempOutputFile, outFile);
-  cmSystemTools::RemoveFile(tempOutputFile);
+  cmSystemTools::MoveFileIfDifferent(tempOutputFile, outFile);
 }
 }