8#if defined(HAVE_CONFIG_H)
9#include <config/bitcoin-config.h>
22#include <system_error>
34#define _POSIX_C_SOURCE 200112L
39#include <sys/resource.h>
53static std::map<std::string, std::unique_ptr<fsbridge::FileLock>>
57 const std::string lockfile_name,
bool probe_only) {
59 fs::path pathLockFile = directory / lockfile_name;
73 auto lock = std::make_unique<fsbridge::FileLock>(pathLockFile);
74 if (!lock->TryLock()) {
75 LogError(
"Error while attempting to lock directory %s: %s\n",
87 const std::string &lockfile_name) {
99 constexpr uint64_t min_disk_space = 52428800;
101 uint64_t free_bytes_available = fs::space(dir).available;
102 return free_bytes_available >= min_disk_space + additional_bytes;
105std::streampos
GetFileSize(
const char *path, std::streamsize max) {
106 std::ifstream file{path, std::ios::binary};
108 return file.gcount();
113 if (fflush(file) != 0) {
118 HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
119 if (FlushFileBuffers(hFile) == 0) {
120 LogPrintf(
"FlushFileBuffers failed: %s\n",
121 Win32ErrorString(GetLastError()));
124#elif defined(MAC_OSX) && defined(F_FULLFSYNC)
126 if (fcntl(fileno(file), F_FULLFSYNC, 0) == -1) {
132 if (fdatasync(fileno(file)) != 0 && errno != EINVAL) {
137 if (fsync(fileno(file)) != 0 && errno != EINVAL) {
157 return _chsize(_fileno(file), length) == 0;
159 return ftruncate(fileno(file), length) == 0;
172 struct rlimit limitFD;
173 if (getrlimit(RLIMIT_NOFILE, &limitFD) != -1) {
174 if (limitFD.rlim_cur < (rlim_t)nMinFD) {
175 limitFD.rlim_cur = nMinFD;
176 if (limitFD.rlim_cur > limitFD.rlim_max) {
177 limitFD.rlim_cur = limitFD.rlim_max;
179 setrlimit(RLIMIT_NOFILE, &limitFD);
180 getrlimit(RLIMIT_NOFILE, &limitFD);
182 return limitFD.rlim_cur;
197 HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
198 LARGE_INTEGER nFileSize;
199 int64_t nEndPos = (int64_t)offset + length;
200 nFileSize.u.LowPart = nEndPos & 0xFFFFFFFF;
201 nFileSize.u.HighPart = nEndPos >> 32;
202 SetFilePointerEx(hFile, nFileSize, 0, FILE_BEGIN);
204#elif defined(MAC_OSX)
209 fst.fst_flags = F_ALLOCATECONTIG;
210 fst.fst_posmode = F_PEOFPOSMODE;
214 fst.fst_length = length;
215 fst.fst_bytesalloc = 0;
216 if (fcntl(fileno(file), F_PREALLOCATE, &fst) == -1) {
217 fst.fst_flags = F_ALLOCATEALL;
218 fcntl(fileno(file), F_PREALLOCATE, &fst);
220 ftruncate(fileno(file),
static_cast<off_t
>(offset) + length);
221#elif defined(HAVE_POSIX_FALLOCATE)
223 off_t nEndPos = (off_t)offset + length;
224 posix_fallocate(fileno(file), 0, nEndPos);
228 static const char buf[65536] = {};
229 if (fseek(file, offset, SEEK_SET)) {
233 unsigned int now = 65536;
238 fwrite(buf, 1, now, file);
245fs::path GetSpecialFolderPath(
int nFolder,
bool fCreate) {
248 if (SHGetSpecialFolderPathW(
nullptr, pszPath, nFolder, fCreate)) {
253 "SHGetSpecialFolderPathW() failed, could not obtain requested path.\n");
259 std::error_code error;
260 fs::rename(src, dest, error);
272 }
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 GlobalMutex cs_dir_locks
Mutex to protect dir_locks.
void UnlockDirectory(const fs::path &directory, const std::string &lockfile_name)
bool RenameOver(fs::path src, fs::path dest)
Rename src to 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)
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 FileCommit(FILE *file)
Ensure file contents are fully committed to disk, using a platform-specific feature analogous to fsyn...
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)
LockResult LockDirectory(const fs::path &directory, const std::string lockfile_name, bool probe_only)
std::string SysErrorString(int err)
Return system error string from errno value.