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