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