mirror of
https://github.com/ElementsProject/elements.git
synced 2026-08-17 13:07:54 +02:00
Add <datadir>/settings.json persistent settings storage.
Persistent settings are used in followup PRs #15936 to unify gui settings between bitcoin-qt and bitcoind, and #15937 to add a load_on_startup flag to the loadwallet RPC and maintain a dynamic list of wallets that should be loaded on startup that also can be shared between bitcoind and bitcoin-qt.
This commit is contained in:
parent
eb682c5700
commit
9c69cfe4c5
13 changed files with 255 additions and 6 deletions
|
|
@ -73,6 +73,7 @@
|
|||
const int64_t nStartupTime = GetTime();
|
||||
|
||||
const char * const BITCOIN_CONF_FILENAME = "bitcoin.conf";
|
||||
const char * const BITCOIN_SETTINGS_FILENAME = "settings.json";
|
||||
|
||||
ArgsManager gArgs;
|
||||
|
||||
|
|
@ -372,6 +373,84 @@ bool ArgsManager::IsArgSet(const std::string& strArg) const
|
|||
return !GetSetting(strArg).isNull();
|
||||
}
|
||||
|
||||
bool ArgsManager::InitSettings(std::string& error)
|
||||
{
|
||||
if (!GetSettingsPath()) {
|
||||
return true; // Do nothing if settings file disabled.
|
||||
}
|
||||
|
||||
std::vector<std::string> errors;
|
||||
if (!ReadSettingsFile(&errors)) {
|
||||
error = strprintf("Failed loading settings file:\n- %s\n", Join(errors, "\n- "));
|
||||
return false;
|
||||
}
|
||||
if (!WriteSettingsFile(&errors)) {
|
||||
error = strprintf("Failed saving settings file:\n- %s\n", Join(errors, "\n- "));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ArgsManager::GetSettingsPath(fs::path* filepath, bool temp) const
|
||||
{
|
||||
if (IsArgNegated("-settings")) {
|
||||
return false;
|
||||
}
|
||||
if (filepath) {
|
||||
std::string settings = GetArg("-settings", BITCOIN_SETTINGS_FILENAME);
|
||||
*filepath = fs::absolute(temp ? settings + ".tmp" : settings, GetDataDir(/* net_specific= */ true));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static void SaveErrors(const std::vector<std::string> errors, std::vector<std::string>* error_out)
|
||||
{
|
||||
for (const auto& error : errors) {
|
||||
if (error_out) {
|
||||
error_out->emplace_back(error);
|
||||
} else {
|
||||
LogPrintf("%s\n", error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool ArgsManager::ReadSettingsFile(std::vector<std::string>* errors)
|
||||
{
|
||||
fs::path path;
|
||||
if (!GetSettingsPath(&path, /* temp= */ false)) {
|
||||
return true; // Do nothing if settings file disabled.
|
||||
}
|
||||
|
||||
LOCK(cs_args);
|
||||
m_settings.rw_settings.clear();
|
||||
std::vector<std::string> read_errors;
|
||||
if (!util::ReadSettings(path, m_settings.rw_settings, read_errors)) {
|
||||
SaveErrors(read_errors, errors);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ArgsManager::WriteSettingsFile(std::vector<std::string>* errors) const
|
||||
{
|
||||
fs::path path, path_tmp;
|
||||
if (!GetSettingsPath(&path, /* temp= */ false) || !GetSettingsPath(&path_tmp, /* temp= */ true)) {
|
||||
throw std::logic_error("Attempt to write settings file when dynamic settings are disabled.");
|
||||
}
|
||||
|
||||
LOCK(cs_args);
|
||||
std::vector<std::string> write_errors;
|
||||
if (!util::WriteSettings(path_tmp, m_settings.rw_settings, write_errors)) {
|
||||
SaveErrors(write_errors, errors);
|
||||
return false;
|
||||
}
|
||||
if (!RenameOver(path_tmp, path)) {
|
||||
SaveErrors({strprintf("Failed renaming settings file %s to %s\n", path_tmp.string(), path.string())}, errors);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ArgsManager::IsArgNegated(const std::string& strArg) const
|
||||
{
|
||||
return GetSetting(strArg).isFalse();
|
||||
|
|
@ -893,6 +972,9 @@ void ArgsManager::LogArgs() const
|
|||
for (const auto& section : m_settings.ro_config) {
|
||||
logArgsPrefix("Config file arg:", section.first, section.second);
|
||||
}
|
||||
for (const auto& setting : m_settings.rw_settings) {
|
||||
LogPrintf("Setting file arg: %s = %s\n", setting.first, setting.second.write());
|
||||
}
|
||||
logArgsPrefix("Command-line arg:", "", m_settings.command_line_options);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue