Skip to content

Commit 62e7add

Browse files
committed
util: Move CheckDiskSpace to util.
1 parent f3f9c1d commit 62e7add

File tree

5 files changed

+23
-27
lines changed

5 files changed

+23
-27
lines changed

src/init.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1675,11 +1675,11 @@ bool AppInitMain(InitInterfaces& interfaces)
16751675

16761676
// ********************************************************* Step 11: import blocks
16771677

1678-
if (!CheckDiskSpace(/* additional_bytes */ 0, /* blocks_dir */ false)) {
1678+
if (!CheckDiskSpace(GetDataDir())) {
16791679
InitError(strprintf(_("Error: Disk space is low for %s"), GetDataDir()));
16801680
return false;
16811681
}
1682-
if (!CheckDiskSpace(/* additional_bytes */ 0, /* blocks_dir */ true)) {
1682+
if (!CheckDiskSpace(GetBlocksDir())) {
16831683
InitError(strprintf(_("Error: Disk space is low for %s"), GetBlocksDir()));
16841684
return false;
16851685
}

src/util/system.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,14 @@ bool DirIsWritable(const fs::path& directory)
135135
return true;
136136
}
137137

138+
bool CheckDiskSpace(const fs::path& dir, uint64_t nAdditionalBytes)
139+
{
140+
constexpr uint64_t nMinDiskSpace = 52428800; // 50 MiB
141+
142+
uint64_t nFreeBytesAvailable = fs::space(dir).available;
143+
return nFreeBytesAvailable >= nMinDiskSpace + nAdditionalBytes;
144+
}
145+
138146
/**
139147
* Interpret a string argument as a boolean.
140148
*

src/util/system.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ bool RenameOver(fs::path src, fs::path dest);
7272
bool LockDirectory(const fs::path& directory, const std::string lockfile_name, bool probe_only=false);
7373
void UnlockDirectory(const fs::path& directory, const std::string& lockfile_name);
7474
bool DirIsWritable(const fs::path& directory);
75+
bool CheckDiskSpace(const fs::path& dir, uint64_t nAdditionalBytes = 0);
7576

7677
/** Release all directory locks. This is used for unit testing only, at runtime
7778
* the global destructor will take care of the locks.

src/validation.cpp

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2134,8 +2134,9 @@ bool static FlushStateToDisk(const CChainParams& chainparams, CValidationState &
21342134
// Write blocks and block index to disk.
21352135
if (fDoFullFlush || fPeriodicWrite) {
21362136
// Depend on nMinDiskSpace to ensure we can write block index
2137-
if (!CheckDiskSpace(0, true))
2138-
return state.Error("out of disk space");
2137+
if (!CheckDiskSpace(GetBlocksDir())) {
2138+
return AbortNode(state, "Disk space is low!", _("Error: Disk space is low!"));
2139+
}
21392140
// First make sure all block and undo data is flushed to disk.
21402141
FlushBlockFile();
21412142
// Then update all block file information (which may refer to block and undo files).
@@ -2168,8 +2169,9 @@ bool static FlushStateToDisk(const CChainParams& chainparams, CValidationState &
21682169
// twice (once in the log, and once in the tables). This is already
21692170
// an overestimation, as most will delete an existing entry or
21702171
// overwrite one. Still, use a conservative safety factor of 2.
2171-
if (!CheckDiskSpace(48 * 2 * 2 * pcoinsTip->GetCacheSize()))
2172-
return state.Error("out of disk space");
2172+
if (!CheckDiskSpace(GetDataDir(), 48 * 2 * 2 * pcoinsTip->GetCacheSize())) {
2173+
return AbortNode(state, "Disk space is low!", _("Error: Disk space is low!"));
2174+
}
21732175
// Flush the chainstate (which may refer to block index entries).
21742176
if (!pcoinsTip->Flush())
21752177
return AbortNode(state, "Failed to write to coin database");
@@ -3014,7 +3016,7 @@ static bool FindBlockPos(CDiskBlockPos &pos, unsigned int nAddSize, unsigned int
30143016
if (nNewChunks > nOldChunks) {
30153017
if (fPruneMode)
30163018
fCheckForPruning = true;
3017-
if (CheckDiskSpace(nNewChunks * BLOCKFILE_CHUNK_SIZE - pos.nPos, true)) {
3019+
if (CheckDiskSpace(GetBlocksDir(), nNewChunks * BLOCKFILE_CHUNK_SIZE - pos.nPos)) {
30183020
FILE *file = OpenBlockFile(pos);
30193021
if (file) {
30203022
LogPrintf("Pre-allocating up to position 0x%x in blk%05u.dat\n", nNewChunks * BLOCKFILE_CHUNK_SIZE, pos.nFile);
@@ -3023,7 +3025,7 @@ static bool FindBlockPos(CDiskBlockPos &pos, unsigned int nAddSize, unsigned int
30233025
}
30243026
}
30253027
else
3026-
return error("out of disk space");
3028+
return AbortNode("Disk space is low!", _("Error: Disk space is low!"));
30273029
}
30283030
}
30293031

@@ -3047,16 +3049,17 @@ static bool FindUndoPos(CValidationState &state, int nFile, CDiskBlockPos &pos,
30473049
if (nNewChunks > nOldChunks) {
30483050
if (fPruneMode)
30493051
fCheckForPruning = true;
3050-
if (CheckDiskSpace(nNewChunks * UNDOFILE_CHUNK_SIZE - pos.nPos, true)) {
3052+
if (CheckDiskSpace(GetBlocksDir(), nNewChunks * UNDOFILE_CHUNK_SIZE - pos.nPos)) {
30513053
FILE *file = OpenUndoFile(pos);
30523054
if (file) {
30533055
LogPrintf("Pre-allocating up to position 0x%x in rev%05u.dat\n", nNewChunks * UNDOFILE_CHUNK_SIZE, pos.nFile);
30543056
AllocateFileRange(file, pos.nPos, nNewChunks * UNDOFILE_CHUNK_SIZE - pos.nPos);
30553057
fclose(file);
30563058
}
30573059
}
3058-
else
3059-
return state.Error("out of disk space");
3060+
else {
3061+
return AbortNode(state, "Disk space is low!", _("Error: Disk space is low!"));
3062+
}
30603063
}
30613064

30623065
return true;
@@ -3763,17 +3766,6 @@ static void FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPruneAfte
37633766
nLastBlockWeCanPrune, count);
37643767
}
37653768

3766-
bool CheckDiskSpace(uint64_t nAdditionalBytes, bool blocks_dir)
3767-
{
3768-
uint64_t nFreeBytesAvailable = fs::space(blocks_dir ? GetBlocksDir() : GetDataDir()).available;
3769-
3770-
// Check for nMinDiskSpace bytes (currently 50MB)
3771-
if (nFreeBytesAvailable < nMinDiskSpace + nAdditionalBytes)
3772-
return AbortNode("Disk space is low!", _("Error: Disk space is low!"));
3773-
3774-
return true;
3775-
}
3776-
37773769
static FILE* OpenDiskFile(const CDiskBlockPos &pos, const char *prefix, bool fReadOnly)
37783770
{
37793771
if (pos.IsNull())

src/validation.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,6 @@ extern arith_uint256 nMinimumChainWork;
181181
/** Best header we've seen so far (used for getheaders queries' starting points). */
182182
extern CBlockIndex *pindexBestHeader;
183183

184-
/** Minimum disk space required - used in CheckDiskSpace() */
185-
static const uint64_t nMinDiskSpace = 52428800;
186-
187184
/** Pruning-related variables and constants */
188185
/** True if any block files have ever been pruned. */
189186
extern bool fHavePruned;
@@ -245,8 +242,6 @@ bool ProcessNewBlock(const CChainParams& chainparams, const std::shared_ptr<cons
245242
*/
246243
bool ProcessNewBlockHeaders(const std::vector<CBlockHeader>& block, CValidationState& state, const CChainParams& chainparams, const CBlockIndex** ppindex = nullptr, CBlockHeader* first_invalid = nullptr) LOCKS_EXCLUDED(cs_main);
247244

248-
/** Check whether enough disk space is available for an incoming block */
249-
bool CheckDiskSpace(uint64_t nAdditionalBytes = 0, bool blocks_dir = false);
250245
/** Open a block file (blk?????.dat) */
251246
FILE* OpenBlockFile(const CDiskBlockPos &pos, bool fReadOnly = false);
252247
/** Translation to a filesystem path */

0 commit comments

Comments
 (0)