Line data Source code
1 : // Copyright (c) 2009-2010 Satoshi Nakamoto
2 : // Copyright (c) 2009-present 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 <ios>
14 : #include <limits>
15 : #include <optional>
16 : #include <string>
17 :
18 : #ifdef __APPLE__
19 : enum class FSType {
20 : EXFAT,
21 : OTHER,
22 : ERROR
23 : };
24 :
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 check
30 : * @return FSType enum indicating the filesystem type
31 : */
32 : FSType GetFilesystemType(const fs::path& path);
33 : #endif
34 :
35 : /**
36 : * Ensure file contents are fully committed to disk, using a platform-specific
37 : * feature analogous to fsync().
38 : */
39 : bool FileCommit(FILE* file);
40 :
41 : /**
42 : * Sync directory contents. This is required on some environments to ensure that
43 : * newly created files are committed to disk.
44 : */
45 : void DirectoryCommit(const fs::path& dirname);
46 :
47 : bool TruncateFile(FILE* file, uint64_t length);
48 :
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 : */
55 506017 : inline int FileSeek64(FILE* file, int64_t offset, int origin)
56 : {
57 : #if defined(WIN32)
58 : return _fseeki64(file, offset, origin);
59 : #else
60 506017 : return std::fseek(file, offset, origin);
61 : #endif
62 : }
63 :
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 : */
70 513049 : inline int64_t FileTell64(FILE* file)
71 : {
72 : #if defined(WIN32)
73 : return _ftelli64(file);
74 : #else
75 513049 : return std::ftell(file);
76 : #endif
77 : }
78 :
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 or
84 : * higher than min_fd. Returns std::numeric_limits<int>::max()
85 : * if the OS imposes no limit (RLIM_INFINITY).
86 : *
87 : */
88 : int RaiseFileDescriptorLimit(int min_fd);
89 :
90 : void AllocateFileRange(FILE* file, uint64_t offset, uint64_t length);
91 :
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);
97 :
98 : namespace util {
99 : enum class LockResult {
100 : Success,
101 : ErrorWrite,
102 : ErrorLock,
103 : };
104 : [[nodiscard]] LockResult LockDirectory(const fs::path& directory, const fs::path& lockfile_name, bool probe_only = false);
105 : } // namespace util
106 : void UnlockDirectory(const fs::path& directory, const fs::path& lockfile_name);
107 : bool CheckDiskSpace(const fs::path& dir, uint64_t additional_bytes = 0);
108 :
109 : /** Get the size of a file by scanning it.
110 : *
111 : * @param[in] path The file path
112 : * @param[in] max Stop seeking beyond this limit
113 : * @return The file size or max
114 : */
115 : std::streampos GetFileSize(const char* path, std::streamsize max = std::numeric_limits<std::streamsize>::max());
116 :
117 : /** Release all directory locks. This is used for unit testing only, at runtime
118 : * the global destructor will take care of the locks.
119 : */
120 : void ReleaseDirectoryLocks();
121 :
122 : bool TryCreateDirectories(const fs::path& p);
123 : fs::path GetDefaultDataDir();
124 :
125 : /** Convert fs::perms to symbolic string of the form 'rwxrwxrwx'
126 : *
127 : * @param[in] p the perms to be converted
128 : * @return Symbolic permissions string
129 : */
130 : std::string PermsToSymbolicString(fs::perms p);
131 : /** Interpret a custom permissions level string as fs::perms
132 : *
133 : * @param[in] s Permission level string
134 : * @return Permissions as fs::perms
135 : */
136 : std::optional<fs::perms> InterpretPermString(const std::string& s);
137 :
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 test
141 : * @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 : */
144 : bool IsDirWritable(const fs::path& dir_path);
145 :
146 : #ifdef WIN32
147 : fs::path GetSpecialFolderPath(int nFolder, bool fCreate = true);
148 : #endif
149 :
150 : #endif // BITCOIN_UTIL_FS_HELPERS_H
|