Branch data Line data Source code
1 : : // Copyright (c) 2009-2010 Satoshi Nakamoto
2 : : // Copyright (c) 2009-present The Bitcoin Core developers
3 : : // Distributed under the MIT software license, see the accompanying
4 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 : :
6 : : #include <common/args.h>
7 : :
8 : : #include <chainparamsbase.h>
9 : : #include <common/settings.h>
10 : : #include <sync.h>
11 : : #include <tinyformat.h>
12 : : #include <univalue.h>
13 : : #include <util/chaintype.h>
14 : : #include <util/check.h>
15 : : #include <util/fs.h>
16 : : #include <util/fs_helpers.h>
17 : : #include <util/log.h>
18 : : #include <util/strencodings.h>
19 : : #include <util/string.h>
20 : :
21 : : #ifdef WIN32
22 : : #include <shlobj.h>
23 : : #endif
24 : :
25 : : #include <algorithm>
26 : : #include <cstdlib>
27 : : #include <cstring>
28 : : #include <map>
29 : : #include <optional>
30 : : #include <stdexcept>
31 : : #include <string>
32 : : #include <utility>
33 : : #include <variant>
34 : :
35 : : const char * const BITCOIN_CONF_FILENAME = "bitcoin.conf";
36 : : const char * const BITCOIN_SETTINGS_FILENAME = "settings.json";
37 : :
38 : : ArgsManager gArgs;
39 : :
40 : : /**
41 : : * Interpret a string argument as a boolean.
42 : : *
43 : : * The definition of LocaleIndependentAtoi<int>() requires that non-numeric string values
44 : : * like "foo", return 0. This means that if a user unintentionally supplies a
45 : : * non-integer argument here, the return value is always false. This means that
46 : : * -foo=false does what the user probably expects, but -foo=true is well defined
47 : : * but does not do what they probably expected.
48 : : *
49 : : * The return value of LocaleIndependentAtoi<int>(...) is zero when given input not
50 : : * representable as an int.
51 : : *
52 : : * For a more extensive discussion of this topic (and a wide range of opinions
53 : : * on the Right Way to change this code), see PR12713.
54 : : */
55 : 22029 : static bool InterpretBool(const std::string& strValue)
56 : : {
57 [ + + ]: 22029 : if (strValue.empty())
58 : : return true;
59 [ - + ]: 16241 : return (LocaleIndependentAtoi<int>(strValue) != 0);
60 : : }
61 : :
62 : 1650042 : static std::string SettingName(const std::string& arg)
63 : : {
64 [ - + + + : 1735748 : return arg.size() > 0 && arg[0] == '-' ? arg.substr(1) : arg;
+ + ]
65 : : }
66 : :
67 : : /**
68 : : * Parse "name", "section.name", "noname", "section.noname" settings keys.
69 : : *
70 : : * @note Where an option was negated can be later checked using the
71 : : * IsArgNegated() method. One use case for this is to have a way to disable
72 : : * options that are not normally boolean (e.g. using -nodebuglogfile to request
73 : : * that debug log output is not sent to any file at all).
74 : : */
75 : 82798 : KeyInfo InterpretKey(std::string key)
76 : : {
77 [ + + + + ]: 165596 : KeyInfo result;
78 : : // Split section name from key name for keys like "testnet.foo" or "regtest.bar"
79 : 82798 : size_t option_index = key.find('.');
80 [ + + ]: 82798 : if (option_index != std::string::npos) {
81 [ + - ]: 2964 : result.section = key.substr(0, option_index);
82 [ + - ]: 2964 : key.erase(0, option_index + 1);
83 : : }
84 [ - + + + ]: 82798 : if (key.starts_with("no")) {
85 [ + - ]: 20109 : key.erase(0, 2);
86 : 20109 : result.negated = true;
87 : : }
88 [ + - ]: 82798 : result.name = key;
89 : 82798 : return result;
90 : 0 : }
91 : :
92 : : /**
93 : : * Interpret settings value based on registered flags.
94 : : *
95 : : * @param[in] key key information to know if key was negated
96 : : * @param[in] value string value of setting to be parsed
97 : : * @param[in] flags ArgsManager registered argument flags
98 : : * @param[out] error Error description if settings value is not valid
99 : : *
100 : : * @return parsed settings value if it is valid, otherwise nullopt accompanied
101 : : * by a descriptive error string
102 : : */
103 : 71351 : std::optional<common::SettingsValue> InterpretValue(const KeyInfo& key, const std::string* value,
104 : : unsigned int flags, std::string& error)
105 : : {
106 : : // Return negated settings as false values.
107 [ + + ]: 71351 : if (key.negated) {
108 [ + + ]: 18145 : if (flags & ArgsManager::DISALLOW_NEGATION) {
109 : 358 : error = strprintf("Negating of -%s is meaningless and therefore forbidden", key.name);
110 : 358 : return std::nullopt;
111 : : }
112 : : // Double negatives like -nofoo=0 are supported (but discouraged)
113 [ + + + + ]: 17787 : if (value && !InterpretBool(*value)) {
114 : 5575 : LogWarning("Parsed potentially confusing double-negative -%s=%s", key.name, *value);
115 : 5575 : return true;
116 : : }
117 : 12212 : return false;
118 : : }
119 [ + + + + ]: 53206 : if (!value && (flags & ArgsManager::DISALLOW_ELISION)) {
120 : 1356 : error = strprintf("Can not set -%s with no value. Please specify value with -%s=value.", key.name, key.name);
121 : 1356 : return std::nullopt;
122 : : }
123 [ + - ]: 51850 : return value ? *value : "";
124 : : }
125 : :
126 : : // Define default constructor and destructor that are not inline, so code instantiating this class doesn't need to
127 : : // #include class definitions for all members.
128 : : // For example, m_settings has an internal dependency on univalue.
129 : 2898 : ArgsManager::ArgsManager() = default;
130 [ - + ]: 14490 : ArgsManager::~ArgsManager() = default;
131 : :
132 : 2657 : std::set<std::string> ArgsManager::GetUnsuitableSectionOnlyArgs() const
133 : : {
134 [ + - ]: 2657 : std::set<std::string> unsuitables;
135 : :
136 [ + - ]: 2657 : LOCK(cs_args);
137 : :
138 : : // if there's no section selected, don't worry
139 [ + + ]: 2657 : if (m_network.empty()) return std::set<std::string> {};
140 : :
141 : : // if it's okay to use the default section for this network, don't worry
142 [ + - + + ]: 2112 : if (m_network == ChainTypeToString(ChainType::MAIN)) return std::set<std::string> {};
143 : :
144 [ + + ]: 22074 : for (const auto& arg : m_network_only_args) {
145 [ + - + - : 19975 : if (OnlyHasDefaultSectionSetting(m_settings, m_network, SettingName(arg))) {
- + ]
146 [ # # ]: 0 : unsuitables.insert(arg);
147 : : }
148 : : }
149 : 2099 : return unsuitables;
150 : 2657 : }
151 : :
152 : 2657 : std::list<SectionInfo> ArgsManager::GetUnrecognizedSections() const
153 : : {
154 : : // Section names to be recognized in the config file.
155 : 2657 : static const std::set<std::string> available_sections{
156 : : ChainTypeToString(ChainType::REGTEST),
157 : : ChainTypeToString(ChainType::SIGNET),
158 : : ChainTypeToString(ChainType::TESTNET),
159 : : ChainTypeToString(ChainType::TESTNET4),
160 : : ChainTypeToString(ChainType::MAIN),
161 [ + + + - : 3239 : };
- + + + -
- ]
162 : :
163 : 2657 : LOCK(cs_args);
164 [ + - ]: 2657 : std::list<SectionInfo> unrecognized = m_config_sections;
165 : 2657 : unrecognized.remove_if([](const SectionInfo& appeared){ return available_sections.contains(appeared.m_name); });
166 [ + - ]: 2657 : return unrecognized;
167 : 2754 : }
[ + - + -
+ - + - +
- - - ]
168 : :
169 : 53933 : void ArgsManager::SelectConfigNetwork(const std::string& network)
170 : : {
171 : 53933 : LOCK(cs_args);
172 [ + - + - ]: 107866 : m_network = network;
173 : 53933 : }
174 : :
175 : 83501 : bool ArgsManager::ParseParameters(int argc, const char* const argv[], std::string& error)
176 : : {
177 : 83501 : LOCK(cs_args);
178 : 83501 : m_settings.command_line_options.clear();
179 : :
180 [ + + ]: 153138 : for (int i = 1; i < argc; i++) {
181 [ + - ]: 134719 : std::string key(argv[i]);
182 : :
183 : : #ifdef __APPLE__
184 : : // At the first time when a user gets the "App downloaded from the
185 : : // internet" warning, and clicks the Open button, macOS passes
186 : : // a unique process serial number (PSN) as -psn_... command-line
187 : : // argument, which we filter out.
188 : : if (key.starts_with("-psn_")) continue;
189 : : #endif
190 : :
191 [ + + ]: 134719 : if (key == "-") break; //bitcoin-tx using stdin
192 : 133637 : std::optional<std::string> val;
193 : 133637 : size_t is_index = key.find('=');
194 [ + + ]: 133637 : if (is_index != std::string::npos) {
195 [ + - ]: 34194 : val = key.substr(is_index + 1);
196 [ + - ]: 34194 : key.erase(is_index);
197 : : }
198 : : #ifdef WIN32
199 : : key = ToLower(key);
200 : : if (key[0] == '/')
201 : : key[0] = '-';
202 : : #endif
203 : :
204 [ + + ]: 133637 : if (key[0] != '-') {
205 [ + + + + ]: 50839 : if (!m_accept_any_command && m_command.empty()) {
206 : : // The first non-dash arg is a registered command
207 [ + - ]: 3265 : std::optional<unsigned int> flags = GetArgFlags_(key);
208 [ + + + + ]: 3265 : if (!flags || !(*flags & ArgsManager::COMMAND)) {
209 [ + - ]: 3233 : error = strprintf("Invalid command '%s'", argv[i]);
210 : 3233 : return false;
211 : : }
212 : : }
213 [ + - ]: 47606 : m_command.push_back(key);
214 [ + + ]: 145725 : while (++i < argc) {
215 : : // The remaining args are command args
216 [ + - ]: 98119 : m_command.emplace_back(argv[i]);
217 : : }
218 : 47606 : break;
219 : : }
220 : :
221 : : // Transform --foo to -foo
222 [ - + + + : 82798 : if (key.length() > 1 && key[1] == '-')
+ + ]
223 [ + - ]: 6576 : key.erase(0, 1);
224 : :
225 : : // Transform -foo to foo
226 [ + - ]: 82798 : key.erase(0, 1);
227 [ - + + - ]: 165596 : KeyInfo keyinfo = InterpretKey(key);
228 [ + - + - ]: 82798 : std::optional<unsigned int> flags = GetArgFlags_('-' + keyinfo.name);
229 : :
230 : : // Unknown command line options and command line options with dot
231 : : // characters (which are returned from InterpretKey with nonempty
232 : : // section strings) are not valid.
233 [ + + + + ]: 82798 : if (!flags || !keyinfo.section.empty()) {
234 [ + - ]: 11447 : error = strprintf("Invalid parameter %s", argv[i]);
235 : 11447 : return false;
236 : : }
237 : :
238 [ + + + - ]: 113645 : std::optional<common::SettingsValue> value = InterpretValue(keyinfo, val ? &*val : nullptr, *flags, error);
239 [ + + ]: 71351 : if (!value) return false;
240 : :
241 [ + - + - ]: 69637 : m_settings.command_line_options[keyinfo.name].push_back(*value);
242 : 266837 : }
243 : :
244 : : // we do not allow -includeconf from command line, only -noincludeconf
245 [ + - + + ]: 67107 : if (auto* includes = common::FindKey(m_settings.command_line_options, "includeconf")) {
246 : 3245 : const common::SettingsSpan values{*includes};
247 : : // Range may be empty if -noincludeconf was passed
248 [ + - + + ]: 3245 : if (!values.empty()) {
249 [ + - + - : 3055 : error = "-includeconf cannot be used from commandline; -includeconf=" + values.begin()->write();
+ - ]
250 : 3055 : return false; // pick first value as example
251 : : }
252 : : }
253 : : return true;
254 : 83501 : }
255 : :
256 : 228911 : std::optional<unsigned int> ArgsManager::GetArgFlags_(const std::string& name) const
257 : : {
258 : 228911 : AssertLockHeld(cs_args);
259 [ + + ]: 632492 : for (const auto& arg_map : m_available_args) {
260 : 541746 : const auto search = arg_map.second.find(name);
261 [ + + ]: 541746 : if (search != arg_map.second.end()) {
262 : 138165 : return search->second.m_flags;
263 : : }
264 : : }
265 : 90746 : return m_default_flags;
266 : : }
267 : :
268 : 142848 : std::optional<unsigned int> ArgsManager::GetArgFlags(const std::string& name) const
269 : : {
270 : 142848 : LOCK(cs_args);
271 [ + - ]: 142848 : return GetArgFlags_(name);
272 : 142848 : }
273 : :
274 : 0 : void ArgsManager::SetDefaultFlags(std::optional<unsigned int> flags)
275 : : {
276 : 0 : LOCK(cs_args);
277 [ # # ]: 0 : m_default_flags = flags;
278 : 0 : }
279 : :
280 : 8718 : fs::path ArgsManager::GetPathArg_(std::string arg, const fs::path& default_value) const
281 : : {
282 : 8718 : AssertLockHeld(cs_args);
283 : 8718 : const auto value = GetSetting_(arg);
284 [ + + ]: 8718 : if (value.isFalse()) return {};
285 [ + - + - ]: 8620 : std::string path_str = SettingToString(value).value_or("");
286 [ + + + - ]: 15568 : if (path_str.empty()) return default_value;
287 [ + - + - ]: 20844 : fs::path result = fs::PathFromString(path_str).lexically_normal();
288 : : // Remove trailing slash, if present.
289 [ + - + - : 20844 : return result.has_filename() ? result : result.parent_path();
- - ]
290 : 15666 : }
291 : :
292 : 1770 : fs::path ArgsManager::GetPathArg(std::string arg, const fs::path& default_value) const
293 : : {
294 : 1770 : LOCK(cs_args);
295 [ + - + - ]: 3540 : return GetPathArg_(std::move(arg), default_value);
296 : 1770 : }
297 : :
298 : 7448 : fs::path ArgsManager::GetBlocksDirPath() const
299 : : {
300 : 7448 : LOCK(cs_args);
301 : 7448 : fs::path& path = m_cached_blocks_path;
302 : :
303 : : // Cache the path to avoid calling fs::create_directories on every call of
304 : : // this function
305 [ + + + - ]: 7448 : if (!path.empty()) return path;
306 : :
307 [ + - + - : 3474 : if (!GetSetting_("-blocksdir").isNull()) {
- + ]
308 [ # # # # : 0 : path = fs::absolute(GetPathArg_("-blocksdir"));
# # ]
309 [ # # # # ]: 0 : if (!fs::is_directory(path)) {
310 [ # # ]: 0 : path = "";
311 [ # # ]: 0 : return path;
312 : : }
313 : : } else {
314 [ + - ]: 10422 : path = GetDataDir(/*net_specific=*/false);
315 : : }
316 : :
317 [ + - + - ]: 6948 : path /= fs::PathFromString(BaseParams().DataDir());
318 [ + - ]: 3474 : path /= "blocks";
319 [ + - ]: 3474 : fs::create_directories(path);
320 [ + - + - ]: 7448 : return path;
321 : 7448 : }
322 : :
323 : 27 : fs::path ArgsManager::GetDataDirBase() const {
324 : 27 : LOCK(cs_args);
325 [ + - ]: 27 : return GetDataDir(/*net_specific=*/false);
326 : 27 : }
327 : :
328 : 36550 : fs::path ArgsManager::GetDataDirNet() const {
329 : 36550 : LOCK(cs_args);
330 [ + - ]: 36550 : return GetDataDir(/*net_specific=*/true);
331 : 36550 : }
332 : :
333 : 40051 : fs::path ArgsManager::GetDataDir(bool net_specific) const
334 : : {
335 : 40051 : AssertLockHeld(cs_args);
336 [ + + ]: 40051 : fs::path& path = net_specific ? m_cached_network_datadir_path : m_cached_datadir_path;
337 : :
338 : : // Used cached path if available
339 [ + + ]: 40051 : if (!path.empty()) return path;
340 : :
341 [ + - + - ]: 13896 : const fs::path datadir{GetPathArg_("-datadir")};
342 [ + - ]: 6948 : if (!datadir.empty()) {
343 [ + - ]: 13896 : path = fs::absolute(datadir);
344 [ + - - + ]: 6948 : if (!fs::is_directory(path)) {
345 [ # # ]: 0 : path = "";
346 [ # # ]: 0 : return path;
347 : : }
348 : : } else {
349 [ # # ]: 0 : path = GetDefaultDataDir();
350 : : }
351 : :
352 [ + + + - : 6948 : if (net_specific && !BaseParams().DataDir().empty()) {
+ + ]
353 [ + - + - ]: 10401 : path /= fs::PathFromString(BaseParams().DataDir());
354 : : }
355 : :
356 [ + - ]: 6948 : return path;
357 : 6948 : }
358 : :
359 : 1770 : void ArgsManager::ClearPathCache()
360 : : {
361 : 1770 : LOCK(cs_args);
362 : :
363 : 1770 : m_cached_datadir_path = fs::path();
364 : 1770 : m_cached_network_datadir_path = fs::path();
365 [ + - ]: 3540 : m_cached_blocks_path = fs::path();
366 : 1770 : }
367 : :
368 : 887 : std::optional<const ArgsManager::Command> ArgsManager::GetCommand() const
369 : : {
370 [ + - ]: 887 : Command ret;
371 [ + - ]: 887 : LOCK(cs_args);
372 [ + + ]: 887 : auto it = m_command.begin();
373 [ + + ]: 887 : if (it == m_command.end()) {
374 : : // No command was passed
375 : 524 : return std::nullopt;
376 : : }
377 [ + + ]: 363 : if (!m_accept_any_command) {
378 : : // The registered command
379 [ + - ]: 132 : ret.command = *(it++);
380 : : }
381 [ + + ]: 145956 : while (it != m_command.end()) {
382 : : // The unregistered command and args (if any)
383 [ + - ]: 145593 : ret.args.push_back(*(it++));
384 : : }
385 : 363 : return ret;
386 : 1774 : }
387 : :
388 : 363 : bool ArgsManager::CheckCommandOptions(const std::string& command, std::vector<std::string>* errors) const
389 : : {
390 : 363 : LOCK(cs_args);
391 : :
392 : 363 : auto command_options = m_available_args.find(OptionsCategory::COMMAND_OPTIONS);
393 [ + + ]: 363 : if (command_options == m_available_args.end()) {
394 : : // There are no command-specific options at all, so everything is fine
395 : : return true;
396 : : }
397 : :
398 : 74 : const auto command_args = m_command_args.find(command);
399 : 1336 : auto is_valid_opt = [&](const auto& opt) EXCLUSIVE_LOCKS_REQUIRED(cs_args) -> bool {
400 [ + + ]: 1262 : if (command_args == m_command_args.end()) {
401 : : // Caller may not have checked that command actually exists
402 : : // before calling this function. In that case, treat it as
403 : : // having no valid command-specific options.
404 : : return false;
405 : : } else {
406 : 802 : return command_args->second.contains(opt);
407 : : }
408 : 74 : };
409 : :
410 : 74 : bool ok = true;
411 [ + - + + ]: 2690 : for (const auto& [arg, _] : command_options->second) {
412 [ + - + + : 2996 : if (!GetSetting_(arg).isNull() && !is_valid_opt(arg)) {
+ + + + ]
413 : 882 : ok = false;
414 [ - + ]: 882 : if (errors != nullptr) {
415 [ # # # # ]: 0 : errors->emplace_back(strprintf("The %s option cannot be used with the '%s' command.", arg, command));
416 : : }
417 : : }
418 : : }
419 : : return ok;
420 : 363 : }
421 : :
422 : 293600 : std::vector<std::string> ArgsManager::GetArgs(const std::string& strArg) const
423 : : {
424 : 293600 : std::vector<std::string> result;
425 [ + - + + ]: 301020 : for (const common::SettingsValue& value : GetSettingsList(strArg)) {
426 : 22196 : result.push_back(value.isFalse() ? "0" : value.isTrue() ? "1" : value.get_str());
[ - + - -
+ + + - +
- - + +
- ]
427 : : }
428 : 293600 : return result;
429 : 0 : }
430 : :
431 : 18864 : bool ArgsManager::IsArgSet(const std::string& strArg) const
432 : : {
433 : 18864 : return !GetSetting(strArg).isNull();
434 : : }
435 : :
436 : 0 : bool ArgsManager::GetSettingsPath(fs::path* filepath, bool temp, bool backup) const
437 : : {
438 [ # # # # ]: 0 : fs::path settings = GetPathArg("-settings", BITCOIN_SETTINGS_FILENAME);
439 [ # # ]: 0 : if (settings.empty()) {
440 : : return false;
441 : : }
442 [ # # ]: 0 : if (backup) {
443 [ # # ]: 0 : settings += ".bak";
444 : : }
445 [ # # ]: 0 : if (filepath) {
446 : 0 : *filepath = fsbridge::AbsPathJoin(GetDataDirNet(), temp ? settings + ".tmp" : settings);
[ # # # #
# # # # #
# # # ]
447 : : }
448 : : return true;
449 : 0 : }
450 : :
451 : 0 : static void SaveErrors(const std::vector<std::string> errors, std::vector<std::string>* error_out)
452 : : {
453 [ # # ]: 0 : for (const auto& error : errors) {
454 [ # # ]: 0 : if (error_out) {
455 : 0 : error_out->emplace_back(error);
456 : : } else {
457 : 0 : LogWarning("%s", error);
458 : : }
459 : : }
460 : 0 : }
461 : :
462 : 0 : bool ArgsManager::ReadSettingsFile(std::vector<std::string>* errors)
463 : : {
464 : 0 : fs::path path;
465 [ # # # # ]: 0 : if (!GetSettingsPath(&path, /* temp= */ false)) {
466 : : return true; // Do nothing if settings file disabled.
467 : : }
468 : :
469 [ # # ]: 0 : LOCK(cs_args);
470 : 0 : m_settings.rw_settings.clear();
471 : 0 : std::vector<std::string> read_errors;
472 [ # # # # ]: 0 : if (!common::ReadSettings(path, m_settings.rw_settings, read_errors)) {
473 [ # # # # ]: 0 : SaveErrors(read_errors, errors);
474 : 0 : return false;
475 : : }
476 [ # # ]: 0 : for (const auto& setting : m_settings.rw_settings) {
477 [ # # # # ]: 0 : KeyInfo key = InterpretKey(setting.first); // Split setting key into section and argname
478 [ # # # # : 0 : if (!GetArgFlags_('-' + key.name)) {
# # ]
479 [ # # ]: 0 : LogWarning("Ignoring unknown rw_settings value %s", setting.first);
480 : : }
481 : 0 : }
482 : : return true;
483 [ # # ]: 0 : }
484 : :
485 : 0 : bool ArgsManager::WriteSettingsFile(std::vector<std::string>* errors, bool backup) const
486 : : {
487 : 0 : fs::path path, path_tmp;
488 [ # # # # : 0 : if (!GetSettingsPath(&path, /*temp=*/false, backup) || !GetSettingsPath(&path_tmp, /*temp=*/true, backup)) {
# # # # ]
489 [ # # ]: 0 : throw std::logic_error("Attempt to write settings file when dynamic settings are disabled.");
490 : : }
491 : :
492 [ # # ]: 0 : LOCK(cs_args);
493 : 0 : std::vector<std::string> write_errors;
494 [ # # # # ]: 0 : if (!common::WriteSettings(path_tmp, m_settings.rw_settings, write_errors)) {
495 [ # # # # ]: 0 : SaveErrors(write_errors, errors);
496 : 0 : return false;
497 : : }
498 [ # # # # : 0 : if (!RenameOver(path_tmp, path)) {
# # # # ]
499 [ # # # # : 0 : SaveErrors({strprintf("Failed renaming settings file %s to %s\n", fs::PathToString(path_tmp), fs::PathToString(path))}, errors);
# # # # #
# ]
500 : 0 : return false;
501 : : }
502 : : return true;
503 [ # # # # : 0 : }
# # ]
504 : :
505 : 0 : common::SettingsValue ArgsManager::GetPersistentSetting(const std::string& name) const
506 : : {
507 : 0 : LOCK(cs_args);
508 [ # # # # : 0 : return common::GetSetting(m_settings, m_network, name, !UseDefaultSection("-" + name),
# # ]
509 [ # # ]: 0 : /*ignore_nonpersistent=*/true, /*get_chain_type=*/false);
510 : 0 : }
511 : :
512 : 2657 : bool ArgsManager::IsArgNegated(const std::string& strArg) const
513 : : {
514 : 2657 : return GetSetting(strArg).isFalse();
515 : : }
516 : :
517 : 21091 : std::string ArgsManager::GetArg(const std::string& strArg, const std::string& strDefault) const
518 : : {
519 [ + - ]: 42182 : return GetArg(strArg).value_or(strDefault);
520 : : }
521 : :
522 : 112156 : std::optional<std::string> ArgsManager::GetArg(const std::string& strArg) const
523 : : {
524 : 112156 : const common::SettingsValue value = GetSetting(strArg);
525 [ + - ]: 224312 : return SettingToString(value);
526 : 112156 : }
527 : :
528 : 120776 : std::optional<std::string> SettingToString(const common::SettingsValue& value)
529 : : {
530 [ + + ]: 120776 : if (value.isNull()) return std::nullopt;
531 [ + + ]: 7366 : if (value.isFalse()) return "0";
532 [ + + ]: 7351 : if (value.isTrue()) return "1";
533 [ - + - - ]: 7336 : if (value.isNum()) return value.getValStr();
534 [ - + ]: 14672 : return value.get_str();
535 : : }
536 : :
537 : : template <std::integral Int>
538 : 773646 : Int ArgsManager::GetArg(const std::string& strArg, Int nDefault) const
539 : : {
540 [ + + ]: 773646 : return GetArg<Int>(strArg).value_or(nDefault);
541 : : }
542 : :
543 : : template <std::integral Int>
544 : 876746 : std::optional<Int> ArgsManager::GetArg(const std::string& strArg) const
545 : : {
546 : 876746 : const common::SettingsValue value = GetSetting(strArg);
547 [ + - ]: 1753492 : return SettingTo<Int>(value);
548 : 876746 : }
549 : :
550 : : template <std::integral Int>
551 [ + + ]: 876746 : std::optional<Int> SettingTo(const common::SettingsValue& value)
552 : : {
553 [ + + ]: 876746 : if (value.isNull()) return std::nullopt;
554 [ + + ]: 35693 : if (value.isFalse()) return 0;
555 [ + + ]: 35679 : if (value.isTrue()) return 1;
556 [ - + ]: 35664 : if (value.isNum()) return value.getInt<Int>();
557 [ - + ]: 35664 : return LocaleIndependentAtoi<Int>(value.get_str());
558 : : }
559 : :
560 : 134447 : bool ArgsManager::GetBoolArg(const std::string& strArg, bool fDefault) const
561 : : {
562 [ + + ]: 134447 : return GetBoolArg(strArg).value_or(fDefault);
563 : : }
564 : :
565 : 151489 : std::optional<bool> ArgsManager::GetBoolArg(const std::string& strArg) const
566 : : {
567 : 151489 : const common::SettingsValue value = GetSetting(strArg);
568 [ + - ]: 302978 : return SettingToBool(value);
569 : 151489 : }
570 : :
571 : 151489 : std::optional<bool> SettingToBool(const common::SettingsValue& value)
572 : : {
573 [ + + ]: 151489 : if (value.isNull()) return std::nullopt;
574 [ + + ]: 8086 : if (value.isBool()) return value.get_bool();
575 : 8055 : return InterpretBool(value.get_str());
576 : : }
577 : :
578 : : #define INSTANTIATE_INT_TYPE(Type) \
579 : : template Type ArgsManager::GetArg<Type>(const std::string&, Type) const; \
580 : : template std::optional<Type> ArgsManager::GetArg<Type>(const std::string&) const; \
581 : : template std::optional<Type> SettingTo<Type>(const common::SettingsValue&)
582 : :
583 : : INSTANTIATE_INT_TYPE(int8_t);
584 : : INSTANTIATE_INT_TYPE(uint8_t);
585 : : INSTANTIATE_INT_TYPE(int16_t);
586 : : INSTANTIATE_INT_TYPE(uint16_t);
587 : : INSTANTIATE_INT_TYPE(int32_t);
588 : : INSTANTIATE_INT_TYPE(uint32_t);
589 : : INSTANTIATE_INT_TYPE(int64_t);
590 : : INSTANTIATE_INT_TYPE(uint64_t);
591 : :
592 : : #undef INSTANTIATE_INT_TYPE
593 : :
594 : 58366 : bool ArgsManager::SoftSetArg(const std::string& strArg, const std::string& strValue)
595 : : {
596 : 58366 : LOCK(cs_args);
597 [ + - + + ]: 58366 : if (!GetSetting_(strArg).isNull()) return false;
598 [ + - + - : 27687 : m_settings.forced_settings[SettingName(strArg)] = strValue;
+ - ]
599 : 27687 : return true;
600 : 58366 : }
601 : :
602 : 37384 : bool ArgsManager::SoftSetBoolArg(const std::string& strArg, bool fValue)
603 : : {
604 [ + + ]: 37384 : if (fValue)
605 [ + - ]: 34427 : return SoftSetArg(strArg, std::string("1"));
606 : : else
607 [ + - ]: 2957 : return SoftSetArg(strArg, std::string("0"));
608 : : }
609 : :
610 : 63066 : void ArgsManager::ForceSetArg(const std::string& strArg, const std::string& strValue)
611 : : {
612 : 63066 : LOCK(cs_args);
613 [ + - + - : 63066 : m_settings.forced_settings[SettingName(strArg)] = strValue;
+ - + - ]
614 : 63066 : }
615 : :
616 : 21335 : void ArgsManager::AddCommand(const std::string& cmd, const std::string& help, std::set<std::string> options)
617 : : {
618 : 21335 : Assert(cmd.find('=') == std::string::npos);
619 : 21335 : Assert(cmd.at(0) != '-');
620 : :
621 : 21335 : LOCK(cs_args);
622 : 21335 : m_accept_any_command = false; // latch to false
623 [ + - ]: 21335 : std::map<std::string, Arg>& arg_map = m_available_args[OptionsCategory::COMMANDS];
624 [ + - - + : 42670 : auto ret = arg_map.emplace(cmd, Arg{"", help, ArgsManager::COMMAND});
+ - ]
625 [ + + ]: 21335 : if (!options.empty()) {
626 [ + - ]: 11113 : auto& cmdopts = m_available_args[OptionsCategory::COMMAND_OPTIONS];
627 : 11113 : bool command_has_all_options_defined = true;
628 [ + + ]: 48840 : for (const auto& opt : options) {
629 [ - + ]: 37727 : if (!cmdopts.contains(opt)) {
630 : 0 : command_has_all_options_defined = false;
631 : : }
632 : : }
633 [ + - ]: 11113 : Assert(command_has_all_options_defined);
634 : :
635 [ + - ]: 11113 : m_command_args.try_emplace(cmd, std::move(options));
636 : : }
637 [ + - ]: 21335 : Assert(ret.second); // Fail on duplicate commands
638 : 21335 : }
639 : :
640 : 399888 : void ArgsManager::AddArg(const std::string& name, const std::string& help, unsigned int flags, const OptionsCategory& cat)
641 : : {
642 : 399888 : Assert((flags & ArgsManager::COMMAND) == 0); // use AddCommand
643 : :
644 : : // Split arg name from its help param
645 : 399888 : size_t eq_index = name.find('=');
646 [ + + ]: 399888 : if (eq_index == std::string::npos) {
647 [ - + ]: 206958 : eq_index = name.size();
648 : : }
649 : 399888 : std::string arg_name = name.substr(0, eq_index);
650 : :
651 [ + - ]: 399888 : LOCK(cs_args);
652 : :
653 : : // Allow duplicates involving HIDDEN — it is used as a placeholder for args
654 : : // unavailable in this binary but tolerated for shared config files (see #13441).
655 [ + + ]: 2495221 : for (const auto& arg_map : m_available_args) {
656 [ + + + + ]: 2095333 : if (arg_map.first == OptionsCategory::HIDDEN || cat == OptionsCategory::HIDDEN) continue;
657 [ + - ]: 1378724 : Assert(!arg_map.second.contains(arg_name));
658 : : }
659 : :
660 [ + - ]: 399888 : std::map<std::string, Arg>& arg_map = m_available_args[cat];
661 [ - + + - : 799776 : auto ret = arg_map.emplace(arg_name, Arg{name.substr(eq_index, name.size() - eq_index), help, flags});
- + + - ]
662 [ - + ]: 399888 : assert(ret.second); // Make sure an insertion actually happened
663 : :
664 [ + + ]: 399888 : if (flags & ArgsManager::NETWORK_ONLY) {
665 [ + - ]: 29151 : m_network_only_args.emplace(arg_name);
666 : : }
667 : 399888 : }
668 : :
669 : 27938 : void ArgsManager::AddHiddenArgs(const std::vector<std::string>& names)
670 : : {
671 [ + + ]: 90441 : for (const std::string& name : names) {
672 [ + - ]: 125006 : AddArg(name, "", ArgsManager::ALLOW_ANY, OptionsCategory::HIDDEN);
673 : : }
674 : 27938 : }
675 : :
676 : 24703 : void ArgsManager::ClearArgs()
677 : : {
678 : 24703 : LOCK(cs_args);
679 : 24703 : m_settings = {};
680 : 24703 : m_available_args.clear();
681 : 24703 : m_command_args.clear();
682 : 24703 : m_network_only_args.clear();
683 [ + - ]: 24703 : m_config_sections.clear();
684 : 24703 : }
685 : :
686 : 0 : void ArgsManager::CheckMultipleCLIArgs() const
687 : : {
688 : 0 : LOCK(cs_args);
689 : 0 : std::vector<std::string> found{};
690 : 0 : auto cmds = m_available_args.find(OptionsCategory::CLI_COMMANDS);
691 [ # # ]: 0 : if (cmds != m_available_args.end()) {
692 [ # # # # ]: 0 : for (const auto& [cmd, argspec] : cmds->second) {
693 [ # # # # ]: 0 : if (!GetSetting_(cmd).isNull()) {
694 [ # # ]: 0 : found.push_back(cmd);
695 : : }
696 : : }
697 [ # # # # ]: 0 : if (found.size() > 1) {
698 [ # # # # : 0 : throw std::runtime_error(strprintf("Only one of %s may be specified.", util::Join(found, ", ")));
# # ]
699 : : }
700 : : }
701 [ # # ]: 0 : }
702 : :
703 : 887 : std::string ArgsManager::GetHelpMessage() const
704 : : {
705 [ + - ]: 887 : const bool show_debug = GetBoolArg("-help-debug", false);
706 : :
707 [ + - ]: 887 : std::string usage;
708 [ + - ]: 887 : LOCK(cs_args);
709 : :
710 : 887 : const auto command_options = m_available_args.find(OptionsCategory::COMMAND_OPTIONS);
711 [ + - ]: 5506 : const auto for_matching_cmd_opts = [&](const std::set<std::string>& select, auto&& fn) EXCLUSIVE_LOCKS_REQUIRED(cs_args) {
712 [ + - ]: 5506 : if (select.empty()) return;
713 [ + - ]: 5506 : if (command_options == m_available_args.end()) return;
714 [ + + + + ]: 66292 : for (const auto& [name, info] : command_options->second) {
715 [ + + + + ]: 60786 : if (!show_debug && (info.m_flags & ArgsManager::DEBUG_ONLY)) continue;
716 [ + + ]: 37492 : if (!select.contains(name)) continue;
717 : 16950 : fn(name, info);
718 : : }
719 : 887 : };
720 : :
721 : 2219 : for (const auto& [category, category_args] : m_available_args) {
[ + + + +
+ + + + +
+ + + + +
+ + + + ]
722 : 1913 : switch(category) {
[ + + + +
+ + + + +
+ + + + +
+ + ]
723 : 507 : case OptionsCategory::OPTIONS:
724 [ + - + - ]: 1014 : usage += HelpMessageGroup("Options:");
725 : 507 : break;
726 : 53 : case OptionsCategory::CONNECTION:
727 [ + - + - ]: 106 : usage += HelpMessageGroup("Connection options:");
728 : 53 : break;
729 : 18 : case OptionsCategory::ZMQ:
730 [ + - + - ]: 36 : usage += HelpMessageGroup("ZeroMQ notification options:");
731 : 18 : break;
732 : 45 : case OptionsCategory::DEBUG_TEST:
733 [ + - + - ]: 90 : usage += HelpMessageGroup("Debugging/Testing options:");
734 : 45 : break;
735 : 76 : case OptionsCategory::NODE_RELAY:
736 [ + - + - ]: 152 : usage += HelpMessageGroup("Node relay options:");
737 : 76 : break;
738 : 18 : case OptionsCategory::BLOCK_CREATION:
739 [ + - + - ]: 36 : usage += HelpMessageGroup("Block creation options:");
740 : 18 : break;
741 : 14 : case OptionsCategory::RPC:
742 [ + - + - ]: 28 : usage += HelpMessageGroup("RPC server options:");
743 : 14 : break;
744 : 23 : case OptionsCategory::IPC:
745 [ + - + - ]: 46 : usage += HelpMessageGroup("IPC interprocess connection options:");
746 : 23 : break;
747 : 14 : case OptionsCategory::WALLET:
748 [ + - + - ]: 28 : usage += HelpMessageGroup("Wallet options:");
749 : 14 : break;
750 : 72 : case OptionsCategory::WALLET_DEBUG_TEST:
751 [ + + + - : 87 : if (show_debug) usage += HelpMessageGroup("Wallet debugging/testing options:");
+ - ]
752 : : break;
753 : 17 : case OptionsCategory::CHAINPARAMS:
754 [ + - + - ]: 34 : usage += HelpMessageGroup("Chain selection options:");
755 : 17 : break;
756 : 21 : case OptionsCategory::GUI:
757 [ + - + - ]: 42 : usage += HelpMessageGroup("UI Options:");
758 : 21 : break;
759 : 245 : case OptionsCategory::COMMANDS:
760 [ + - + - ]: 490 : usage += HelpMessageGroup("Commands:");
761 : 245 : break;
762 : 35 : case OptionsCategory::REGISTER_COMMANDS:
763 [ + - + - ]: 70 : usage += HelpMessageGroup("Register Commands:");
764 : 35 : break;
765 : 35 : case OptionsCategory::CLI_COMMANDS:
766 [ + - + - ]: 70 : usage += HelpMessageGroup("CLI Commands:");
767 : 35 : break;
768 : : case OptionsCategory::COMMAND_OPTIONS:
769 : : case OptionsCategory::HIDDEN:
770 : : break;
771 : : } // no default case, so the compiler can warn about missing cases
772 : :
773 [ + + ]: 1913 : if (category == OptionsCategory::COMMAND_OPTIONS) continue;
774 : :
775 : : // When we get to the hidden options, stop
776 [ + + ]: 1774 : if (category == OptionsCategory::HIDDEN) break;
777 : :
778 [ + + + + ]: 16788 : for (const auto& [arg_name, arg_info] : category_args) {
779 [ + + + + ]: 15595 : if (show_debug || !(arg_info.m_flags & ArgsManager::DEBUG_ONLY)) {
780 [ - + - + : 23680 : usage += HelpMessageOpt(arg_name, arg_info.m_help_param, arg_info.m_help_text);
- + + - ]
781 : :
782 [ + + ]: 11840 : if (category == OptionsCategory::COMMANDS) {
783 : 8067 : const auto cmd_args = m_command_args.find(arg_name);
784 [ + + ]: 8067 : if (cmd_args == m_command_args.end()) continue;
785 [ + - ]: 5506 : for_matching_cmd_opts(cmd_args->second, [&](const auto& cmdopt_name, const auto& cmdopt_info) {
786 [ - + - + : 33900 : usage += HelpMessageOpt(cmdopt_name, cmdopt_info.m_help_param, cmdopt_info.m_help_text, /*subopt=*/true);
- + - + ]
787 : 16950 : });
788 : : }
789 : : }
790 : : }
791 : : }
792 [ + - ]: 887 : return usage;
793 : 887 : }
794 : :
795 : 887 : bool HelpRequested(const ArgsManager& args)
796 : : {
797 : 3956 : return args.IsArgSet("-?") || args.IsArgSet("-h") || args.IsArgSet("-help") || args.IsArgSet("-help-debug");
[ + - + -
+ + + - +
- + + + -
+ - + + +
- + - + +
+ + + + -
- - - -
- ]
798 : : }
799 : :
800 : 2470 : void SetupHelpOptions(ArgsManager& args)
801 : : {
802 [ + - + - ]: 4940 : args.AddArg("-help", "Print this help message and exit (also -h or -?)", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
803 [ + - ]: 2470 : args.AddHiddenArgs({"-h", "-?"});
804 : 2470 : }
805 : :
806 : 2304 : std::string HelpMessageGroup(const std::string &message) {
807 [ + - ]: 6912 : return std::string(message) + std::string("\n\n");
808 : : }
809 : :
810 : 31126 : std::string HelpMessageOpt(std::string_view option, std::string_view help_param, std::string_view message, bool subopt)
811 : : {
812 : 31126 : constexpr int screen_width = 79;
813 : 31126 : int opt_indent = 2;
814 : 31126 : int msg_indent = 7;
815 : :
816 [ + + ]: 31126 : if (subopt) {
817 : 16950 : int bump = msg_indent - opt_indent;
818 : 16950 : opt_indent += bump; // opt_indent now at the old msg_indent level
819 : 16950 : msg_indent += bump; // indent by the same amount
820 : : }
821 : 31126 : int msg_width = screen_width - msg_indent;
822 : :
823 : 31126 : return strprintf("%*s%s%s\n%*s%s\n\n",
824 : : opt_indent, "", option, help_param,
825 [ + - ]: 62252 : msg_indent, "", FormatParagraph(message, msg_width, msg_indent));
826 : : }
827 : :
828 : : const std::vector<std::string> TEST_OPTIONS_DOC{
829 : : "addrman (use deterministic addrman)",
830 : : "reindex_after_failure_noninteractive_yes (When asked for a reindex after failure interactively, simulate as-if answered with 'yes')",
831 : : "bip94 (enforce BIP94 consensus rules)",
832 : : };
833 : :
834 : 3550 : bool HasTestOption(const ArgsManager& args, const std::string& test_option)
835 : : {
836 [ + - ]: 3550 : const auto options = args.GetArgs("-test");
837 : 42600 : return std::any_of(options.begin(), options.end(), [test_option](const auto& option) {
[ - + - +
- + + - -
+ + - - +
+ - - + +
- ]
838 [ # # ]: 0 : return option == test_option;
839 : 3550 : });
840 : 3550 : }
841 : :
842 : 0 : fs::path GetDefaultDataDir()
843 : : {
844 : : // Windows:
845 : : // old: C:\Users\Username\AppData\Roaming\Bitcoin
846 : : // new: C:\Users\Username\AppData\Local\Bitcoin
847 : : // macOS: ~/Library/Application Support/Bitcoin
848 : : // Unix-like: ~/.bitcoin
849 : : #ifdef WIN32
850 : : // Windows
851 : : // Check for existence of datadir in old location and keep it there
852 : : fs::path legacy_path = GetSpecialFolderPath(CSIDL_APPDATA) / "Bitcoin";
853 : : if (fs::exists(legacy_path)) return legacy_path;
854 : :
855 : : // Otherwise, fresh installs can start in the new, "proper" location
856 : : return GetSpecialFolderPath(CSIDL_LOCAL_APPDATA) / "Bitcoin";
857 : : #else
858 : 0 : fs::path pathRet;
859 : 0 : char* pszHome = getenv("HOME");
860 [ # # # # ]: 0 : if (pszHome == nullptr || strlen(pszHome) == 0)
861 [ # # ]: 0 : pathRet = fs::path("/");
862 : : else
863 [ # # ]: 0 : pathRet = fs::path(pszHome);
864 : : #ifdef __APPLE__
865 : : // macOS
866 : : return pathRet / "Library/Application Support/Bitcoin";
867 : : #else
868 : : // Unix-like
869 [ # # # # ]: 0 : return pathRet / ".bitcoin";
870 : : #endif
871 : : #endif
872 : 0 : }
873 : :
874 : 0 : bool CheckDataDirOption(const ArgsManager& args)
875 : : {
876 [ # # # # ]: 0 : const fs::path datadir{args.GetPathArg("-datadir")};
877 [ # # # # : 0 : return datadir.empty() || fs::is_directory(fs::absolute(datadir));
# # # # ]
878 : 0 : }
879 : :
880 : 0 : fs::path ArgsManager::GetConfigFilePath() const
881 : : {
882 : 0 : LOCK(cs_args);
883 [ # # # # : 0 : return *Assert(m_config_path);
# # ]
884 : 0 : }
885 : :
886 : 0 : void ArgsManager::SetConfigFilePath(fs::path path)
887 : : {
888 : 0 : LOCK(cs_args);
889 [ # # ]: 0 : assert(!m_config_path);
890 [ # # ]: 0 : m_config_path = path;
891 : 0 : }
892 : :
893 : 1770 : ChainType ArgsManager::GetChainType() const
894 : : {
895 : 1770 : std::variant<ChainType, std::string> arg = GetChainArg();
896 [ + - ]: 1770 : if (auto* parsed = std::get_if<ChainType>(&arg)) return *parsed;
897 [ # # # # : 0 : throw std::runtime_error(strprintf("Unknown chain %s.", std::get<std::string>(arg)));
# # ]
898 : 1770 : }
899 : :
900 : 887 : std::string ArgsManager::GetChainTypeString() const
901 : : {
902 : 887 : auto arg = GetChainArg();
903 [ + + + - ]: 875 : if (auto* parsed = std::get_if<ChainType>(&arg)) return ChainTypeToString(*parsed);
904 [ - + - + ]: 896 : return std::get<std::string>(arg);
905 : 875 : }
906 : :
907 : 2657 : std::variant<ChainType, std::string> ArgsManager::GetChainArg() const
908 : : {
909 : 13285 : auto get_net = [&](const std::string& arg) {
910 : 10628 : LOCK(cs_args);
911 [ + - + - ]: 21256 : common::SettingsValue value = common::GetSetting(m_settings, /* section= */ "", SettingName(arg),
912 : : /* ignore_default_section_config= */ false,
913 : : /*ignore_nonpersistent=*/false,
914 [ + - ]: 10628 : /* get_chain_type= */ true);
915 [ + + + + : 10628 : return value.isNull() ? false : value.isBool() ? value.get_bool() : InterpretBool(value.get_str());
+ - + - +
- ]
916 [ + - ]: 21256 : };
917 : :
918 [ + - ]: 2657 : const bool fRegTest = get_net("-regtest");
919 [ + - ]: 2657 : const bool fSigNet = get_net("-signet");
920 [ + - ]: 2657 : const bool fTestNet = get_net("-testnet");
921 [ + - ]: 2657 : const bool fTestNet4 = get_net("-testnet4");
922 [ + - ]: 2657 : const auto chain_arg = GetArg("-chain");
923 : :
924 [ + + ]: 2657 : if ((int)chain_arg.has_value() + (int)fRegTest + (int)fSigNet + (int)fTestNet + (int)fTestNet4 > 1) {
925 [ + - ]: 12 : throw std::runtime_error("Invalid combination of -regtest, -signet, -testnet, -testnet4 and -chain. Can use at most one.");
926 : : }
927 [ + + ]: 2645 : if (chain_arg) {
928 [ - + + - : 28 : if (auto parsed = ChainTypeFromString(*chain_arg)) return *parsed;
+ + ]
929 : : // Not a known string, so return original string
930 [ - + ]: 42 : return *chain_arg;
931 : : }
932 [ + + ]: 2617 : if (fRegTest) return ChainType::REGTEST;
933 [ + + ]: 2609 : if (fSigNet) return ChainType::SIGNET;
934 [ + + ]: 2605 : if (fTestNet) return ChainType::TESTNET;
935 [ + + ]: 2603 : if (fTestNet4) return ChainType::TESTNET4;
936 : 2599 : return ChainType::MAIN;
937 : 2645 : }
938 : :
939 : 1528686 : bool ArgsManager::UseDefaultSection(const std::string& arg) const
940 : : {
941 : 1528686 : AssertLockHeld(cs_args);
942 [ + + + + ]: 1528686 : return m_network == ChainTypeToString(ChainType::MAIN) || !m_network_only_args.contains(arg);
943 : : }
944 : :
945 : 1235086 : common::SettingsValue ArgsManager::GetSetting_(const std::string& arg) const
946 : : {
947 : 1235086 : AssertLockHeld(cs_args);
948 : 1235086 : return common::GetSetting(
949 : 1235086 : m_settings, m_network, SettingName(arg), !UseDefaultSection(arg),
950 [ + - ]: 2470172 : /*ignore_nonpersistent=*/false, /*get_chain_type=*/false);
951 : : }
952 : :
953 : 1161912 : common::SettingsValue ArgsManager::GetSetting(const std::string& arg) const
954 : : {
955 : 1161912 : LOCK(cs_args);
956 [ + - ]: 1161912 : return GetSetting_(arg);
957 : 1161912 : }
958 : :
959 : 293600 : std::vector<common::SettingsValue> ArgsManager::GetSettingsList(const std::string& arg) const
960 : : {
961 : 293600 : LOCK(cs_args);
962 [ + - + - : 587200 : return common::GetSettingsList(m_settings, m_network, SettingName(arg), !UseDefaultSection(arg));
+ - + - ]
963 : 293600 : }
964 : :
965 : 0 : void ArgsManager::logArgsPrefix(
966 : : const std::string& prefix,
967 : : const std::string& section,
968 : : const std::map<std::string, std::vector<common::SettingsValue>>& args) const
969 : : {
970 : 0 : AssertLockHeld(cs_args);
971 [ # # # # ]: 0 : std::string section_str = section.empty() ? "" : "[" + section + "] ";
972 [ # # ]: 0 : for (const auto& arg : args) {
973 [ # # ]: 0 : for (const auto& value : arg.second) {
974 [ # # # # ]: 0 : std::optional<unsigned int> flags = GetArgFlags_('-' + arg.first);
975 [ # # ]: 0 : if (flags) {
976 [ # # # # : 0 : std::string value_str = (*flags & SENSITIVE) ? "****" : value.write();
# # ]
977 [ # # ]: 0 : LogInfo("%s %s%s=%s\n", prefix, section_str, arg.first, value_str);
978 : 0 : }
979 : : }
980 : : }
981 : 0 : }
982 : :
983 : 0 : void ArgsManager::LogArgs() const
984 : : {
985 : 0 : LOCK(cs_args);
986 [ # # ]: 0 : for (const auto& section : m_settings.ro_config) {
987 [ # # # # ]: 0 : logArgsPrefix("Config file arg:", section.first, section.second);
988 : : }
989 [ # # ]: 0 : for (const auto& setting : m_settings.rw_settings) {
990 [ # # # # ]: 0 : LogInfo("Setting file arg: %s = %s\n", setting.first, setting.second.write());
991 : : }
992 [ # # # # : 0 : logArgsPrefix("Command-line arg:", "", m_settings.command_line_options);
# # # # ]
993 : 0 : }
|