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#ifndef BITCOIN_UTIL_FS_HELPERS_H7#define BITCOIN_UTIL_FS_HELPERS_H9#include <util/fs.h>11#include <cstdint>12#include <cstdio>13#include <ios>14#include <limits>15#include <optional>16#include <string>18#ifdef __APPLE__19enum class FSType {20EXFAT,21OTHER,22ERROR23};25/**26* Detect filesystem type for a given path.27* Currently identifies exFAT filesystems which cause issues on macOS.28*29* @param[in] path The directory path to check30* @return FSType enum indicating the filesystem type31*/32FSType GetFilesystemType(const fs::path& path);33#endif35/**36* Ensure file contents are fully committed to disk, using a platform-specific37* feature analogous to fsync().38*/39bool FileCommit(FILE* file);41/**42* Sync directory contents. This is required on some environments to ensure that43* newly created files are committed to disk.44*/45void DirectoryCommit(const fs::path& dirname);47bool TruncateFile(FILE* file, uint64_t length);49/**50* 64-bit-clean file positioning.51*52* The wrapper is necessary for Windows, because `long` is only 32 bits there,53* even in 64-bit builds (LLP64).54*/55inline int FileSeek64(FILE* file, int64_t offset, int origin)56{57#if defined(WIN32)58return _fseeki64(file, offset, origin);59#else60return std::fseek(file, offset, origin);61#endif62}64/**65* 64-bit-clean file positioning.66*67* The wrapper is necessary for Windows, because `long` is only 32 bits there,68* even in 64-bit builds (LLP64).69*/70inline int64_t FileTell64(FILE* file)71{72#if defined(WIN32)73return _ftelli64(file);74#else75return std::ftell(file);76#endif77}79/**80* Try to raise the file descriptor limit to the requested number.81*82* @param[in] min_fd The requested minimum number of file descriptors.83* @returns The actual file descriptor limit. It may be lower or84* higher than min_fd. Returns std::numeric_limits<int>::max()85* if the OS imposes no limit (RLIM_INFINITY).86*87*/88int RaiseFileDescriptorLimit(int min_fd);90void AllocateFileRange(FILE* file, uint64_t offset, unsigned int length);92/**93* Rename src to dest.94* @return true if the rename was successful.95*/96[[nodiscard]] bool RenameOver(fs::path src, fs::path dest);98namespace util {99enum class LockResult {100Success,101ErrorWrite,102ErrorLock,103};104[[nodiscard]] LockResult LockDirectory(const fs::path& directory, const fs::path& lockfile_name, bool probe_only = false);105} // namespace util106void UnlockDirectory(const fs::path& directory, const fs::path& lockfile_name);107bool CheckDiskSpace(const fs::path& dir, uint64_t additional_bytes = 0);109/** Get the size of a file by scanning it.110*111* @param[in] path The file path112* @param[in] max Stop seeking beyond this limit113* @return The file size or max114*/115std::streampos GetFileSize(const char* path, std::streamsize max = std::numeric_limits<std::streamsize>::max());117/** Release all directory locks. This is used for unit testing only, at runtime118* the global destructor will take care of the locks.119*/120void ReleaseDirectoryLocks();122bool TryCreateDirectories(const fs::path& p);123fs::path GetDefaultDataDir();125/** Convert fs::perms to symbolic string of the form 'rwxrwxrwx'126*127* @param[in] p the perms to be converted128* @return Symbolic permissions string129*/130std::string PermsToSymbolicString(fs::perms p);131/** Interpret a custom permissions level string as fs::perms132*133* @param[in] s Permission level string134* @return Permissions as fs::perms135*/136std::optional<fs::perms> InterpretPermString(const std::string& s);138/** Check if a directory is writable by creating a temporary file on it.139*140* @param[in] dir_path Path of the directory to test141* @return true if a temporary file could be created and removed, false otherwise.142* @throw std::runtime_error if dir_path is not a directory.143*/144bool IsDirWritable(const fs::path& dir_path);146#ifdef WIN32147fs::path GetSpecialFolderPath(int nFolder, bool fCreate = true);148#endif150#endif // BITCOIN_UTIL_FS_HELPERS_H
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.