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