Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2009-2010 Satoshi Nakamoto2// Copyright (c) 2009-present The Bitcoin Core developers3// Distributed under the MIT software license, see the accompanying4// file COPYING or http://www.opensource.org/licenses/mit-license.php.6#include <flatfile.h>8#include <tinyformat.h>9#include <util/fs_helpers.h>10#include <util/log.h>11#include <util/overflow.h>13#include <stdexcept>15FlatFileSeq::FlatFileSeq(fs::path dir, const char* prefix, size_t chunk_size) :16m_dir(std::move(dir)),17m_prefix(prefix),18m_chunk_size(chunk_size)19{20if (chunk_size == 0) {21throw std::invalid_argument("chunk_size must be positive");22}23}25std::string FlatFilePos::ToString() const26{27return strprintf("FlatFilePos(nFile=%i, nPos=%i)", nFile, nPos);28}30fs::path FlatFileSeq::FileName(const FlatFilePos& pos) const31{32return m_dir / fs::u8path(strprintf("%s%05u.dat", m_prefix, pos.nFile));33}35FILE* FlatFileSeq::Open(const FlatFilePos& pos, bool read_only) const36{37if (pos.IsNull()) {38return nullptr;39}40fs::path path = FileName(pos);41fs::create_directories(path.parent_path());42FILE* file = fsbridge::fopen(path, read_only ? "rb": "rb+");43if (!file && !read_only)44file = fsbridge::fopen(path, "wb+");45if (!file) {46LogError("Unable to open file %s", fs::PathToString(path));47return nullptr;48}49if (pos.nPos && FileSeek64(file, pos.nPos, SEEK_SET)) {50LogError("Unable to seek to position %u of %s", pos.nPos, fs::PathToString(path));51if (fclose(file) != 0) {52LogError("Unable to close file %s", fs::PathToString(path));53}54return nullptr;55}56return file;57}59size_t FlatFileSeq::Allocate(const FlatFilePos& pos, size_t add_size, bool& out_of_space) const60{61out_of_space = false;63uint64_t n_old_chunks = CeilDiv(pos.nPos, uint64_t{m_chunk_size});64uint64_t n_new_chunks = CeilDiv(pos.nPos + add_size, uint64_t{m_chunk_size});65if (n_new_chunks > n_old_chunks) {66uint64_t old_size = pos.nPos;67uint64_t new_size = n_new_chunks * m_chunk_size;68size_t inc_size = new_size - old_size;70if (CheckDiskSpace(m_dir, inc_size)) {71FILE *file = Open(pos);72if (file) {73LogDebug(BCLog::VALIDATION, "Pre-allocating up to position 0x%x in %s%05u.dat\n", new_size, m_prefix, pos.nFile);74AllocateFileRange(file, pos.nPos, inc_size);75if (fclose(file) != 0) {76LogError("Cannot close file %s%05u.dat after extending it with %u bytes", m_prefix, pos.nFile, new_size);77return 0;78}79return inc_size;80}81} else {82out_of_space = true;83}84}85return 0;86}88bool FlatFileSeq::Flush(const FlatFilePos& pos, bool finalize) const89{90FILE* file = Open(FlatFilePos(pos.nFile, 0)); // Avoid fseek to nPos91if (!file) {92LogError("%s: failed to open file %d\n", __func__, pos.nFile);93return false;94}95if (finalize && !TruncateFile(file, pos.nPos)) {96LogError("%s: failed to truncate file %d\n", __func__, pos.nFile);97if (fclose(file) != 0) {98LogError("Failed to close file %d", pos.nFile);99}100return false;101}102if (!FileCommit(file)) {103LogError("%s: failed to commit file %d\n", __func__, pos.nFile);104if (fclose(file) != 0) {105LogError("Failed to close file %d", pos.nFile);106}107return false;108}109DirectoryCommit(m_dir);111if (fclose(file) != 0) {112LogError("Failed to close file %d after flush", pos.nFile);113return false;114}115return true;116}