diff options
author | Kiminuo <kiminuo@protonmail.com> | 2021-04-08 23:15:01 +0200 |
---|---|---|
committer | Kiminuo <kiminuo@protonmail.com> | 2021-04-17 20:09:01 +0200 |
commit | 1add318704108faa98f5b1b8e9c96d960e9d23a8 (patch) | |
tree | 0f1c4d6c1215508224c2ebbf74f47ded717b594c /src/util | |
parent | 70cdf679f8e665dbdc3301873a0267fe9faa72cd (diff) |
Move GetDataDir(fNetSpecific) implementation to ArgsManager.
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/system.cpp | 76 | ||||
-rw-r--r-- | src/util/system.h | 16 |
2 files changed, 58 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(); } diff --git a/src/util/system.h b/src/util/system.h index 29657e56e2..a49055b891 100644 --- a/src/util/system.h +++ b/src/util/system.h @@ -200,6 +200,8 @@ protected: std::map<OptionsCategory, std::map<std::string, Arg>> m_available_args GUARDED_BY(cs_args); bool m_accept_any_command GUARDED_BY(cs_args){true}; std::list<SectionInfo> m_config_sections GUARDED_BY(cs_args); + mutable fs::path m_cached_datadir_path GUARDED_BY(cs_args); + mutable fs::path m_cached_network_datadir_path GUARDED_BY(cs_args); [[nodiscard]] bool ReadConfigStream(std::istream& stream, const std::string& filepath, std::string& error, bool ignore_invalid_keys = false); @@ -264,6 +266,20 @@ public: std::optional<const Command> GetCommand() const; /** + * Get data directory path + * + * @param net_specific Append network identifier to the returned path + * @return Absolute path on success, otherwise an empty path when a non-directory path would be returned + * @post Returned directory path is created unless it is empty + */ + const fs::path& GetDataDirPath(bool net_specific = true) const; + + /** + * For testing + */ + void ClearDatadirPathCache(); + + /** * Return a vector of strings of the given argument * * @param strArg Argument to get (e.g. "-foo") |