From 5eaf1e1be289245267263536f5464e40d2a677c0 Mon Sep 17 00:00:00 2001 From: Rolf Eike Beer Date: Mon, 9 Sep 2019 09:09:48 +0200 Subject: cmSystemTools: introduce MoveFileIfDifferent() --- Source/cmSystemTools.cxx | 12 ++++++++++++ Source/cmSystemTools.h | 4 ++++ 2 files changed, 16 insertions(+) diff --git a/Source/cmSystemTools.cxx b/Source/cmSystemTools.cxx index b7287d9..074faa5 100644 --- a/Source/cmSystemTools.cxx +++ b/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) { diff --git a/Source/cmSystemTools.h b/Source/cmSystemTools.h index 863db3f..8ca4939 100644 --- a/Source/cmSystemTools.h +++ b/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); -- cgit v0.12