Skip to content

Commit 56e370f

Browse files
committed
util: add ArgsManager datadir helper functions
* Add ArgsManager::EnsureDataDir() Creates data directory if it doesn't exist * Add ArgsManager::GetConfigFilePath() Return config file path (read-only)
1 parent 2c1fe27 commit 56e370f

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/util/system.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,27 @@ const fs::path& ArgsManager::GetDataDir(bool net_specific) const
446446
return path;
447447
}
448448

449+
void ArgsManager::EnsureDataDir() const
450+
{
451+
/**
452+
* "/wallets" subdirectories are created in all **new**
453+
* datadirs, because wallet code will create new wallets in the "wallets"
454+
* subdirectory only if exists already, otherwise it will create them in
455+
* the top-level datadir where they could interfere with other files.
456+
* Wallet init code currently avoids creating "wallets" directories itself
457+
* for backwards compatibility, but this be changed in the future and
458+
* wallet code here could go away (#16220).
459+
*/
460+
auto path{GetDataDir(false)};
461+
if (!fs::exists(path)) {
462+
fs::create_directories(path / "wallets");
463+
}
464+
path = GetDataDir(true);
465+
if (!fs::exists(path)) {
466+
fs::create_directories(path / "wallets");
467+
}
468+
}
469+
449470
void ArgsManager::ClearPathCache()
450471
{
451472
LOCK(cs_args);
@@ -965,6 +986,11 @@ bool ArgsManager::ReadConfigStream(std::istream& stream, const std::string& file
965986
return true;
966987
}
967988

989+
fs::path ArgsManager::GetConfigFilePath() const
990+
{
991+
return GetConfigFile(GetPathArg("-conf", BITCOIN_CONF_FILENAME));
992+
}
993+
968994
bool ArgsManager::ReadConfigFiles(std::string& error, bool ignore_invalid_keys)
969995
{
970996
{

src/util/system.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,11 @@ class ArgsManager
242242
void SelectConfigNetwork(const std::string& network);
243243

244244
[[nodiscard]] bool ParseParameters(int argc, const char* const argv[], std::string& error);
245+
246+
/**
247+
* Return config file path (read-only)
248+
*/
249+
fs::path GetConfigFilePath() const;
245250
[[nodiscard]] bool ReadConfigFiles(std::string& error, bool ignore_invalid_keys = false);
246251

247252
/**
@@ -475,6 +480,12 @@ class ArgsManager
475480
*/
476481
void LogArgs() const;
477482

483+
/**
484+
* If datadir does not exist, create it along with wallets/
485+
* subdirectory(s).
486+
*/
487+
void EnsureDataDir() const;
488+
478489
private:
479490
/**
480491
* Get data directory path

0 commit comments

Comments
 (0)