mirror of
https://github.com/dashpay/dash.git
synced 2024-12-25 20:12:57 +01:00
Merge #12630: Provide useful error message if datadir is not writable.
8674e74
Provide relevant error message if datadir is not writable. (murrayn)
Pull request description:
If the --datadir exists, but is not writable, the current error message on startup is 'Cannot obtain a lock on data directory foo. Bitcoin Core is probably already running.' This is misleading.
I believe this PR addresses #11668, although the issue is not Windows-specific.
Tree-SHA512: 10cbbaea433072aee4fb3e8938a72073c7a5c841f7a7685c9e12549c322b2925c7d34bac254ac33021b23132bfc352c058712bc9542298cf86f8fd9757f528b2
This commit is contained in:
parent
162bba0a6c
commit
ef0a58ba37
@ -1550,6 +1550,9 @@ static bool LockDataDirectory(bool probeOnly)
|
||||
{
|
||||
// Make sure only a single Dash Core process is using the data directory.
|
||||
fs::path datadir = GetDataDir();
|
||||
if (!DirIsWritable(datadir)) {
|
||||
return InitError(strprintf(_("Cannot write to data directory '%s'; check permissions."), datadir.string()));
|
||||
}
|
||||
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)));
|
||||
}
|
||||
|
@ -1195,4 +1195,20 @@ BOOST_AUTO_TEST_CASE(test_LockDirectory)
|
||||
fs::remove_all(dirname);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(test_DirIsWritable)
|
||||
{
|
||||
// Should be able to write to the system tmp dir.
|
||||
fs::path tmpdirname = fs::temp_directory_path();
|
||||
BOOST_CHECK_EQUAL(DirIsWritable(tmpdirname), true);
|
||||
|
||||
// Should not be able to write to a non-existent dir.
|
||||
tmpdirname = fs::temp_directory_path() / fs::unique_path();
|
||||
BOOST_CHECK_EQUAL(DirIsWritable(tmpdirname), false);
|
||||
|
||||
fs::create_directory(tmpdirname);
|
||||
// Should be able to write to it now.
|
||||
BOOST_CHECK_EQUAL(DirIsWritable(tmpdirname), true);
|
||||
fs::remove(tmpdirname);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
13
src/util.cpp
13
src/util.cpp
@ -197,6 +197,19 @@ void ReleaseDirectoryLocks()
|
||||
dir_locks.clear();
|
||||
}
|
||||
|
||||
bool DirIsWritable(const fs::path& directory)
|
||||
{
|
||||
fs::path tmpFile = directory / fs::unique_path();
|
||||
|
||||
FILE* file = fsbridge::fopen(tmpFile, "a");
|
||||
if (!file) return false;
|
||||
|
||||
fclose(file);
|
||||
remove(tmpFile);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Interpret a string argument as a boolean.
|
||||
*
|
||||
|
@ -97,6 +97,7 @@ int RaiseFileDescriptorLimit(int nMinFD);
|
||||
void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length);
|
||||
bool RenameOver(fs::path src, fs::path dest);
|
||||
bool LockDirectory(const fs::path& directory, const std::string lockfile_name, bool probe_only=false);
|
||||
bool DirIsWritable(const fs::path& directory);
|
||||
|
||||
/** Release all directory locks. This is used for unit testing only, at runtime
|
||||
* the global destructor will take care of the locks.
|
||||
|
Loading…
Reference in New Issue
Block a user