Skip to content

Commit 83112db

Browse files
author
MarcoFalke
committed
Merge #15864: Fix datadir handling
ffea41f Enable all tests in feature_config_args.py (Hennadii Stepanov) 66f5c17 Use CheckDataDirOption() for code uniformity (Hennadii Stepanov) 7e33a18 Fix datadir handling in bitcoin-cli (Hennadii Stepanov) b28dada Fix datadir handling in bitcoin-qt (Hennadii Stepanov) 5082409 Fix datadir handling in bitcoind (Hennadii Stepanov) 740d41c Add CheckDataDirOption() function (Hennadii Stepanov) c1f3251 Return absolute path early in AbsPathForConfigVal (Hennadii Stepanov) Pull request description: Fix #15240, see: bitcoin/bitcoin#15240 (comment) Fix #15745 Fix broken `feature_config_args.py` tests (disabled by MarcoFalke@fabe28a0cdcfa13e0e595a0905e3642a960d3077). All test are enabled now. This PR is alternative to #13621. User's `$HOME` directory is not touched unnecessarily now. ~To make reviewing easier only `bitcoind` code is modified (neither `bitcoin-cli` nor `bitcoin-qt`).~ Refs: - bitcoin/bitcoin#15745 (comment) by **laanwj** - #16220 Top commit has no ACKs. Tree-SHA512: 4a4cda10e0b67c8f374da0c9567003d2b566d948e7f8550fe246868b5794c15010e88ea206009480b9cd2f737f310a15e984f920730448f99a895893bed351df
2 parents 8b42db1 + ffea41f commit 83112db

File tree

7 files changed

+26
-18
lines changed

7 files changed

+26
-18
lines changed

src/bitcoin-cli.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ static int AppInitRPC(int argc, char* argv[])
125125
}
126126
return EXIT_SUCCESS;
127127
}
128-
if (!fs::is_directory(GetDataDir(false))) {
128+
if (!CheckDataDirOption()) {
129129
tfm::format(std::cerr, "Error: Specified data directory \"%s\" does not exist.\n", gArgs.GetArg("-datadir", "").c_str());
130130
return EXIT_FAILURE;
131131
}

src/bitcoin-wallet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ static bool WalletAppInit(int argc, char* argv[])
5757
// check for printtoconsole, allow -debug
5858
LogInstance().m_print_to_console = gArgs.GetBoolArg("-printtoconsole", gArgs.GetBoolArg("-debug", false));
5959

60-
if (!fs::is_directory(GetDataDir(false))) {
60+
if (!CheckDataDirOption()) {
6161
tfm::format(std::cerr, "Error: Specified data directory \"%s\" does not exist.\n", gArgs.GetArg("-datadir", "").c_str());
6262
return false;
6363
}

src/bitcoind.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,7 @@ static bool AppInit(int argc, char* argv[])
9595

9696
try
9797
{
98-
if (!fs::is_directory(GetDataDir(false)))
99-
{
98+
if (!CheckDataDirOption()) {
10099
return InitError(strprintf("Specified data directory \"%s\" does not exist.\n", gArgs.GetArg("-datadir", "")));
101100
}
102101
if (!gArgs.ReadConfigFiles(error, true)) {

src/qt/bitcoin.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -490,10 +490,9 @@ int GuiMain(int argc, char* argv[])
490490
if (!Intro::pickDataDirectory(*node))
491491
return EXIT_SUCCESS;
492492

493-
/// 6. Determine availability of data and blocks directory and parse bitcoin.conf
493+
/// 6. Determine availability of data directory and parse bitcoin.conf
494494
/// - Do not call GetDataDir(true) before this step finishes
495-
if (!fs::is_directory(GetDataDir(false)))
496-
{
495+
if (!CheckDataDirOption()) {
497496
node->initError(strprintf("Specified data directory \"%s\" does not exist.\n", gArgs.GetArg("-datadir", "")));
498497
QMessageBox::critical(nullptr, PACKAGE_NAME,
499498
QObject::tr("Error: Specified data directory \"%1\" does not exist.").arg(QString::fromStdString(gArgs.GetArg("-datadir", ""))));

src/util/system.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -748,8 +748,9 @@ const fs::path &GetDataDir(bool fNetSpecific)
748748
// this function
749749
if (!path.empty()) return path;
750750

751-
if (gArgs.IsArgSet("-datadir")) {
752-
path = fs::system_complete(gArgs.GetArg("-datadir", ""));
751+
std::string datadir = gArgs.GetArg("-datadir", "");
752+
if (!datadir.empty()) {
753+
path = fs::system_complete(datadir);
753754
if (!fs::is_directory(path)) {
754755
path = "";
755756
return path;
@@ -768,6 +769,12 @@ const fs::path &GetDataDir(bool fNetSpecific)
768769
return path;
769770
}
770771

772+
bool CheckDataDirOption()
773+
{
774+
std::string datadir = gArgs.GetArg("-datadir", "");
775+
return datadir.empty() || fs::is_directory(fs::system_complete(datadir));
776+
}
777+
771778
void ClearDatadirCache()
772779
{
773780
LOCK(csPathCached);
@@ -937,7 +944,7 @@ bool ArgsManager::ReadConfigFiles(std::string& error, bool ignore_invalid_keys)
937944

938945
// If datadir is changed in .conf file:
939946
ClearDatadirCache();
940-
if (!fs::is_directory(GetDataDir(false))) {
947+
if (!CheckDataDirOption()) {
941948
error = strprintf("specified data directory \"%s\" does not exist.", gArgs.GetArg("-datadir", "").c_str());
942949
return false;
943950
}
@@ -1205,6 +1212,9 @@ int64_t GetStartupTime()
12051212

12061213
fs::path AbsPathForConfigVal(const fs::path& path, bool net_specific)
12071214
{
1215+
if (path.is_absolute()) {
1216+
return path;
1217+
}
12081218
return fs::absolute(path, GetDataDir(net_specific));
12091219
}
12101220

src/util/system.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ fs::path GetDefaultDataDir();
7171
// The blocks directory is always net specific.
7272
const fs::path &GetBlocksDir();
7373
const fs::path &GetDataDir(bool fNetSpecific = true);
74+
// Return true if -datadir option points to a valid directory or is not specified.
75+
bool CheckDataDirOption();
7476
/** Tests only */
7577
void ClearDatadirCache();
7678
fs::path GetConfigFile(const std::string& confPath);

test/functional/feature_config_args.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,17 +109,15 @@ def run_test(self):
109109
f.write("datadir=" + new_data_dir + "\n")
110110
f.write(conf_file_contents)
111111

112-
# Temporarily disabled, because this test would access the user's home dir (~/.bitcoin)
113-
#self.nodes[0].assert_start_raises_init_error(['-conf=' + conf_file], 'Error reading configuration file: specified data directory "' + new_data_dir + '" does not exist.')
112+
self.nodes[0].assert_start_raises_init_error(['-conf=' + conf_file], 'Error: Error reading configuration file: specified data directory "' + new_data_dir + '" does not exist.')
114113

115114
# Create the directory and ensure the config file now works
116115
os.mkdir(new_data_dir)
117-
# Temporarily disabled, because this test would access the user's home dir (~/.bitcoin)
118-
#self.start_node(0, ['-conf='+conf_file, '-wallet=w1'])
119-
#self.stop_node(0)
120-
#assert os.path.exists(os.path.join(new_data_dir, 'regtest', 'blocks'))
121-
#if self.is_wallet_compiled():
122-
#assert os.path.exists(os.path.join(new_data_dir, 'regtest', 'wallets', 'w1'))
116+
self.start_node(0, ['-conf='+conf_file, '-wallet=w1'])
117+
self.stop_node(0)
118+
assert os.path.exists(os.path.join(new_data_dir, 'regtest', 'blocks'))
119+
if self.is_wallet_compiled():
120+
assert os.path.exists(os.path.join(new_data_dir, 'regtest', 'wallets', 'w1'))
123121

124122
# Ensure command line argument overrides datadir in conf
125123
os.mkdir(new_data_dir_2)

0 commit comments

Comments
 (0)