settings: Add resetSettings() method

Allows the GUI to clear settings.json file and save settings.json.bak file when
GUI "Reset Options" button is pressed or -resetguisettings command line option
is used. (GUI code already backs up and resets the "guisettings.ini" file this
way, so this just makes the same behavior possible for "settings.json")
This commit is contained in:
Ryan Ofsky
2022-05-16 14:37:00 -04:00
parent 77fabffef4
commit f9fdcec7e9
4 changed files with 22 additions and 6 deletions

View File

@@ -112,6 +112,10 @@ public:
//! source, but not being persisted. //! source, but not being persisted.
virtual void forceSetting(const std::string& name, const util::SettingsValue& value) = 0; virtual void forceSetting(const std::string& name, const util::SettingsValue& value) = 0;
//! Clear all settings in <datadir>/settings.json and store a backup of
//! previous settings in <datadir>/settings.json.bak.
virtual void resetSettings() = 0;
//! Map port. //! Map port.
virtual void mapPort(bool use_upnp, bool use_natpmp) = 0; virtual void mapPort(bool use_upnp, bool use_natpmp) = 0;

View File

@@ -144,6 +144,14 @@ public:
} }
}); });
} }
void resetSettings() override
{
gArgs.WriteSettingsFile(/*errors=*/nullptr, /*backup=*/true);
gArgs.LockSettings([&](util::Settings& settings) {
settings.rw_settings.clear();
});
gArgs.WriteSettingsFile();
}
void mapPort(bool use_upnp, bool use_natpmp) override { StartMapPort(use_upnp, use_natpmp); } void mapPort(bool use_upnp, bool use_natpmp) override { StartMapPort(use_upnp, use_natpmp); }
bool getProxy(Network net, Proxy& proxy_info) override { return GetProxy(net, proxy_info); } bool getProxy(Network net, Proxy& proxy_info) override { return GetProxy(net, proxy_info); }
size_t getNodeCount(ConnectionDirection flags) override size_t getNodeCount(ConnectionDirection flags) override

View File

@@ -526,12 +526,15 @@ bool ArgsManager::InitSettings(std::string& error)
return true; return true;
} }
bool ArgsManager::GetSettingsPath(fs::path* filepath, bool temp) const bool ArgsManager::GetSettingsPath(fs::path* filepath, bool temp, bool backup) const
{ {
fs::path settings = GetPathArg("-settings", fs::path{BITCOIN_SETTINGS_FILENAME}); fs::path settings = GetPathArg("-settings", fs::path{BITCOIN_SETTINGS_FILENAME});
if (settings.empty()) { if (settings.empty()) {
return false; return false;
} }
if (backup) {
settings += ".bak";
}
if (filepath) { if (filepath) {
*filepath = fsbridge::AbsPathJoin(GetDataDirNet(), temp ? settings + ".tmp" : settings); *filepath = fsbridge::AbsPathJoin(GetDataDirNet(), temp ? settings + ".tmp" : settings);
} }
@@ -572,10 +575,10 @@ bool ArgsManager::ReadSettingsFile(std::vector<std::string>* errors)
return true; return true;
} }
bool ArgsManager::WriteSettingsFile(std::vector<std::string>* errors) const bool ArgsManager::WriteSettingsFile(std::vector<std::string>* errors, bool backup) const
{ {
fs::path path, path_tmp; fs::path path, path_tmp;
if (!GetSettingsPath(&path, /* temp= */ false) || !GetSettingsPath(&path_tmp, /* temp= */ true)) { if (!GetSettingsPath(&path, /*temp=*/false, backup) || !GetSettingsPath(&path_tmp, /*temp=*/true, backup)) {
throw std::logic_error("Attempt to write settings file when dynamic settings are disabled."); throw std::logic_error("Attempt to write settings file when dynamic settings are disabled.");
} }

View File

@@ -440,7 +440,7 @@ protected:
* Get settings file path, or return false if read-write settings were * Get settings file path, or return false if read-write settings were
* disabled with -nosettings. * disabled with -nosettings.
*/ */
bool GetSettingsPath(fs::path* filepath = nullptr, bool temp = false) const; bool GetSettingsPath(fs::path* filepath = nullptr, bool temp = false, bool backup = false) const;
/** /**
* Read settings file. Push errors to vector, or log them if null. * Read settings file. Push errors to vector, or log them if null.
@@ -448,9 +448,10 @@ protected:
bool ReadSettingsFile(std::vector<std::string>* errors = nullptr); bool ReadSettingsFile(std::vector<std::string>* errors = nullptr);
/** /**
* Write settings file. Push errors to vector, or log them if null. * Write settings file or backup settings file. Push errors to vector, or
* log them if null.
*/ */
bool WriteSettingsFile(std::vector<std::string>* errors = nullptr) const; bool WriteSettingsFile(std::vector<std::string>* errors = nullptr, bool backup = false) const;
/** /**
* Get current setting from config file or read/write settings file, * Get current setting from config file or read/write settings file,