mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-13 07:28:59 +01:00
Move GetDataDir(fNetSpecific) implementation to ArgsManager.
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user