diff options
author | Ryan Ofsky <ryan@ofsky.org> | 2023-02-27 14:21:13 -0500 |
---|---|---|
committer | Ryan Ofsky <ryan@ofsky.org> | 2023-02-27 14:21:13 -0500 |
commit | 9a9d5da11fa6033f82dcf8e2298aee29587f5396 (patch) | |
tree | cbc98608c2d6791b8d8b7cf38e9c3ce81a6b2422 | |
parent | b20b34f5b33230fe253c81008496bd9b13fd6ecf (diff) |
refactor: Stop using gArgs global in system.cpp
Most of the code in util/system.cpp that was hardcoded to use the global
ArgsManager instance `gArgs` has been changed to work with explicit ArgsManager
instances (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.
-rw-r--r-- | src/bitcoin-cli.cpp | 2 | ||||
-rw-r--r-- | src/bitcoin-wallet.cpp | 2 | ||||
-rw-r--r-- | src/bitcoind.cpp | 2 | ||||
-rw-r--r-- | src/init.cpp | 2 | ||||
-rw-r--r-- | src/init/common.cpp | 2 | ||||
-rw-r--r-- | src/qt/bitcoin.cpp | 2 | ||||
-rw-r--r-- | src/rpc/request.cpp | 2 | ||||
-rw-r--r-- | src/util/system.cpp | 20 | ||||
-rw-r--r-- | src/util/system.h | 8 |
9 files changed, 22 insertions, 20 deletions
diff --git a/src/bitcoin-cli.cpp b/src/bitcoin-cli.cpp index 3cb25e5c01..77613f389c 100644 --- a/src/bitcoin-cli.cpp +++ b/src/bitcoin-cli.cpp @@ -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; } diff --git a/src/bitcoin-wallet.cpp b/src/bitcoin-wallet.cpp index 12cb60e6de..fcb845fe64 100644 --- a/src/bitcoin-wallet.cpp +++ b/src/bitcoin-wallet.cpp @@ -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; } diff --git a/src/bitcoind.cpp b/src/bitcoind.cpp index 07002cc8a3..6851f86297 100644 --- a/src/bitcoind.cpp +++ b/src/bitcoind.cpp @@ -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)) { diff --git a/src/init.cpp b/src/init.cpp index 4e06d44cb0..281e0d4966 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -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) diff --git a/src/init/common.cpp b/src/init/common.cpp index f6f97f3914..791424f5f6 100644 --- a/src/init/common.cpp +++ b/src/init/common.cpp @@ -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); diff --git a/src/qt/bitcoin.cpp b/src/qt/bitcoin.cpp index c383c8bd58..99faa51ea0 100644 --- a/src/qt/bitcoin.cpp +++ b/src/qt/bitcoin.cpp @@ -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", "")))); diff --git a/src/rpc/request.cpp b/src/rpc/request.cpp index 6f37fe2a99..b6ef1909c9 100644 --- a/src/rpc/request.cpp +++ b/src/rpc/request.cpp @@ -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) diff --git a/src/util/system.cpp b/src/util/system.cpp index 77b659df7e..58afd264ae 100644 --- a/src/util/system.cpp +++ b/src/util/system.cpp @@ -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() diff --git a/src/util/system.h b/src/util/system.h index 14f093501a..8d4bedeb36 100644 --- a/src/util/system.h +++ b/src/util/system.h @@ -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) { |