aboutsummaryrefslogtreecommitdiff
path: root/src/util/system.cpp
diff options
context:
space:
mode:
authorKiminuo <kiminuo@protonmail.com>2021-04-08 23:15:01 +0200
committerKiminuo <kiminuo@protonmail.com>2021-04-17 20:09:01 +0200
commit1add318704108faa98f5b1b8e9c96d960e9d23a8 (patch)
tree0f1c4d6c1215508224c2ebbf74f47ded717b594c /src/util/system.cpp
parent70cdf679f8e665dbdc3301873a0267fe9faa72cd (diff)
downloadbitcoin-1add318704108faa98f5b1b8e9c96d960e9d23a8.tar.xz
Move GetDataDir(fNetSpecific) implementation to ArgsManager.
Diffstat (limited to 'src/util/system.cpp')
-rw-r--r--src/util/system.cpp76
1 files changed, 42 insertions, 34 deletions
diff --git a/src/util/system.cpp b/src/util/system.cpp
index 702cfdf1e4..0a2ece7b10 100644
--- a/src/util/system.cpp
+++ b/src/util/system.cpp
@@ -388,6 +388,45 @@ std::optional<unsigned int> ArgsManager::GetArgFlags(const std::string& name) co
return std::nullopt;
}
+const fs::path& ArgsManager::GetDataDirPath(bool net_specific) const
+{
+ LOCK(cs_args);
+ fs::path& path = net_specific ? m_cached_network_datadir_path : m_cached_datadir_path;
+
+ // Cache the path to avoid calling fs::create_directories on every call of
+ // this function
+ if (!path.empty()) return path;
+
+ std::string datadir = GetArg("-datadir", "");
+ if (!datadir.empty()) {
+ path = fs::system_complete(datadir);
+ if (!fs::is_directory(path)) {
+ path = "";
+ return path;
+ }
+ } else {
+ path = GetDefaultDataDir();
+ }
+ if (net_specific)
+ path /= BaseParams().DataDir();
+
+ if (fs::create_directories(path)) {
+ // This is the first run, create wallets subdirectory too
+ fs::create_directories(path / "wallets");
+ }
+
+ path = StripRedundantLastElementsOfPath(path);
+ return path;
+}
+
+void ArgsManager::ClearDatadirPathCache()
+{
+ LOCK(cs_args);
+
+ m_cached_datadir_path = fs::path();
+ m_cached_network_datadir_path = fs::path();
+}
+
std::optional<const ArgsManager::Command> ArgsManager::GetCommand() const
{
Command ret;
@@ -447,7 +486,7 @@ bool ArgsManager::GetSettingsPath(fs::path* filepath, bool temp) const
}
if (filepath) {
std::string settings = GetArg("-settings", BITCOIN_SETTINGS_FILENAME);
- *filepath = fsbridge::AbsPathJoin(GetDataDir(/* net_specific= */ true), temp ? settings + ".tmp" : settings);
+ *filepath = fsbridge::AbsPathJoin(GetDataDirPath(/* net_specific= */ true), temp ? settings + ".tmp" : settings);
}
return true;
}
@@ -737,8 +776,6 @@ fs::path GetDefaultDataDir()
}
static fs::path g_blocks_path_cache_net_specific;
-static fs::path pathCached;
-static fs::path pathCachedNetSpecific;
static RecursiveMutex csPathCached;
const fs::path &GetBlocksDir()
@@ -769,33 +806,7 @@ const fs::path &GetBlocksDir()
const fs::path &GetDataDir(bool fNetSpecific)
{
- LOCK(csPathCached);
- fs::path &path = fNetSpecific ? pathCachedNetSpecific : pathCached;
-
- // Cache the path to avoid calling fs::create_directories on every call of
- // this function
- if (!path.empty()) return path;
-
- std::string datadir = gArgs.GetArg("-datadir", "");
- if (!datadir.empty()) {
- path = fs::system_complete(datadir);
- if (!fs::is_directory(path)) {
- path = "";
- return path;
- }
- } else {
- path = GetDefaultDataDir();
- }
- if (fNetSpecific)
- path /= BaseParams().DataDir();
-
- if (fs::create_directories(path)) {
- // This is the first run, create wallets subdirectory too
- fs::create_directories(path / "wallets");
- }
-
- path = StripRedundantLastElementsOfPath(path);
- return path;
+ return gArgs.GetDataDirPath(fNetSpecific);
}
bool CheckDataDirOption()
@@ -806,10 +817,7 @@ bool CheckDataDirOption()
void ClearDatadirCache()
{
- LOCK(csPathCached);
-
- pathCached = fs::path();
- pathCachedNetSpecific = fs::path();
+ gArgs.ClearDatadirPathCache();
g_blocks_path_cache_net_specific = fs::path();
}