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