mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-12-12 21:52:38 +01:00
Abstract directory locking into util.cpp
This commit is contained in:
19
src/init.cpp
19
src/init.cpp
@@ -1143,23 +1143,10 @@ bool AppInitParameterInteraction()
|
||||
|
||||
static bool LockDataDirectory(bool probeOnly)
|
||||
{
|
||||
std::string strDataDir = GetDataDir().string();
|
||||
|
||||
// Make sure only a single Bitcoin process is using the data directory.
|
||||
fs::path pathLockFile = GetDataDir() / ".lock";
|
||||
FILE* file = fsbridge::fopen(pathLockFile, "a"); // empty lock file; created if it doesn't exist.
|
||||
if (file) fclose(file);
|
||||
|
||||
try {
|
||||
static boost::interprocess::file_lock lock(pathLockFile.string().c_str());
|
||||
if (!lock.try_lock()) {
|
||||
return InitError(strprintf(_("Cannot obtain a lock on data directory %s. %s is probably already running."), strDataDir, _(PACKAGE_NAME)));
|
||||
}
|
||||
if (probeOnly) {
|
||||
lock.unlock();
|
||||
}
|
||||
} catch(const boost::interprocess::interprocess_exception& e) {
|
||||
return InitError(strprintf(_("Cannot obtain a lock on data directory %s. %s is probably already running.") + " %s.", strDataDir, _(PACKAGE_NAME), e.what()));
|
||||
fs::path datadir = GetDataDir();
|
||||
if (!LockDirectory(datadir, ".lock", probeOnly)) {
|
||||
return InitError(strprintf(_("Cannot obtain a lock on data directory %s. %s is probably already running."), datadir.string(), _(PACKAGE_NAME)));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user