1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-10-16 22:37:30 +08:00

cmFileCopier: remember error statuses and get their strings

The last error may have changed between the original call and the
`GetLastSystemError()` call. Remember the status explicitly and ask it
for its error string.

Reported on Discourse: https://discourse.cmake.org/t/9539
This commit is contained in:
Ben Boeckel
2023-12-01 22:18:20 -05:00
parent 0639a32d3a
commit 5cf7018af6

View File

@@ -86,10 +86,11 @@ bool cmFileCopier::SetPermissions(const std::string& toFile,
}
#endif
if (!cmSystemTools::SetPermissions(toFile, permissions)) {
auto perm_status = cmSystemTools::SetPermissions(toFile, permissions);
if (!perm_status) {
std::ostringstream e;
e << this->Name << " cannot set permissions on \"" << toFile
<< "\": " << cmSystemTools::GetLastSystemError() << ".";
<< "\": " << perm_status.GetString() << ".";
this->Status.SetError(e.str());
return false;
}
@@ -530,11 +531,13 @@ bool cmFileCopier::InstallSymlink(const std::string& fromFile,
{
// Read the original symlink.
std::string symlinkTarget;
if (!cmSystemTools::ReadSymlink(fromFile, symlinkTarget)) {
auto read_symlink_status =
cmSystemTools::ReadSymlink(fromFile, symlinkTarget);
if (!read_symlink_status) {
std::ostringstream e;
e << this->Name << " cannot read symlink \"" << fromFile
<< "\" to duplicate at \"" << toFile
<< "\": " << cmSystemTools::GetLastSystemError() << ".";
<< "\": " << read_symlink_status.GetString() << ".";
this->Status.SetError(e.str());
return false;
}
@@ -604,12 +607,15 @@ bool cmFileCopier::InstallFile(const std::string& fromFile,
this->ReportCopy(toFile, TypeFile, copy);
// Copy the file.
if (copy && !cmSystemTools::CopyAFile(fromFile, toFile, true)) {
std::ostringstream e;
e << this->Name << " cannot copy file \"" << fromFile << "\" to \""
<< toFile << "\": " << cmSystemTools::GetLastSystemError() << ".";
this->Status.SetError(e.str());
return false;
if (copy) {
auto copy_status = cmSystemTools::CopyAFile(fromFile, toFile, true);
if (!copy_status) {
std::ostringstream e;
e << this->Name << " cannot copy file \"" << fromFile << "\" to \""
<< toFile << "\": " << copy_status.GetString() << ".";
this->Status.SetError(e.str());
return false;
}
}
// Set the file modification time of the destination file.
@@ -620,10 +626,11 @@ bool cmFileCopier::InstallFile(const std::string& fromFile,
if (cmSystemTools::GetPermissions(toFile, perm)) {
cmSystemTools::SetPermissions(toFile, perm | mode_owner_write);
}
if (!cmFileTimes::Copy(fromFile, toFile)) {
auto copy_status = cmFileTimes::Copy(fromFile, toFile);
if (!copy_status) {
std::ostringstream e;
e << this->Name << " cannot set modification time on \"" << toFile
<< "\": " << cmSystemTools::GetLastSystemError() << ".";
<< "\": " << copy_status.GetString() << ".";
this->Status.SetError(e.str());
return false;
}
@@ -660,10 +667,12 @@ bool cmFileCopier::InstallDirectory(const std::string& source,
}
// Make sure the destination directory exists.
if (!cmSystemTools::MakeDirectory(destination, default_dir_mode)) {
auto makedir_status =
cmSystemTools::MakeDirectory(destination, default_dir_mode);
if (!makedir_status) {
std::ostringstream e;
e << this->Name << " cannot make directory \"" << destination
<< "\": " << cmSystemTools::GetLastSystemError() << ".";
<< "\": " << makedir_status.GetString() << ".";
this->Status.SetError(e.str());
return false;
}