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