Bitcoin Core  25.99.0
P2P Digital Currency
fs_helpers.cpp
Go to the documentation of this file.
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 #include <util/fs_helpers.h>
7 
8 #if defined(HAVE_CONFIG_H)
10 #endif
11 
12 #include <logging.h>
13 #include <sync.h>
14 #include <tinyformat.h>
15 #include <util/fs.h>
16 #include <util/getuniquepath.h>
17 #include <util/syserror.h>
18 
19 #include <cerrno>
20 #include <filesystem>
21 #include <fstream>
22 #include <map>
23 #include <memory>
24 #include <string>
25 #include <system_error>
26 #include <utility>
27 
28 #ifndef WIN32
29 // for posix_fallocate, in configure.ac we check if it is present after this
30 #ifdef __linux__
31 
32 #ifdef _POSIX_C_SOURCE
33 #undef _POSIX_C_SOURCE
34 #endif
35 
36 #define _POSIX_C_SOURCE 200112L
37 
38 #endif // __linux__
39 
40 #include <fcntl.h>
41 #include <sys/resource.h>
42 #include <unistd.h>
43 #else
44 #include <io.h> /* For _get_osfhandle, _chsize */
45 #include <shlobj.h> /* For SHGetSpecialFolderPathW */
46 #endif // WIN32
47 
55 static std::map<std::string, std::unique_ptr<fsbridge::FileLock>> dir_locks GUARDED_BY(cs_dir_locks);
56 
57 bool LockDirectory(const fs::path& directory, const fs::path& lockfile_name, bool probe_only)
58 {
60  fs::path pathLockFile = directory / lockfile_name;
61 
62  // If a lock for this directory already exists in the map, don't try to re-lock it
63  if (dir_locks.count(fs::PathToString(pathLockFile))) {
64  return true;
65  }
66 
67  // Create empty lock file if it doesn't exist.
68  FILE* file = fsbridge::fopen(pathLockFile, "a");
69  if (file) fclose(file);
70  auto lock = std::make_unique<fsbridge::FileLock>(pathLockFile);
71  if (!lock->TryLock()) {
72  return error("Error while attempting to lock directory %s: %s", fs::PathToString(directory), lock->GetReason());
73  }
74  if (!probe_only) {
75  // Lock successful and we're not just probing, put it into the map
76  dir_locks.emplace(fs::PathToString(pathLockFile), std::move(lock));
77  }
78  return true;
79 }
80 
81 void UnlockDirectory(const fs::path& directory, const fs::path& lockfile_name)
82 {
84  dir_locks.erase(fs::PathToString(directory / lockfile_name));
85 }
86 
88 {
90  dir_locks.clear();
91 }
92 
93 bool DirIsWritable(const fs::path& directory)
94 {
95  fs::path tmpFile = GetUniquePath(directory);
96 
97  FILE* file = fsbridge::fopen(tmpFile, "a");
98  if (!file) return false;
99 
100  fclose(file);
101  remove(tmpFile);
102 
103  return true;
104 }
105 
106 bool CheckDiskSpace(const fs::path& dir, uint64_t additional_bytes)
107 {
108  constexpr uint64_t min_disk_space = 52428800; // 50 MiB
109 
110  uint64_t free_bytes_available = fs::space(dir).available;
111  return free_bytes_available >= min_disk_space + additional_bytes;
112 }
113 
114 std::streampos GetFileSize(const char* path, std::streamsize max)
115 {
116  std::ifstream file{path, std::ios::binary};
117  file.ignore(max);
118  return file.gcount();
119 }
120 
121 bool FileCommit(FILE* file)
122 {
123  if (fflush(file) != 0) { // harmless if redundantly called
124  LogPrintf("fflush failed: %s\n", SysErrorString(errno));
125  return false;
126  }
127 #ifdef WIN32
128  HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
129  if (FlushFileBuffers(hFile) == 0) {
130  LogPrintf("FlushFileBuffers failed: %s\n", Win32ErrorString(GetLastError()));
131  return false;
132  }
133 #elif defined(MAC_OSX) && defined(F_FULLFSYNC)
134  if (fcntl(fileno(file), F_FULLFSYNC, 0) == -1) { // Manpage says "value other than -1" is returned on success
135  LogPrintf("fcntl F_FULLFSYNC failed: %s\n", SysErrorString(errno));
136  return false;
137  }
138 #elif HAVE_FDATASYNC
139  if (fdatasync(fileno(file)) != 0 && errno != EINVAL) { // Ignore EINVAL for filesystems that don't support sync
140  LogPrintf("fdatasync failed: %s\n", SysErrorString(errno));
141  return false;
142  }
143 #else
144  if (fsync(fileno(file)) != 0 && errno != EINVAL) {
145  LogPrintf("fsync failed: %s\n", SysErrorString(errno));
146  return false;
147  }
148 #endif
149  return true;
150 }
151 
152 void DirectoryCommit(const fs::path& dirname)
153 {
154 #ifndef WIN32
155  FILE* file = fsbridge::fopen(dirname, "r");
156  if (file) {
157  fsync(fileno(file));
158  fclose(file);
159  }
160 #endif
161 }
162 
163 bool TruncateFile(FILE* file, unsigned int length)
164 {
165 #if defined(WIN32)
166  return _chsize(_fileno(file), length) == 0;
167 #else
168  return ftruncate(fileno(file), length) == 0;
169 #endif
170 }
171 
177 {
178 #if defined(WIN32)
179  return 2048;
180 #else
181  struct rlimit limitFD;
182  if (getrlimit(RLIMIT_NOFILE, &limitFD) != -1) {
183  if (limitFD.rlim_cur < (rlim_t)nMinFD) {
184  limitFD.rlim_cur = nMinFD;
185  if (limitFD.rlim_cur > limitFD.rlim_max)
186  limitFD.rlim_cur = limitFD.rlim_max;
187  setrlimit(RLIMIT_NOFILE, &limitFD);
188  getrlimit(RLIMIT_NOFILE, &limitFD);
189  }
190  return limitFD.rlim_cur;
191  }
192  return nMinFD; // getrlimit failed, assume it's fine
193 #endif
194 }
195 
200 void AllocateFileRange(FILE* file, unsigned int offset, unsigned int length)
201 {
202 #if defined(WIN32)
203  // Windows-specific version
204  HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
205  LARGE_INTEGER nFileSize;
206  int64_t nEndPos = (int64_t)offset + length;
207  nFileSize.u.LowPart = nEndPos & 0xFFFFFFFF;
208  nFileSize.u.HighPart = nEndPos >> 32;
209  SetFilePointerEx(hFile, nFileSize, 0, FILE_BEGIN);
210  SetEndOfFile(hFile);
211 #elif defined(MAC_OSX)
212  // OSX specific version
213  // NOTE: Contrary to other OS versions, the OSX version assumes that
214  // NOTE: offset is the size of the file.
215  fstore_t fst;
216  fst.fst_flags = F_ALLOCATECONTIG;
217  fst.fst_posmode = F_PEOFPOSMODE;
218  fst.fst_offset = 0;
219  fst.fst_length = length; // mac os fst_length takes the # of free bytes to allocate, not desired file size
220  fst.fst_bytesalloc = 0;
221  if (fcntl(fileno(file), F_PREALLOCATE, &fst) == -1) {
222  fst.fst_flags = F_ALLOCATEALL;
223  fcntl(fileno(file), F_PREALLOCATE, &fst);
224  }
225  ftruncate(fileno(file), static_cast<off_t>(offset) + length);
226 #else
227 #if defined(HAVE_POSIX_FALLOCATE)
228  // Version using posix_fallocate
229  off_t nEndPos = (off_t)offset + length;
230  if (0 == posix_fallocate(fileno(file), 0, nEndPos)) return;
231 #endif
232  // Fallback version
233  // TODO: just write one byte per block
234  static const char buf[65536] = {};
235  if (fseek(file, offset, SEEK_SET)) {
236  return;
237  }
238  while (length > 0) {
239  unsigned int now = 65536;
240  if (length < now)
241  now = length;
242  fwrite(buf, 1, now, file); // allowed to fail; this function is advisory anyway
243  length -= now;
244  }
245 #endif
246 }
247 
248 #ifdef WIN32
249 fs::path GetSpecialFolderPath(int nFolder, bool fCreate)
250 {
251  WCHAR pszPath[MAX_PATH] = L"";
252 
253  if (SHGetSpecialFolderPathW(nullptr, pszPath, nFolder, fCreate)) {
254  return fs::path(pszPath);
255  }
256 
257  LogPrintf("SHGetSpecialFolderPathW() failed, could not obtain requested path.\n");
258  return fs::path("");
259 }
260 #endif
261 
262 bool RenameOver(fs::path src, fs::path dest)
263 {
264 #ifdef __MINGW64__
265  // This is a workaround for a bug in libstdc++ which
266  // implements std::filesystem::rename with _wrename function.
267  // This bug has been fixed in upstream:
268  // - GCC 10.3: 8dd1c1085587c9f8a21bb5e588dfe1e8cdbba79e
269  // - GCC 11.1: 1dfd95f0a0ca1d9e6cbc00e6cbfd1fa20a98f312
270  // For more details see the commits mentioned above.
271  return MoveFileExW(src.wstring().c_str(), dest.wstring().c_str(),
272  MOVEFILE_REPLACE_EXISTING) != 0;
273 #else
274  std::error_code error;
275  fs::rename(src, dest, error);
276  return !error;
277 #endif
278 }
279 
286 {
287  try {
288  return fs::create_directories(p);
289  } catch (const fs::filesystem_error&) {
290  if (!fs::exists(p) || !fs::is_directory(p))
291  throw;
292  }
293 
294  // create_directories didn't create the directory, it had to have existed already
295  return false;
296 }
Different type to mark Mutex at global scope.
Definition: sync.h:141
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:31
#define MAX_PATH
Definition: compat.h:67
bool LockDirectory(const fs::path &directory, const fs::path &lockfile_name, bool probe_only)
Definition: fs_helpers.cpp:57
static std::map< std::string, std::unique_ptr< fsbridge::FileLock > > dir_locks GUARDED_BY(cs_dir_locks)
A map that contains all the currently held directory locks.
static GlobalMutex cs_dir_locks
Mutex to protect dir_locks.
Definition: fs_helpers.cpp:49
bool DirIsWritable(const fs::path &directory)
Definition: fs_helpers.cpp:93
bool RenameOver(fs::path src, fs::path dest)
Rename src to dest.
Definition: fs_helpers.cpp:262
std::streampos GetFileSize(const char *path, std::streamsize max)
Get the size of a file by scanning it.
Definition: fs_helpers.cpp:114
int RaiseFileDescriptorLimit(int nMinFD)
this function tries to raise the file descriptor limit to the requested number.
Definition: fs_helpers.cpp:176
void DirectoryCommit(const fs::path &dirname)
Sync directory contents.
Definition: fs_helpers.cpp:152
void ReleaseDirectoryLocks()
Release all directory locks.
Definition: fs_helpers.cpp:87
bool TryCreateDirectories(const fs::path &p)
Ignores exceptions thrown by create_directories if the requested directory exists.
Definition: fs_helpers.cpp:285
void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length)
this function tries to make a particular range of a file allocated (corresponding to disk space) it i...
Definition: fs_helpers.cpp:200
bool CheckDiskSpace(const fs::path &dir, uint64_t additional_bytes)
Definition: fs_helpers.cpp:106
bool TruncateFile(FILE *file, unsigned int length)
Definition: fs_helpers.cpp:163
bool FileCommit(FILE *file)
Ensure file contents are fully committed to disk, using a platform-specific feature analogous to fsyn...
Definition: fs_helpers.cpp:121
void UnlockDirectory(const fs::path &directory, const fs::path &lockfile_name)
Definition: fs_helpers.cpp:81
fs::path GetUniquePath(const fs::path &base)
Helper function for getting a unique path.
bool error(const char *fmt, const Args &... args)
Definition: logging.h:262
#define LogPrintf(...)
Definition: logging.h:237
static bool create_directories(const std::filesystem::path &p)
Create directory (and if necessary its parents), unless the leaf directory already exists or is a sym...
Definition: fs.h:188
static bool exists(const path &p)
Definition: fs.h:88
static std::string PathToString(const path &path)
Convert path object to a byte string.
Definition: fs.h:150
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:25
#define LOCK(cs)
Definition: sync.h:258
std::string SysErrorString(int err)
Return system error string from errno value.
Definition: syserror.cpp:21