mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-24 07:28:02 +01: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:
parent
312ec64cc0
commit
483f0dacc4
3 changed files with 41 additions and 30 deletions
|
@ -128,12 +128,14 @@ 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 (IsArgSet("-conf") && !stream.good()) {
|
// If the file is explicitly specified, it must be readable
|
||||||
error = strprintf("specified config file \"%s\" could not be opened.", fs::PathToString(conf_path));
|
if (IsArgSet("-conf") && !stream.good()) {
|
||||||
return false;
|
error = strprintf("specified config file \"%s\" could not be opened.", fs::PathToString(conf_path));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// ok to not have a config file
|
// ok to not have a config file
|
||||||
if (stream.good()) {
|
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)
|
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);
|
||||||
|
|
|
@ -62,29 +62,36 @@ 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)) {
|
||||||
const std::string cli_config_path = args.GetArg("-conf", "");
|
if (orig_config_path.empty()) {
|
||||||
const std::string config_source = cli_config_path.empty()
|
LogInfo(
|
||||||
? strprintf("data directory %s", fs::quoted(fs::PathToString(orig_datadir_path)))
|
"Data directory %s contains a %s file which is explicitly ignored using -noconf.",
|
||||||
: strprintf("command line argument %s", fs::quoted("-conf=" + cli_config_path));
|
fs::quoted(fs::PathToString(base_path)),
|
||||||
const std::string error = strprintf(
|
fs::quoted(BITCOIN_CONF_FILENAME));
|
||||||
"Data directory %1$s contains a %2$s file which is ignored, because a different configuration file "
|
} else if (!fs::equivalent(orig_config_path, base_config_path)) {
|
||||||
"%3$s from %4$s is being used instead. Possible ways to address this would be to:\n"
|
const std::string cli_config_path = args.GetArg("-conf", "");
|
||||||
"- Delete or rename the %2$s file in data directory %1$s.\n"
|
const std::string config_source = cli_config_path.empty()
|
||||||
"- Change datadir= or conf= options to specify one configuration file, not two, and use "
|
? strprintf("data directory %s", fs::quoted(fs::PathToString(orig_datadir_path)))
|
||||||
"includeconf= to include any other configuration files.\n"
|
: strprintf("command line argument %s", fs::quoted("-conf=" + cli_config_path));
|
||||||
"- Set allowignoredconf=1 option to treat this condition as a warning, not an error.",
|
std::string error = strprintf(
|
||||||
fs::quoted(fs::PathToString(base_path)),
|
"Data directory %1$s contains a %2$s file which is ignored, because a different configuration file "
|
||||||
fs::quoted(BITCOIN_CONF_FILENAME),
|
"%3$s from %4$s is being used instead. Possible ways to address this would be to:\n"
|
||||||
fs::quoted(fs::PathToString(orig_config_path)),
|
"- Delete or rename the %2$s file in data directory %1$s.\n"
|
||||||
config_source);
|
"- Change datadir= or conf= options to specify one configuration file, not two, and use "
|
||||||
if (args.GetBoolArg("-allowignoredconf", false)) {
|
"includeconf= to include any other configuration files.",
|
||||||
LogPrintf("Warning: %s\n", error);
|
fs::quoted(fs::PathToString(base_path)),
|
||||||
} else {
|
fs::quoted(BITCOIN_CONF_FILENAME),
|
||||||
return ConfigError{ConfigStatus::FAILED, Untranslated(error)};
|
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)};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Add table
Reference in a new issue