Skip to content

Commit 1add318

Browse files
committed
Move GetDataDir(fNetSpecific) implementation to ArgsManager.
1 parent 70cdf67 commit 1add318

File tree

3 files changed

+63
-37
lines changed

3 files changed

+63
-37
lines changed

src/test/util_tests.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,21 +1143,23 @@ BOOST_AUTO_TEST_CASE(util_ReadWriteSettings)
11431143
{
11441144
// Test writing setting.
11451145
TestArgsManager args1;
1146+
args1.ForceSetArg("-datadir", m_path_root.string());
11461147
args1.LockSettings([&](util::Settings& settings) { settings.rw_settings["name"] = "value"; });
11471148
args1.WriteSettingsFile();
11481149

11491150
// Test reading setting.
11501151
TestArgsManager args2;
1152+
args2.ForceSetArg("-datadir", m_path_root.string());
11511153
args2.ReadSettingsFile();
11521154
args2.LockSettings([&](util::Settings& settings) { BOOST_CHECK_EQUAL(settings.rw_settings["name"].get_str(), "value"); });
11531155

11541156
// Test error logging, and remove previously written setting.
11551157
{
11561158
ASSERT_DEBUG_LOG("Failed renaming settings file");
1157-
fs::remove(GetDataDir() / "settings.json");
1158-
fs::create_directory(GetDataDir() / "settings.json");
1159+
fs::remove(args1.GetDataDirPath() / "settings.json");
1160+
fs::create_directory(args1.GetDataDirPath() / "settings.json");
11591161
args2.WriteSettingsFile();
1160-
fs::remove(GetDataDir() / "settings.json");
1162+
fs::remove(args1.GetDataDirPath() / "settings.json");
11611163
}
11621164
}
11631165

src/util/system.cpp

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,45 @@ std::optional<unsigned int> ArgsManager::GetArgFlags(const std::string& name) co
388388
return std::nullopt;
389389
}
390390

391+
const fs::path& ArgsManager::GetDataDirPath(bool net_specific) const
392+
{
393+
LOCK(cs_args);
394+
fs::path& path = net_specific ? m_cached_network_datadir_path : m_cached_datadir_path;
395+
396+
// Cache the path to avoid calling fs::create_directories on every call of
397+
// this function
398+
if (!path.empty()) return path;
399+
400+
std::string datadir = GetArg("-datadir", "");
401+
if (!datadir.empty()) {
402+
path = fs::system_complete(datadir);
403+
if (!fs::is_directory(path)) {
404+
path = "";
405+
return path;
406+
}
407+
} else {
408+
path = GetDefaultDataDir();
409+
}
410+
if (net_specific)
411+
path /= BaseParams().DataDir();
412+
413+
if (fs::create_directories(path)) {
414+
// This is the first run, create wallets subdirectory too
415+
fs::create_directories(path / "wallets");
416+
}
417+
418+
path = StripRedundantLastElementsOfPath(path);
419+
return path;
420+
}
421+
422+
void ArgsManager::ClearDatadirPathCache()
423+
{
424+
LOCK(cs_args);
425+
426+
m_cached_datadir_path = fs::path();
427+
m_cached_network_datadir_path = fs::path();
428+
}
429+
391430
std::optional<const ArgsManager::Command> ArgsManager::GetCommand() const
392431
{
393432
Command ret;
@@ -447,7 +486,7 @@ bool ArgsManager::GetSettingsPath(fs::path* filepath, bool temp) const
447486
}
448487
if (filepath) {
449488
std::string settings = GetArg("-settings", BITCOIN_SETTINGS_FILENAME);
450-
*filepath = fsbridge::AbsPathJoin(GetDataDir(/* net_specific= */ true), temp ? settings + ".tmp" : settings);
489+
*filepath = fsbridge::AbsPathJoin(GetDataDirPath(/* net_specific= */ true), temp ? settings + ".tmp" : settings);
451490
}
452491
return true;
453492
}
@@ -737,8 +776,6 @@ fs::path GetDefaultDataDir()
737776
}
738777

739778
static fs::path g_blocks_path_cache_net_specific;
740-
static fs::path pathCached;
741-
static fs::path pathCachedNetSpecific;
742779
static RecursiveMutex csPathCached;
743780

744781
const fs::path &GetBlocksDir()
@@ -769,33 +806,7 @@ const fs::path &GetBlocksDir()
769806

770807
const fs::path &GetDataDir(bool fNetSpecific)
771808
{
772-
LOCK(csPathCached);
773-
fs::path &path = fNetSpecific ? pathCachedNetSpecific : pathCached;
774-
775-
// Cache the path to avoid calling fs::create_directories on every call of
776-
// this function
777-
if (!path.empty()) return path;
778-
779-
std::string datadir = gArgs.GetArg("-datadir", "");
780-
if (!datadir.empty()) {
781-
path = fs::system_complete(datadir);
782-
if (!fs::is_directory(path)) {
783-
path = "";
784-
return path;
785-
}
786-
} else {
787-
path = GetDefaultDataDir();
788-
}
789-
if (fNetSpecific)
790-
path /= BaseParams().DataDir();
791-
792-
if (fs::create_directories(path)) {
793-
// This is the first run, create wallets subdirectory too
794-
fs::create_directories(path / "wallets");
795-
}
796-
797-
path = StripRedundantLastElementsOfPath(path);
798-
return path;
809+
return gArgs.GetDataDirPath(fNetSpecific);
799810
}
800811

801812
bool CheckDataDirOption()
@@ -806,10 +817,7 @@ bool CheckDataDirOption()
806817

807818
void ClearDatadirCache()
808819
{
809-
LOCK(csPathCached);
810-
811-
pathCached = fs::path();
812-
pathCachedNetSpecific = fs::path();
820+
gArgs.ClearDatadirPathCache();
813821
g_blocks_path_cache_net_specific = fs::path();
814822
}
815823

src/util/system.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ class ArgsManager
200200
std::map<OptionsCategory, std::map<std::string, Arg>> m_available_args GUARDED_BY(cs_args);
201201
bool m_accept_any_command GUARDED_BY(cs_args){true};
202202
std::list<SectionInfo> m_config_sections GUARDED_BY(cs_args);
203+
mutable fs::path m_cached_datadir_path GUARDED_BY(cs_args);
204+
mutable fs::path m_cached_network_datadir_path GUARDED_BY(cs_args);
203205

204206
[[nodiscard]] bool ReadConfigStream(std::istream& stream, const std::string& filepath, std::string& error, bool ignore_invalid_keys = false);
205207

@@ -263,6 +265,20 @@ class ArgsManager
263265
*/
264266
std::optional<const Command> GetCommand() const;
265267

268+
/**
269+
* Get data directory path
270+
*
271+
* @param net_specific Append network identifier to the returned path
272+
* @return Absolute path on success, otherwise an empty path when a non-directory path would be returned
273+
* @post Returned directory path is created unless it is empty
274+
*/
275+
const fs::path& GetDataDirPath(bool net_specific = true) const;
276+
277+
/**
278+
* For testing
279+
*/
280+
void ClearDatadirPathCache();
281+
266282
/**
267283
* Return a vector of strings of the given argument
268284
*

0 commit comments

Comments
 (0)