diff options
author | Gavin Andresen <gavinandresen@gmail.com> | 2013-07-17 12:20:09 +1000 |
---|---|---|
committer | Gavin Andresen <gavinandresen@gmail.com> | 2013-08-22 11:05:44 +1000 |
commit | b94595bb7f6b98c11182df5373fe6461f4776098 (patch) | |
tree | d78a22b32c7f581773118f194f3135b6b8d29adc /src | |
parent | 57d80467f123741f228910dea8c1245134bbfbfe (diff) |
GetDataDir(): cache paths for each network separately
Diffstat (limited to 'src')
-rw-r--r-- | src/chainparams.h | 2 | ||||
-rw-r--r-- | 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 <algorithm> #include <fcntl.h> #include <sys/stat.h> #include <sys/resource.h> @@ -83,7 +84,6 @@ bool fNoListen = false; bool fLogTimestamps = false; CMedianFilter<int64> 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); + + int nNet = CChainParams::MAX_NETWORK_TYPES; + if (fNetSpecific) nNet = Params().NetworkID(); - fs::path &path = pathCached[fNetSpecific]; + 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<string, string>& mapSettingsRet, if (!streamConfig.good()) return; // No bitcoin.conf file is OK - // clear path cache after loading config file - fCachedPath[0] = fCachedPath[1] = false; - set<string> setOptions; setOptions.insert("*"); @@ -1109,6 +1114,8 @@ void ReadConfigFile(map<string, string>& mapSettingsRet, } mapMultiSettingsRet[strKey].push_back(it->value[0]); } + // If datadir is changed in .conf file: + ClearDatadirCache(); } boost::filesystem::path GetPidFile() |