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...").
This commit is contained in:
Hodlinator
2024-12-03 09:45:47 +01:00
parent 312ec64cc0
commit 483f0dacc4
3 changed files with 41 additions and 30 deletions

View File

@@ -128,13 +128,15 @@ bool ArgsManager::ReadConfigFiles(std::string& error, bool ignore_invalid_keys)
} }
const auto conf_path{GetConfigFilePath()}; const auto conf_path{GetConfigFilePath()};
std::ifstream stream{conf_path}; std::ifstream stream;
if (!conf_path.empty()) { // path is empty when -noconf is specified
// not ok to have a config file specified that cannot be opened stream = std::ifstream{conf_path};
// If the file is explicitly specified, it must be readable
if (IsArgSet("-conf") && !stream.good()) { if (IsArgSet("-conf") && !stream.good()) {
error = strprintf("specified config file \"%s\" could not be opened.", fs::PathToString(conf_path)); error = strprintf("specified config file \"%s\" could not be opened.", fs::PathToString(conf_path));
return false; return false;
} }
}
// ok to not have a config file // ok to not have a config file
if (stream.good()) { if (stream.good()) {
if (!ReadConfigStream(stream, fs::PathToString(conf_path), error, ignore_invalid_keys)) { if (!ReadConfigStream(stream, fs::PathToString(conf_path), error, ignore_invalid_keys)) {
@@ -213,7 +215,7 @@ bool ArgsManager::ReadConfigFiles(std::string& error, bool ignore_invalid_keys)
fs::path AbsPathForConfigVal(const ArgsManager& args, const fs::path& path, bool net_specific) fs::path AbsPathForConfigVal(const ArgsManager& args, const fs::path& path, bool net_specific)
{ {
if (path.is_absolute()) { if (path.is_absolute() || path.empty()) {
return path; return path;
} }
return fsbridge::AbsPathJoin(net_specific ? args.GetDataDirNet() : args.GetDataDirBase(), path); return fsbridge::AbsPathJoin(net_specific ? args.GetDataDirNet() : args.GetDataDirBase(), path);

View File

@@ -62,31 +62,38 @@ std::optional<ConfigError> InitConfig(ArgsManager& args, SettingsAbortFn setting
fs::create_directories(net_path / "wallets"); fs::create_directories(net_path / "wallets");
} }
// Show an error or warning if there is a bitcoin.conf file in the // Show an error or warn/log if there is a bitcoin.conf file in the
// datadir that is being ignored. // datadir that is being ignored.
const fs::path base_config_path = base_path / BITCOIN_CONF_FILENAME; const fs::path base_config_path = base_path / BITCOIN_CONF_FILENAME;
if (fs::exists(base_config_path) && !fs::equivalent(orig_config_path, base_config_path)) { if (fs::exists(base_config_path)) {
if (orig_config_path.empty()) {
LogInfo(
"Data directory %s contains a %s file which is explicitly ignored using -noconf.",
fs::quoted(fs::PathToString(base_path)),
fs::quoted(BITCOIN_CONF_FILENAME));
} else if (!fs::equivalent(orig_config_path, base_config_path)) {
const std::string cli_config_path = args.GetArg("-conf", ""); const std::string cli_config_path = args.GetArg("-conf", "");
const std::string config_source = cli_config_path.empty() const std::string config_source = cli_config_path.empty()
? strprintf("data directory %s", fs::quoted(fs::PathToString(orig_datadir_path))) ? strprintf("data directory %s", fs::quoted(fs::PathToString(orig_datadir_path)))
: strprintf("command line argument %s", fs::quoted("-conf=" + cli_config_path)); : strprintf("command line argument %s", fs::quoted("-conf=" + cli_config_path));
const std::string error = strprintf( std::string error = strprintf(
"Data directory %1$s contains a %2$s file which is ignored, because a different configuration file " "Data directory %1$s contains a %2$s file which is ignored, because a different configuration file "
"%3$s from %4$s is being used instead. Possible ways to address this would be to:\n" "%3$s from %4$s is being used instead. Possible ways to address this would be to:\n"
"- Delete or rename the %2$s file in data directory %1$s.\n" "- Delete or rename the %2$s file in data directory %1$s.\n"
"- Change datadir= or conf= options to specify one configuration file, not two, and use " "- Change datadir= or conf= options to specify one configuration file, not two, and use "
"includeconf= to include any other configuration files.\n" "includeconf= to include any other configuration files.",
"- Set allowignoredconf=1 option to treat this condition as a warning, not an error.",
fs::quoted(fs::PathToString(base_path)), fs::quoted(fs::PathToString(base_path)),
fs::quoted(BITCOIN_CONF_FILENAME), fs::quoted(BITCOIN_CONF_FILENAME),
fs::quoted(fs::PathToString(orig_config_path)), fs::quoted(fs::PathToString(orig_config_path)),
config_source); config_source);
if (args.GetBoolArg("-allowignoredconf", false)) { if (args.GetBoolArg("-allowignoredconf", false)) {
LogPrintf("Warning: %s\n", error); LogWarning("%s", error);
} else { } else {
error += "\n- Set allowignoredconf=1 option to treat this condition as a warning, not an error.";
return ConfigError{ConfigStatus::FAILED, Untranslated(error)}; return ConfigError{ConfigStatus::FAILED, Untranslated(error)};
} }
} }
}
// Create settings.json if -nosettings was not specified. // Create settings.json if -nosettings was not specified.
if (args.GetSettingsPath()) { if (args.GetSettingsPath()) {

View File

@@ -17,6 +17,7 @@
#include <util/translation.h> #include <util/translation.h>
#include <algorithm> #include <algorithm>
#include <filesystem>
#include <string> #include <string>
#include <vector> #include <vector>
@@ -122,10 +123,11 @@ bool StartLogging(const ArgsManager& args)
// Only log conf file usage message if conf file actually exists. // Only log conf file usage message if conf file actually exists.
fs::path config_file_path = args.GetConfigFilePath(); fs::path config_file_path = args.GetConfigFilePath();
if (fs::exists(config_file_path)) { if (args.IsArgNegated("-conf")) {
LogInfo("Config file: <disabled>");
} else if (fs::exists(config_file_path)) {
LogPrintf("Config file: %s\n", fs::PathToString(config_file_path)); LogPrintf("Config file: %s\n", fs::PathToString(config_file_path));
} else if (args.IsArgSet("-conf")) { } else if (args.IsArgSet("-conf")) {
// Warn if no conf file exists at path provided by user
InitWarning(strprintf(_("The specified config file %s does not exist"), fs::PathToString(config_file_path))); InitWarning(strprintf(_("The specified config file %s does not exist"), fs::PathToString(config_file_path)));
} else { } else {
// Not categorizing as "Warning" because it's the default behavior // Not categorizing as "Warning" because it's the default behavior