Browse Source

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
Ben Boeckel 1 year ago
parent
commit
5cf7018af6
1 changed files with 23 additions and 14 deletions
  1. 23 14
      Source/cmFileCopier.cxx

+ 23 - 14
Source/cmFileCopier.cxx

@@ -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;
   }