35static 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)
38 std::string::size_type pos;
40 while (std::getline(stream, str)) {
41 bool used_hash =
false;
42 if ((pos = str.find(
'#')) != std::string::npos) {
43 str = str.substr(0, pos);
46 const static std::string pattern =
" \t\r\n";
49 if (*str.begin() ==
'[' && *str.rbegin() ==
']') {
50 const std::string section = str.substr(1, str.size() - 2);
51 sections.emplace_back(
SectionInfo{section, filepath, linenr});
53 }
else if (*str.begin() ==
'-') {
54 error =
strprintf(
"parse error on line %i: %s, options in configuration file must be specified without leading -", linenr, str);
56 }
else if ((pos = str.find(
'=')) != std::string::npos) {
58 std::string_view value =
TrimStringView(std::string_view{str}.substr(pos + 1), pattern);
59 if (used_hash &&
name.find(
"rpcpassword") != std::string::npos) {
60 error =
strprintf(
"parse error on line %i, using # in rpcpassword can be ambiguous and should be avoided", linenr);
63 options.emplace_back(
name, value);
64 if ((pos =
name.rfind(
'.')) != std::string::npos &&
prefix.length() <= pos) {
65 sections.emplace_back(
SectionInfo{
name.substr(0, pos), filepath, linenr});
68 error =
strprintf(
"parse error on line %i: %s", linenr, str);
69 if (str.size() >= 2 && str.starts_with(
"no")) {
70 error +=
strprintf(
", if you intended to specify a negated option, use %s=1 instead", str);
81 if (key.
name ==
"conf") {
82 error =
"conf cannot be set in the configuration file; use includeconf= if you want to include additional config files";
85 if (key.
name ==
"reindex") {
88 LogWarning(
"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");
97 std::vector<std::pair<std::string, std::string>> options;
98 if (!
GetConfigOptions(stream, filepath, error, options, m_config_sections)) {
101 for (
const std::pair<std::string, std::string>& option : options) {
106 std::optional<common::SettingsValue> value =
InterpretValue(key, &option.second, *
flags, error);
110 m_settings.ro_config[key.
section][key.
name].push_back(*value);
112 if (ignore_invalid_keys) {
113 LogWarning(
"Ignoring unknown configuration value %s", option.first);
115 error =
strprintf(
"Invalid configuration value %s", option.first);
125 std::istringstream streamConfig(str_config);
128 m_settings.ro_config.clear();
129 m_config_sections.clear();
139 m_settings.ro_config.clear();
140 m_config_sections.clear();
146 std::ifstream stream;
147 if (!conf_path.empty()) {
148 if (fs::is_directory(conf_path)) {
152 stream = std::ifstream{conf_path.std_path()};
154 if (
IsArgSet(
"-conf") && !stream.good()) {
166 bool use_conf_file{
true};
169 if (
auto* includes =
common::FindKey(m_settings.command_line_options,
"includeconf")) {
172 use_conf_file =
false;
177 std::vector<std::string> conf_file_names;
179 auto add_includes = [&](
const std::string& network,
size_t skip = 0) {
180 size_t num_values = 0;
185 conf_file_names.push_back((*
values)[i].get_str());
187 num_values =
values->size();
195 const size_t chain_includes = add_includes(chain_id);
196 const size_t default_includes = add_includes({});
198 for (
const std::string& conf_file_name : conf_file_names) {
200 if (fs::is_directory(include_conf_path)) {
204 std::ifstream conf_file_stream{include_conf_path.std_path()};
205 if (conf_file_stream.good()) {
206 if (!
ReadConfigStream(conf_file_stream, conf_file_name, error, ignore_invalid_keys)) {
209 LogInfo(
"Included configuration file %s\n", conf_file_name);
211 error =
"Failed to include configuration file " + conf_file_name;
217 conf_file_names.clear();
218 add_includes(chain_id, chain_includes);
219 add_includes({}, default_includes);
221 if (chain_id_final != chain_id) {
223 add_includes(chain_id_final);
225 for (
const std::string& conf_file_name : conf_file_names) {
226 tfm::format(std::cerr,
"warning: -includeconf cannot be used from included files; ignoring -includeconf=%s\n", conf_file_name);
234 error =
strprintf(
"specified data directory \"%s\" does not exist.",
GetArg(
"-datadir",
""));
242 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
bool ReadConfigString(const std::string &str_config) EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
fs::path GetDataDirBase() const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Get data directory path.
fs::path GetConfigFilePath() const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Return config file path (read-only)
fs::path GetPathArg_(std::string arg, const fs::path &default_value={}) const EXCLUSIVE_LOCKS_REQUIRED(cs_args)
bool ReadConfigFiles(std::string &error, bool ignore_invalid_keys=false) EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
void ClearPathCache() EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Clear cached directory paths.
fs::path GetDataDir(bool net_specific) const EXCLUSIVE_LOCKS_REQUIRED(cs_args)
Get data directory path.
std::string GetArg(const std::string &strArg, const std::string &strDefault) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Return string argument or default value.
std::string GetChainTypeString() const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Returns the appropriate chain type string from the program arguments.
bool IsArgSet(const std::string &strArg) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Return true if the given argument has been manually set.
fs::path GetDataDirNet() const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Get data directory path with appended network identifier.
bool ReadConfigStream(std::istream &stream, const std::string &filepath, std::string &error, bool ignore_invalid_keys=false) EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
std::optional< unsigned int > GetArgFlags_(const std::string &name) const EXCLUSIVE_LOCKS_REQUIRED(cs_args)
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...
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.
auto FindKey(Map &&map, Key &&key) -> decltype(&map.at(key))
Map lookup helper.
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.