mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-21 14:34:49 +01:00
Merge bitcoin/bitcoin#27170: refactor: Stop using gArgs global in system.cpp
9a9d5da11f
refactor: Stop using gArgs global in system.cpp (Ryan Ofsky)b20b34f5b3
refactor: Use new GetConfigFilePath function (Ryan Ofsky) Pull request description: Most of the code in `util/system.cpp` that was hardcoded to use the global `ArgsManager` instance `gArgs` has been changed to stop using it (for example in https://github.com/bitcoin/bitcoin/pull/20092). But a few hardcoded references to `gArgs` remain. This commit removes the last ones so these functions aren't reading or writing global state. Noticed these `gArgs` references while reviewing #27073 ACKs for top commit: achow101: ACK9a9d5da11f
stickies-v: ACK9a9d5da11
willcl-ark: tACK9a9d5da11
Tree-SHA512: 2c74b0d5fc83e9ed2ec6562eb26ec735512f75db8876a11a5d5f04e6cdbe0cd8beec19894091aa2cbf29319194d2429ccbf8036f5520ecc394f6fe89a0079a7b
This commit is contained in:
commit
8303f11e10
10 changed files with 25 additions and 23 deletions
|
@ -161,7 +161,7 @@ static int AppInitRPC(int argc, char* argv[])
|
|||
}
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
if (!CheckDataDirOption()) {
|
||||
if (!CheckDataDirOption(gArgs)) {
|
||||
tfm::format(std::cerr, "Error: Specified data directory \"%s\" does not exist.\n", gArgs.GetArg("-datadir", ""));
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
@ -817,7 +817,7 @@ static UniValue CallRPC(BaseRequestHandler* rh, const std::string& strMethod, co
|
|||
if (failedToGetAuthCookie) {
|
||||
throw std::runtime_error(strprintf(
|
||||
"Could not locate RPC credentials. No authentication cookie could be found, and RPC password is not set. See -rpcpassword and -stdinrpcpass. Configuration file: (%s)",
|
||||
fs::PathToString(GetConfigFile(gArgs.GetPathArg("-conf", BITCOIN_CONF_FILENAME)))));
|
||||
fs::PathToString(gArgs.GetConfigFilePath())));
|
||||
} else {
|
||||
throw std::runtime_error("Authorization failed: Incorrect rpcuser or rpcpassword");
|
||||
}
|
||||
|
|
|
@ -84,7 +84,7 @@ static std::optional<int> WalletAppInit(ArgsManager& args, int argc, char* argv[
|
|||
// check for printtoconsole, allow -debug
|
||||
LogInstance().m_print_to_console = args.GetBoolArg("-printtoconsole", args.GetBoolArg("-debug", false));
|
||||
|
||||
if (!CheckDataDirOption()) {
|
||||
if (!CheckDataDirOption(args)) {
|
||||
tfm::format(std::cerr, "Error: Specified data directory \"%s\" does not exist.\n", args.GetArg("-datadir", ""));
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
|
|
@ -150,7 +150,7 @@ static bool AppInit(NodeContext& node, int argc, char* argv[])
|
|||
std::any context{&node};
|
||||
try
|
||||
{
|
||||
if (!CheckDataDirOption()) {
|
||||
if (!CheckDataDirOption(args)) {
|
||||
return InitError(Untranslated(strprintf("Specified data directory \"%s\" does not exist.\n", args.GetArg("-datadir", ""))));
|
||||
}
|
||||
if (!args.ReadConfigFiles(error, true)) {
|
||||
|
|
|
@ -148,7 +148,7 @@ static const char* BITCOIN_PID_FILENAME = "bitcoind.pid";
|
|||
|
||||
static fs::path GetPidFile(const ArgsManager& args)
|
||||
{
|
||||
return AbsPathForConfigVal(args.GetPathArg("-pid", BITCOIN_PID_FILENAME));
|
||||
return AbsPathForConfigVal(args, args.GetPathArg("-pid", BITCOIN_PID_FILENAME));
|
||||
}
|
||||
|
||||
[[nodiscard]] static bool CreatePidFile(const ArgsManager& args)
|
||||
|
|
|
@ -45,7 +45,7 @@ void AddLoggingArgs(ArgsManager& argsman)
|
|||
void SetLoggingOptions(const ArgsManager& args)
|
||||
{
|
||||
LogInstance().m_print_to_file = !args.IsArgNegated("-debuglogfile");
|
||||
LogInstance().m_file_path = AbsPathForConfigVal(args.GetPathArg("-debuglogfile", DEFAULT_DEBUGLOGFILE));
|
||||
LogInstance().m_file_path = AbsPathForConfigVal(args, args.GetPathArg("-debuglogfile", DEFAULT_DEBUGLOGFILE));
|
||||
LogInstance().m_print_to_console = args.GetBoolArg("-printtoconsole", !args.GetBoolArg("-daemon", false));
|
||||
LogInstance().m_log_timestamps = args.GetBoolArg("-logtimestamps", DEFAULT_LOGTIMESTAMPS);
|
||||
LogInstance().m_log_time_micros = args.GetBoolArg("-logtimemicros", DEFAULT_LOGTIMEMICROS);
|
||||
|
@ -121,7 +121,7 @@ bool StartLogging(const ArgsManager& args)
|
|||
LogPrintf("Using data directory %s\n", fs::PathToString(gArgs.GetDataDirNet()));
|
||||
|
||||
// Only log conf file usage message if conf file actually exists.
|
||||
fs::path config_file_path = GetConfigFile(args.GetPathArg("-conf", BITCOIN_CONF_FILENAME));
|
||||
fs::path config_file_path = args.GetConfigFilePath();
|
||||
if (fs::exists(config_file_path)) {
|
||||
LogPrintf("Config file: %s\n", fs::PathToString(config_file_path));
|
||||
} else if (args.IsArgSet("-conf")) {
|
||||
|
|
|
@ -588,7 +588,7 @@ int GuiMain(int argc, char* argv[])
|
|||
if (!Intro::showIfNeeded(did_show_intro, prune_MiB)) return EXIT_SUCCESS;
|
||||
|
||||
/// 6a. Determine availability of data directory
|
||||
if (!CheckDataDirOption()) {
|
||||
if (!CheckDataDirOption(gArgs)) {
|
||||
InitError(strprintf(Untranslated("Specified data directory \"%s\" does not exist.\n"), gArgs.GetArg("-datadir", "")));
|
||||
QMessageBox::critical(nullptr, PACKAGE_NAME,
|
||||
QObject::tr("Error: Specified data directory \"%1\" does not exist.").arg(QString::fromStdString(gArgs.GetArg("-datadir", ""))));
|
||||
|
|
|
@ -428,7 +428,7 @@ void openDebugLogfile()
|
|||
|
||||
bool openBitcoinConf()
|
||||
{
|
||||
fs::path pathConfig = GetConfigFile(gArgs.GetPathArg("-conf", BITCOIN_CONF_FILENAME));
|
||||
fs::path pathConfig = gArgs.GetConfigFilePath();
|
||||
|
||||
/* Create the file */
|
||||
std::ofstream configFile{pathConfig, std::ios_base::app};
|
||||
|
|
|
@ -75,7 +75,7 @@ static fs::path GetAuthCookieFile(bool temp=false)
|
|||
if (temp) {
|
||||
arg += ".tmp";
|
||||
}
|
||||
return AbsPathForConfigVal(arg);
|
||||
return AbsPathForConfigVal(gArgs, arg);
|
||||
}
|
||||
|
||||
bool GenerateAuthCookie(std::string *cookie_out)
|
||||
|
|
|
@ -880,15 +880,15 @@ fs::path GetDefaultDataDir()
|
|||
#endif
|
||||
}
|
||||
|
||||
bool CheckDataDirOption()
|
||||
bool CheckDataDirOption(const ArgsManager& args)
|
||||
{
|
||||
const fs::path datadir{gArgs.GetPathArg("-datadir")};
|
||||
const fs::path datadir{args.GetPathArg("-datadir")};
|
||||
return datadir.empty() || fs::is_directory(fs::absolute(datadir));
|
||||
}
|
||||
|
||||
fs::path GetConfigFile(const fs::path& configuration_file_path)
|
||||
fs::path GetConfigFile(const ArgsManager& args, const fs::path& configuration_file_path)
|
||||
{
|
||||
return AbsPathForConfigVal(configuration_file_path, /*net_specific=*/false);
|
||||
return AbsPathForConfigVal(args, configuration_file_path, /*net_specific=*/false);
|
||||
}
|
||||
|
||||
static bool GetConfigOptions(std::istream& stream, const std::string& filepath, std::string& error, std::vector<std::pair<std::string, std::string>>& options, std::list<SectionInfo>& sections)
|
||||
|
@ -981,7 +981,7 @@ bool ArgsManager::ReadConfigStream(std::istream& stream, const std::string& file
|
|||
|
||||
fs::path ArgsManager::GetConfigFilePath() const
|
||||
{
|
||||
return GetConfigFile(GetPathArg("-conf", BITCOIN_CONF_FILENAME));
|
||||
return GetConfigFile(*this, GetPathArg("-conf", BITCOIN_CONF_FILENAME));
|
||||
}
|
||||
|
||||
bool ArgsManager::ReadConfigFiles(std::string& error, bool ignore_invalid_keys)
|
||||
|
@ -1040,7 +1040,7 @@ bool ArgsManager::ReadConfigFiles(std::string& error, bool ignore_invalid_keys)
|
|||
const size_t default_includes = add_includes({});
|
||||
|
||||
for (const std::string& conf_file_name : conf_file_names) {
|
||||
std::ifstream conf_file_stream{GetConfigFile(fs::PathFromString(conf_file_name))};
|
||||
std::ifstream conf_file_stream{GetConfigFile(*this, fs::PathFromString(conf_file_name))};
|
||||
if (conf_file_stream.good()) {
|
||||
if (!ReadConfigStream(conf_file_stream, conf_file_name, error, ignore_invalid_keys)) {
|
||||
return false;
|
||||
|
@ -1068,8 +1068,8 @@ bool ArgsManager::ReadConfigFiles(std::string& error, bool ignore_invalid_keys)
|
|||
}
|
||||
|
||||
// If datadir is changed in .conf file:
|
||||
gArgs.ClearPathCache();
|
||||
if (!CheckDataDirOption()) {
|
||||
ClearPathCache();
|
||||
if (!CheckDataDirOption(*this)) {
|
||||
error = strprintf("specified data directory \"%s\" does not exist.", GetArg("-datadir", ""));
|
||||
return false;
|
||||
}
|
||||
|
@ -1409,12 +1409,12 @@ int64_t GetStartupTime()
|
|||
return nStartupTime;
|
||||
}
|
||||
|
||||
fs::path AbsPathForConfigVal(const fs::path& path, bool net_specific)
|
||||
fs::path AbsPathForConfigVal(const ArgsManager& args, const fs::path& path, bool net_specific)
|
||||
{
|
||||
if (path.is_absolute()) {
|
||||
return path;
|
||||
}
|
||||
return fsbridge::AbsPathJoin(net_specific ? gArgs.GetDataDirNet() : gArgs.GetDataDirBase(), path);
|
||||
return fsbridge::AbsPathJoin(net_specific ? args.GetDataDirNet() : args.GetDataDirBase(), path);
|
||||
}
|
||||
|
||||
void ScheduleBatchPriority()
|
||||
|
|
|
@ -33,6 +33,7 @@
|
|||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
class ArgsManager;
|
||||
class UniValue;
|
||||
|
||||
// Application startup time (used for uptime calculation)
|
||||
|
@ -96,8 +97,8 @@ void ReleaseDirectoryLocks();
|
|||
bool TryCreateDirectories(const fs::path& p);
|
||||
fs::path GetDefaultDataDir();
|
||||
// Return true if -datadir option points to a valid directory or is not specified.
|
||||
bool CheckDataDirOption();
|
||||
fs::path GetConfigFile(const fs::path& configuration_file_path);
|
||||
bool CheckDataDirOption(const ArgsManager& args);
|
||||
fs::path GetConfigFile(const ArgsManager& args, const fs::path& configuration_file_path);
|
||||
#ifdef WIN32
|
||||
fs::path GetSpecialFolderPath(int nFolder, bool fCreate = true);
|
||||
#endif
|
||||
|
@ -112,11 +113,12 @@ void runCommand(const std::string& strCommand);
|
|||
* Most paths passed as configuration arguments are treated as relative to
|
||||
* the datadir if they are not absolute.
|
||||
*
|
||||
* @param args Parsed arguments and settings.
|
||||
* @param path The path to be conditionally prefixed with datadir.
|
||||
* @param net_specific Use network specific datadir variant
|
||||
* @return The normalized path.
|
||||
*/
|
||||
fs::path AbsPathForConfigVal(const fs::path& path, bool net_specific = true);
|
||||
fs::path AbsPathForConfigVal(const ArgsManager& args, const fs::path& path, bool net_specific = true);
|
||||
|
||||
inline bool IsSwitchChar(char c)
|
||||
{
|
||||
|
|
Loading…
Add table
Reference in a new issue