cmSystemTools: add utilities to copy a file with error handling

This commit is contained in:
Ben Boeckel
2021-03-04 11:51:03 -05:00
parent b5f60c54d0
commit 100016e9cb
2 changed files with 64 additions and 0 deletions
+46
View File
@@ -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)
{
+18
View File
@@ -128,6 +128,24 @@ public:
static bool SimpleGlob(const std::string& glob,
std::vector<std::string>& files, int type = 0);
enum class CopyWhen
{
Always,
OnlyIfDifferent,
};
enum class CopyResult
{
Success,
Failure,
};
/** Copy a file. */
static bool CopySingleFile(const std::string& oldname,
const std::string& newname);
static CopyResult CopySingleFile(std::string const& oldname,
std::string const& newname, CopyWhen when,
std::string* err = nullptr);
enum class Replace
{
Yes,