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