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