Skip to content

Commit 483f0da

Browse files
committed
args: Properly support -noconf
-noconf would previously lead to an ifstream "successfully" being opened to the ".bitcoin"-directory (not a file). (Guards against the general case of directories as configs are added in grandchild commit to this one). Other users of AbsPathForConfigVal() in combination with negated args have been updated earlier in this PR ("args: Support -nopid" and "args: Support -norpccookiefile...").
1 parent 312ec64 commit 483f0da

File tree

3 files changed

+41
-30
lines changed

3 files changed

+41
-30
lines changed

src/common/config.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,14 @@ bool ArgsManager::ReadConfigFiles(std::string& error, bool ignore_invalid_keys)
128128
}
129129

130130
const auto conf_path{GetConfigFilePath()};
131-
std::ifstream stream{conf_path};
132-
133-
// not ok to have a config file specified that cannot be opened
134-
if (IsArgSet("-conf") && !stream.good()) {
135-
error = strprintf("specified config file \"%s\" could not be opened.", fs::PathToString(conf_path));
136-
return false;
131+
std::ifstream stream;
132+
if (!conf_path.empty()) { // path is empty when -noconf is specified
133+
stream = std::ifstream{conf_path};
134+
// If the file is explicitly specified, it must be readable
135+
if (IsArgSet("-conf") && !stream.good()) {
136+
error = strprintf("specified config file \"%s\" could not be opened.", fs::PathToString(conf_path));
137+
return false;
138+
}
137139
}
138140
// ok to not have a config file
139141
if (stream.good()) {
@@ -213,7 +215,7 @@ bool ArgsManager::ReadConfigFiles(std::string& error, bool ignore_invalid_keys)
213215

214216
fs::path AbsPathForConfigVal(const ArgsManager& args, const fs::path& path, bool net_specific)
215217
{
216-
if (path.is_absolute()) {
218+
if (path.is_absolute() || path.empty()) {
217219
return path;
218220
}
219221
return fsbridge::AbsPathJoin(net_specific ? args.GetDataDirNet() : args.GetDataDirBase(), path);

src/common/init.cpp

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -62,29 +62,36 @@ std::optional<ConfigError> InitConfig(ArgsManager& args, SettingsAbortFn setting
6262
fs::create_directories(net_path / "wallets");
6363
}
6464

65-
// Show an error or warning if there is a bitcoin.conf file in the
65+
// Show an error or warn/log if there is a bitcoin.conf file in the
6666
// datadir that is being ignored.
6767
const fs::path base_config_path = base_path / BITCOIN_CONF_FILENAME;
68-
if (fs::exists(base_config_path) && !fs::equivalent(orig_config_path, base_config_path)) {
69-
const std::string cli_config_path = args.GetArg("-conf", "");
70-
const std::string config_source = cli_config_path.empty()
71-
? strprintf("data directory %s", fs::quoted(fs::PathToString(orig_datadir_path)))
72-
: strprintf("command line argument %s", fs::quoted("-conf=" + cli_config_path));
73-
const std::string error = strprintf(
74-
"Data directory %1$s contains a %2$s file which is ignored, because a different configuration file "
75-
"%3$s from %4$s is being used instead. Possible ways to address this would be to:\n"
76-
"- Delete or rename the %2$s file in data directory %1$s.\n"
77-
"- Change datadir= or conf= options to specify one configuration file, not two, and use "
78-
"includeconf= to include any other configuration files.\n"
79-
"- Set allowignoredconf=1 option to treat this condition as a warning, not an error.",
80-
fs::quoted(fs::PathToString(base_path)),
81-
fs::quoted(BITCOIN_CONF_FILENAME),
82-
fs::quoted(fs::PathToString(orig_config_path)),
83-
config_source);
84-
if (args.GetBoolArg("-allowignoredconf", false)) {
85-
LogPrintf("Warning: %s\n", error);
86-
} else {
87-
return ConfigError{ConfigStatus::FAILED, Untranslated(error)};
68+
if (fs::exists(base_config_path)) {
69+
if (orig_config_path.empty()) {
70+
LogInfo(
71+
"Data directory %s contains a %s file which is explicitly ignored using -noconf.",
72+
fs::quoted(fs::PathToString(base_path)),
73+
fs::quoted(BITCOIN_CONF_FILENAME));
74+
} else if (!fs::equivalent(orig_config_path, base_config_path)) {
75+
const std::string cli_config_path = args.GetArg("-conf", "");
76+
const std::string config_source = cli_config_path.empty()
77+
? strprintf("data directory %s", fs::quoted(fs::PathToString(orig_datadir_path)))
78+
: strprintf("command line argument %s", fs::quoted("-conf=" + cli_config_path));
79+
std::string error = strprintf(
80+
"Data directory %1$s contains a %2$s file which is ignored, because a different configuration file "
81+
"%3$s from %4$s is being used instead. Possible ways to address this would be to:\n"
82+
"- Delete or rename the %2$s file in data directory %1$s.\n"
83+
"- Change datadir= or conf= options to specify one configuration file, not two, and use "
84+
"includeconf= to include any other configuration files.",
85+
fs::quoted(fs::PathToString(base_path)),
86+
fs::quoted(BITCOIN_CONF_FILENAME),
87+
fs::quoted(fs::PathToString(orig_config_path)),
88+
config_source);
89+
if (args.GetBoolArg("-allowignoredconf", false)) {
90+
LogWarning("%s", error);
91+
} else {
92+
error += "\n- Set allowignoredconf=1 option to treat this condition as a warning, not an error.";
93+
return ConfigError{ConfigStatus::FAILED, Untranslated(error)};
94+
}
8895
}
8996
}
9097

src/init/common.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <util/translation.h>
1818

1919
#include <algorithm>
20+
#include <filesystem>
2021
#include <string>
2122
#include <vector>
2223

@@ -122,10 +123,11 @@ bool StartLogging(const ArgsManager& args)
122123

123124
// Only log conf file usage message if conf file actually exists.
124125
fs::path config_file_path = args.GetConfigFilePath();
125-
if (fs::exists(config_file_path)) {
126+
if (args.IsArgNegated("-conf")) {
127+
LogInfo("Config file: <disabled>");
128+
} else if (fs::exists(config_file_path)) {
126129
LogPrintf("Config file: %s\n", fs::PathToString(config_file_path));
127130
} else if (args.IsArgSet("-conf")) {
128-
// Warn if no conf file exists at path provided by user
129131
InitWarning(strprintf(_("The specified config file %s does not exist"), fs::PathToString(config_file_path)));
130132
} else {
131133
// Not categorizing as "Warning" because it's the default behavior

0 commit comments

Comments
 (0)