Bitcoin Core  29.1.0
P2P Digital Currency
init.cpp
Go to the documentation of this file.
1 // Copyright (c) 2023 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <chainparams.h>
6 #include <common/args.h>
7 #include <common/init.h>
8 #include <logging.h>
9 #include <tinyformat.h>
10 #include <util/fs.h>
11 #include <util/translation.h>
12 
13 #include <algorithm>
14 #include <exception>
15 #include <optional>
16 
17 namespace common {
18 std::optional<ConfigError> InitConfig(ArgsManager& args, SettingsAbortFn settings_abort_fn)
19 {
20  try {
21  if (!CheckDataDirOption(args)) {
22  return ConfigError{ConfigStatus::FAILED, strprintf(_("Specified data directory \"%s\" does not exist."), args.GetArg("-datadir", ""))};
23  }
24 
25  // Record original datadir and config paths before parsing the config
26  // file. It is possible for the config file to contain a datadir= line
27  // that changes the datadir path after it is parsed. This is useful for
28  // CLI tools to let them use a different data storage location without
29  // needing to pass it every time on the command line. (It is not
30  // possible for the config file to cause another configuration to be
31  // used, though. Specifying a conf= option in the config file causes a
32  // parse error, and specifying a datadir= location containing another
33  // bitcoin.conf file just ignores the other file.)
34  const fs::path orig_datadir_path{args.GetDataDirBase()};
35  const fs::path orig_config_path{AbsPathForConfigVal(args, args.GetPathArg("-conf", BITCOIN_CONF_FILENAME), /*net_specific=*/false)};
36 
37  std::string error;
38  if (!args.ReadConfigFiles(error, true)) {
39  return ConfigError{ConfigStatus::FAILED, strprintf(_("Error reading configuration file: %s"), error)};
40  }
41 
42  // Check for chain settings (Params() calls are only valid after this clause)
44 
45  // Create datadir if it does not exist.
46  const auto base_path{args.GetDataDirBase()};
47  if (!fs::exists(base_path)) {
48  // When creating a *new* datadir, also create a "wallets" subdirectory,
49  // whether or not the wallet is enabled now, so if the wallet is enabled
50  // in the future, it will use the "wallets" subdirectory for creating
51  // and listing wallets, rather than the top-level directory where
52  // wallets could be mixed up with other files. For backwards
53  // compatibility, wallet code will use the "wallets" subdirectory only
54  // if it already exists, but never create it itself. There is discussion
55  // in https://github.com/bitcoin/bitcoin/issues/16220 about ways to
56  // change wallet code so it would no longer be necessary to create
57  // "wallets" subdirectories here.
58  fs::create_directories(base_path / "wallets");
59  }
60  const auto net_path{args.GetDataDirNet()};
61  if (!fs::exists(net_path)) {
62  fs::create_directories(net_path / "wallets");
63  }
64 
65  // Show an error or warn/log if there is a bitcoin.conf file in the
66  // datadir that is being ignored.
67  const fs::path base_config_path = base_path / BITCOIN_CONF_FILENAME;
68  if (fs::exists(base_config_path)) {
69  if (orig_config_path.empty()) {
70  LogInfo(
71  "Data directory %s contains a %s file which is explicitly ignored using -noconf.",
72  fs::quoted(fs::PathToString(base_path)),
74  } else if (!fs::equivalent(orig_config_path, base_config_path)) {
75  const std::string cli_config_path = args.GetArg("-conf", "");
76  const std::string config_source = cli_config_path.empty()
77  ? strprintf("data directory %s", fs::quoted(fs::PathToString(orig_datadir_path)))
78  : strprintf("command line argument %s", fs::quoted("-conf=" + cli_config_path));
79  std::string error = strprintf(
80  "Data directory %1$s contains a %2$s file which is ignored, because a different configuration file "
81  "%3$s from %4$s is being used instead. Possible ways to address this would be to:\n"
82  "- Delete or rename the %2$s file in data directory %1$s.\n"
83  "- Change datadir= or conf= options to specify one configuration file, not two, and use "
84  "includeconf= to include any other configuration files.",
85  fs::quoted(fs::PathToString(base_path)),
87  fs::quoted(fs::PathToString(orig_config_path)),
88  config_source);
89  if (args.GetBoolArg("-allowignoredconf", false)) {
90  LogWarning("%s", error);
91  } else {
92  error += "\n- Set allowignoredconf=1 option to treat this condition as a warning, not an error.";
94  }
95  }
96  }
97 
98  // Create settings.json if -nosettings was not specified.
99  if (args.GetSettingsPath()) {
100  std::vector<std::string> details;
101  if (!args.ReadSettingsFile(&details)) {
102  const bilingual_str& message = _("Settings file could not be read");
103  if (!settings_abort_fn) {
104  return ConfigError{ConfigStatus::FAILED, message, details};
105  } else if (settings_abort_fn(message, details)) {
106  return ConfigError{ConfigStatus::ABORTED, message, details};
107  } else {
108  details.clear(); // User chose to ignore the error and proceed.
109  }
110  }
111  if (!args.WriteSettingsFile(&details)) {
112  const bilingual_str& message = _("Settings file could not be written");
113  return ConfigError{ConfigStatus::FAILED_WRITE, message, details};
114  }
115  }
116  } catch (const std::exception& e) {
117  return ConfigError{ConfigStatus::FAILED, Untranslated(e.what())};
118  }
119  return {};
120 }
121 } // namespace common
bool ReadConfigFiles(std::string &error, bool ignore_invalid_keys=false)
Definition: config.cpp:122
fs::path AbsPathForConfigVal(const ArgsManager &args, const fs::path &path, bool net_specific=true)
Most paths passed as configuration arguments are treated as relative to the datadir if they are not a...
Definition: config.cpp:226
fs::path GetPathArg(std::string arg, const fs::path &default_value={}) const
Return path argument or default value.
Definition: args.cpp:272
Bilingual messages:
Definition: translation.h:24
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1172
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Definition: translation.h:82
Failed to write settings.json.
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: args.cpp:507
consteval auto _(util::TranslatedLiteral str)
Definition: translation.h:79
ChainType GetChainType() const
Returns the appropriate chain type from the program arguments.
Definition: args.cpp:774
fs::path GetDataDirBase() const
Get data directory path.
Definition: args.h:227
bool GetSettingsPath(fs::path *filepath=nullptr, bool temp=false, bool backup=false) const
Get settings file path, or return false if read-write settings were disabled with -nosettings...
Definition: args.cpp:376
bool ReadSettingsFile(std::vector< std::string > *errors=nullptr)
Read settings file.
Definition: args.cpp:402
fs::path GetDataDirNet() const
Get data directory path with appended network identifier.
Definition: args.h:234
static std::string PathToString(const path &path)
Convert path object to a byte string.
Definition: fs.h:151
ArgsManager & args
Definition: bitcoind.cpp:277
void clear()
Definition: translation.h:40
static bool create_directories(const std::filesystem::path &p)
Create directory (and if necessary its parents), unless the leaf directory already exists or is a sym...
Definition: fs.h:190
Definition: args.cpp:868
#define LogInfo(...)
Definition: logging.h:356
bool WriteSettingsFile(std::vector< std::string > *errors=nullptr, bool backup=false) const
Write settings file or backup settings file.
Definition: args.cpp:425
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: args.cpp:457
const char *const BITCOIN_CONF_FILENAME
Definition: args.cpp:39
Failed generically.
#define LogWarning(...)
Definition: logging.h:357
static auto quoted(const std::string &s)
Definition: fs.h:95
static bool exists(const path &p)
Definition: fs.h:89
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:32
std::optional< ConfigError > InitConfig(ArgsManager &args, SettingsAbortFn settings_abort_fn)
Definition: init.cpp:18
std::function< bool(const bilingual_str &message, const std::vector< std::string > &details)> SettingsAbortFn
Callback function to let the user decide whether to abort loading if settings.json file exists and ca...
Definition: init.h:33
void SelectParams(const ChainType chain)
Sets the params returned by Params() to those for the given chain type.
bool CheckDataDirOption(const ArgsManager &args)
Definition: args.cpp:755