1 //===- unittests/LockFileManagerTest.cpp - LockFileManager tests ----------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/Support/LockFileManager.h"
10 #include "llvm/Support/FileSystem.h"
11 #include "llvm/Support/Path.h"
12 #include "gtest/gtest.h"
13 #include <memory>
14 
15 using namespace llvm;
16 
17 namespace {
18 
19 TEST(LockFileManagerTest, Basic) {
20   SmallString<64> TmpDir;
21   std::error_code EC;
22   EC = sys::fs::createUniqueDirectory("LockFileManagerTestDir", TmpDir);
23   ASSERT_FALSE(EC);
24 
25   SmallString<64> LockedFile(TmpDir);
26   sys::path::append(LockedFile, "file.lock");
27 
28   {
29     // The lock file should not exist, so we should successfully acquire it.
30     LockFileManager Locked1(LockedFile);
31     EXPECT_EQ(LockFileManager::LFS_Owned, Locked1.getState());
32 
33     // Attempting to reacquire the lock should fail.  Waiting on it would cause
34     // deadlock, so don't try that.
35     LockFileManager Locked2(LockedFile);
36     EXPECT_NE(LockFileManager::LFS_Owned, Locked2.getState());
37   }
38 
39   // Now that the lock is out of scope, the file should be gone.
40   EXPECT_FALSE(sys::fs::exists(StringRef(LockedFile)));
41 
42   EC = sys::fs::remove(StringRef(TmpDir));
43   ASSERT_FALSE(EC);
44 }
45 
46 TEST(LockFileManagerTest, LinkLockExists) {
47   SmallString<64> TmpDir;
48   std::error_code EC;
49   EC = sys::fs::createUniqueDirectory("LockFileManagerTestDir", TmpDir);
50   ASSERT_FALSE(EC);
51 
52   SmallString<64> LockedFile(TmpDir);
53   sys::path::append(LockedFile, "file");
54 
55   SmallString<64> FileLocK(TmpDir);
56   sys::path::append(FileLocK, "file.lock");
57 
58   SmallString<64> TmpFileLock(TmpDir);
59   sys::path::append(TmpFileLock, "file.lock-000");
60 
61   int FD;
62   EC = sys::fs::openFileForWrite(StringRef(TmpFileLock), FD);
63   ASSERT_FALSE(EC);
64 
65   int Ret = close(FD);
66   ASSERT_EQ(Ret, 0);
67 
68   EC = sys::fs::create_link(TmpFileLock.str(), FileLocK.str());
69   ASSERT_FALSE(EC);
70 
71   EC = sys::fs::remove(StringRef(TmpFileLock));
72   ASSERT_FALSE(EC);
73 
74   {
75     // The lock file doesn't point to a real file, so we should successfully
76     // acquire it.
77     LockFileManager Locked(LockedFile);
78     EXPECT_EQ(LockFileManager::LFS_Owned, Locked.getState());
79   }
80 
81   // Now that the lock is out of scope, the file should be gone.
82   EXPECT_FALSE(sys::fs::exists(StringRef(LockedFile)));
83 
84   EC = sys::fs::remove(StringRef(TmpDir));
85   ASSERT_FALSE(EC);
86 }
87 
88 
89 TEST(LockFileManagerTest, RelativePath) {
90   SmallString<64> TmpDir;
91   std::error_code EC;
92   EC = sys::fs::createUniqueDirectory("LockFileManagerTestDir", TmpDir);
93   ASSERT_FALSE(EC);
94 
95   char PathBuf[1024];
96   const char *OrigPath = getcwd(PathBuf, 1024);
97   ASSERT_FALSE(chdir(TmpDir.c_str()));
98 
99   sys::fs::create_directory("inner");
100   SmallString<64> LockedFile("inner");
101   sys::path::append(LockedFile, "file");
102 
103   SmallString<64> FileLock(LockedFile);
104   FileLock += ".lock";
105 
106   {
107     // The lock file should not exist, so we should successfully acquire it.
108     LockFileManager Locked(LockedFile);
109     EXPECT_EQ(LockFileManager::LFS_Owned, Locked.getState());
110     EXPECT_TRUE(sys::fs::exists(FileLock.str()));
111   }
112 
113   // Now that the lock is out of scope, the file should be gone.
114   EXPECT_FALSE(sys::fs::exists(LockedFile.str()));
115   EXPECT_FALSE(sys::fs::exists(FileLock.str()));
116 
117   EC = sys::fs::remove("inner");
118   ASSERT_FALSE(EC);
119 
120   ASSERT_FALSE(chdir(OrigPath));
121 
122   EC = sys::fs::remove(StringRef(TmpDir));
123   ASSERT_FALSE(EC);
124 }
125 
126 } // end anonymous namespace
127