8 #if defined(HAVE_CONFIG_H)
9 #include <config/bitcoin-config.h>
24 #include <system_error>
32 #ifdef _POSIX_C_SOURCE
33 #undef _POSIX_C_SOURCE
36 #define _POSIX_C_SOURCE 200112L
41 #include <sys/resource.h>
55 static std::map<std::string, std::unique_ptr<fsbridge::FileLock>>
61 fs::path pathLockFile = directory / lockfile_name;
74 auto lock = std::make_unique<fsbridge::FileLock>(pathLockFile);
75 if (!lock->TryLock()) {
76 return error(
"Error while attempting to lock directory %s: %s",
87 const std::string &lockfile_name) {
113 constexpr uint64_t min_disk_space = 52428800;
115 uint64_t free_bytes_available = fs::space(dir).available;
116 return free_bytes_available >= min_disk_space + additional_bytes;
119 std::streampos
GetFileSize(
const char *path, std::streamsize max) {
120 std::ifstream file{path, std::ios::binary};
122 return file.gcount();
127 if (fflush(file) != 0) {
128 LogPrintf(
"%s: fflush failed: %d\n", __func__, errno);
132 HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
133 if (FlushFileBuffers(hFile) == 0) {
134 LogPrintf(
"%s: FlushFileBuffers failed: %d\n", __func__,
138 #elif defined(MAC_OSX) && defined(F_FULLFSYNC)
140 if (fcntl(fileno(file), F_FULLFSYNC, 0) == -1) {
141 LogPrintf(
"%s: fcntl F_FULLFSYNC failed: %d\n", __func__, errno);
146 if (fdatasync(fileno(file)) != 0 && errno != EINVAL) {
147 LogPrintf(
"%s: fdatasync failed: %d\n", __func__, errno);
151 if (fsync(fileno(file)) != 0 && errno != EINVAL) {
152 LogPrintf(
"%s: fsync failed: %d\n", __func__, errno);
171 return _chsize(_fileno(file), length) == 0;
173 return ftruncate(fileno(file), length) == 0;
186 struct rlimit limitFD;
187 if (getrlimit(RLIMIT_NOFILE, &limitFD) != -1) {
188 if (limitFD.rlim_cur < (rlim_t)nMinFD) {
189 limitFD.rlim_cur = nMinFD;
190 if (limitFD.rlim_cur > limitFD.rlim_max) {
191 limitFD.rlim_cur = limitFD.rlim_max;
193 setrlimit(RLIMIT_NOFILE, &limitFD);
194 getrlimit(RLIMIT_NOFILE, &limitFD);
196 return limitFD.rlim_cur;
211 HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
212 LARGE_INTEGER nFileSize;
213 int64_t nEndPos = (int64_t)offset + length;
214 nFileSize.u.LowPart = nEndPos & 0xFFFFFFFF;
215 nFileSize.u.HighPart = nEndPos >> 32;
216 SetFilePointerEx(hFile, nFileSize, 0, FILE_BEGIN);
218 #elif defined(MAC_OSX)
223 fst.fst_flags = F_ALLOCATECONTIG;
224 fst.fst_posmode = F_PEOFPOSMODE;
228 fst.fst_length = length;
229 fst.fst_bytesalloc = 0;
230 if (fcntl(fileno(file), F_PREALLOCATE, &fst) == -1) {
231 fst.fst_flags = F_ALLOCATEALL;
232 fcntl(fileno(file), F_PREALLOCATE, &fst);
234 ftruncate(fileno(file),
static_cast<off_t
>(offset) + length);
235 #elif defined(HAVE_POSIX_FALLOCATE)
237 off_t nEndPos = (off_t)offset + length;
238 posix_fallocate(fileno(file), 0, nEndPos);
242 static const char buf[65536] = {};
243 if (fseek(file, offset, SEEK_SET)) {
247 unsigned int now = 65536;
252 fwrite(buf, 1, now, file);
259 fs::path GetSpecialFolderPath(
int nFolder,
bool fCreate) {
262 if (SHGetSpecialFolderPathW(
nullptr, pszPath, nFolder, fCreate)) {
267 "SHGetSpecialFolderPathW() failed, could not obtain requested path.\n");
274 return MoveFileExW(src.wstring().c_str(), dest.wstring().c_str(),
275 MOVEFILE_REPLACE_EXISTING) != 0;
277 int rc = std::rename(src.c_str(), dest.c_str());
290 }
catch (
const fs::filesystem_error &) {
Different type to mark Mutex at global scope.
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
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.
bool LockDirectory(const fs::path &directory, const std::string lockfile_name, bool probe_only)
static GlobalMutex cs_dir_locks
Mutex to protect dir_locks.
void UnlockDirectory(const fs::path &directory, const std::string &lockfile_name)
bool DirIsWritable(const fs::path &directory)
bool RenameOver(fs::path src, fs::path dest)
std::streampos GetFileSize(const char *path, std::streamsize max)
Get the size of a file by scanning it.
int RaiseFileDescriptorLimit(int nMinFD)
This function tries to raise the file descriptor limit to the requested number.
void DirectoryCommit(const fs::path &dirname)
Sync directory contents.
void ReleaseDirectoryLocks()
Release all directory locks.
bool TryCreateDirectories(const fs::path &p)
Ignores exceptions thrown by create_directories if the requested directory exists.
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...
bool CheckDiskSpace(const fs::path &dir, uint64_t additional_bytes)
bool TruncateFile(FILE *file, unsigned int length)
bool FileCommit(FILE *file)
Ensure file contents are fully committed to disk, using a platform-specific feature analogous to fsyn...
fs::path GetUniquePath(const fs::path &base)
Helper function for getting a unique path.
bool error(const char *fmt, const Args &...args)
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...
static bool exists(const path &p)
static std::string PathToString(const path &path)
Convert path object to byte string.
FILE * fopen(const fs::path &p, const char *mode)