Skip to content

Commit b4190ef

Browse files
committed
Change GetBlocksDir() to ArgsManager.GetBlocksDirPath().
1 parent 83292e2 commit b4190ef

File tree

5 files changed

+43
-40
lines changed

5 files changed

+43
-40
lines changed

src/init.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ static void CleanupBlockRevFiles()
637637
// Remove the rev files immediately and insert the blk file paths into an
638638
// ordered map keyed by block file index.
639639
LogPrintf("Removing unusable blk?????.dat and rev?????.dat files for -reindex with -prune\n");
640-
fs::path blocksdir = GetBlocksDir();
640+
fs::path blocksdir = gArgs.GetBlocksDirPath();
641641
for (fs::directory_iterator it(blocksdir); it != fs::directory_iterator(); it++) {
642642
if (fs::is_regular_file(*it) &&
643643
it->path().filename().string().length() == 12 &&
@@ -919,7 +919,7 @@ bool AppInitParameterInteraction(const ArgsManager& args)
919919
InitWarning(warnings);
920920
}
921921

922-
if (!fs::is_directory(GetBlocksDir())) {
922+
if (!fs::is_directory(gArgs.GetBlocksDirPath())) {
923923
return InitError(strprintf(_("Specified blocks directory \"%s\" does not exist."), args.GetArg("-blocksdir", "")));
924924
}
925925

@@ -1759,8 +1759,8 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
17591759
InitError(strprintf(_("Error: Disk space is low for %s"), GetDataDir()));
17601760
return false;
17611761
}
1762-
if (!CheckDiskSpace(GetBlocksDir())) {
1763-
InitError(strprintf(_("Error: Disk space is low for %s"), GetBlocksDir()));
1762+
if (!CheckDiskSpace(gArgs.GetBlocksDirPath())) {
1763+
InitError(strprintf(_("Error: Disk space is low for %s"), gArgs.GetBlocksDirPath()));
17641764
return false;
17651765
}
17661766

src/qt/clientmodel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ QString ClientModel::dataDir() const
216216

217217
QString ClientModel::blocksDir() const
218218
{
219-
return GUIUtil::boostPathToQString(GetBlocksDir());
219+
return GUIUtil::boostPathToQString(gArgs.GetBlocksDirPath());
220220
}
221221

222222
void ClientModel::updateBanlist()

src/util/system.cpp

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

391+
const fs::path& ArgsManager::GetBlocksDirPath()
392+
{
393+
LOCK(cs_args);
394+
fs::path& path = m_cached_blocks_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+
if (IsArgSet("-blocksdir")) {
401+
path = fs::system_complete(GetArg("-blocksdir", ""));
402+
if (!fs::is_directory(path)) {
403+
path = "";
404+
return path;
405+
}
406+
} else {
407+
path = GetDataDirPath(false);
408+
}
409+
410+
path /= BaseParams().DataDir();
411+
path /= "blocks";
412+
fs::create_directories(path);
413+
path = StripRedundantLastElementsOfPath(path);
414+
return path;
415+
}
416+
391417
const fs::path& ArgsManager::GetDataDirPath(bool net_specific) const
392418
{
393419
LOCK(cs_args);
@@ -425,6 +451,7 @@ void ArgsManager::ClearDatadirPathCache()
425451

426452
m_cached_datadir_path = fs::path();
427453
m_cached_network_datadir_path = fs::path();
454+
m_cached_blocks_path = fs::path();
428455
}
429456

430457
std::optional<const ArgsManager::Command> ArgsManager::GetCommand() const
@@ -775,35 +802,6 @@ fs::path GetDefaultDataDir()
775802
#endif
776803
}
777804

778-
static fs::path g_blocks_path_cache_net_specific;
779-
static RecursiveMutex csPathCached;
780-
781-
const fs::path &GetBlocksDir()
782-
{
783-
LOCK(csPathCached);
784-
fs::path &path = g_blocks_path_cache_net_specific;
785-
786-
// Cache the path to avoid calling fs::create_directories on every call of
787-
// this function
788-
if (!path.empty()) return path;
789-
790-
if (gArgs.IsArgSet("-blocksdir")) {
791-
path = fs::system_complete(gArgs.GetArg("-blocksdir", ""));
792-
if (!fs::is_directory(path)) {
793-
path = "";
794-
return path;
795-
}
796-
} else {
797-
path = GetDataDir(false);
798-
}
799-
800-
path /= BaseParams().DataDir();
801-
path /= "blocks";
802-
fs::create_directories(path);
803-
path = StripRedundantLastElementsOfPath(path);
804-
return path;
805-
}
806-
807805
const fs::path &GetDataDir(bool fNetSpecific)
808806
{
809807
return gArgs.GetDataDirPath(fNetSpecific);
@@ -818,7 +816,6 @@ bool CheckDataDirOption()
818816
void ClearDatadirCache()
819817
{
820818
gArgs.ClearDatadirPathCache();
821-
g_blocks_path_cache_net_specific = fs::path();
822819
}
823820

824821
fs::path GetConfigFile(const std::string& confPath)

src/util/system.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,6 @@ void ReleaseDirectoryLocks();
9191

9292
bool TryCreateDirectories(const fs::path& p);
9393
fs::path GetDefaultDataDir();
94-
// The blocks directory is always net specific.
95-
const fs::path &GetBlocksDir();
9694
const fs::path &GetDataDir(bool fNetSpecific = true);
9795
// Return true if -datadir option points to a valid directory or is not specified.
9896
bool CheckDataDirOption();
@@ -200,6 +198,7 @@ class ArgsManager
200198
std::map<OptionsCategory, std::map<std::string, Arg>> m_available_args GUARDED_BY(cs_args);
201199
bool m_accept_any_command GUARDED_BY(cs_args){true};
202200
std::list<SectionInfo> m_config_sections GUARDED_BY(cs_args);
201+
fs::path m_cached_blocks_path GUARDED_BY(cs_args);
203202
mutable fs::path m_cached_datadir_path GUARDED_BY(cs_args);
204203
mutable fs::path m_cached_network_datadir_path GUARDED_BY(cs_args);
205204

@@ -265,6 +264,13 @@ class ArgsManager
265264
*/
266265
std::optional<const Command> GetCommand() const;
267266

267+
/**
268+
* Get blocks directory path
269+
*
270+
* @return Blocks path which is network specific
271+
*/
272+
const fs::path& GetBlocksDirPath();
273+
268274
/**
269275
* Get data directory path
270276
*

src/validation.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2204,7 +2204,7 @@ bool CChainState::FlushStateToDisk(
22042204
// Write blocks and block index to disk.
22052205
if (fDoFullFlush || fPeriodicWrite) {
22062206
// Depend on nMinDiskSpace to ensure we can write block index
2207-
if (!CheckDiskSpace(GetBlocksDir())) {
2207+
if (!CheckDiskSpace(gArgs.GetBlocksDirPath())) {
22082208
return AbortNode(state, "Disk space is too low!", _("Disk space is too low!"));
22092209
}
22102210
{
@@ -3890,12 +3890,12 @@ void BlockManager::FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPr
38903890

38913891
static FlatFileSeq BlockFileSeq()
38923892
{
3893-
return FlatFileSeq(GetBlocksDir(), "blk", gArgs.GetBoolArg("-fastprune", false) ? 0x4000 /* 16kb */ : BLOCKFILE_CHUNK_SIZE);
3893+
return FlatFileSeq(gArgs.GetBlocksDirPath(), "blk", gArgs.GetBoolArg("-fastprune", false) ? 0x4000 /* 16kb */ : BLOCKFILE_CHUNK_SIZE);
38943894
}
38953895

38963896
static FlatFileSeq UndoFileSeq()
38973897
{
3898-
return FlatFileSeq(GetBlocksDir(), "rev", UNDOFILE_CHUNK_SIZE);
3898+
return FlatFileSeq(gArgs.GetBlocksDirPath(), "rev", UNDOFILE_CHUNK_SIZE);
38993899
}
39003900

39013901
FILE* OpenBlockFile(const FlatFilePos &pos, bool fReadOnly) {

0 commit comments

Comments
 (0)