Inside Bitcoin
Opening the selected file…
Inside Bitcoin
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.
251};253class WindowsWritableFile : public WritableFile {254public:255WindowsWritableFile(std::string filename, ScopedHandle handle)256: pos_(0), handle_(std::move(handle)), filename_(std::move(filename)) {}258~WindowsWritableFile() override = default;260Status Append(const Slice& data) override {261size_t write_size = data.size();262const char* write_data = data.data();264// Fit as much as possible into buffer.265size_t copy_size = std::min(write_size, kWritableFileBufferSize - pos_);266std::memcpy(buf_ + pos_, write_data, copy_size);267write_data += copy_size;268write_size -= copy_size;269pos_ += copy_size;270if (write_size == 0) {271return Status::OK();272}274// Can't fit in buffer, so need to do at least one write.275Status status = FlushBuffer();276if (!status.ok()) {277return status;278}280// Small writes go to buffer, large writes are written directly.281if (write_size < kWritableFileBufferSize) {282std::memcpy(buf_, write_data, write_size);283pos_ = write_size;284return Status::OK();285}286return WriteUnbuffered(write_data, write_size);287}289Status Close() override {290Status status = FlushBuffer();291if (!handle_.Close() && status.ok()) {292status = WindowsError(filename_, ::GetLastError());293}294return status;295}297Status Flush() override { return FlushBuffer(); }299Status Sync() override {300// On Windows no need to sync parent directory. Its metadata will be updated301// via the creation of the new file, without an explicit sync.303Status status = FlushBuffer();304if (!status.ok()) {305return status;306}308if (!::FlushFileBuffers(handle_.get())) {309return Status::IOError(filename_,310GetWindowsErrorMessage(::GetLastError()));311}312return Status::OK();313}315std::string GetName() const override { return filename_; }317private:318Status FlushBuffer() {319Status status = WriteUnbuffered(buf_, pos_);320pos_ = 0;321return status;322}324Status WriteUnbuffered(const char* data, size_t size) {325DWORD bytes_written;326if (!::WriteFile(handle_.get(), data, static_cast<DWORD>(size),327&bytes_written, nullptr)) {328return Status::IOError(filename_,329GetWindowsErrorMessage(::GetLastError()));330}331return Status::OK();332}334// buf_[0, pos_-1] contains data to be written to handle_.335char buf_[kWritableFileBufferSize];336size_t pos_;338ScopedHandle handle_;339const std::string filename_;340};342// Lock or unlock the entire file as specified by |lock|. Returns true343// when successful, false upon failure. Caller should call ::GetLastError()344// to determine cause of failure345bool LockOrUnlock(HANDLE handle, bool lock) {346if (lock) {347return ::LockFile(handle,348/*dwFileOffsetLow=*/0, /*dwFileOffsetHigh=*/0,349/*nNumberOfBytesToLockLow=*/MAXDWORD,350/*nNumberOfBytesToLockHigh=*/MAXDWORD);351} else {352return ::UnlockFile(handle,353/*dwFileOffsetLow=*/0, /*dwFileOffsetHigh=*/0,354/*nNumberOfBytesToLockLow=*/MAXDWORD,355/*nNumberOfBytesToLockHigh=*/MAXDWORD);356}357}359class WindowsFileLock : public FileLock {360public:361WindowsFileLock(ScopedHandle handle, std::string filename)362: handle_(std::move(handle)), filename_(std::move(filename)) {}364const ScopedHandle& handle() const { return handle_; }365const std::string& filename() const { return filename_; }367private:368const ScopedHandle handle_;369const std::string filename_;370};372class WindowsEnv : public Env {373public:374WindowsEnv();375~WindowsEnv() override {376static const char msg[] =377"WindowsEnv singleton destroyed. Unsupported behavior!\n";378std::fwrite(msg, 1, sizeof(msg), stderr);379std::abort();380}382Status NewSequentialFile(const std::string& filename,383SequentialFile** result) override {384*result = nullptr;385DWORD desired_access = GENERIC_READ;386DWORD share_mode = FILE_SHARE_READ;387auto wFilename = toUtf16(filename);388ScopedHandle handle = ::CreateFileW(389wFilename.c_str(), desired_access, share_mode,390/*lpSecurityAttributes=*/nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,391/*hTemplateFile=*/nullptr);392if (!handle.is_valid()) {393return WindowsError(filename, ::GetLastError());394}396*result = new WindowsSequentialFile(filename, std::move(handle));397return Status::OK();398}400Status NewRandomAccessFile(const std::string& filename,401RandomAccessFile** result) override {402*result = nullptr;403DWORD desired_access = GENERIC_READ;404DWORD share_mode = FILE_SHARE_READ;405auto wFilename = toUtf16(filename);406ScopedHandle handle =407::CreateFileW(wFilename.c_str(), desired_access, share_mode,408/*lpSecurityAttributes=*/nullptr, OPEN_EXISTING,409FILE_ATTRIBUTE_READONLY,410/*hTemplateFile=*/nullptr);411if (!handle.is_valid()) {412return WindowsError(filename, ::GetLastError());413}414if (!mmap_limiter_.Acquire()) {415*result = new WindowsRandomAccessFile(filename, std::move(handle));416return Status::OK();417}419LARGE_INTEGER file_size;420Status status;421if (!::GetFileSizeEx(handle.get(), &file_size)) {422mmap_limiter_.Release();423return WindowsError(filename, ::GetLastError());424}426ScopedHandle mapping =427::CreateFileMappingW(handle.get(),428/*security attributes=*/nullptr, PAGE_READONLY,429/*dwMaximumSizeHigh=*/0,430/*dwMaximumSizeLow=*/0,431/*lpName=*/nullptr);432if (mapping.is_valid()) {433void* mmap_base = ::MapViewOfFile(mapping.get(), FILE_MAP_READ,434/*dwFileOffsetHigh=*/0,435/*dwFileOffsetLow=*/0,436/*dwNumberOfBytesToMap=*/0);437if (mmap_base) {438*result = new WindowsMmapReadableFile(439filename, reinterpret_cast<char*>(mmap_base),440static_cast<size_t>(file_size.QuadPart), &mmap_limiter_);441return Status::OK();442}443}444mmap_limiter_.Release();445return WindowsError(filename, ::GetLastError());446}448Status NewWritableFile(const std::string& filename,449WritableFile** result) override {450DWORD desired_access = GENERIC_WRITE;451DWORD share_mode = 0; // Exclusive access.452auto wFilename = toUtf16(filename);453ScopedHandle handle = ::CreateFileW(454wFilename.c_str(), desired_access, share_mode,455/*lpSecurityAttributes=*/nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL,456/*hTemplateFile=*/nullptr);457if (!handle.is_valid()) {458*result = nullptr;459return WindowsError(filename, ::GetLastError());460}462*result = new WindowsWritableFile(filename, std::move(handle));463return Status::OK();464}466Status NewAppendableFile(const std::string& filename,467WritableFile** result) override {468DWORD desired_access = FILE_APPEND_DATA;469DWORD share_mode = 0; // Exclusive access.470auto wFilename = toUtf16(filename);471ScopedHandle handle = ::CreateFileW(472wFilename.c_str(), desired_access, share_mode,473/*lpSecurityAttributes=*/nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL,474/*hTemplateFile=*/nullptr);475if (!handle.is_valid()) {476*result = nullptr;477return WindowsError(filename, ::GetLastError());478}480*result = new WindowsWritableFile(filename, std::move(handle));481return Status::OK();482}484bool FileExists(const std::string& filename) override {485auto wFilename = toUtf16(filename);486return GetFileAttributesW(wFilename.c_str()) != INVALID_FILE_ATTRIBUTES;487}489Status GetChildren(const std::string& directory_path,490std::vector<std::string>* result) override {491const std::string find_pattern = directory_path + "\\*";492WIN32_FIND_DATAW find_data;493auto wFind_pattern = toUtf16(find_pattern);494HANDLE dir_handle = ::FindFirstFileW(wFind_pattern.c_str(), &find_data);495if (dir_handle == INVALID_HANDLE_VALUE) {496DWORD last_error = ::GetLastError();497if (last_error == ERROR_FILE_NOT_FOUND) {498return Status::OK();499}500return WindowsError(directory_path, last_error);
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.