diff --git a/src/init.cpp b/src/init.cpp --- a/src/init.cpp +++ b/src/init.cpp @@ -2315,12 +2315,12 @@ } // Step 11: import blocks - if (!CheckDiskSpace(/* additional_bytes */ 0, /* blocks_dir */ false)) { + if (!CheckDiskSpace(GetDataDir())) { InitError( strprintf(_("Error: Disk space is low for %s"), GetDataDir())); return false; } - if (!CheckDiskSpace(/* additional_bytes */ 0, /* blocks_dir */ true)) { + if (!CheckDiskSpace(GetBlocksDir())) { InitError( strprintf(_("Error: Disk space is low for %s"), GetBlocksDir())); return false; diff --git a/src/util.h b/src/util.h --- a/src/util.h +++ b/src/util.h @@ -75,6 +75,7 @@ bool LockDirectory(const fs::path &directory, const std::string lockfile_name, bool probe_only = false); bool DirIsWritable(const fs::path &directory); +bool CheckDiskSpace(const fs::path &dir, uint64_t nAdditionalBytes = 0); /** * Release all directory locks. This is used for unit testing only, at runtime diff --git a/src/util.cpp b/src/util.cpp --- a/src/util.cpp +++ b/src/util.cpp @@ -203,6 +203,14 @@ return true; } +bool CheckDiskSpace(const fs::path &dir, uint64_t nAdditionalBytes) { + // 50 MiB + constexpr uint64_t nMinDiskSpace = 52428800; + + uint64_t nFreeBytesAvailable = fs::space(dir).available; + return nFreeBytesAvailable >= nMinDiskSpace + nAdditionalBytes; +} + /** * Interpret a string argument as a boolean. * diff --git a/src/validation.h b/src/validation.h --- a/src/validation.h +++ b/src/validation.h @@ -256,9 +256,6 @@ */ extern CBlockIndex *pindexBestHeader; -/** Minimum disk space required - used in CheckDiskSpace() */ -static const uint64_t nMinDiskSpace = 52428800; - /** Pruning-related variables and constants */ /** True if any block files have ever been pruned. */ extern bool fHavePruned; @@ -350,11 +347,6 @@ const CBlockIndex **ppindex = nullptr, CBlockHeader *first_invalid = nullptr); -/** - * Check whether enough disk space is available for an incoming block. - */ -bool CheckDiskSpace(uint64_t nAdditionalBytes = 0, bool blocks_dir = false); - /** * Open a block file (blk?????.dat). */ diff --git a/src/validation.cpp b/src/validation.cpp --- a/src/validation.cpp +++ b/src/validation.cpp @@ -2029,8 +2029,9 @@ // Write blocks and block index to disk. if (fDoFullFlush || fPeriodicWrite) { // Depend on nMinDiskSpace to ensure we can write block index - if (!CheckDiskSpace(0, true)) { - return state.Error("out of disk space"); + if (!CheckDiskSpace(GetBlocksDir())) { + return AbortNode(state, "Disk space is low!", + _("Error: Disk space is low!")); } // First make sure all block and undo data is flushed to disk. @@ -2076,8 +2077,10 @@ // already an overestimation, as most will delete an existing // entry or overwrite one. Still, use a conservative safety // factor of 2. - if (!CheckDiskSpace(48 * 2 * 2 * pcoinsTip->GetCacheSize())) { - return state.Error("out of disk space"); + if (!CheckDiskSpace(GetDataDir(), + 48 * 2 * 2 * pcoinsTip->GetCacheSize())) { + return AbortNode(state, "Disk space is low!", + _("Error: Disk space is low!")); } // Flush the chainstate (which may refer to block index @@ -3344,8 +3347,8 @@ fCheckForPruning = true; } - if (CheckDiskSpace(nNewChunks * BLOCKFILE_CHUNK_SIZE - pos.nPos, - true)) { + if (CheckDiskSpace(GetBlocksDir(), + nNewChunks * BLOCKFILE_CHUNK_SIZE - pos.nPos)) { FILE *file = OpenBlockFile(pos); if (file) { LogPrintf( @@ -3357,7 +3360,8 @@ fclose(file); } } else { - return error("out of disk space"); + return AbortNode("Disk space is low!", + _("Error: Disk space is low!")); } } } @@ -3386,7 +3390,8 @@ fCheckForPruning = true; } - if (CheckDiskSpace(nNewChunks * UNDOFILE_CHUNK_SIZE - pos.nPos, true)) { + if (CheckDiskSpace(GetBlocksDir(), + nNewChunks * UNDOFILE_CHUNK_SIZE - pos.nPos)) { FILE *file = OpenUndoFile(pos); if (file) { LogPrintf("Pre-allocating up to position 0x%x in rev%05u.dat\n", @@ -3396,7 +3401,8 @@ fclose(file); } } else { - return state.Error("out of disk space"); + return AbortNode(state, "Disk space is low!", + _("Error: Disk space is low!")); } } @@ -4307,18 +4313,6 @@ nLastBlockWeCanPrune, count); } -bool CheckDiskSpace(uint64_t nAdditionalBytes, bool blocks_dir) { - uint64_t nFreeBytesAvailable = - fs::space(blocks_dir ? GetBlocksDir() : GetDataDir()).available; - - // Check for nMinDiskSpace bytes (currently 50MB) - if (nFreeBytesAvailable < nMinDiskSpace + nAdditionalBytes) { - return AbortNode("Disk space is low!", _("Error: Disk space is low!")); - } - - return true; -} - static FILE *OpenDiskFile(const CDiskBlockPos &pos, const char *prefix, bool fReadOnly) { if (pos.IsNull()) {