aboutsummaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorRyan Ofsky <ryan@ofsky.org>2019-04-29 15:29:00 -0400
committerRyan Ofsky <ryan@ofsky.org>2022-05-19 11:32:56 -0400
commit0e55bc6e7fe439404dc56093a0949395dae51e6b (patch)
tree5f76e812d3c8c202d07f1b5b6b9dc1a0ab980298 /src/util
parent0de36941eca1bff91420dd878eb097db2b1a596c (diff)
downloadbitcoin-0e55bc6e7fe439404dc56093a0949395dae51e6b.tar.xz
settings: Add update/getPersistent/isIgnored methods
Add interfaces::Node methods to give GUI finer grained control over settings.json file. Update method is used to write settings to the file, getPersistent and isIgnored methods are used to find out about settings file and command line option interactions.
Diffstat (limited to 'src/util')
-rw-r--r--src/util/settings.cpp4
-rw-r--r--src/util/settings.h6
-rw-r--r--src/util/system.cpp26
-rw-r--r--src/util/system.h10
4 files changed, 45 insertions, 1 deletions
diff --git a/src/util/settings.cpp b/src/util/settings.cpp
index 26439b010b..924a9cfab2 100644
--- a/src/util/settings.cpp
+++ b/src/util/settings.cpp
@@ -127,6 +127,7 @@ SettingsValue GetSetting(const Settings& settings,
const std::string& section,
const std::string& name,
bool ignore_default_section_config,
+ bool ignore_nonpersistent,
bool get_chain_name)
{
SettingsValue result;
@@ -162,6 +163,9 @@ SettingsValue GetSetting(const Settings& settings,
return;
}
+ // Ignore nonpersistent settings if requested.
+ if (ignore_nonpersistent && (source == Source::COMMAND_LINE || source == Source::FORCED)) return;
+
// Skip negated command line settings.
if (skip_negated_command_line && span.last_negated()) return;
diff --git a/src/util/settings.h b/src/util/settings.h
index 261a0a032f..e97158dc09 100644
--- a/src/util/settings.h
+++ b/src/util/settings.h
@@ -55,12 +55,18 @@ bool WriteSettings(const fs::path& path,
//! @param ignore_default_section_config - ignore values in the default section
//! of the config file (part before any
//! [section] keywords)
+//! @param ignore_nonpersistent - ignore non-persistent settings values (forced
+//! settings values and values specified on the
+//! command line). Only return settings in the
+//! read-only config and read-write settings
+//! files.
//! @param get_chain_name - enable special backwards compatible behavior
//! for GetChainName
SettingsValue GetSetting(const Settings& settings,
const std::string& section,
const std::string& name,
bool ignore_default_section_config,
+ bool ignore_nonpersistent,
bool get_chain_name);
//! Get combined setting value similar to GetSetting(), except if setting was
diff --git a/src/util/system.cpp b/src/util/system.cpp
index cbe9d2ceb0..87d3be9d23 100644
--- a/src/util/system.cpp
+++ b/src/util/system.cpp
@@ -592,6 +592,13 @@ bool ArgsManager::WriteSettingsFile(std::vector<std::string>* errors) const
return true;
}
+util::SettingsValue ArgsManager::GetPersistentSetting(const std::string& name) const
+{
+ LOCK(cs_args);
+ return util::GetSetting(m_settings, m_network, name, !UseDefaultSection("-" + name),
+ /*ignore_nonpersistent=*/true, /*get_chain_name=*/false);
+}
+
bool ArgsManager::IsArgNegated(const std::string& strArg) const
{
return GetSetting(strArg).isFalse();
@@ -600,18 +607,33 @@ bool ArgsManager::IsArgNegated(const std::string& strArg) const
std::string ArgsManager::GetArg(const std::string& strArg, const std::string& strDefault) const
{
const util::SettingsValue value = GetSetting(strArg);
+ return SettingToString(value, strDefault);
+}
+
+std::string SettingToString(const util::SettingsValue& value, const std::string& strDefault)
+{
return value.isNull() ? strDefault : value.isFalse() ? "0" : value.isTrue() ? "1" : value.isNum() ? value.getValStr() : value.get_str();
}
int64_t ArgsManager::GetIntArg(const std::string& strArg, int64_t nDefault) const
{
const util::SettingsValue value = GetSetting(strArg);
+ return SettingToInt(value, nDefault);
+}
+
+int64_t SettingToInt(const util::SettingsValue& value, int64_t nDefault)
+{
return value.isNull() ? nDefault : value.isFalse() ? 0 : value.isTrue() ? 1 : value.isNum() ? value.getInt<int64_t>() : LocaleIndependentAtoi<int64_t>(value.get_str());
}
bool ArgsManager::GetBoolArg(const std::string& strArg, bool fDefault) const
{
const util::SettingsValue value = GetSetting(strArg);
+ return SettingToBool(value, fDefault);
+}
+
+bool SettingToBool(const util::SettingsValue& value, bool fDefault)
+{
return value.isNull() ? fDefault : value.isBool() ? value.get_bool() : InterpretBool(value.get_str());
}
@@ -1002,6 +1024,7 @@ std::string ArgsManager::GetChainName() const
LOCK(cs_args);
util::SettingsValue value = util::GetSetting(m_settings, /* section= */ "", SettingName(arg),
/* ignore_default_section_config= */ false,
+ /*ignore_nonpersistent=*/false,
/* get_chain_name= */ true);
return value.isNull() ? false : value.isBool() ? value.get_bool() : InterpretBool(value.get_str());
};
@@ -1034,7 +1057,8 @@ util::SettingsValue ArgsManager::GetSetting(const std::string& arg) const
{
LOCK(cs_args);
return util::GetSetting(
- m_settings, m_network, SettingName(arg), !UseDefaultSection(arg), /* get_chain_name= */ false);
+ m_settings, m_network, SettingName(arg), !UseDefaultSection(arg),
+ /*ignore_nonpersistent=*/false, /*get_chain_name=*/false);
}
std::vector<util::SettingsValue> ArgsManager::GetSettingsList(const std::string& arg) const
diff --git a/src/util/system.h b/src/util/system.h
index a7f4d16911..bfc9698221 100644
--- a/src/util/system.h
+++ b/src/util/system.h
@@ -161,6 +161,10 @@ struct SectionInfo
int m_line;
};
+std::string SettingToString(const util::SettingsValue&, const std::string&);
+int64_t SettingToInt(const util::SettingsValue&, int64_t);
+bool SettingToBool(const util::SettingsValue&, bool);
+
class ArgsManager
{
public:
@@ -449,6 +453,12 @@ protected:
bool WriteSettingsFile(std::vector<std::string>* errors = nullptr) const;
/**
+ * Get current setting from config file or read/write settings file,
+ * ignoring nonpersistent command line or forced settings values.
+ */
+ util::SettingsValue GetPersistentSetting(const std::string& name) const;
+
+ /**
* Access settings with lock held.
*/
template <typename Fn>