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