[llvm] r203596 - Cleanup the interface for creating soft or hard links.

Rafael Espindola rafael.espindola at gmail.com
Tue Mar 11 11:40:24 PDT 2014


Author: rafael
Date: Tue Mar 11 13:40:24 2014
New Revision: 203596

URL: http://llvm.org/viewvc/llvm-project?rev=203596&view=rev
Log:
Cleanup the interface for creating soft or hard links.

Before this patch the unix code for creating hardlinks was unused. The code
for creating symbolic links was implemented in lib/Support/LockFileManager.cpp
and the code for creating hard links in lib/Support/*/Path.inc.

The only use we have for these is in LockFileManager.cpp and it can use both
soft and hard links. Just have a create_link function that creates one or the
other depending on the platform.

Modified:
    llvm/trunk/include/llvm/Support/FileSystem.h
    llvm/trunk/lib/Support/LockFileManager.cpp
    llvm/trunk/lib/Support/Unix/Path.inc
    llvm/trunk/lib/Support/Windows/Path.inc
    llvm/trunk/unittests/Support/Path.cpp

Modified: llvm/trunk/include/llvm/Support/FileSystem.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/FileSystem.h?rev=203596&r1=203595&r2=203596&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/FileSystem.h (original)
+++ llvm/trunk/include/llvm/Support/FileSystem.h Tue Mar 11 13:40:24 2014
@@ -285,13 +285,18 @@ error_code create_directories(const Twin
 ///          error if the directory already existed.
 error_code create_directory(const Twine &path, bool IgnoreExisting = true);
 
-/// @brief Create a hard link from \a from to \a to.
+/// @brief Create a link from \a from to \a to.
+///
+/// The link may be a soft or a hard link, depending on the platform. The caller
+/// may not assume which one. Currently on windows it creates a hard link since
+/// soft links require extra privileges. On unix, it creates a soft link since
+/// hard links don't work on SMB file systems.
 ///
 /// @param to The path to hard link to.
 /// @param from The path to hard link from. This is created.
-/// @returns errc::success if exists(to) && exists(from) && equivalent(to, from)
-///          , otherwise a platform specific error_code.
-error_code create_hard_link(const Twine &to, const Twine &from);
+/// @returns errc::success if the link was created, otherwise a platform
+/// specific error_code.
+error_code create_link(const Twine &to, const Twine &from);
 
 /// @brief Get the current path.
 ///

Modified: llvm/trunk/lib/Support/LockFileManager.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/LockFileManager.cpp?rev=203596&r1=203595&r2=203596&view=diff
==============================================================================
--- llvm/trunk/lib/Support/LockFileManager.cpp (original)
+++ llvm/trunk/lib/Support/LockFileManager.cpp Tue Mar 11 13:40:24 2014
@@ -67,22 +67,6 @@ bool LockFileManager::processStillExecut
   return true;
 }
 
-#if LLVM_ON_UNIX
-static error_code unix_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();
-}
-#endif
-
 LockFileManager::LockFileManager(StringRef FileName)
 {
   this->FileName = FileName;
@@ -132,20 +116,9 @@ LockFileManager::LockFileManager(StringR
   }
 
   while (1) {
-#if LLVM_ON_UNIX
-    // Create a symbolic link from the lock file name. If this succeeds, we're
-    // done. Note that we are using symbolic link because hard links are not
-    // supported by all filesystems.
-    error_code EC
-      = unix_create_symbolic_link(UniqueLockFileName.str(),
-                                        LockFileName.str());
-#else
-    // We can't use symbolic links for windows.
-    // Create a hard link from the lock file name. If this succeeds, we're done.
-    error_code EC
-      = sys::fs::create_hard_link(UniqueLockFileName.str(),
-                                        LockFileName.str());
-#endif
+    // Create a link from the lock file name. If this succeeds, we're done.
+    error_code EC =
+        sys::fs::create_link(UniqueLockFileName.str(), LockFileName.str());
     if (EC == errc::success)
       return;
 

Modified: llvm/trunk/lib/Support/Unix/Path.inc
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Unix/Path.inc?rev=203596&r1=203595&r2=203596&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Unix/Path.inc (original)
+++ llvm/trunk/lib/Support/Unix/Path.inc Tue Mar 11 13:40:24 2014
@@ -272,14 +272,16 @@ error_code create_directory(const Twine
   return error_code::success();
 }
 
-error_code create_hard_link(const Twine &to, const Twine &from) {
+// Note that we are using symbolic link because hard links are not supported by
+// all filesystems (SMB doesn't).
+error_code create_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)
+  if (::symlink(t.begin(), f.begin()) == -1)
     return error_code(errno, system_category());
 
   return error_code::success();

Modified: llvm/trunk/lib/Support/Windows/Path.inc
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Windows/Path.inc?rev=203596&r1=203595&r2=203596&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Windows/Path.inc (original)
+++ llvm/trunk/lib/Support/Windows/Path.inc Tue Mar 11 13:40:24 2014
@@ -158,7 +158,8 @@ error_code create_directory(const Twine
   return error_code::success();
 }
 
-error_code create_hard_link(const Twine &to, const Twine &from) {
+// We can't use symbolic links for windows.
+error_code create_link(const Twine &to, const Twine &from) {
   // Get arguments.
   SmallString<128> from_storage;
   SmallString<128> to_storage;

Modified: llvm/trunk/unittests/Support/Path.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/Path.cpp?rev=203596&r1=203595&r2=203596&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/Path.cpp (original)
+++ llvm/trunk/unittests/Support/Path.cpp Tue Mar 11 13:40:24 2014
@@ -299,7 +299,7 @@ TEST_F(FileSystemTest, Unique) {
 
   // Two paths representing the same file on disk should still provide the
   // same unique id.  We can test this by making a hard link.
-  ASSERT_NO_ERROR(fs::create_hard_link(Twine(TempPath), Twine(TempPath2)));
+  ASSERT_NO_ERROR(fs::create_link(Twine(TempPath), Twine(TempPath2)));
   fs::UniqueID D2;
   ASSERT_NO_ERROR(fs::getUniqueID(Twine(TempPath2), D2));
   ASSERT_EQ(D2, F1);
@@ -365,7 +365,7 @@ TEST_F(FileSystemTest, TempFiles) {
   ASSERT_FALSE(TempPath3.endswith("."));
 
   // Create a hard link to Temp1.
-  ASSERT_NO_ERROR(fs::create_hard_link(Twine(TempPath), Twine(TempPath2)));
+  ASSERT_NO_ERROR(fs::create_link(Twine(TempPath), Twine(TempPath2)));
   bool equal;
   ASSERT_NO_ERROR(fs::equivalent(Twine(TempPath), Twine(TempPath2), equal));
   EXPECT_TRUE(equal);





More information about the llvm-commits mailing list