mirror of
https://github.com/ElementsProject/elements.git
synced 2026-08-15 12:51:00 +02:00
Abstract directory locking into util.cpp
This commit is contained in:
parent
5260a4aca1
commit
2f3bd47d44
4 changed files with 27 additions and 36 deletions
22
src/util.cpp
22
src/util.cpp
|
|
@ -72,6 +72,7 @@
|
|||
|
||||
#include <boost/algorithm/string/case_conv.hpp> // for to_lower()
|
||||
#include <boost/algorithm/string/predicate.hpp> // for startswith() and endswith()
|
||||
#include <boost/interprocess/sync/file_lock.hpp>
|
||||
#include <boost/program_options/detail/config_file.hpp>
|
||||
#include <boost/thread.hpp>
|
||||
#include <openssl/crypto.h>
|
||||
|
|
@ -375,6 +376,27 @@ int LogPrintStr(const std::string &str)
|
|||
return ret;
|
||||
}
|
||||
|
||||
bool LockDirectory(const fs::path& directory, const std::string lockfile_name, bool probe_only)
|
||||
{
|
||||
fs::path pathLockFile = directory / lockfile_name;
|
||||
FILE* file = fsbridge::fopen(pathLockFile, "a"); // empty lock file; created if it doesn't exist.
|
||||
if (file) fclose(file);
|
||||
|
||||
try {
|
||||
static std::map<std::string, boost::interprocess::file_lock> locks;
|
||||
boost::interprocess::file_lock& lock = locks.emplace(pathLockFile.string(), pathLockFile.string().c_str()).first->second;
|
||||
if (!lock.try_lock()) {
|
||||
return false;
|
||||
}
|
||||
if (probe_only) {
|
||||
lock.unlock();
|
||||
}
|
||||
} catch (const boost::interprocess::interprocess_exception& e) {
|
||||
return error("Error while attempting to lock directory %s: %s", directory.string(), e.what());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/** Interpret string as boolean, for argument parsing */
|
||||
static bool InterpretBool(const std::string& strValue)
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue