Skip to content

Commit

Permalink
util: Move CheckDiskSpace to util.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimpo authored and furszy committed May 14, 2021
1 parent 3fe7d52 commit e65acf0
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 28 deletions.
4 changes: 2 additions & 2 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1735,11 +1735,11 @@ bool AppInitMain()
#endif
// ********************************************************* Step 9: import blocks

if (!CheckDiskSpace(/* additional_bytes */ 0, /* blocks_dir */ false)) {
if (!CheckDiskSpace(GetDataDir())) {
UIError(strprintf(_("Error: Disk space is low for %s"), GetDataDir()));
return false;
}
if (!CheckDiskSpace(/* additional_bytes */ 0, /* blocks_dir */ true)) {
if (!CheckDiskSpace(GetBlocksDir())) {
UIError(strprintf(_("Error: Disk space is low for %s"), GetBlocksDir()));
return false;
}
Expand Down
8 changes: 8 additions & 0 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ ArgsManager gArgs;
bool fDaemon = false;
CTranslationInterface translationInterface;

bool CheckDiskSpace(const fs::path& dir, uint64_t nAdditionalBytes)
{
constexpr uint64_t nMinDiskSpace = 52428800; // 50 MiB

uint64_t nFreeBytesAvailable = fs::space(dir).available;
return nFreeBytesAvailable >= nMinDiskSpace + nAdditionalBytes;
}

/**
* Interpret a string argument as a boolean.
*
Expand Down
1 change: 1 addition & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ bool FileCommit(FILE* file);
bool TruncateFile(FILE* file, unsigned int length);
int RaiseFileDescriptorLimit(int nMinFD);
void AllocateFileRange(FILE* file, unsigned int offset, unsigned int length);
bool CheckDiskSpace(const fs::path& dir, uint64_t nAdditionalBytes = 0);
bool RenameOver(fs::path src, fs::path dest);
bool TryCreateDirectories(const fs::path& p);
fs::path GetDefaultDataDir();
Expand Down
35 changes: 14 additions & 21 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1853,8 +1853,9 @@ bool static FlushStateToDisk(CValidationState& state, FlushStateMode mode)
// 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.
FlushBlockFile();
// Then update all block file information (which may refer to block and undo files).
Expand Down Expand Up @@ -1885,8 +1886,9 @@ bool static FlushStateToDisk(CValidationState& state, FlushStateMode mode)
// twice (once in the log, and once in the tables). This is 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 entries).
if (!pcoinsTip->Flush())
return AbortNode(state, "Failed to write to coin database");
Expand Down Expand Up @@ -2659,15 +2661,16 @@ bool FindBlockPos(CValidationState& state, CDiskBlockPos& pos, unsigned int nAdd
unsigned int nOldChunks = (pos.nPos + BLOCKFILE_CHUNK_SIZE - 1) / BLOCKFILE_CHUNK_SIZE;
unsigned int nNewChunks = (vinfoBlockFile[nFile].nSize + BLOCKFILE_CHUNK_SIZE - 1) / BLOCKFILE_CHUNK_SIZE;
if (nNewChunks > nOldChunks) {
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("Pre-allocating up to position 0x%x in blk%05u.dat\n", nNewChunks * BLOCKFILE_CHUNK_SIZE, pos.nFile);
AllocateFileRange(file, pos.nPos, nNewChunks * BLOCKFILE_CHUNK_SIZE - pos.nPos);
fclose(file);
}
} else
return state.Error("out of disk space");
} else {
return AbortNode("Disk space is low!", _("Error: Disk space is low!"));
}
}
}

Expand All @@ -2689,15 +2692,16 @@ bool FindUndoPos(CValidationState& state, int nFile, CDiskBlockPos& pos, unsigne
unsigned int nOldChunks = (pos.nPos + UNDOFILE_CHUNK_SIZE - 1) / UNDOFILE_CHUNK_SIZE;
unsigned int nNewChunks = (nNewSize + UNDOFILE_CHUNK_SIZE - 1) / UNDOFILE_CHUNK_SIZE;
if (nNewChunks > nOldChunks) {
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", nNewChunks * UNDOFILE_CHUNK_SIZE, pos.nFile);
AllocateFileRange(file, pos.nPos, nNewChunks * UNDOFILE_CHUNK_SIZE - pos.nPos);
fclose(file);
}
} else
return state.Error("out of disk space");
} else {
return AbortNode(state, "Disk space is low!", _("Error: Disk space is low!"));
}
}

return true;
Expand Down Expand Up @@ -3469,17 +3473,6 @@ bool TestBlockValidity(CValidationState& state, const CBlock& block, CBlockIndex
return true;
}

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;
}

FILE* OpenDiskFile(const CDiskBlockPos& pos, const char* prefix, bool fReadOnly)
{
if (pos.IsNull())
Expand Down
6 changes: 1 addition & 5 deletions src/validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,6 @@ extern CMoneySupply MoneySupply;
/** Best header we've seen so far (used for getheaders queries' starting points). */
extern CBlockIndex* pindexBestHeader;

/** Minimum disk space required - used in CheckDiskSpace() */
static const uint64_t nMinDiskSpace = 52428800;

/**
* Process an incoming block. This only returns after the best known valid
* block is made active. Note that it does not, however, guarantee that the
Expand All @@ -175,8 +172,7 @@ static const uint64_t nMinDiskSpace = 52428800;
* @return True if state.IsValid()
*/
bool ProcessNewBlock(CValidationState& state, CNode* pfrom, const std::shared_ptr<const CBlock> pblock, CDiskBlockPos* dbp, bool* fAccepted = 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) */
FILE* OpenBlockFile(const CDiskBlockPos& pos, bool fReadOnly = false);
/** Open an undo file (rev?????.dat) */
Expand Down

0 comments on commit e65acf0

Please sign in to comment.