Browse Source

Merge topic 'windows-artifact-symlinks'

d0c31cbff9 Windows: Use real artifact versioning symlinks if possible

Acked-by: Kitware Robot <[email protected]>
Merge-request: !6093
Brad King 4 years ago
parent
commit
ee87e53d37
1 changed files with 13 additions and 2 deletions
  1. 13 2
      Source/cmcmd.cxx

+ 13 - 2
Source/cmcmd.cxx

@@ -1643,10 +1643,21 @@ cmsys::Status cmcmd::SymlinkInternal(std::string const& file,
   if (cmSystemTools::FileExists(link) || cmSystemTools::FileIsSymlink(link)) {
     cmSystemTools::RemoveFile(link);
   }
+  std::string linktext = cmSystemTools::GetFilenameName(file);
 #if defined(_WIN32) && !defined(__CYGWIN__)
-  return cmSystemTools::CopyFileAlways(file, link);
+  std::string errorMessage;
+  cmsys::Status status =
+    cmSystemTools::CreateSymlink(linktext, link, &errorMessage);
+  // Creating a symlink will fail with ERROR_PRIVILEGE_NOT_HELD if the user
+  // does not have SeCreateSymbolicLinkPrivilege, or if developer mode is not
+  // active. In that case, we try to copy the file.
+  if (status.GetWindows() == ERROR_PRIVILEGE_NOT_HELD) {
+    status = cmSystemTools::CopyFileAlways(file, link);
+  } else if (!status) {
+    cmSystemTools::Error(errorMessage);
+  }
+  return status;
 #else
-  std::string linktext = cmSystemTools::GetFilenameName(file);
   return cmSystemTools::CreateSymlink(linktext, link);
 #endif
 }