[Support/FileSystem] Introduce llvm::sys::fs::create_symbolic_link().
authorArgyrios Kyrtzidis <akyrtzi@gmail.com>
Thu, 6 Mar 2014 17:36:46 +0000 (17:36 +0000)
committerArgyrios Kyrtzidis <akyrtzi@gmail.com>
Thu, 6 Mar 2014 17:36:46 +0000 (17:36 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@203136 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Support/FileSystem.h
lib/Support/Unix/Path.inc
lib/Support/Windows/Path.inc

index 1812d24ead723f24de733c66a46287816bd3de18..c1314a2a23f528f9602b5a94db9809709c52885c 100644 (file)
@@ -293,6 +293,14 @@ error_code create_directory(const Twine &path, bool IgnoreExisting = true);
 ///          , otherwise a platform specific error_code.
 error_code create_hard_link(const Twine &to, const Twine &from);
 
+/// @brief Create a symbolic link from \a from to \a to.
+///
+/// @param to The path to link to.
+/// @param from The path to link from. This is created.
+/// @returns errc::success if successful
+///          , otherwise a platform specific error_code.
+error_code create_symbolic_link(const Twine &to, const Twine &from);
+
 /// @brief Get the current path.
 ///
 /// @param result Holds the current path on return.
index caa30c7533a27717e07c522eae3c3585e1319a50..fe3f43a6fd80d247330a4d3e976b4d14f8245f09 100644 (file)
@@ -285,6 +285,19 @@ error_code create_hard_link(const Twine &to, const Twine &from) {
   return error_code::success();
 }
 
+error_code create_symbolic_link(const Twine &to, const Twine &from) {
+  // Get arguments.
+  SmallString<128> from_storage;
+  SmallString<128> to_storage;
+  StringRef f = from.toNullTerminatedStringRef(from_storage);
+  StringRef t = to.toNullTerminatedStringRef(to_storage);
+
+  if (::symlink(t.begin(), f.begin()) == -1)
+    return error_code(errno, system_category());
+
+  return error_code::success();
+}
+
 error_code remove(const Twine &path, bool IgnoreNonExisting) {
   SmallString<128> path_storage;
   StringRef p = path.toNullTerminatedStringRef(path_storage);
index c39600357be3a7a898016a68024462d7db331af4..bb68a3eff57520428378255881ec1f485548c8bd 100644 (file)
@@ -177,6 +177,25 @@ error_code create_hard_link(const Twine &to, const Twine &from) {
   return error_code::success();
 }
 
+error_code create_symbolic_link(const Twine &to, const Twine &from) {
+  // Get arguments.
+  SmallString<128> from_storage;
+  SmallString<128> to_storage;
+  StringRef f = from.toStringRef(from_storage);
+  StringRef t = to.toStringRef(to_storage);
+
+  // Convert to utf-16.
+  SmallVector<wchar_t, 128> wide_from;
+  SmallVector<wchar_t, 128> wide_to;
+  if (error_code ec = UTF8ToUTF16(f, wide_from)) return ec;
+  if (error_code ec = UTF8ToUTF16(t, wide_to)) return ec;
+
+  if (!::CreateSymbolicLinkW(wide_from.begin(), wide_to.begin(), NULL))
+    return windows_error(::GetLastError());
+
+  return error_code::success();
+}
+
 error_code remove(const Twine &path, bool IgnoreNonExisting) {
   SmallString<128> path_storage;
   SmallVector<wchar_t, 128> path_utf16;