@@ -388,6 +388,45 @@ std::optional<unsigned int> ArgsManager::GetArgFlags(const std::string& name) co
388
388
return std::nullopt;
389
389
}
390
390
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
+
391
430
std::optional<const ArgsManager::Command> ArgsManager::GetCommand () const
392
431
{
393
432
Command ret;
@@ -447,7 +486,7 @@ bool ArgsManager::GetSettingsPath(fs::path* filepath, bool temp) const
447
486
}
448
487
if (filepath) {
449
488
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);
451
490
}
452
491
return true ;
453
492
}
@@ -737,8 +776,6 @@ fs::path GetDefaultDataDir()
737
776
}
738
777
739
778
static fs::path g_blocks_path_cache_net_specific;
740
- static fs::path pathCached;
741
- static fs::path pathCachedNetSpecific;
742
779
static RecursiveMutex csPathCached;
743
780
744
781
const fs::path &GetBlocksDir ()
@@ -769,33 +806,7 @@ const fs::path &GetBlocksDir()
769
806
770
807
const fs::path &GetDataDir (bool fNetSpecific )
771
808
{
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 );
799
810
}
800
811
801
812
bool CheckDataDirOption ()
@@ -806,10 +817,7 @@ bool CheckDataDirOption()
806
817
807
818
void ClearDatadirCache ()
808
819
{
809
- LOCK (csPathCached);
810
-
811
- pathCached = fs::path ();
812
- pathCachedNetSpecific = fs::path ();
820
+ gArgs .ClearDatadirPathCache ();
813
821
g_blocks_path_cache_net_specific = fs::path ();
814
822
}
815
823
0 commit comments