Browse Source

Merge topic 'report-system-error'

c2d2772f15 try_compile: Improve error message when a file cannot be removed
79a2f1e22a cmcmd: Improve error message from cmake_symlink_{library,executable}
7f89053953 cmSystemTools: Return KWSys Status from CreateLink and CreateSymlink

Acked-by: Kitware Robot <[email protected]>
Merge-request: !6007
Brad King 4 years ago
parent
commit
09dee5f9ba
6 changed files with 50 additions and 35 deletions
  1. 9 5
      Source/cmCoreTryCompile.cxx
  2. 4 2
      Source/cmFileCommand.cxx
  3. 12 12
      Source/cmSystemTools.cxx
  4. 6 6
      Source/cmSystemTools.h
  5. 15 8
      Source/cmcmd.cxx
  6. 4 2
      Source/cmcmd.h

+ 9 - 5
Source/cmCoreTryCompile.cxx

@@ -1015,17 +1015,21 @@ void cmCoreTryCompile::CleanupFiles(std::string const& binDir)
           // cannot delete them immediately.  Try a few times.
           cmSystemTools::WindowsFileRetry retry =
             cmSystemTools::GetWindowsFileRetry();
-          while (!cmSystemTools::RemoveFile(fullPath) && --retry.Count &&
-                 cmSystemTools::FileExists(fullPath)) {
+          cmsys::Status status;
+          while (!((status = cmSystemTools::RemoveFile(fullPath))) &&
+                 --retry.Count && cmSystemTools::FileExists(fullPath)) {
             cmSystemTools::Delay(retry.Delay);
           }
           if (retry.Count == 0)
 #else
-          if (!cmSystemTools::RemoveFile(fullPath))
+          cmsys::Status status = cmSystemTools::RemoveFile(fullPath);
+          if (!status)
 #endif
           {
-            std::string m = "Remove failed on file: " + fullPath;
-            cmSystemTools::ReportLastSystemError(m.c_str());
+            this->Makefile->IssueMessage(
+              MessageType::FATAL_ERROR,
+              cmStrCat("The file:\n  ", fullPath,
+                       "\ncould not be removed:\n  ", status.GetString()));
           }
         }
       }

+ 4 - 2
Source/cmFileCommand.cxx

@@ -2949,9 +2949,11 @@ bool HandleCreateLinkCommand(std::vector<std::string> const& args,
 
   // Check if the command requires a symbolic link.
   if (arguments.Symbolic) {
-    completed = cmSystemTools::CreateSymlink(fileName, newFileName, &result);
+    completed = static_cast<bool>(
+      cmSystemTools::CreateSymlink(fileName, newFileName, &result));
   } else {
-    completed = cmSystemTools::CreateLink(fileName, newFileName, &result);
+    completed = static_cast<bool>(
+      cmSystemTools::CreateLink(fileName, newFileName, &result));
   }
 
   // Check if copy-on-error is enabled in the arguments.

+ 12 - 12
Source/cmSystemTools.cxx

@@ -3158,9 +3158,9 @@ std::string cmSystemTools::EncodeURL(std::string const& in, bool escapeSlashes)
   return out;
 }
 
-bool cmSystemTools::CreateSymlink(const std::string& origName,
-                                  const std::string& newName,
-                                  std::string* errorMessage)
+cmsys::Status cmSystemTools::CreateSymlink(std::string const& origName,
+                                           std::string const& newName,
+                                           std::string* errorMessage)
 {
   uv_fs_t req;
   int flags = 0;
@@ -3171,7 +3171,9 @@ bool cmSystemTools::CreateSymlink(const std::string& origName,
 #endif
   int err = uv_fs_symlink(nullptr, &req, origName.c_str(), newName.c_str(),
                           flags, nullptr);
+  cmsys::Status status;
   if (err) {
+    status = cmsys::Status::POSIX(-err);
     std::string e =
       "failed to create symbolic link '" + newName + "': " + uv_strerror(err);
     if (errorMessage) {
@@ -3179,20 +3181,20 @@ bool cmSystemTools::CreateSymlink(const std::string& origName,
     } else {
       cmSystemTools::Error(e);
     }
-    return false;
   }
-
-  return true;
+  return status;
 }
 
-bool cmSystemTools::CreateLink(const std::string& origName,
-                               const std::string& newName,
-                               std::string* errorMessage)
+cmsys::Status cmSystemTools::CreateLink(std::string const& origName,
+                                        std::string const& newName,
+                                        std::string* errorMessage)
 {
   uv_fs_t req;
   int err =
     uv_fs_link(nullptr, &req, origName.c_str(), newName.c_str(), nullptr);
+  cmsys::Status status;
   if (err) {
+    status = cmsys::Status::POSIX(-err);
     std::string e =
       "failed to create link '" + newName + "': " + uv_strerror(err);
     if (errorMessage) {
@@ -3200,10 +3202,8 @@ bool cmSystemTools::CreateLink(const std::string& origName,
     } else {
       cmSystemTools::Error(e);
     }
-    return false;
   }
-
-  return true;
+  return status;
 }
 
 cm::string_view cmSystemTools::GetSystemName()

+ 6 - 6
Source/cmSystemTools.h

@@ -489,15 +489,15 @@ public:
 
   /** Create a symbolic link if the platform supports it.  Returns whether
       creation succeeded. */
-  static bool CreateSymlink(const std::string& origName,
-                            const std::string& newName,
-                            std::string* errorMessage = nullptr);
+  static cmsys::Status CreateSymlink(std::string const& origName,
+                                     std::string const& newName,
+                                     std::string* errorMessage = nullptr);
 
   /** Create a hard link if the platform supports it.  Returns whether
       creation succeeded. */
-  static bool CreateLink(const std::string& origName,
-                         const std::string& newName,
-                         std::string* errorMessage = nullptr);
+  static cmsys::Status CreateLink(std::string const& origName,
+                                  std::string const& newName,
+                                  std::string* errorMessage = nullptr);
 
   /** Get the system name. */
   static cm::string_view GetSystemName();

+ 15 - 8
Source/cmcmd.cxx

@@ -1601,14 +1601,18 @@ int cmcmd::SymlinkLibrary(std::vector<std::string> const& args)
   cmSystemTools::ConvertToUnixSlashes(soName);
   cmSystemTools::ConvertToUnixSlashes(name);
   if (soName != realName) {
-    if (!cmcmd::SymlinkInternal(realName, soName)) {
-      cmSystemTools::ReportLastSystemError("cmake_symlink_library");
+    cmsys::Status status = cmcmd::SymlinkInternal(realName, soName);
+    if (!status) {
+      cmSystemTools::Error(
+        cmStrCat("cmake_symlink_library: System Error: ", status.GetString()));
       result = 1;
     }
   }
   if (name != soName) {
-    if (!cmcmd::SymlinkInternal(soName, name)) {
-      cmSystemTools::ReportLastSystemError("cmake_symlink_library");
+    cmsys::Status status = cmcmd::SymlinkInternal(soName, name);
+    if (!status) {
+      cmSystemTools::Error(
+        cmStrCat("cmake_symlink_library: System Error: ", status.GetString()));
       result = 1;
     }
   }
@@ -1621,21 +1625,24 @@ int cmcmd::SymlinkExecutable(std::vector<std::string> const& args)
   std::string const& realName = args[2];
   std::string const& name = args[3];
   if (name != realName) {
-    if (!cmcmd::SymlinkInternal(realName, name)) {
-      cmSystemTools::ReportLastSystemError("cmake_symlink_executable");
+    cmsys::Status status = cmcmd::SymlinkInternal(realName, name);
+    if (!status) {
+      cmSystemTools::Error(cmStrCat("cmake_symlink_executable: System Error: ",
+                                    status.GetString()));
       result = 1;
     }
   }
   return result;
 }
 
-bool cmcmd::SymlinkInternal(std::string const& file, std::string const& link)
+cmsys::Status cmcmd::SymlinkInternal(std::string const& file,
+                                     std::string const& link)
 {
   if (cmSystemTools::FileExists(link) || cmSystemTools::FileIsSymlink(link)) {
     cmSystemTools::RemoveFile(link);
   }
 #if defined(_WIN32) && !defined(__CYGWIN__)
-  return static_cast<bool>(cmSystemTools::CopyFileAlways(file, link));
+  return cmSystemTools::CopyFileAlways(file, link);
 #else
   std::string linktext = cmSystemTools::GetFilenameName(file);
   return cmSystemTools::CreateSymlink(linktext, link);

+ 4 - 2
Source/cmcmd.h

@@ -8,6 +8,8 @@
 #include <string>
 #include <vector>
 
+#include "cmsys/Status.hxx"
+
 #include "cmCryptoHash.h"
 
 class cmConsoleBuf;
@@ -28,8 +30,8 @@ protected:
                          cmCryptoHash::Algo algo);
   static int SymlinkLibrary(std::vector<std::string> const& args);
   static int SymlinkExecutable(std::vector<std::string> const& args);
-  static bool SymlinkInternal(std::string const& file,
-                              std::string const& link);
+  static cmsys::Status SymlinkInternal(std::string const& file,
+                                       std::string const& link);
   static int ExecuteEchoColor(std::vector<std::string> const& args);
   static int ExecuteLinkScript(std::vector<std::string> const& args);
   static int WindowsCEEnvironment(const char* version,