8#include <bitcoin-build-config.h>
21#include <system_error>
32#define _POSIX_C_SOURCE 200112L
37#include <sys/resource.h>
56 fs::path pathLockFile = directory / lockfile_name;
69 auto lock = std::make_unique<fsbridge::FileLock>(pathLockFile);
70 if (!lock->TryLock()) {
95 constexpr uint64_t min_disk_space = 52428800;
97 uint64_t free_bytes_available = fs::space(dir).available;
98 return free_bytes_available >= min_disk_space + additional_bytes;
103 std::ifstream file{path, std::ios::binary};
105 return file.gcount();
110 if (fflush(file) != 0) {
115 HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
116 if (FlushFileBuffers(hFile) == 0) {
117 LogPrintf(
"FlushFileBuffers failed: %s\n", Win32ErrorString(GetLastError()));
120#elif defined(__APPLE__) && defined(F_FULLFSYNC)
121 if (fcntl(fileno(file), F_FULLFSYNC, 0) == -1) {
126 if (fdatasync(fileno(file)) != 0 && errno != EINVAL) {
131 if (fsync(fileno(file)) != 0 && errno != EINVAL) {
153 return _chsize(_fileno(file), length) == 0;
155 return ftruncate(fileno(file), length) == 0;
168 struct rlimit limitFD;
169 if (getrlimit(RLIMIT_NOFILE, &limitFD) != -1) {
170 if (limitFD.rlim_cur < (rlim_t)nMinFD) {
171 limitFD.rlim_cur = nMinFD;
172 if (limitFD.rlim_cur > limitFD.rlim_max)
173 limitFD.rlim_cur = limitFD.rlim_max;
174 setrlimit(RLIMIT_NOFILE, &limitFD);
175 getrlimit(RLIMIT_NOFILE, &limitFD);
177 return limitFD.rlim_cur;
191 HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
192 LARGE_INTEGER nFileSize;
193 int64_t nEndPos = (int64_t)offset + length;
194 nFileSize.u.LowPart = nEndPos & 0xFFFFFFFF;
195 nFileSize.u.HighPart = nEndPos >> 32;
196 SetFilePointerEx(hFile, nFileSize, 0, FILE_BEGIN);
198#elif defined(__APPLE__)
203 fst.fst_flags = F_ALLOCATECONTIG;
204 fst.fst_posmode = F_PEOFPOSMODE;
206 fst.fst_length = length;
207 fst.fst_bytesalloc = 0;
208 if (fcntl(fileno(file), F_PREALLOCATE, &fst) == -1) {
209 fst.fst_flags = F_ALLOCATEALL;
210 fcntl(fileno(file), F_PREALLOCATE, &fst);
212 ftruncate(fileno(file),
static_cast<off_t
>(offset) + length);
214#if defined(HAVE_POSIX_FALLOCATE)
216 off_t nEndPos = (off_t)offset + length;
217 if (0 == posix_fallocate(fileno(file), 0, nEndPos))
return;
221 static const char buf[65536] = {};
222 if (fseek(file, offset, SEEK_SET)) {
226 unsigned int now = 65536;
229 fwrite(buf, 1, now, file);
236fs::path GetSpecialFolderPath(
int nFolder,
bool fCreate)
240 if (SHGetSpecialFolderPathW(
nullptr, pszPath, nFolder, fCreate)) {
244 LogPrintf(
"SHGetSpecialFolderPathW() failed, could not obtain requested path.\n");
251 std::error_code error;
252 fs::rename(src, dest, error);
265 }
catch (
const fs::filesystem_error&) {
276 std::string perm_str(9,
'-');
278 auto set_perm = [&](
size_t pos, fs::perms required_perm,
char letter) {
279 if ((p & required_perm) != fs::perms::none) {
280 perm_str[pos] = letter;
284 set_perm(0, fs::perms::owner_read,
'r');
285 set_perm(1, fs::perms::owner_write,
'w');
286 set_perm(2, fs::perms::owner_exec,
'x');
287 set_perm(3, fs::perms::group_read,
'r');
288 set_perm(4, fs::perms::group_write,
'w');
289 set_perm(5, fs::perms::group_exec,
'x');
290 set_perm(6, fs::perms::others_read,
'r');
291 set_perm(7, fs::perms::others_write,
'w');
292 set_perm(8, fs::perms::others_exec,
'x');
300 return fs::perms::owner_read | fs::perms::owner_write;
301 }
else if (
s ==
"group") {
302 return fs::perms::owner_read | fs::perms::owner_write |
303 fs::perms::group_read;
304 }
else if (
s ==
"all") {
305 return fs::perms::owner_read | fs::perms::owner_write |
306 fs::perms::group_read |
307 fs::perms::others_read;
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.
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)
std::optional< fs::perms > InterpretPermString(const std::string &s)
Interpret a custom permissions level string as fs::perms.
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.
std::string PermsToSymbolicString(fs::perms p)
Convert fs::perms to symbolic string of the form 'rwxrwxrwx'.
bool FileCommit(FILE *file)
Ensure file contents are fully committed to disk, using a platform-specific feature analogous to fsyn...
void UnlockDirectory(const fs::path &directory, const fs::path &lockfile_name)
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 a byte string.
FILE * fopen(const fs::path &p, const char *mode)
LockResult LockDirectory(const fs::path &directory, const fs::path &lockfile_name, bool probe_only)
std::string SysErrorString(int err)
Return system error string from errno value.