mirror of
https://github.com/Kitware/CMake.git
synced 2026-04-24 07:08:38 -05:00
cmSystemTools: add utilities to copy a file with error handling
This commit is contained in:
@@ -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)
|
||||
{
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user