Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
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/fs.h>
6#include <util/settings.h>
7
8#include <tinyformat.h>
9#include <univalue.h>
10
11#include <fstream>
12#include <map>
13#include <string>
14#include <vector>
15
16namespace util {
17namespace {
18
19 enum class Source {
20 FORCED,
21 COMMAND_LINE,
22 RW_SETTINGS,
23 CONFIG_FILE_NETWORK_SECTION,
24 CONFIG_FILE_DEFAULT_SECTION
25 };
26
33 template <typename Fn>
34 static void MergeSettings(const Settings &settings,
35 const std::string &section,
36 const std::string &name, Fn &&fn) {
37 // Merge in the forced settings
38 if (auto *value = FindKey(settings.forced_settings, name)) {
39 fn(SettingsSpan(*value), Source::FORCED);
40 }
41 // Merge in the command-line options
42 if (auto *values = FindKey(settings.command_line_options, name)) {
43 fn(SettingsSpan(*values), Source::COMMAND_LINE);
44 }
45 // Merge in the read-write settings
46 if (const SettingsValue *value = FindKey(settings.rw_settings, name)) {
47 fn(SettingsSpan(*value), Source::RW_SETTINGS);
48 }
49 // Merge in the network-specific section of the config file
50 if (!section.empty()) {
51 if (auto *map = FindKey(settings.ro_config, section)) {
52 if (auto *values = FindKey(*map, name)) {
53 fn(SettingsSpan(*values),
54 Source::CONFIG_FILE_NETWORK_SECTION);
55 }
56 }
57 }
58 // Merge in the default section of the config file
59 if (auto *map = FindKey(settings.ro_config, "")) {
60 if (auto *values = FindKey(*map, name)) {
61 fn(SettingsSpan(*values), Source::CONFIG_FILE_DEFAULT_SECTION);
62 }
63 }
64 }
65} // namespace
66
67bool ReadSettings(const fs::path &path,
68 std::map<std::string, SettingsValue> &values,
69 std::vector<std::string> &errors) {
70 values.clear();
71 errors.clear();
72
73 // Ok for file to not exist
74 if (!fs::exists(path)) {
75 return true;
76 }
77
78 std::ifstream file;
79 file.open(path);
80 if (!file.is_open()) {
81 errors.emplace_back(
82 strprintf("%s. Please check permissions.", fs::PathToString(path)));
83 return false;
84 }
85
87 if (!in.read(std::string{std::istreambuf_iterator<char>(file),
88 std::istreambuf_iterator<char>()})) {
89 errors.emplace_back(strprintf("Unable to parse settings file %s",
90 fs::PathToString(path)));
91 return false;
92 }
93
94 if (file.fail()) {
95 errors.emplace_back(strprintf("Failed reading settings file %s",
96 fs::PathToString(path)));
97 return false;
98 }
99 // Done with file descriptor. Release while copying data.
100 file.close();
101
102 if (!in.isObject()) {
103 errors.emplace_back(
104 strprintf("Found non-object value %s in settings file %s",
105 in.write(), fs::PathToString(path)));
106 return false;
107 }
108
109 const std::vector<std::string> &in_keys = in.getKeys();
110 const std::vector<SettingsValue> &in_values = in.getValues();
111 for (size_t i = 0; i < in_keys.size(); ++i) {
112 auto inserted = values.emplace(in_keys[i], in_values[i]);
113 if (!inserted.second) {
114 errors.emplace_back(
115 strprintf("Found duplicate key %s in settings file %s",
116 in_keys[i], fs::PathToString(path)));
117 }
118 }
119 return errors.empty();
120}
121
122bool WriteSettings(const fs::path &path,
123 const std::map<std::string, SettingsValue> &values,
124 std::vector<std::string> &errors) {
126 for (const auto &value : values) {
127 out.pushKVEnd(value.first, value.second);
128 }
129 std::ofstream file;
130 file.open(path);
131 if (file.fail()) {
132 errors.emplace_back(
133 strprintf("Error: Unable to open settings file %s for writing",
134 fs::PathToString(path)));
135 return false;
136 }
137 file << out.write(/* prettyIndent= */ 1, /* indentLevel= */ 4) << std::endl;
138 file.close();
139 return true;
140}
141
143 const std::string &name,
146 SettingsValue result;
147 // Done merging any more settings sources.
148 bool done = false;
150 settings, section, name, [&](SettingsSpan span, Source source) {
151 // Weird behavior preserved for backwards compatibility: Apply
152 // negated setting even if non-negated setting would be ignored. A
153 // negated value in the default section is applied to network
154 // specific options, even though normal non-negated values there
155 // would be ignored.
156 const bool never_ignore_negated_setting = span.last_negated();
157
158 // Weird behavior preserved for backwards compatibility: Take first
159 // assigned value instead of last. In general, later settings take
160 // precedence over early settings, but for backwards compatibility
161 // in the config file the precedence is reversed for all settings
162 // except chain name settings.
163 const bool reverse_precedence =
164 (source == Source::CONFIG_FILE_NETWORK_SECTION ||
165 source == Source::CONFIG_FILE_DEFAULT_SECTION) &&
167
168 // Weird behavior preserved for backwards compatibility: Negated
169 // -regtest and -testnet arguments which you would expect to
170 // override values set in the configuration file are currently
171 // accepted but silently ignored. It would be better to apply these
172 // just like other negated values, or at least warn they are
173 // ignored.
175
176 if (done) {
177 return;
178 }
179
180 // Ignore settings in default config section if requested.
182 source == Source::CONFIG_FILE_DEFAULT_SECTION &&
184 return;
185 }
186
187 // Ignore nonpersistent settings if requested.
189 (source == Source::COMMAND_LINE || source == Source::FORCED)) {
190 return;
191 }
192
193 // Skip negated command line settings.
194 if (skip_negated_command_line && span.last_negated()) {
195 return;
196 }
197
198 if (!span.empty()) {
199 result = reverse_precedence ? span.begin()[0] : span.end()[-1];
200 done = true;
201 } else if (span.last_negated()) {
202 result = false;
203 done = true;
204 }
205 });
206 return result;
207}
208
209std::vector<SettingsValue> GetSettingsList(const Settings &settings,
210 const std::string &section,
211 const std::string &name,
213 std::vector<SettingsValue> result;
214 // Done merging any more settings sources.
215 bool done = false;
216 bool prev_negated_empty = false;
218 settings, section, name, [&](SettingsSpan span, Source source) {
219 // Weird behavior preserved for backwards compatibility: Apply
220 // config file settings even if negated on command line. Negating a
221 // setting on command line will ignore earlier settings on the
222 // command line and ignore settings in the config file, unless the
223 // negated command line value is followed by non-negated value, in
224 // which case config file settings will be brought back from the
225 // dead (but earlier command line settings will still be ignored).
226 const bool add_zombie_config_values =
227 (source == Source::CONFIG_FILE_NETWORK_SECTION ||
228 source == Source::CONFIG_FILE_DEFAULT_SECTION) &&
230
231 // Ignore settings in default config section if requested.
233 source == Source::CONFIG_FILE_DEFAULT_SECTION) {
234 return;
235 }
236
237 // Add new settings to the result if isn't already complete, or if
238 // the values are zombies.
239 if (!done || add_zombie_config_values) {
240 for (const auto &value : span) {
241 if (value.isArray()) {
242 result.insert(result.end(), value.getValues().begin(),
243 value.getValues().end());
244 } else {
245 result.push_back(value);
246 }
247 }
248 }
249
250 // If a setting was negated, or if a setting was forced, set done to
251 // true to ignore any later lower priority settings.
252 done |= span.negated() > 0 || source == Source::FORCED;
253
254 // Update the negated and empty state used for the zombie values
255 // check.
256 prev_negated_empty |= span.last_negated() && result.empty();
257 });
258 return result;
259}
260
262 const std::string &section,
263 const std::string &name) {
264 bool has_default_section_setting = false;
265 bool has_other_setting = false;
267 settings, section, name, [&](SettingsSpan span, Source source) {
268 if (span.empty()) {
269 return;
270 } else if (source == Source::CONFIG_FILE_DEFAULT_SECTION) {
272 } else {
273 has_other_setting = true;
274 }
275 });
276 // If a value is set in the default section and not explicitly overwritten
277 // by the user on the command line or in a different section, then we want
278 // to enable warnings about the value being ignored.
280}
281
282SettingsSpan::SettingsSpan(const std::vector<SettingsValue> &vec) noexcept
283 : SettingsSpan(vec.data(), vec.size()) {}
285 return data + negated();
286}
288 return data + size;
289}
291 return size == 0 || last_negated();
292}
294 return size > 0 && data[size - 1].isFalse();
295}
296size_t SettingsSpan::negated() const {
297 for (size_t i = size; i > 0; --i) {
298 if (data[i - 1].isFalse()) {
299 // Return number of negated values (position of last false value)
300 return i;
301 }
302 }
303 return 0;
304}
305
306} // namespace util
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
const std::vector< std::string > & getKeys() const
void pushKVEnd(std::string key, UniValue val)
Definition univalue.cpp:108
bool isFalse() const
Definition univalue.h:106
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition fs.h:30
static bool exists(const path &p)
Definition fs.h:102
static std::string PathToString(const path &path)
Convert path object to byte string.
Definition fs.h:142
Definition any.h:10
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:209
SettingsValue GetSetting(const Settings &settings, const std::string &section, const std::string &name, bool ignore_default_section_config, bool ignore_nonpersistent, bool get_chain_name)
Get settings value from combined sources: forced settings, command line arguments,...
Definition settings.cpp:142
bool ReadSettings(const fs::path &path, std::map< std::string, SettingsValue > &values, std::vector< std::string > &errors)
Read settings file.
Definition settings.cpp:67
UniValue SettingsValue
Settings value type (string/integer/boolean/null variant).
Definition settings.h:27
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:261
bool WriteSettings(const fs::path &path, const std::map< std::string, SettingsValue > &values, std::vector< std::string > &errors)
Write settings file.
Definition settings.cpp:122
auto FindKey(Map &&map, Key &&key) -> decltype(&map.at(key))
Map lookup helper.
Definition settings.h:115
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85
const char * name
Definition rest.cpp:47
const char * source
Stored settings.
Definition settings.h:31
Accessor for list of settings that skips negated values when iterated over.
Definition settings.h:91
size_t negated() const
Number of negated values.
Definition settings.cpp:296
const SettingsValue * end() const
Pointer to end of values.
Definition settings.cpp:287
bool empty() const
True if there are any non-negated values.
Definition settings.cpp:290
SettingsSpan()=default
const SettingsValue * data
Definition settings.h:109
bool last_negated() const
True if the last value is negated.
Definition settings.cpp:293
const SettingsValue * begin() const
Pointer to first non-negated value.
Definition settings.cpp:284
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...