0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-03 09:56:38 -05:00

util: make GetDataDir read-only & create datadir..

.. only in bitcoind and bitcoin-qt

This changes behaviour of GetConfigFilePath which now always returns the
absolute path of the provided -conf argument.
This commit is contained in:
willcl-ark 2023-02-09 19:51:41 +00:00
parent 56e370fbb9
commit 64c105442c
No known key found for this signature in database
GPG key ID: CE6EC49945C17EA6
3 changed files with 5 additions and 12 deletions

View file

@ -167,6 +167,7 @@ static void initTranslations(QTranslator &qtTranslatorBase, QTranslator &qtTrans
static bool InitSettings() static bool InitSettings()
{ {
gArgs.EnsureDataDir();
if (!gArgs.GetSettingsPath()) { if (!gArgs.GetSettingsPath()) {
return true; // Do nothing if settings file disabled. return true; // Do nothing if settings file disabled.
} }

View file

@ -417,8 +417,7 @@ const fs::path& ArgsManager::GetDataDir(bool net_specific) const
LOCK(cs_args); LOCK(cs_args);
fs::path& path = net_specific ? m_cached_network_datadir_path : m_cached_datadir_path; 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 // Used cached path if available
// this function
if (!path.empty()) return path; if (!path.empty()) return path;
const fs::path datadir{GetPathArg("-datadir")}; const fs::path datadir{GetPathArg("-datadir")};
@ -432,15 +431,8 @@ const fs::path& ArgsManager::GetDataDir(bool net_specific) const
path = GetDefaultDataDir(); path = GetDefaultDataDir();
} }
if (!fs::exists(path)) {
fs::create_directories(path / "wallets");
}
if (net_specific && !BaseParams().DataDir().empty()) { if (net_specific && !BaseParams().DataDir().empty()) {
path /= fs::PathFromString(BaseParams().DataDir()); path /= fs::PathFromString(BaseParams().DataDir());
if (!fs::exists(path)) {
fs::create_directories(path / "wallets");
}
} }
return path; return path;
@ -512,6 +504,7 @@ bool ArgsManager::IsArgSet(const std::string& strArg) const
bool ArgsManager::InitSettings(std::string& error) bool ArgsManager::InitSettings(std::string& error)
{ {
EnsureDataDir();
if (!GetSettingsPath()) { if (!GetSettingsPath()) {
return true; // Do nothing if settings file disabled. return true; // Do nothing if settings file disabled.
} }
@ -999,8 +992,8 @@ bool ArgsManager::ReadConfigFiles(std::string& error, bool ignore_invalid_keys)
m_config_sections.clear(); m_config_sections.clear();
} }
const fs::path conf_path = GetPathArg("-conf", BITCOIN_CONF_FILENAME); const auto conf_path{GetConfigFilePath()};
std::ifstream stream{GetConfigFile(conf_path)}; std::ifstream stream{conf_path};
// not ok to have a config file specified that cannot be opened // not ok to have a config file specified that cannot be opened
if (IsArgSet("-conf") && !stream.good()) { if (IsArgSet("-conf") && !stream.good()) {

View file

@ -492,7 +492,6 @@ private:
* *
* @param net_specific Append network identifier to the returned 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 * @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& GetDataDir(bool net_specific) const; const fs::path& GetDataDir(bool net_specific) const;