Bitcoin Core 22.99.0
P2P Digital Currency
settings.cpp
Go to the documentation of this file.
1// Copyright (c) 2019 The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <util/settings.h>
6
7#include <tinyformat.h>
8#include <univalue.h>
9
10namespace util {
11namespace {
12
13enum class Source {
14 FORCED,
15 COMMAND_LINE,
16 RW_SETTINGS,
17 CONFIG_FILE_NETWORK_SECTION,
18 CONFIG_FILE_DEFAULT_SECTION
19};
20
26template <typename Fn>
27static void MergeSettings(const Settings& settings, const std::string& section, const std::string& name, Fn&& fn)
28{
29 // Merge in the forced settings
30 if (auto* value = FindKey(settings.forced_settings, name)) {
31 fn(SettingsSpan(*value), Source::FORCED);
32 }
33 // Merge in the command-line options
34 if (auto* values = FindKey(settings.command_line_options, name)) {
35 fn(SettingsSpan(*values), Source::COMMAND_LINE);
36 }
37 // Merge in the read-write settings
38 if (const SettingsValue* value = FindKey(settings.rw_settings, name)) {
39 fn(SettingsSpan(*value), Source::RW_SETTINGS);
40 }
41 // Merge in the network-specific section of the config file
42 if (!section.empty()) {
43 if (auto* map = FindKey(settings.ro_config, section)) {
44 if (auto* values = FindKey(*map, name)) {
45 fn(SettingsSpan(*values), Source::CONFIG_FILE_NETWORK_SECTION);
46 }
47 }
48 }
49 // Merge in the default section of the config file
50 if (auto* map = FindKey(settings.ro_config, "")) {
51 if (auto* values = FindKey(*map, name)) {
52 fn(SettingsSpan(*values), Source::CONFIG_FILE_DEFAULT_SECTION);
53 }
54 }
55}
56} // namespace
57
58bool ReadSettings(const fs::path& path, std::map<std::string, SettingsValue>& values, std::vector<std::string>& errors)
59{
60 values.clear();
61 errors.clear();
62
63 // Ok for file to not exist
64 if (!fs::exists(path)) return true;
65
67 file.open(path);
68 if (!file.is_open()) {
69 errors.emplace_back(strprintf("%s. Please check permissions.", fs::PathToString(path)));
70 return false;
71 }
72
74 if (!in.read(std::string{std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>()})) {
75 errors.emplace_back(strprintf("Unable to parse settings file %s", fs::PathToString(path)));
76 return false;
77 }
78
79 if (file.fail()) {
80 errors.emplace_back(strprintf("Failed reading settings file %s", fs::PathToString(path)));
81 return false;
82 }
83 file.close(); // Done with file descriptor. Release while copying data.
84
85 if (!in.isObject()) {
86 errors.emplace_back(strprintf("Found non-object value %s in settings file %s", in.write(), fs::PathToString(path)));
87 return false;
88 }
89
90 const std::vector<std::string>& in_keys = in.getKeys();
91 const std::vector<SettingsValue>& in_values = in.getValues();
92 for (size_t i = 0; i < in_keys.size(); ++i) {
93 auto inserted = values.emplace(in_keys[i], in_values[i]);
94 if (!inserted.second) {
95 errors.emplace_back(strprintf("Found duplicate key %s in settings file %s", in_keys[i], fs::PathToString(path)));
96 }
97 }
98 return errors.empty();
99}
100
101bool WriteSettings(const fs::path& path,
102 const std::map<std::string, SettingsValue>& values,
103 std::vector<std::string>& errors)
104{
106 for (const auto& value : values) {
107 out.__pushKV(value.first, value.second);
108 }
110 file.open(path);
111 if (file.fail()) {
112 errors.emplace_back(strprintf("Error: Unable to open settings file %s for writing", fs::PathToString(path)));
113 return false;
114 }
115 file << out.write(/* prettyIndent= */ 1, /* indentLevel= */ 4) << std::endl;
116 file.close();
117 return true;
118}
119
121 const std::string& section,
122 const std::string& name,
123 bool ignore_default_section_config,
124 bool get_chain_name)
125{
126 SettingsValue result;
127 bool done = false; // Done merging any more settings sources.
128 MergeSettings(settings, section, name, [&](SettingsSpan span, Source source) {
129 // Weird behavior preserved for backwards compatibility: Apply negated
130 // setting even if non-negated setting would be ignored. A negated
131 // value in the default section is applied to network specific options,
132 // even though normal non-negated values there would be ignored.
133 const bool never_ignore_negated_setting = span.last_negated();
134
135 // Weird behavior preserved for backwards compatibility: Take first
136 // assigned value instead of last. In general, later settings take
137 // precedence over early settings, but for backwards compatibility in
138 // the config file the precedence is reversed for all settings except
139 // chain name settings.
140 const bool reverse_precedence =
141 (source == Source::CONFIG_FILE_NETWORK_SECTION || source == Source::CONFIG_FILE_DEFAULT_SECTION) &&
142 !get_chain_name;
143
144 // Weird behavior preserved for backwards compatibility: Negated
145 // -regtest and -testnet arguments which you would expect to override
146 // values set in the configuration file are currently accepted but
147 // silently ignored. It would be better to apply these just like other
148 // negated values, or at least warn they are ignored.
149 const bool skip_negated_command_line = get_chain_name;
150
151 if (done) return;
152
153 // Ignore settings in default config section if requested.
154 if (ignore_default_section_config && source == Source::CONFIG_FILE_DEFAULT_SECTION &&
155 !never_ignore_negated_setting) {
156 return;
157 }
158
159 // Skip negated command line settings.
160 if (skip_negated_command_line && span.last_negated()) return;
161
162 if (!span.empty()) {
163 result = reverse_precedence ? span.begin()[0] : span.end()[-1];
164 done = true;
165 } else if (span.last_negated()) {
166 result = false;
167 done = true;
168 }
169 });
170 return result;
171}
172
173std::vector<SettingsValue> GetSettingsList(const Settings& settings,
174 const std::string& section,
175 const std::string& name,
176 bool ignore_default_section_config)
177{
178 std::vector<SettingsValue> result;
179 bool done = false; // Done merging any more settings sources.
180 bool prev_negated_empty = false;
181 MergeSettings(settings, section, name, [&](SettingsSpan span, Source source) {
182 // Weird behavior preserved for backwards compatibility: Apply config
183 // file settings even if negated on command line. Negating a setting on
184 // command line will ignore earlier settings on the command line and
185 // ignore settings in the config file, unless the negated command line
186 // value is followed by non-negated value, in which case config file
187 // settings will be brought back from the dead (but earlier command
188 // line settings will still be ignored).
189 const bool add_zombie_config_values =
190 (source == Source::CONFIG_FILE_NETWORK_SECTION || source == Source::CONFIG_FILE_DEFAULT_SECTION) &&
191 !prev_negated_empty;
192
193 // Ignore settings in default config section if requested.
194 if (ignore_default_section_config && source == Source::CONFIG_FILE_DEFAULT_SECTION) return;
195
196 // Add new settings to the result if isn't already complete, or if the
197 // values are zombies.
198 if (!done || add_zombie_config_values) {
199 for (const auto& value : span) {
200 if (value.isArray()) {
201 result.insert(result.end(), value.getValues().begin(), value.getValues().end());
202 } else {
203 result.push_back(value);
204 }
205 }
206 }
207
208 // If a setting was negated, or if a setting was forced, set
209 // done to true to ignore any later lower priority settings.
210 done |= span.negated() > 0 || source == Source::FORCED;
211
212 // Update the negated and empty state used for the zombie values check.
213 prev_negated_empty |= span.last_negated() && result.empty();
214 });
215 return result;
216}
217
218bool OnlyHasDefaultSectionSetting(const Settings& settings, const std::string& section, const std::string& name)
219{
220 bool has_default_section_setting = false;
221 bool has_other_setting = false;
222 MergeSettings(settings, section, name, [&](SettingsSpan span, Source source) {
223 if (span.empty()) return;
224 else if (source == Source::CONFIG_FILE_DEFAULT_SECTION) has_default_section_setting = true;
225 else has_other_setting = true;
226 });
227 // If a value is set in the default section and not explicitly overwritten by the
228 // user on the command line or in a different section, then we want to enable
229 // warnings about the value being ignored.
230 return has_default_section_setting && !has_other_setting;
231}
232
233SettingsSpan::SettingsSpan(const std::vector<SettingsValue>& vec) noexcept : SettingsSpan(vec.data(), vec.size()) {}
234const SettingsValue* SettingsSpan::begin() const { return data + negated(); }
235const SettingsValue* SettingsSpan::end() const { return data + size; }
236bool SettingsSpan::empty() const { return size == 0 || last_negated(); }
237bool SettingsSpan::last_negated() const { return size > 0 && data[size - 1].isFalse(); }
239{
240 for (size_t i = size; i > 0; --i) {
241 if (data[i - 1].isFalse()) return i; // Return number of negated values (position of last false value)
242 }
243 return 0;
244}
245
246} // namespace util
@ VOBJ
Definition: univalue.h:19
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
void __pushKV(const std::string &key, const UniValue &val)
Definition: univalue.cpp:127
const std::vector< UniValue > & getValues() const
const std::vector< std::string > & getKeys() const
bool isFalse() const
Definition: univalue.h:77
bool read(const char *raw, size_t len)
bool isObject() const
Definition: univalue.h:82
Path class wrapper to prepare application code for transition from boost::filesystem library to std::...
Definition: fs.h:34
if(na.IsAddrV1Compatible())
static bool exists(const path &p)
Definition: fs.h:77
static std::string PathToString(const path &path)
Convert path object to byte string.
Definition: fs.h:120
fs::ofstream ofstream
Definition: fs.h:225
fs::ifstream ifstream
Definition: fs.h:224
std::vector< SettingsValue > GetSettingsList(const Settings &settings, const std::string &section, const std::string &name, bool ignore_default_section_config)
Get combined setting value similar to GetSetting(), except if setting was specified multiple times,...
Definition: settings.cpp:173
bool ReadSettings(const fs::path &path, std::map< std::string, SettingsValue > &values, std::vector< std::string > &errors)
Read settings file.
Definition: settings.cpp:58
bool OnlyHasDefaultSectionSetting(const Settings &settings, const std::string &section, const std::string &name)
Return true if a setting is set in the default config file section, and not overridden by a higher pr...
Definition: settings.cpp:218
bool WriteSettings(const fs::path &path, const std::map< std::string, SettingsValue > &values, std::vector< std::string > &errors)
Write settings file.
Definition: settings.cpp:101
auto FindKey(Map &&map, Key &&key) -> decltype(&map.at(key))
Map lookup helper.
Definition: settings.h:100
SettingsValue GetSetting(const Settings &settings, const std::string &section, const std::string &name, bool ignore_default_section_config, bool get_chain_name)
Get settings value from combined sources: forced settings, command line arguments,...
Definition: settings.cpp:120
const char * name
Definition: rest.cpp:43
const char * source
Definition: rpcconsole.cpp:63
static const int64_t values[]
A selection of numbers that do not trigger int64_t overflow when added/subtracted.
Stored settings.
Definition: settings.h:31
std::map< std::string, SettingsValue > rw_settings
Map of setting name to read-write file setting value.
Definition: settings.h:37
std::map< std::string, SettingsValue > forced_settings
Map of setting name to forced setting value.
Definition: settings.h:33
std::map< std::string, std::map< std::string, std::vector< SettingsValue > > > ro_config
Map of config section name and setting name to list of config file values.
Definition: settings.h:39
std::map< std::string, std::vector< SettingsValue > > command_line_options
Map of setting name to list of command line values.
Definition: settings.h:35
Accessor for list of settings that skips negated values when iterated over.
Definition: settings.h:83
size_t negated() const
Number of negated values.
Definition: settings.cpp:238
const SettingsValue * end() const
Pointer to end of values.
Definition: settings.cpp:235
bool empty() const
True if there are any non-negated values.
Definition: settings.cpp:236
SettingsSpan()=default
const SettingsValue * data
Definition: settings.h:94
bool last_negated() const
True if the last value is negated.
Definition: settings.cpp:237
const SettingsValue * begin() const
Pointer to first non-negated value.
Definition: settings.cpp:234
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164