5#include <bitcoin-build-config.h>
27 CONFIG_FILE_NETWORK_SECTION,
28 CONFIG_FILE_DEFAULT_SECTION
32const std::string SETTINGS_WARN_MSG_KEY{
"_warning_"};
40static void MergeSettings(
const Settings& settings,
const std::string& section,
const std::string& name, Fn&& fn)
43 if (
auto* value =
FindKey(settings.forced_settings, name)) {
47 if (
auto* values =
FindKey(settings.command_line_options, name)) {
55 if (!section.empty()) {
56 if (
auto* map =
FindKey(settings.ro_config, section)) {
57 if (
auto* values =
FindKey(*map, name)) {
58 fn(
SettingsSpan(*values), Source::CONFIG_FILE_NETWORK_SECTION);
63 if (
auto* map =
FindKey(settings.ro_config,
"")) {
64 if (
auto* values =
FindKey(*map, name)) {
65 fn(
SettingsSpan(*values), Source::CONFIG_FILE_DEFAULT_SECTION);
71bool ReadSettings(
const fs::path& path, std::map<std::string, SettingsValue>& values, std::vector<std::string>& errors)
77 if (!fs::exists(path))
return true;
80 file.open(path.std_path());
81 if (!file.is_open()) {
82 errors.emplace_back(
strprintf(
"%s. Please check permissions.", fs::PathToString(path)));
87 if (!in.
read(std::string{std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>()})) {
88 errors.emplace_back(
strprintf(
"Settings file %s does not contain valid JSON. This may be caused by a crash, power loss, full disk, or storage error, "
89 "and can be fixed by removing the file, which will reset settings to default values.",
90 fs::PathToString(path)));
95 errors.emplace_back(
strprintf(
"Failed reading settings file %s", fs::PathToString(path)));
101 errors.emplace_back(
strprintf(
"Found non-object value %s in settings file %s", in.
write(), fs::PathToString(path)));
105 const std::vector<std::string>& in_keys = in.
getKeys();
106 const std::vector<SettingsValue>& in_values = in.
getValues();
107 for (
size_t i = 0; i < in_keys.size(); ++i) {
108 auto inserted = values.emplace(in_keys[i], in_values[i]);
109 if (!inserted.second) {
110 errors.emplace_back(
strprintf(
"Found duplicate key %s in settings file %s", in_keys[i], fs::PathToString(path)));
117 values.erase(SETTINGS_WARN_MSG_KEY);
119 return errors.empty();
123 const std::map<std::string, SettingsValue>& values,
124 std::vector<std::string>& errors)
128 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 "
129 "is running, as any changes might be ignored or overwritten.", CLIENT_NAME));
131 for (
const auto& value : values) {
132 out.pushKVEnd(value.first, value.second);
135 file.open(path.std_path());
137 errors.emplace_back(
strprintf(
"Error: Unable to open settings file %s for writing", fs::PathToString(path)));
140 file << out.write( 4, 1) << std::endl;
142 errors.emplace_back(
strprintf(
"Error: Unable to write settings file %s", fs::PathToString(path)));
147 errors.emplace_back(
strprintf(
"Error: Unable to close settings file %s", fs::PathToString(path)));
154 const std::string& section,
155 const std::string& name,
156 bool ignore_default_section_config,
157 bool ignore_nonpersistent,
162 MergeSettings(settings, section, name, [&](
SettingsSpan span, Source source) {
167 const bool never_ignore_negated_setting = span.
last_negated();
174 const bool reverse_precedence =
175 (source == Source::CONFIG_FILE_NETWORK_SECTION || source == Source::CONFIG_FILE_DEFAULT_SECTION) &&
183 const bool skip_negated_command_line = get_chain_type;
188 if (ignore_default_section_config && source == Source::CONFIG_FILE_DEFAULT_SECTION &&
189 !never_ignore_negated_setting) {
194 if (ignore_nonpersistent && (source == Source::COMMAND_LINE || source == Source::FORCED))
return;
197 if (skip_negated_command_line && span.
last_negated())
return;
200 result = reverse_precedence ? span.
begin()[0] : span.
end()[-1];
211 const std::string& section,
212 const std::string& name,
213 bool ignore_default_section_config)
215 std::vector<SettingsValue> result;
217 bool prev_negated_empty =
false;
218 MergeSettings(settings, section, name, [&](
SettingsSpan span, Source source) {
226 const bool add_zombie_config_values =
227 (source == Source::CONFIG_FILE_NETWORK_SECTION || source == Source::CONFIG_FILE_DEFAULT_SECTION) &&
231 if (ignore_default_section_config && source == Source::CONFIG_FILE_DEFAULT_SECTION)
return;
235 if (!done || add_zombie_config_values) {
236 for (
const auto& value : span) {
237 if (value.isArray()) {
238 result.insert(result.end(), value.getValues().begin(), value.getValues().end());
240 result.push_back(value);
247 done |= span.
negated() > 0 || source == Source::FORCED;
250 prev_negated_empty |= span.
last_negated() && result.empty();
257 bool has_default_section_setting =
false;
258 bool has_other_setting =
false;
259 MergeSettings(settings, section, name, [&](
SettingsSpan span, Source source) {
260 if (span.
empty())
return;
261 else if (source == Source::CONFIG_FILE_DEFAULT_SECTION) has_default_section_setting =
true;
262 else has_other_setting =
true;
267 return has_default_section_setting && !has_other_setting;
277 for (
size_t i =
size; i > 0; --i) {
278 if (
data[i - 1].isFalse())
return i;
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 json)
Parse JSON; set value to null on failure.
bool WriteSettings(const fs::path &path, const std::map< std::string, SettingsValue > &values, std::vector< std::string > &errors)
Write settings file.
bool ReadSettings(const fs::path &path, std::map< std::string, SettingsValue > &values, std::vector< std::string > &errors)
Read settings file.
SettingsValue GetSetting(const Settings &settings, const std::string §ion, const std::string &name, bool ignore_default_section_config, bool ignore_nonpersistent, bool get_chain_type)
auto FindKey(Map &&map, Key &&key) -> decltype(&map.at(key))
Map lookup helper.
std::vector< SettingsValue > GetSettingsList(const Settings &settings, const std::string §ion, const std::string &name, bool ignore_default_section_config)
bool OnlyHasDefaultSectionSetting(const Settings &settings, const std::string §ion, const std::string &name)
bool last_negated() const
True if the last value is negated.
const SettingsValue * begin() const
Pointer to first non-negated value.
const SettingsValue * end() const
Pointer to end of values.
bool empty() const
True if there are any non-negated values.
size_t negated() const
Number of negated values.
const SettingsValue * data