fs_helpers.h
1 // Copyright (c) 2009-2010 Satoshi Nakamoto 2 // Copyright (c) 2009-2023 The Bitcoin Core developers 3 // Distributed under the MIT software license, see the accompanying 4 // file COPYING or http://www.opensource.org/licenses/mit-license.php. 5 6 #ifndef BITCOIN_UTIL_FS_HELPERS_H 7 #define BITCOIN_UTIL_FS_HELPERS_H 8 9 #include <util/fs.h> 10 11 #include <cstdint> 12 #include <cstdio> 13 #include <iosfwd> 14 #include <limits> 15 #include <optional> 16 17 #ifdef __APPLE__ 18 enum class FSType { 19 EXFAT, 20 OTHER, 21 ERROR 22 }; 23 24 /** 25 * Detect filesystem type for a given path. 26 * Currently identifies exFAT filesystems which cause issues on macOS. 27 * 28 * @param[in] path The directory path to check 29 * @return FSType enum indicating the filesystem type 30 */ 31 FSType GetFilesystemType(const fs::path& path); 32 #endif 33 34 /** 35 * Ensure file contents are fully committed to disk, using a platform-specific 36 * feature analogous to fsync(). 37 */ 38 bool FileCommit(FILE* file); 39 40 /** 41 * Sync directory contents. This is required on some environments to ensure that 42 * newly created files are committed to disk. 43 */ 44 void DirectoryCommit(const fs::path& dirname); 45 46 bool TruncateFile(FILE* file, unsigned int length); 47 int RaiseFileDescriptorLimit(int nMinFD); 48 void AllocateFileRange(FILE* file, unsigned int offset, unsigned int length); 49 50 /** 51 * Rename src to dest. 52 * @return true if the rename was successful. 53 */ 54 [[nodiscard]] bool RenameOver(fs::path src, fs::path dest); 55 56 namespace util { 57 enum class LockResult { 58 Success, 59 ErrorWrite, 60 ErrorLock, 61 }; 62 [[nodiscard]] LockResult LockDirectory(const fs::path& directory, const fs::path& lockfile_name, bool probe_only = false); 63 } // namespace util 64 void UnlockDirectory(const fs::path& directory, const fs::path& lockfile_name); 65 bool CheckDiskSpace(const fs::path& dir, uint64_t additional_bytes = 0); 66 67 /** Get the size of a file by scanning it. 68 * 69 * @param[in] path The file path 70 * @param[in] max Stop seeking beyond this limit 71 * @return The file size or max 72 */ 73 std::streampos GetFileSize(const char* path, std::streamsize max = std::numeric_limits<std::streamsize>::max()); 74 75 /** Release all directory locks. This is used for unit testing only, at runtime 76 * the global destructor will take care of the locks. 77 */ 78 void ReleaseDirectoryLocks(); 79 80 bool TryCreateDirectories(const fs::path& p); 81 fs::path GetDefaultDataDir(); 82 83 /** Convert fs::perms to symbolic string of the form 'rwxrwxrwx' 84 * 85 * @param[in] p the perms to be converted 86 * @return Symbolic permissions string 87 */ 88 std::string PermsToSymbolicString(fs::perms p); 89 /** Interpret a custom permissions level string as fs::perms 90 * 91 * @param[in] s Permission level string 92 * @return Permissions as fs::perms 93 */ 94 std::optional<fs::perms> InterpretPermString(const std::string& s); 95 96 #ifdef WIN32 97 fs::path GetSpecialFolderPath(int nFolder, bool fCreate = true); 98 #endif 99 100 #endif // BITCOIN_UTIL_FS_HELPERS_H