Skip to content

Commit

Permalink
[Support] Make sure LockFileManager works correctly with relative paths.
Browse files Browse the repository at this point in the history
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@204426 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
akyrtzi committed Mar 21, 2014
1 parent c53b3db commit fb307be
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/Support/LockFileManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"
#include <sys/stat.h>
#include <sys/types.h>
Expand Down Expand Up @@ -115,7 +116,8 @@ LockFileManager::LockFileManager(StringRef FileName)
while (1) {
// 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());
sys::fs::create_link(sys::path::filename(UniqueLockFileName.str()),
LockFileName.str());
if (EC == errc::success)
return;

Expand Down
37 changes: 37 additions & 0 deletions unittests/Support/LockFileManagerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,41 @@ TEST(LockFileManagerTest, LinkLockExists) {
ASSERT_FALSE(EC);
}


TEST(LockFileManagerTest, RelativePath) {
SmallString<64> TmpDir;
error_code EC;
EC = sys::fs::createUniqueDirectory("LockFileManagerTestDir", TmpDir);
ASSERT_FALSE(EC);

char PathBuf[1024];
const char *OrigPath = getcwd(PathBuf, 1024);
chdir(TmpDir.c_str());

sys::fs::create_directory("inner");
SmallString<64> LockedFile("inner");
sys::path::append(LockedFile, "file");

SmallString<64> FileLock(LockedFile);
FileLock += ".lock";

{
// The lock file should not exist, so we should successfully acquire it.
LockFileManager Locked(LockedFile);
EXPECT_EQ(LockFileManager::LFS_Owned, Locked.getState());
EXPECT_TRUE(sys::fs::exists(FileLock.str()));
}

// Now that the lock is out of scope, the file should be gone.
EXPECT_FALSE(sys::fs::exists(LockedFile.str()));
EXPECT_FALSE(sys::fs::exists(FileLock.str()));

EC = sys::fs::remove("inner");
ASSERT_FALSE(EC);
EC = sys::fs::remove(StringRef(TmpDir));
ASSERT_FALSE(EC);

chdir(OrigPath);
}

} // end anonymous namespace

0 comments on commit fb307be

Please sign in to comment.