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-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or https://opensource.org/license/mit/.5#include <memusage.h>6#include <span.h>7#include <streams.h>8#include <util/fs_helpers.h>9#include <util/obfuscation.h>11#include <array>13AutoFile::AutoFile(std::FILE* file, const Obfuscation& obfuscation) : m_file{file}, m_obfuscation{obfuscation}14{15if (!IsNull()) {16auto pos{FileTell64(m_file)};17if (pos >= 0) m_position = pos;18}19}21std::size_t AutoFile::detail_fread(std::span<std::byte> dst)22{23if (!m_file) throw std::ios_base::failure("AutoFile::read: file handle is nullptr");24const size_t ret = std::fread(dst.data(), 1, dst.size(), m_file);25if (m_obfuscation) {26if (!m_position) throw std::ios_base::failure("AutoFile::read: position unknown");27m_obfuscation(dst.subspan(0, ret), *m_position);28}29if (m_position) *m_position += ret;30return ret;31}33void AutoFile::seek(int64_t offset, int origin)34{35if (IsNull()) {36throw std::ios_base::failure("AutoFile::seek: file handle is nullptr");37}38if (FileSeek64(m_file, offset, origin) != 0) {39throw std::ios_base::failure(feof() ? "AutoFile::seek: end of file" : "AutoFile::seek: fseek failed");40}41if (origin == SEEK_SET) {42m_position = offset;43} else if (origin == SEEK_CUR && m_position.has_value()) {44*m_position += offset;45} else {46int64_t r{FileTell64(m_file)};47if (r < 0) {48throw std::ios_base::failure("AutoFile::seek: ftell failed");49}50m_position = r;51}52}54int64_t AutoFile::tell()55{56if (!m_position.has_value()) throw std::ios_base::failure("AutoFile::tell: position unknown");57return *m_position;58}60int64_t AutoFile::size()61{62if (IsNull()) {63throw std::ios_base::failure("AutoFile::size: file handle is nullptr");64}65// Temporarily save the current position66int64_t current_pos = tell();67seek(0, SEEK_END);68int64_t file_size = tell();69// Restore the original position70seek(current_pos, SEEK_SET);71return file_size;72}74void AutoFile::read(std::span<std::byte> dst)75{76if (detail_fread(dst) != dst.size()) {77throw std::ios_base::failure(feof() ? "AutoFile::read: end of file" : "AutoFile::read: fread failed");78}79}81void AutoFile::ignore(size_t nSize)82{83if (!m_file) throw std::ios_base::failure("AutoFile::ignore: file handle is nullptr");84unsigned char data[4096];85while (nSize > 0) {86size_t nNow = std::min<size_t>(nSize, sizeof(data));87if (std::fread(data, 1, nNow, m_file) != nNow) {88throw std::ios_base::failure(feof() ? "AutoFile::ignore: end of file" : "AutoFile::ignore: fread failed");89}90nSize -= nNow;91if (m_position.has_value()) *m_position += nNow;92}93}95void AutoFile::write(std::span<const std::byte> src)96{97if (!m_file) throw std::ios_base::failure("AutoFile::write: file handle is nullptr");98if (!m_obfuscation) {99if (std::fwrite(src.data(), 1, src.size(), m_file) != src.size()) {100throw std::ios_base::failure("AutoFile::write: write failed");101}102m_was_written = true;103if (m_position.has_value()) *m_position += src.size();104} else {105std::array<std::byte, 4096> buf;106while (src.size()) {107auto buf_now{std::span{buf}.first(std::min<size_t>(src.size(), buf.size()))};108std::copy_n(src.begin(), buf_now.size(), buf_now.begin());109write_buffer(buf_now);110src = src.subspan(buf_now.size());111}112}113}115void AutoFile::write_buffer(std::span<std::byte> src)116{117if (!m_file) throw std::ios_base::failure("AutoFile::write_buffer: file handle is nullptr");118if (m_obfuscation) {119if (!m_position) throw std::ios_base::failure("AutoFile::write_buffer: obfuscation position unknown");120m_obfuscation(src, *m_position); // obfuscate in-place121}122if (std::fwrite(src.data(), 1, src.size(), m_file) != src.size()) {123throw std::ios_base::failure("AutoFile::write_buffer: write failed");124}125m_was_written = true;126if (m_position) *m_position += src.size();127}129bool AutoFile::Commit()130{131return ::FileCommit(m_file);132}134bool AutoFile::Truncate(uint64_t size)135{136m_was_written = true;137return ::TruncateFile(m_file, size);138}140size_t DataStream::GetMemoryUsage() const noexcept141{142return sizeof(*this) + memusage::DynamicUsage(vch);143}
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.