Skip to content

Commit 6687bb2

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#24306: util: Make ArgsManager::GetPathArg more widely usable
60aa179 Use GetPathArg where possible (Pavol Rusnak) 5b946ed util, refactor: Use GetPathArg to read "-settings" value (Ryan Ofsky) 687e655 util: Add GetPathArg default path argument (Ryan Ofsky) Pull request description: Improve `ArgsManager::GetPathArg` method added in recent PR #24265, so it is usable more places. This PR starts to use it for the `-settings` option. This can also be helpful for #24274 which is parsing more path options. - Add `GetPathArg` default argument so it is less awkward to use to parse options that have default values. - Fix `GetPathArg` negated argument handling. Return path{} not path{"0"} when path argument is negated. - Add unit tests for default and negated cases - Move `GetPathArg` method declaration next to `GetArg` declaration. The two methods are close substitutes for each, so this should help keep them consistent and make them more discoverable. ACKs for top commit: w0xlt: Tested ACK 60aa179 on Ubuntu 21.10 hebasto: re-ACK 60aa179 Tree-SHA512: 3d24b885d8bbeef39ea5d0556e2f09b9e5f4a21179cef11cbbbc1b84da29c8fb66ba698889054ce28d80bc25926687654c8532ed46054bf5b2dd1837866bd1cd
2 parents 384866e + 60aa179 commit 6687bb2

File tree

6 files changed

+43
-23
lines changed

6 files changed

+43
-23
lines changed

src/bench/bench_bitcoin.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ int main(int argc, char** argv)
109109
args.asymptote = parseAsymptote(argsman.GetArg("-asymptote", ""));
110110
args.is_list_only = argsman.GetBoolArg("-list", false);
111111
args.min_time = std::chrono::milliseconds(argsman.GetIntArg("-min_time", DEFAULT_MIN_TIME_MS));
112-
args.output_csv = fs::PathFromString(argsman.GetArg("-output_csv", ""));
113-
args.output_json = fs::PathFromString(argsman.GetArg("-output_json", ""));
112+
args.output_csv = argsman.GetPathArg("-output_csv");
113+
args.output_json = argsman.GetPathArg("-output_json");
114114
args.regex_filter = argsman.GetArg("-filter", DEFAULT_BENCH_FILTER);
115115

116116
benchmark::BenchRunner::RunAll(args);

src/init.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ static const char* BITCOIN_PID_FILENAME = "bitcoind.pid";
135135

136136
static fs::path GetPidFile(const ArgsManager& args)
137137
{
138-
return AbsPathForConfigVal(fs::PathFromString(args.GetArg("-pid", BITCOIN_PID_FILENAME)));
138+
return AbsPathForConfigVal(args.GetPathArg("-pid", BITCOIN_PID_FILENAME));
139139
}
140140

141141
[[nodiscard]] static bool CreatePidFile(const ArgsManager& args)
@@ -1229,10 +1229,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
12291229
// Read asmap file if configured
12301230
std::vector<bool> asmap;
12311231
if (args.IsArgSet("-asmap")) {
1232-
fs::path asmap_path = fs::PathFromString(args.GetArg("-asmap", ""));
1233-
if (asmap_path.empty()) {
1234-
asmap_path = fs::PathFromString(DEFAULT_ASMAP_FILENAME);
1235-
}
1232+
fs::path asmap_path = args.GetPathArg("-asmap", DEFAULT_ASMAP_FILENAME);
12361233
if (!asmap_path.is_absolute()) {
12371234
asmap_path = gArgs.GetDataDirNet() / asmap_path;
12381235
}

src/init/common.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ void AddLoggingArgs(ArgsManager& argsman)
8181
void SetLoggingOptions(const ArgsManager& args)
8282
{
8383
LogInstance().m_print_to_file = !args.IsArgNegated("-debuglogfile");
84-
LogInstance().m_file_path = AbsPathForConfigVal(fs::PathFromString(args.GetArg("-debuglogfile", DEFAULT_DEBUGLOGFILE)));
84+
LogInstance().m_file_path = AbsPathForConfigVal(args.GetPathArg("-debuglogfile", DEFAULT_DEBUGLOGFILE));
8585
LogInstance().m_print_to_console = args.GetBoolArg("-printtoconsole", !args.GetBoolArg("-daemon", false));
8686
LogInstance().m_log_timestamps = args.GetBoolArg("-logtimestamps", DEFAULT_LOGTIMESTAMPS);
8787
LogInstance().m_log_time_micros = args.GetBoolArg("-logtimemicros", DEFAULT_LOGTIMEMICROS);

src/test/getarg_tests.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,24 @@ BOOST_AUTO_TEST_CASE(patharg)
245245

246246
ResetArgs(local_args, "-dir=user/.bitcoin/.//");
247247
BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir"), relative_path);
248+
249+
// Check negated and default argument handling. Specifying an empty argument
250+
// is the same as not specifying the argument. This is convenient for
251+
// scripting so later command line arguments can override earlier command
252+
// line arguments or bitcoin.conf values. Currently the -dir= case cannot be
253+
// distinguished from -dir case with no assignment, but #16545 would add the
254+
// ability to distinguish these in the future (and treat the no-assign case
255+
// like an imperative command or an error).
256+
ResetArgs(local_args, "");
257+
BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir", "default"), fs::path{"default"});
258+
ResetArgs(local_args, "-dir=override");
259+
BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir", "default"), fs::path{"override"});
260+
ResetArgs(local_args, "-dir=");
261+
BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir", "default"), fs::path{"default"});
262+
ResetArgs(local_args, "-dir");
263+
BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir", "default"), fs::path{"default"});
264+
ResetArgs(local_args, "-nodir");
265+
BOOST_CHECK_EQUAL(local_args.GetPathArg("-dir", "default"), fs::path{""});
248266
}
249267

250268
BOOST_AUTO_TEST_CASE(doubledash)

src/util/system.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -387,9 +387,12 @@ std::optional<unsigned int> ArgsManager::GetArgFlags(const std::string& name) co
387387
return std::nullopt;
388388
}
389389

390-
fs::path ArgsManager::GetPathArg(std::string pathlike_arg) const
390+
fs::path ArgsManager::GetPathArg(std::string arg, const fs::path& default_value) const
391391
{
392-
auto result = fs::PathFromString(GetArg(pathlike_arg, "")).lexically_normal();
392+
if (IsArgNegated(arg)) return fs::path{};
393+
std::string path_str = GetArg(arg, "");
394+
if (path_str.empty()) return default_value;
395+
fs::path result = fs::PathFromString(path_str).lexically_normal();
393396
// Remove trailing slash, if present.
394397
return result.has_filename() ? result : result.parent_path();
395398
}
@@ -516,12 +519,12 @@ bool ArgsManager::InitSettings(std::string& error)
516519

517520
bool ArgsManager::GetSettingsPath(fs::path* filepath, bool temp) const
518521
{
519-
if (IsArgNegated("-settings")) {
522+
fs::path settings = GetPathArg("-settings", fs::path{BITCOIN_SETTINGS_FILENAME});
523+
if (settings.empty()) {
520524
return false;
521525
}
522526
if (filepath) {
523-
std::string settings = GetArg("-settings", BITCOIN_SETTINGS_FILENAME);
524-
*filepath = fsbridge::AbsPathJoin(GetDataDirNet(), fs::PathFromString(temp ? settings + ".tmp" : settings));
527+
*filepath = fsbridge::AbsPathJoin(GetDataDirNet(), temp ? settings + ".tmp" : settings);
525528
}
526529
return true;
527530
}

src/util/system.h

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -270,16 +270,6 @@ class ArgsManager
270270
*/
271271
std::optional<const Command> GetCommand() const;
272272

273-
/**
274-
* Get a normalized path from a specified pathlike argument
275-
*
276-
* It is guaranteed that the returned path has no trailing slashes.
277-
*
278-
* @param pathlike_arg Pathlike argument to get a path from (e.g., "-datadir", "-blocksdir" or "-walletdir")
279-
* @return Normalized path which is get from a specified pathlike argument
280-
*/
281-
fs::path GetPathArg(std::string pathlike_arg) const;
282-
283273
/**
284274
* Get blocks directory path
285275
*
@@ -342,6 +332,18 @@ class ArgsManager
342332
*/
343333
std::string GetArg(const std::string& strArg, const std::string& strDefault) const;
344334

335+
/**
336+
* Return path argument or default value
337+
*
338+
* @param arg Argument to get a path from (e.g., "-datadir", "-blocksdir" or "-walletdir")
339+
* @param default_value Optional default value to return instead of the empty path.
340+
* @return normalized path if argument is set, with redundant "." and ".."
341+
* path components and trailing separators removed (see patharg unit test
342+
* for examples or implementation for details). If argument is empty or not
343+
* set, default_value is returned unchanged.
344+
*/
345+
fs::path GetPathArg(std::string arg, const fs::path& default_value = {}) const;
346+
345347
/**
346348
* Return integer argument or default value
347349
*

0 commit comments

Comments
 (0)