Skip to content

Commit f9fdcec

Browse files
committed
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")
1 parent 77fabff commit f9fdcec

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

src/interfaces/node.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ class Node
112112
//! source, but not being persisted.
113113
virtual void forceSetting(const std::string& name, const util::SettingsValue& value) = 0;
114114

115+
//! Clear all settings in <datadir>/settings.json and store a backup of
116+
//! previous settings in <datadir>/settings.json.bak.
117+
virtual void resetSettings() = 0;
118+
115119
//! Map port.
116120
virtual void mapPort(bool use_upnp, bool use_natpmp) = 0;
117121

src/node/interfaces.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,14 @@ class NodeImpl : public Node
144144
}
145145
});
146146
}
147+
void resetSettings() override
148+
{
149+
gArgs.WriteSettingsFile(/*errors=*/nullptr, /*backup=*/true);
150+
gArgs.LockSettings([&](util::Settings& settings) {
151+
settings.rw_settings.clear();
152+
});
153+
gArgs.WriteSettingsFile();
154+
}
147155
void mapPort(bool use_upnp, bool use_natpmp) override { StartMapPort(use_upnp, use_natpmp); }
148156
bool getProxy(Network net, Proxy& proxy_info) override { return GetProxy(net, proxy_info); }
149157
size_t getNodeCount(ConnectionDirection flags) override

src/util/system.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -526,12 +526,15 @@ bool ArgsManager::InitSettings(std::string& error)
526526
return true;
527527
}
528528

529-
bool ArgsManager::GetSettingsPath(fs::path* filepath, bool temp) const
529+
bool ArgsManager::GetSettingsPath(fs::path* filepath, bool temp, bool backup) const
530530
{
531531
fs::path settings = GetPathArg("-settings", fs::path{BITCOIN_SETTINGS_FILENAME});
532532
if (settings.empty()) {
533533
return false;
534534
}
535+
if (backup) {
536+
settings += ".bak";
537+
}
535538
if (filepath) {
536539
*filepath = fsbridge::AbsPathJoin(GetDataDirNet(), temp ? settings + ".tmp" : settings);
537540
}
@@ -572,10 +575,10 @@ bool ArgsManager::ReadSettingsFile(std::vector<std::string>* errors)
572575
return true;
573576
}
574577

575-
bool ArgsManager::WriteSettingsFile(std::vector<std::string>* errors) const
578+
bool ArgsManager::WriteSettingsFile(std::vector<std::string>* errors, bool backup) const
576579
{
577580
fs::path path, path_tmp;
578-
if (!GetSettingsPath(&path, /* temp= */ false) || !GetSettingsPath(&path_tmp, /* temp= */ true)) {
581+
if (!GetSettingsPath(&path, /*temp=*/false, backup) || !GetSettingsPath(&path_tmp, /*temp=*/true, backup)) {
579582
throw std::logic_error("Attempt to write settings file when dynamic settings are disabled.");
580583
}
581584

src/util/system.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -440,17 +440,18 @@ class ArgsManager
440440
* Get settings file path, or return false if read-write settings were
441441
* disabled with -nosettings.
442442
*/
443-
bool GetSettingsPath(fs::path* filepath = nullptr, bool temp = false) const;
443+
bool GetSettingsPath(fs::path* filepath = nullptr, bool temp = false, bool backup = false) const;
444444

445445
/**
446446
* Read settings file. Push errors to vector, or log them if null.
447447
*/
448448
bool ReadSettingsFile(std::vector<std::string>* errors = nullptr);
449449

450450
/**
451-
* Write settings file. Push errors to vector, or log them if null.
451+
* Write settings file or backup settings file. Push errors to vector, or
452+
* log them if null.
452453
*/
453-
bool WriteSettingsFile(std::vector<std::string>* errors = nullptr) const;
454+
bool WriteSettingsFile(std::vector<std::string>* errors = nullptr, bool backup = false) const;
454455

455456
/**
456457
* Get current setting from config file or read/write settings file,

0 commit comments

Comments
 (0)