Support/FileSystem: Add create_hard_link implementation.
authorMichael J. Spencer <bigcheesegs@gmail.com>
Fri, 3 Dec 2010 05:58:41 +0000 (05:58 +0000)
committerMichael J. Spencer <bigcheesegs@gmail.com>
Fri, 3 Dec 2010 05:58:41 +0000 (05:58 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@120792 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Support/Unix/PathV2.inc
lib/Support/Windows/PathV2.inc

index fc0d4a09a73a44bf58620fa1d0219792075d3c32..2bde7805f0985a1da2f4b8aed18206835a47f5a2 100644 (file)
@@ -160,6 +160,19 @@ error_code create_directory(const Twine &path, bool &existed) {
   return make_error_code(errc::success);
 }
 
+error_code create_hard_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 (::link(t.begin(), f.begin()) == -1)
+    return error_code(errno, system_category());
+
+  return make_error_code(errc::success);
+}
+
 error_code exists(const Twine &path, bool &result) {
   SmallString<128> path_storage;
   StringRef p = path.toNullTerminatedStringRef(path_storage);
index 4adbda3cab1e52bb59a050b7d1537f87e2e41222..d374dccdeb6ebf8aadc184d51fa6f67d8fc85a31 100644 (file)
@@ -201,6 +201,25 @@ error_code create_directory(const Twine &path, bool &existed) {
   return make_error_code(errc::success);
 }
 
+error_code create_hard_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 (!::CreateHardLinkW(wide_from.begin(), wide_to.begin(), NULL))
+    return make_error_code(windows_error(::GetLastError()));
+
+  return make_error_code(errc::success);
+}
+
 error_code exists(const Twine &path, bool &result) {
   SmallString<128> path_storage;
   SmallVector<wchar_t, 128> path_utf16;