MCPcopy Create free account
hub / github.com/ElementsProject/elements / LockDirectory

Function LockDirectory

src/util/system.cpp:111–133  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

109static std::map<std::string, std::unique_ptr<fsbridge::FileLock>> dir_locks GUARDED_BY(cs_dir_locks);
110
111bool LockDirectory(const fs::path& directory, const std::string lockfile_name, bool probe_only)
112{
113 LOCK(cs_dir_locks);
114 fs::path pathLockFile = directory / lockfile_name;
115
116 // If a lock for this directory already exists in the map, don't try to re-lock it
117 if (dir_locks.count(fs::PathToString(pathLockFile))) {
118 return true;
119 }
120
121 // Create empty lock file if it doesn't exist.
122 FILE* file = fsbridge::fopen(pathLockFile, "a");
123 if (file) fclose(file);
124 auto lock = std::make_unique<fsbridge::FileLock>(pathLockFile);
125 if (!lock->TryLock()) {
126 return error("Error while attempting to lock directory %s: %s", fs::PathToString(directory), lock->GetReason());
127 }
128 if (!probe_only) {
129 // Lock successful and we're not just probing, put it into the map
130 dir_locks.emplace(fs::PathToString(pathLockFile), std::move(lock));
131 }
132 return true;
133}
134
135void UnlockDirectory(const fs::path& directory, const std::string& lockfile_name)
136{

Callers 5

LockDataDirectoryFunction · 0.85
OpenMethod · 0.85
TestOtherThreadFunction · 0.85
TestOtherProcessFunction · 0.85
BOOST_AUTO_TEST_CASEFunction · 0.85

Calls 6

PathToStringFunction · 0.85
fopenFunction · 0.85
errorFunction · 0.85
countMethod · 0.80
TryLockMethod · 0.80
GetReasonMethod · 0.80

Tested by 3

TestOtherThreadFunction · 0.68
TestOtherProcessFunction · 0.68
BOOST_AUTO_TEST_CASEFunction · 0.68