mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-10-11 03:53:22 +02:00
validation: Refactor block file pre-allocation into FlatFileSeq.
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
#include <flatfile.h>
|
||||
#include <logging.h>
|
||||
#include <tinyformat.h>
|
||||
#include <util/system.h>
|
||||
|
||||
FlatFileSeq::FlatFileSeq(fs::path dir, const char* prefix, size_t chunk_size) :
|
||||
m_dir(std::move(dir)),
|
||||
@@ -45,3 +46,29 @@ FILE* FlatFileSeq::Open(const CDiskBlockPos& pos, bool fReadOnly)
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
size_t FlatFileSeq::Allocate(const CDiskBlockPos& pos, size_t add_size, bool& out_of_space)
|
||||
{
|
||||
out_of_space = false;
|
||||
|
||||
unsigned int n_old_chunks = (pos.nPos + m_chunk_size - 1) / m_chunk_size;
|
||||
unsigned int n_new_chunks = (pos.nPos + add_size + m_chunk_size - 1) / m_chunk_size;
|
||||
if (n_new_chunks > n_old_chunks) {
|
||||
size_t old_size = pos.nPos;
|
||||
size_t new_size = n_new_chunks * m_chunk_size;
|
||||
size_t inc_size = new_size - old_size;
|
||||
|
||||
if (CheckDiskSpace(m_dir, inc_size)) {
|
||||
FILE *file = Open(pos);
|
||||
if (file) {
|
||||
LogPrintf("Pre-allocating up to position 0x%x in %s%05u.dat\n", new_size, m_prefix, pos.nFile);
|
||||
AllocateFileRange(file, pos.nPos, inc_size);
|
||||
fclose(file);
|
||||
return inc_size;
|
||||
}
|
||||
} else {
|
||||
out_of_space = true;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
Reference in New Issue
Block a user