0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-07 10:27:47 -05:00

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
No known key found for this signature in database
3 changed files with 41 additions and 30 deletions

View file

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

View file

@ -62,29 +62,36 @@ std::optional<ConfigError> InitConfig(ArgsManager& args, SettingsAbortFn setting
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.
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)) {
const std::string cli_config_path = args.GetArg("-conf", "");
const std::string config_source = cli_config_path.empty()
? strprintf("data directory %s", fs::quoted(fs::PathToString(orig_datadir_path)))
: strprintf("command line argument %s", fs::quoted("-conf=" + cli_config_path));
const std::string error = strprintf(
"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"
"- 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 "
"includeconf= to include any other configuration files.\n"
"- Set allowignoredconf=1 option to treat this condition as a warning, not an error.",
fs::quoted(fs::PathToString(base_path)),
fs::quoted(BITCOIN_CONF_FILENAME),
fs::quoted(fs::PathToString(orig_config_path)),
config_source);
if (args.GetBoolArg("-allowignoredconf", false)) {
LogPrintf("Warning: %s\n", error);
} else {
return ConfigError{ConfigStatus::FAILED, Untranslated(error)};
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 config_source = cli_config_path.empty()
? strprintf("data directory %s", fs::quoted(fs::PathToString(orig_datadir_path)))
: strprintf("command line argument %s", fs::quoted("-conf=" + cli_config_path));
std::string error = strprintf(
"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"
"- 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 "
"includeconf= to include any other configuration files.",
fs::quoted(fs::PathToString(base_path)),
fs::quoted(BITCOIN_CONF_FILENAME),
fs::quoted(fs::PathToString(orig_config_path)),
config_source);
if (args.GetBoolArg("-allowignoredconf", false)) {
LogWarning("%s", error);
} else {
error += "\n- Set allowignoredconf=1 option to treat this condition as a warning, not an error.";
return ConfigError{ConfigStatus::FAILED, Untranslated(error)};
}
}
}

View file

@ -17,6 +17,7 @@
#include <util/translation.h>
#include <algorithm>
#include <filesystem>
#include <string>
#include <vector>
@ -122,10 +123,11 @@ bool StartLogging(const ArgsManager& args)
// Only log conf file usage message if conf file actually exists.
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));
} 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)));
} else {
// Not categorizing as "Warning" because it's the default behavior