Skip to content

Commit 9a9d5da

Browse files
committed
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 bitcoin/bitcoin#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.
1 parent b20b34f commit 9a9d5da

File tree

9 files changed

+22
-20
lines changed

9 files changed

+22
-20
lines changed

src/bitcoin-cli.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ static int AppInitRPC(int argc, char* argv[])
161161
}
162162
return EXIT_SUCCESS;
163163
}
164-
if (!CheckDataDirOption()) {
164+
if (!CheckDataDirOption(gArgs)) {
165165
tfm::format(std::cerr, "Error: Specified data directory \"%s\" does not exist.\n", gArgs.GetArg("-datadir", ""));
166166
return EXIT_FAILURE;
167167
}

src/bitcoin-wallet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ static std::optional<int> WalletAppInit(ArgsManager& args, int argc, char* argv[
8484
// check for printtoconsole, allow -debug
8585
LogInstance().m_print_to_console = args.GetBoolArg("-printtoconsole", args.GetBoolArg("-debug", false));
8686

87-
if (!CheckDataDirOption()) {
87+
if (!CheckDataDirOption(args)) {
8888
tfm::format(std::cerr, "Error: Specified data directory \"%s\" does not exist.\n", args.GetArg("-datadir", ""));
8989
return EXIT_FAILURE;
9090
}

src/bitcoind.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ static bool AppInit(NodeContext& node, int argc, char* argv[])
150150
std::any context{&node};
151151
try
152152
{
153-
if (!CheckDataDirOption()) {
153+
if (!CheckDataDirOption(args)) {
154154
return InitError(Untranslated(strprintf("Specified data directory \"%s\" does not exist.\n", args.GetArg("-datadir", ""))));
155155
}
156156
if (!args.ReadConfigFiles(error, true)) {

src/init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ static const char* BITCOIN_PID_FILENAME = "bitcoind.pid";
148148

149149
static fs::path GetPidFile(const ArgsManager& args)
150150
{
151-
return AbsPathForConfigVal(args.GetPathArg("-pid", BITCOIN_PID_FILENAME));
151+
return AbsPathForConfigVal(args, args.GetPathArg("-pid", BITCOIN_PID_FILENAME));
152152
}
153153

154154
[[nodiscard]] static bool CreatePidFile(const ArgsManager& args)

src/init/common.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void AddLoggingArgs(ArgsManager& argsman)
4545
void SetLoggingOptions(const ArgsManager& args)
4646
{
4747
LogInstance().m_print_to_file = !args.IsArgNegated("-debuglogfile");
48-
LogInstance().m_file_path = AbsPathForConfigVal(args.GetPathArg("-debuglogfile", DEFAULT_DEBUGLOGFILE));
48+
LogInstance().m_file_path = AbsPathForConfigVal(args, args.GetPathArg("-debuglogfile", DEFAULT_DEBUGLOGFILE));
4949
LogInstance().m_print_to_console = args.GetBoolArg("-printtoconsole", !args.GetBoolArg("-daemon", false));
5050
LogInstance().m_log_timestamps = args.GetBoolArg("-logtimestamps", DEFAULT_LOGTIMESTAMPS);
5151
LogInstance().m_log_time_micros = args.GetBoolArg("-logtimemicros", DEFAULT_LOGTIMEMICROS);

src/qt/bitcoin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ int GuiMain(int argc, char* argv[])
588588
if (!Intro::showIfNeeded(did_show_intro, prune_MiB)) return EXIT_SUCCESS;
589589

590590
/// 6a. Determine availability of data directory
591-
if (!CheckDataDirOption()) {
591+
if (!CheckDataDirOption(gArgs)) {
592592
InitError(strprintf(Untranslated("Specified data directory \"%s\" does not exist.\n"), gArgs.GetArg("-datadir", "")));
593593
QMessageBox::critical(nullptr, PACKAGE_NAME,
594594
QObject::tr("Error: Specified data directory \"%1\" does not exist.").arg(QString::fromStdString(gArgs.GetArg("-datadir", ""))));

src/rpc/request.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ static fs::path GetAuthCookieFile(bool temp=false)
7575
if (temp) {
7676
arg += ".tmp";
7777
}
78-
return AbsPathForConfigVal(arg);
78+
return AbsPathForConfigVal(gArgs, arg);
7979
}
8080

8181
bool GenerateAuthCookie(std::string *cookie_out)

src/util/system.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -880,15 +880,15 @@ fs::path GetDefaultDataDir()
880880
#endif
881881
}
882882

883-
bool CheckDataDirOption()
883+
bool CheckDataDirOption(const ArgsManager& args)
884884
{
885-
const fs::path datadir{gArgs.GetPathArg("-datadir")};
885+
const fs::path datadir{args.GetPathArg("-datadir")};
886886
return datadir.empty() || fs::is_directory(fs::absolute(datadir));
887887
}
888888

889-
fs::path GetConfigFile(const fs::path& configuration_file_path)
889+
fs::path GetConfigFile(const ArgsManager& args, const fs::path& configuration_file_path)
890890
{
891-
return AbsPathForConfigVal(configuration_file_path, /*net_specific=*/false);
891+
return AbsPathForConfigVal(args, configuration_file_path, /*net_specific=*/false);
892892
}
893893

894894
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
981981

982982
fs::path ArgsManager::GetConfigFilePath() const
983983
{
984-
return GetConfigFile(GetPathArg("-conf", BITCOIN_CONF_FILENAME));
984+
return GetConfigFile(*this, GetPathArg("-conf", BITCOIN_CONF_FILENAME));
985985
}
986986

987987
bool ArgsManager::ReadConfigFiles(std::string& error, bool ignore_invalid_keys)
@@ -1040,7 +1040,7 @@ bool ArgsManager::ReadConfigFiles(std::string& error, bool ignore_invalid_keys)
10401040
const size_t default_includes = add_includes({});
10411041

10421042
for (const std::string& conf_file_name : conf_file_names) {
1043-
std::ifstream conf_file_stream{GetConfigFile(fs::PathFromString(conf_file_name))};
1043+
std::ifstream conf_file_stream{GetConfigFile(*this, fs::PathFromString(conf_file_name))};
10441044
if (conf_file_stream.good()) {
10451045
if (!ReadConfigStream(conf_file_stream, conf_file_name, error, ignore_invalid_keys)) {
10461046
return false;
@@ -1068,8 +1068,8 @@ bool ArgsManager::ReadConfigFiles(std::string& error, bool ignore_invalid_keys)
10681068
}
10691069

10701070
// If datadir is changed in .conf file:
1071-
gArgs.ClearPathCache();
1072-
if (!CheckDataDirOption()) {
1071+
ClearPathCache();
1072+
if (!CheckDataDirOption(*this)) {
10731073
error = strprintf("specified data directory \"%s\" does not exist.", GetArg("-datadir", ""));
10741074
return false;
10751075
}
@@ -1409,12 +1409,12 @@ int64_t GetStartupTime()
14091409
return nStartupTime;
14101410
}
14111411

1412-
fs::path AbsPathForConfigVal(const fs::path& path, bool net_specific)
1412+
fs::path AbsPathForConfigVal(const ArgsManager& args, const fs::path& path, bool net_specific)
14131413
{
14141414
if (path.is_absolute()) {
14151415
return path;
14161416
}
1417-
return fsbridge::AbsPathJoin(net_specific ? gArgs.GetDataDirNet() : gArgs.GetDataDirBase(), path);
1417+
return fsbridge::AbsPathJoin(net_specific ? args.GetDataDirNet() : args.GetDataDirBase(), path);
14181418
}
14191419

14201420
void ScheduleBatchPriority()

src/util/system.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <utility>
3434
#include <vector>
3535

36+
class ArgsManager;
3637
class UniValue;
3738

3839
// Application startup time (used for uptime calculation)
@@ -96,8 +97,8 @@ void ReleaseDirectoryLocks();
9697
bool TryCreateDirectories(const fs::path& p);
9798
fs::path GetDefaultDataDir();
9899
// Return true if -datadir option points to a valid directory or is not specified.
99-
bool CheckDataDirOption();
100-
fs::path GetConfigFile(const fs::path& configuration_file_path);
100+
bool CheckDataDirOption(const ArgsManager& args);
101+
fs::path GetConfigFile(const ArgsManager& args, const fs::path& configuration_file_path);
101102
#ifdef WIN32
102103
fs::path GetSpecialFolderPath(int nFolder, bool fCreate = true);
103104
#endif
@@ -112,11 +113,12 @@ void runCommand(const std::string& strCommand);
112113
* Most paths passed as configuration arguments are treated as relative to
113114
* the datadir if they are not absolute.
114115
*
116+
* @param args Parsed arguments and settings.
115117
* @param path The path to be conditionally prefixed with datadir.
116118
* @param net_specific Use network specific datadir variant
117119
* @return The normalized path.
118120
*/
119-
fs::path AbsPathForConfigVal(const fs::path& path, bool net_specific = true);
121+
fs::path AbsPathForConfigVal(const ArgsManager& args, const fs::path& path, bool net_specific = true);
120122

121123
inline bool IsSwitchChar(char c)
122124
{

0 commit comments

Comments
 (0)