Changeset View
Changeset View
Standalone View
Standalone View
src/util/system.cpp
Show First 20 Lines • Show All 395 Lines • ▼ Show 20 Lines | for (const auto &arg_map : m_available_args) { | ||||
const auto search = arg_map.second.find(name); | const auto search = arg_map.second.find(name); | ||||
if (search != arg_map.second.end()) { | if (search != arg_map.second.end()) { | ||||
return search->second.m_flags; | return search->second.m_flags; | ||||
} | } | ||||
} | } | ||||
return std::nullopt; | 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::vector<std::string> ArgsManager::GetArgs(const std::string &strArg) const { | std::vector<std::string> ArgsManager::GetArgs(const std::string &strArg) const { | ||||
std::vector<std::string> result; | std::vector<std::string> result; | ||||
for (const util::SettingsValue &value : GetSettingsList(strArg)) { | for (const util::SettingsValue &value : GetSettingsList(strArg)) { | ||||
result.push_back( | result.push_back( | ||||
value.isFalse() ? "0" : value.isTrue() ? "1" : value.get_str()); | value.isFalse() ? "0" : value.isTrue() ? "1" : value.get_str()); | ||||
} | } | ||||
return result; | return result; | ||||
} | } | ||||
Show All 23 Lines | |||||
bool ArgsManager::GetSettingsPath(fs::path *filepath, bool temp) const { | bool ArgsManager::GetSettingsPath(fs::path *filepath, bool temp) const { | ||||
if (IsArgNegated("-settings")) { | if (IsArgNegated("-settings")) { | ||||
return false; | return false; | ||||
} | } | ||||
if (filepath) { | if (filepath) { | ||||
std::string settings = GetArg("-settings", BITCOIN_SETTINGS_FILENAME); | std::string settings = GetArg("-settings", BITCOIN_SETTINGS_FILENAME); | ||||
*filepath = fs::absolute(temp ? settings + ".tmp" : settings, | *filepath = fs::absolute(temp ? settings + ".tmp" : settings, | ||||
GetDataDir(/* net_specific= */ true)); | GetDataDirPath(/* net_specific= */ true)); | ||||
} | } | ||||
return true; | return true; | ||||
} | } | ||||
static void SaveErrors(const std::vector<std::string> errors, | static void SaveErrors(const std::vector<std::string> errors, | ||||
std::vector<std::string> *error_out) { | std::vector<std::string> *error_out) { | ||||
for (const auto &error : errors) { | for (const auto &error : errors) { | ||||
if (error_out) { | if (error_out) { | ||||
▲ Show 20 Lines • Show All 301 Lines • ▼ Show 20 Lines | |||||
#else | #else | ||||
// Unix-like | // Unix-like | ||||
return pathRet / ".bitcoin"; | return pathRet / ".bitcoin"; | ||||
#endif | #endif | ||||
#endif | #endif | ||||
} | } | ||||
static fs::path g_blocks_path_cache_net_specific; | static fs::path g_blocks_path_cache_net_specific; | ||||
static fs::path pathCached; | |||||
static fs::path pathCachedNetSpecific; | |||||
static RecursiveMutex csPathCached; | static RecursiveMutex csPathCached; | ||||
const fs::path &GetBlocksDir() { | const fs::path &GetBlocksDir() { | ||||
LOCK(csPathCached); | LOCK(csPathCached); | ||||
fs::path &path = g_blocks_path_cache_net_specific; | fs::path &path = g_blocks_path_cache_net_specific; | ||||
// Cache the path to avoid calling fs::create_directories on every call of | // Cache the path to avoid calling fs::create_directories on every call of | ||||
// this function | // this function | ||||
Show All 14 Lines | const fs::path &GetBlocksDir() { | ||||
path /= BaseParams().DataDir(); | path /= BaseParams().DataDir(); | ||||
path /= "blocks"; | path /= "blocks"; | ||||
fs::create_directories(path); | fs::create_directories(path); | ||||
path = StripRedundantLastElementsOfPath(path); | path = StripRedundantLastElementsOfPath(path); | ||||
return path; | return path; | ||||
} | } | ||||
const fs::path &GetDataDir(bool fNetSpecific) { | const fs::path &GetDataDir(bool fNetSpecific) { | ||||
LOCK(csPathCached); | return gArgs.GetDataDirPath(fNetSpecific); | ||||
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 | |||||
// | |||||
// TODO: this is an ugly way to create the wallets/ directory and | |||||
// really shouldn't be done here. Once this is fixed, please | |||||
// also remove the corresponding line in bitcoind.cpp AppInit. | |||||
// See more info at: | |||||
// https://reviews.bitcoinabc.org/D3312 | |||||
fs::create_directories(path / "wallets"); | |||||
} | |||||
path = StripRedundantLastElementsOfPath(path); | |||||
return path; | |||||
} | } | ||||
bool CheckDataDirOption() { | bool CheckDataDirOption() { | ||||
std::string datadir = gArgs.GetArg("-datadir", ""); | std::string datadir = gArgs.GetArg("-datadir", ""); | ||||
return datadir.empty() || fs::is_directory(fs::system_complete(datadir)); | return datadir.empty() || fs::is_directory(fs::system_complete(datadir)); | ||||
} | } | ||||
void ClearDatadirCache() { | void ClearDatadirCache() { | ||||
LOCK(csPathCached); | gArgs.ClearDatadirPathCache(); | ||||
pathCached = fs::path(); | |||||
pathCachedNetSpecific = fs::path(); | |||||
g_blocks_path_cache_net_specific = fs::path(); | g_blocks_path_cache_net_specific = fs::path(); | ||||
} | } | ||||
fs::path GetConfigFile(const std::string &confPath) { | fs::path GetConfigFile(const std::string &confPath) { | ||||
return AbsPathForConfigVal(fs::path(confPath), false); | return AbsPathForConfigVal(fs::path(confPath), false); | ||||
} | } | ||||
static bool | static bool | ||||
▲ Show 20 Lines • Show All 575 Lines • Show Last 20 Lines |