Changeset View
Changeset View
Standalone View
Standalone View
src/util/settings.cpp
// Copyright (c) 2019 The Bitcoin Core developers | // Copyright (c) 2019 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include <util/settings.h> | #include <util/settings.h> | ||||
#include <tinyformat.h> | |||||
#include <univalue.h> | #include <univalue.h> | ||||
namespace util { | namespace util { | ||||
namespace { | namespace { | ||||
enum class Source { | enum class Source { | ||||
FORCED, | FORCED, | ||||
COMMAND_LINE, | COMMAND_LINE, | ||||
Show All 32 Lines | static void MergeSettings(const Settings &settings, | ||||
if (auto *map = FindKey(settings.ro_config, "")) { | if (auto *map = FindKey(settings.ro_config, "")) { | ||||
if (auto *values = FindKey(*map, name)) { | if (auto *values = FindKey(*map, name)) { | ||||
fn(SettingsSpan(*values), Source::CONFIG_FILE_DEFAULT_SECTION); | fn(SettingsSpan(*values), Source::CONFIG_FILE_DEFAULT_SECTION); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
} // namespace | } // namespace | ||||
bool ReadSettings(const fs::path &path, | |||||
std::map<std::string, SettingsValue> &values, | |||||
std::vector<std::string> &errors) { | |||||
values.clear(); | |||||
errors.clear(); | |||||
fsbridge::ifstream file; | |||||
file.open(path); | |||||
if (!file.is_open()) { | |||||
// Ok for file not to exist. | |||||
return true; | |||||
} | |||||
SettingsValue in; | |||||
if (!in.read(std::string{std::istreambuf_iterator<char>(file), | |||||
std::istreambuf_iterator<char>()})) { | |||||
errors.emplace_back( | |||||
strprintf("Unable to parse settings file %s", path.string())); | |||||
return false; | |||||
} | |||||
if (file.fail()) { | |||||
errors.emplace_back( | |||||
strprintf("Failed reading settings file %s", path.string())); | |||||
return false; | |||||
} | |||||
// Done with file descriptor. Release while copying data. | |||||
file.close(); | |||||
if (!in.isObject()) { | |||||
errors.emplace_back( | |||||
strprintf("Found non-object value %s in settings file %s", | |||||
in.write(), path.string())); | |||||
return false; | |||||
} | |||||
const std::vector<std::string> &in_keys = in.getKeys(); | |||||
const std::vector<SettingsValue> &in_values = in.getValues(); | |||||
for (size_t i = 0; i < in_keys.size(); ++i) { | |||||
auto inserted = values.emplace(in_keys[i], in_values[i]); | |||||
if (!inserted.second) { | |||||
errors.emplace_back( | |||||
strprintf("Found duplicate key %s in settings file %s", | |||||
in_keys[i], path.string())); | |||||
} | |||||
} | |||||
return errors.empty(); | |||||
} | |||||
bool WriteSettings(const fs::path &path, | |||||
const std::map<std::string, SettingsValue> &values, | |||||
std::vector<std::string> &errors) { | |||||
SettingsValue out(SettingsValue::VOBJ); | |||||
for (const auto &value : values) { | |||||
out.__pushKV(value.first, value.second); | |||||
} | |||||
fsbridge::ofstream file; | |||||
file.open(path); | |||||
if (file.fail()) { | |||||
errors.emplace_back( | |||||
strprintf("Error: Unable to open settings file %s for writing", | |||||
path.string())); | |||||
return false; | |||||
} | |||||
file << out.write(/* prettyIndent= */ 1, /* indentLevel= */ 4) << std::endl; | |||||
file.close(); | |||||
return true; | |||||
} | |||||
SettingsValue GetSetting(const Settings &settings, const std::string §ion, | SettingsValue GetSetting(const Settings &settings, const std::string §ion, | ||||
const std::string &name, | const std::string &name, | ||||
bool ignore_default_section_config, | bool ignore_default_section_config, | ||||
bool get_chain_name) { | bool get_chain_name) { | ||||
SettingsValue result; | SettingsValue result; | ||||
// Done merging any more settings sources. | // Done merging any more settings sources. | ||||
bool done = false; | bool done = false; | ||||
MergeSettings( | MergeSettings( | ||||
▲ Show 20 Lines • Show All 151 Lines • Show Last 20 Lines |