26 #include <string_view>
30 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>& sections)
33 std::string::size_type pos;
35 while (std::getline(stream, str)) {
36 bool used_hash =
false;
37 if ((pos = str.find(
'#')) != std::string::npos) {
38 str = str.substr(0, pos);
41 const static std::string pattern =
" \t\r\n";
44 if (*str.begin() ==
'[' && *str.rbegin() ==
']') {
45 const std::string section = str.substr(1, str.size() - 2);
46 sections.emplace_back(
SectionInfo{section, filepath, linenr});
48 }
else if (*str.begin() ==
'-') {
49 error =
strprintf(
"parse error on line %i: %s, options in configuration file must be specified without leading -", linenr, str);
51 }
else if ((pos = str.find(
'=')) != std::string::npos) {
53 std::string_view value =
TrimStringView(std::string_view{str}.substr(pos + 1), pattern);
54 if (used_hash &&
name.find(
"rpcpassword") != std::string::npos) {
55 error =
strprintf(
"parse error on line %i, using # in rpcpassword can be ambiguous and should be avoided", linenr);
58 options.emplace_back(
name, value);
59 if ((pos =
name.rfind(
'.')) != std::string::npos &&
prefix.length() <= pos) {
60 sections.emplace_back(
SectionInfo{
name.substr(0, pos), filepath, linenr});
64 if (str.size() >= 2 && str.substr(0, 2) ==
"no") {
65 error +=
strprintf(
", if you intended to specify a negated option, use %s=1 instead", str);
76 if (key.
name ==
"conf") {
77 error =
"conf cannot be set in the configuration file; use includeconf= if you want to include additional config files";
80 if (key.
name ==
"reindex") {
83 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");
92 std::vector<std::pair<std::string, std::string>> options;
96 for (
const std::pair<std::string, std::string>& option : options) {
105 m_settings.ro_config[key.
section][key.
name].push_back(*value);
107 if (ignore_invalid_keys) {
108 LogPrintf(
"Ignoring unknown configuration value %s\n", option.first);
110 error =
strprintf(
"Invalid configuration value %s", option.first);
122 m_settings.ro_config.clear();
123 m_config_sections.clear();
128 std::ifstream stream{conf_path};
131 if (
IsArgSet(
"-conf") && !stream.good()) {
142 bool use_conf_file{
true};
145 if (
auto* includes =
common::FindKey(m_settings.command_line_options,
"includeconf")) {
148 use_conf_file =
false;
153 std::vector<std::string> conf_file_names;
155 auto add_includes = [&](
const std::string& network,
size_t skip = 0) {
156 size_t num_values = 0;
161 conf_file_names.push_back((*
values)[i].get_str());
163 num_values =
values->size();
171 const size_t chain_includes = add_includes(chain_id);
172 const size_t default_includes = add_includes({});
174 for (
const std::string& conf_file_name : conf_file_names) {
176 if (conf_file_stream.good()) {
180 LogPrintf(
"Included configuration file %s\n", conf_file_name);
182 error =
"Failed to include configuration file " + conf_file_name;
188 conf_file_names.clear();
189 add_includes(chain_id, chain_includes);
190 add_includes({}, default_includes);
192 if (chain_id_final != chain_id) {
194 add_includes(chain_id_final);
196 for (
const std::string& conf_file_name : conf_file_names) {
197 tfm::format(std::cerr,
"warning: -includeconf cannot be used from included files; ignoring -includeconf=%s\n", conf_file_name);
213 if (path.is_absolute()) {
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.
std::optional< unsigned int > GetArgFlags(const std::string &name) const
Return Flags for known arg.
const fs::path & GetDataDirBase() const
Get data directory path.
void ClearPathCache()
Clear cached directory paths.
bool IsArgSet(const std::string &strArg) const
Return true if the given argument has been manually set.
const fs::path & GetDataDirNet() const
Get data directory path with appended network identifier.
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...
bool error(const char *fmt, const Args &... args)
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.
static const int64_t values[]
A selection of numbers that do not trigger int64_t overflow when added/subtracted.
std::string TrimString(std::string_view str, std::string_view pattern=" \f\n\r\t\v")
std::string_view TrimStringView(std::string_view str, std::string_view pattern=" \f\n\r\t\v")
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.