34static bool GetConfigOptions(std::istream& stream,
const std::string& filepath, std::string& error, std::vector<std::pair<std::string, std::string>>& options, std::list<SectionInfo>& sections)
37 std::string::size_type pos;
39 while (std::getline(stream, str)) {
40 bool used_hash =
false;
41 if ((pos = str.find(
'#')) != std::string::npos) {
42 str = str.substr(0, pos);
45 const static std::string pattern =
" \t\r\n";
48 if (*str.begin() ==
'[' && *str.rbegin() ==
']') {
49 const std::string section = str.substr(1, str.size() - 2);
50 sections.emplace_back(
SectionInfo{section, filepath, linenr});
52 }
else if (*str.begin() ==
'-') {
53 error =
strprintf(
"parse error on line %i: %s, options in configuration file must be specified without leading -", linenr, str);
55 }
else if ((pos = str.find(
'=')) != std::string::npos) {
57 std::string_view value =
TrimStringView(std::string_view{str}.substr(pos + 1), pattern);
58 if (used_hash &&
name.find(
"rpcpassword") != std::string::npos) {
59 error =
strprintf(
"parse error on line %i, using # in rpcpassword can be ambiguous and should be avoided", linenr);
62 options.emplace_back(
name, value);
63 if ((pos =
name.rfind(
'.')) != std::string::npos &&
prefix.length() <= pos) {
64 sections.emplace_back(
SectionInfo{
name.substr(0, pos), filepath, linenr});
67 error =
strprintf(
"parse error on line %i: %s", linenr, str);
68 if (str.size() >= 2 && str.substr(0, 2) ==
"no") {
69 error +=
strprintf(
", if you intended to specify a negated option, use %s=1 instead", str);
80 if (key.
name ==
"conf") {
81 error =
"conf cannot be set in the configuration file; use includeconf= if you want to include additional config files";
84 if (key.
name ==
"reindex") {
87 LogPrintf(
"Warning: reindex=1 is set in the configuration file, which will significantly slow down startup. Consider removing or commenting out this option for better performance, unless there is currently a condition which makes rebuilding the indexes necessary\n");
96 std::vector<std::pair<std::string, std::string>> options;
97 if (!
GetConfigOptions(stream, filepath, error, options, m_config_sections)) {
100 for (
const std::pair<std::string, std::string>& option : options) {
105 std::optional<common::SettingsValue> value =
InterpretValue(key, &option.second, *
flags, error);
109 m_settings.ro_config[key.
section][key.
name].push_back(*value);
111 if (ignore_invalid_keys) {
112 LogPrintf(
"Ignoring unknown configuration value %s\n", option.first);
114 error =
strprintf(
"Invalid configuration value %s", option.first);
126 m_settings.ro_config.clear();
127 m_config_sections.clear();
132 std::ifstream stream;
133 if (!conf_path.empty()) {
134 if (fs::is_directory(conf_path)) {
138 stream = std::ifstream{conf_path};
140 if (
IsArgSet(
"-conf") && !stream.good()) {
152 bool use_conf_file{
true};
155 if (
auto* includes =
common::FindKey(m_settings.command_line_options,
"includeconf")) {
158 use_conf_file =
false;
163 std::vector<std::string> conf_file_names;
165 auto add_includes = [&](
const std::string& network,
size_t skip = 0) {
166 size_t num_values = 0;
171 conf_file_names.push_back((*
values)[i].get_str());
173 num_values =
values->size();
181 const size_t chain_includes = add_includes(chain_id);
182 const size_t default_includes = add_includes({});
184 for (
const std::string& conf_file_name : conf_file_names) {
186 if (fs::is_directory(include_conf_path)) {
190 std::ifstream conf_file_stream{include_conf_path};
191 if (conf_file_stream.good()) {
192 if (!
ReadConfigStream(conf_file_stream, conf_file_name, error, ignore_invalid_keys)) {
195 LogPrintf(
"Included configuration file %s\n", conf_file_name);
197 error =
"Failed to include configuration file " + conf_file_name;
203 conf_file_names.clear();
204 add_includes(chain_id, chain_includes);
205 add_includes({}, default_includes);
207 if (chain_id_final != chain_id) {
209 add_includes(chain_id_final);
211 for (
const std::string& conf_file_name : conf_file_names) {
212 tfm::format(std::cerr,
"warning: -includeconf cannot be used from included files; ignoring -includeconf=%s\n", conf_file_name);
220 error =
strprintf(
"specified data directory \"%s\" does not exist.",
GetArg(
"-datadir",
""));
228 if (path.is_absolute() || path.empty()) {
std::optional< common::SettingsValue > InterpretValue(const KeyInfo &key, const std::string *value, unsigned int flags, std::string &error)
Interpret settings value based on registered flags.
bool CheckDataDirOption(const ArgsManager &args)
KeyInfo InterpretKey(std::string key)
Parse "name", "section.name", "noname", "section.noname" settings keys.
const char *const BITCOIN_CONF_FILENAME
std::string GetChainTypeString() const
Returns the appropriate chain type string from the program arguments.
fs::path GetDataDirNet() const
Get data directory path with appended network identifier.
std::optional< unsigned int > GetArgFlags(const std::string &name) const
Return Flags for known arg.
void ClearPathCache()
Clear cached directory paths.
bool IsArgSet(const std::string &strArg) const
Return true if the given argument has been manually set.
fs::path GetDataDirBase() const
Get data directory path.
fs::path GetConfigFilePath() const
Return config file path (read-only)
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
bool ReadConfigStream(std::istream &stream, const std::string &filepath, std::string &error, bool ignore_invalid_keys=false)
bool ReadConfigFiles(std::string &error, bool ignore_invalid_keys=false)
fs::path GetPathArg(std::string arg, const fs::path &default_value={}) const
Return path argument or default value.
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
static bool GetConfigOptions(std::istream &stream, const std::string &filepath, std::string &error, std::vector< std::pair< std::string, std::string > > &options, std::list< SectionInfo > §ions)
bool IsConfSupported(KeyInfo &key, std::string &error)
fs::path AbsPathForConfigVal(const ArgsManager &args, const fs::path &path, bool net_specific)
Most paths passed as configuration arguments are treated as relative to the datadir if they are not a...
auto FindKey(Map &&map, Key &&key) -> decltype(&map.at(key))
Map lookup helper.
static std::string PathToString(const path &path)
Convert path object to a byte string.
static path PathFromString(const std::string &string)
Convert byte string to path object.
fs::path AbsPathJoin(const fs::path &base, const fs::path &path)
Helper function for joining two paths.
std::string_view TrimStringView(std::string_view str, std::string_view pattern=" \f\n\r\t\v")
std::string TrimString(std::string_view str, std::string_view pattern=" \f\n\r\t\v")
static const int64_t values[]
A selection of numbers that do not trigger int64_t overflow when added/subtracted.
Accessor for list of settings that skips negated values when iterated over.
bool last_negated() const
True if the last value is negated.
size_t negated() const
Number of negated values.