diff options
author | Ben Boeckel <ben.boeckel@kitware.com> | 2021-03-04 16:51:03 (GMT) |
---|---|---|
committer | Ben Boeckel <ben.boeckel@kitware.com> | 2021-03-09 14:47:26 (GMT) |
commit | 100016e9cb31aad7b642a9733409c7294cd6652f (patch) | |
tree | aaecf7949a2822a9e277512369db417f1069900e /Source/cmSystemTools.cxx | |
parent | b5f60c54d02ba9df7f2ba67200efb97446e0417a (diff) | |
download | CMake-100016e9cb31aad7b642a9733409c7294cd6652f.zip CMake-100016e9cb31aad7b642a9733409c7294cd6652f.tar.gz CMake-100016e9cb31aad7b642a9733409c7294cd6652f.tar.bz2 |
cmSystemTools: add utilities to copy a file with error handling
Diffstat (limited to 'Source/cmSystemTools.cxx')
-rw-r--r-- | Source/cmSystemTools.cxx | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/Source/cmSystemTools.cxx b/Source/cmSystemTools.cxx index db5b1ac..87ba152 100644 --- a/Source/cmSystemTools.cxx +++ b/Source/cmSystemTools.cxx @@ -87,6 +87,7 @@ # include <unistd.h> # include <sys/time.h> +# include <sys/types.h> #endif #if defined(_WIN32) && \ @@ -990,6 +991,51 @@ bool cmMoveFile(std::wstring const& oldname, std::wstring const& newname, } #endif +bool cmSystemTools::CopySingleFile(const std::string& oldname, + const std::string& newname) +{ + return cmSystemTools::CopySingleFile(oldname, newname, CopyWhen::Always) == + CopyResult::Success; +} + +cmSystemTools::CopyResult cmSystemTools::CopySingleFile( + std::string const& oldname, std::string const& newname, CopyWhen when, + std::string* err) +{ + switch (when) { + case CopyWhen::Always: + break; + case CopyWhen::OnlyIfDifferent: + if (!FilesDiffer(oldname, newname)) { + return CopyResult::Success; + } + break; + } + + mode_t perm = 0; + bool perms = SystemTools::GetPermissions(oldname, perm); + + // If files are the same do not copy + if (SystemTools::SameFile(oldname, newname)) { + return CopyResult::Success; + } + + if (!cmsys::SystemTools::CloneFileContent(oldname, newname)) { + // if cloning did not succeed, fall back to blockwise copy + if (!cmsys::SystemTools::CopyFileContentBlockwise(oldname, newname)) { + ReportError(err); + return CopyResult::Failure; + } + } + if (perms) { + if (!SystemTools::SetPermissions(newname, perm)) { + ReportError(err); + return CopyResult::Failure; + } + } + return CopyResult::Success; +} + bool cmSystemTools::RenameFile(const std::string& oldname, const std::string& newname) { |