From b94595bb7f6b98c11182df5373fe6461f4776098 Mon Sep 17 00:00:00 2001 From: Gavin Andresen Date: Wed, 17 Jul 2013 12:20:09 +1000 Subject: [PATCH] GetDataDir(): cache paths for each network separately --- src/chainparams.h | 2 ++ src/util.cpp | 29 ++++++++++++++++++----------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/chainparams.h b/src/chainparams.h index 0dac79aed4..ce3c14306d 100644 --- a/src/chainparams.h +++ b/src/chainparams.h @@ -39,6 +39,8 @@ public: MAIN, TESTNET, REGTEST, + + MAX_NETWORK_TYPES }; enum Base58Type { diff --git a/src/util.cpp b/src/util.cpp index 049e55b7d6..136a035485 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -8,6 +8,7 @@ #ifdef __linux__ #define _POSIX_C_SOURCE 200112L #endif +#include #include #include #include @@ -83,7 +84,6 @@ bool fNoListen = false; bool fLogTimestamps = false; CMedianFilter vTimeOffsets(200,0); volatile bool fReopenDebugLog = false; -bool fCachedPath[2] = {false, false}; // Init OpenSSL library multithreading support static CCriticalSection** ppmutexOpenSSL; @@ -1043,22 +1043,25 @@ boost::filesystem::path GetDefaultDataDir() #endif } +static boost::filesystem::path pathCached[CChainParams::MAX_NETWORK_TYPES+1]; +static CCriticalSection csPathCached; + const boost::filesystem::path &GetDataDir(bool fNetSpecific) { namespace fs = boost::filesystem; - static fs::path pathCached[2]; - static CCriticalSection csPathCached; + LOCK(csPathCached); - fs::path &path = pathCached[fNetSpecific]; + int nNet = CChainParams::MAX_NETWORK_TYPES; + if (fNetSpecific) nNet = Params().NetworkID(); + + fs::path &path = pathCached[nNet]; // This can be called during exceptions by printf, so we cache the // value so we don't have to do memory allocations after that. - if (fCachedPath[fNetSpecific]) + if (!path.empty()) return path; - LOCK(csPathCached); - if (mapArgs.count("-datadir")) { path = fs::system_complete(mapArgs["-datadir"]); if (!fs::is_directory(path)) { @@ -1073,10 +1076,15 @@ const boost::filesystem::path &GetDataDir(bool fNetSpecific) fs::create_directories(path); - fCachedPath[fNetSpecific] = true; return path; } +void ClearDatadirCache() +{ + std::fill(&pathCached[0], &pathCached[CChainParams::MAX_NETWORK_TYPES+1], + boost::filesystem::path()); +} + boost::filesystem::path GetConfigFile() { boost::filesystem::path pathConfigFile(GetArg("-conf", "bitcoin.conf")); @@ -1091,9 +1099,6 @@ void ReadConfigFile(map& mapSettingsRet, if (!streamConfig.good()) return; // No bitcoin.conf file is OK - // clear path cache after loading config file - fCachedPath[0] = fCachedPath[1] = false; - set setOptions; setOptions.insert("*"); @@ -1109,6 +1114,8 @@ void ReadConfigFile(map& mapSettingsRet, } mapMultiSettingsRet[strKey].push_back(it->value[0]); } + // If datadir is changed in .conf file: + ClearDatadirCache(); } boost::filesystem::path GetPidFile()