| 109 | static std::map<std::string, std::unique_ptr<fsbridge::FileLock>> dir_locks GUARDED_BY(cs_dir_locks); |
| 110 | |
| 111 | bool 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 | |
| 135 | void UnlockDirectory(const fs::path& directory, const std::string& lockfile_name) |
| 136 | { |