GetDataDir(): cache paths for each network separately

This commit is contained in:
Gavin Andresen 2013-07-17 12:20:09 +10:00
parent 57d80467f1
commit b94595bb7f
2 changed files with 20 additions and 11 deletions

View File

@ -39,6 +39,8 @@ public:
MAIN, MAIN,
TESTNET, TESTNET,
REGTEST, REGTEST,
MAX_NETWORK_TYPES
}; };
enum Base58Type { enum Base58Type {

View File

@ -8,6 +8,7 @@
#ifdef __linux__ #ifdef __linux__
#define _POSIX_C_SOURCE 200112L #define _POSIX_C_SOURCE 200112L
#endif #endif
#include <algorithm>
#include <fcntl.h> #include <fcntl.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/resource.h> #include <sys/resource.h>
@ -83,7 +84,6 @@ bool fNoListen = false;
bool fLogTimestamps = false; bool fLogTimestamps = false;
CMedianFilter<int64> vTimeOffsets(200,0); CMedianFilter<int64> vTimeOffsets(200,0);
volatile bool fReopenDebugLog = false; volatile bool fReopenDebugLog = false;
bool fCachedPath[2] = {false, false};
// Init OpenSSL library multithreading support // Init OpenSSL library multithreading support
static CCriticalSection** ppmutexOpenSSL; static CCriticalSection** ppmutexOpenSSL;
@ -1043,22 +1043,25 @@ boost::filesystem::path GetDefaultDataDir()
#endif #endif
} }
static boost::filesystem::path pathCached[CChainParams::MAX_NETWORK_TYPES+1];
static CCriticalSection csPathCached;
const boost::filesystem::path &GetDataDir(bool fNetSpecific) const boost::filesystem::path &GetDataDir(bool fNetSpecific)
{ {
namespace fs = boost::filesystem; namespace fs = boost::filesystem;
static fs::path pathCached[2]; LOCK(csPathCached);
static CCriticalSection 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 // This can be called during exceptions by printf, so we cache the
// value so we don't have to do memory allocations after that. // value so we don't have to do memory allocations after that.
if (fCachedPath[fNetSpecific]) if (!path.empty())
return path; return path;
LOCK(csPathCached);
if (mapArgs.count("-datadir")) { if (mapArgs.count("-datadir")) {
path = fs::system_complete(mapArgs["-datadir"]); path = fs::system_complete(mapArgs["-datadir"]);
if (!fs::is_directory(path)) { if (!fs::is_directory(path)) {
@ -1073,10 +1076,15 @@ const boost::filesystem::path &GetDataDir(bool fNetSpecific)
fs::create_directories(path); fs::create_directories(path);
fCachedPath[fNetSpecific] = true;
return path; return path;
} }
void ClearDatadirCache()
{
std::fill(&pathCached[0], &pathCached[CChainParams::MAX_NETWORK_TYPES+1],
boost::filesystem::path());
}
boost::filesystem::path GetConfigFile() boost::filesystem::path GetConfigFile()
{ {
boost::filesystem::path pathConfigFile(GetArg("-conf", "bitcoin.conf")); boost::filesystem::path pathConfigFile(GetArg("-conf", "bitcoin.conf"));
@ -1091,9 +1099,6 @@ void ReadConfigFile(map<string, string>& mapSettingsRet,
if (!streamConfig.good()) if (!streamConfig.good())
return; // No bitcoin.conf file is OK return; // No bitcoin.conf file is OK
// clear path cache after loading config file
fCachedPath[0] = fCachedPath[1] = false;
set<string> setOptions; set<string> setOptions;
setOptions.insert("*"); setOptions.insert("*");
@ -1109,6 +1114,8 @@ void ReadConfigFile(map<string, string>& mapSettingsRet,
} }
mapMultiSettingsRet[strKey].push_back(it->value[0]); mapMultiSettingsRet[strKey].push_back(it->value[0]);
} }
// If datadir is changed in .conf file:
ClearDatadirCache();
} }
boost::filesystem::path GetPidFile() boost::filesystem::path GetPidFile()