6#include <bitcoin-build-config.h>
22#include <system_error>
27#include <sys/resource.h>
52 fs::path pathLockFile = directory / lockfile_name;
65 auto lock = std::make_unique<fsbridge::FileLock>(pathLockFile);
66 if (!lock->TryLock()) {
91 constexpr uint64_t min_disk_space{50_MiB};
93 uint64_t free_bytes_available = fs::space(dir).available;
94 return free_bytes_available >= min_disk_space + additional_bytes;
97std::streampos
GetFileSize(
const char* path, std::streamsize max)
99 std::ifstream file{path, std::ios::binary};
101 return file.gcount();
106 if (fflush(file) != 0) {
111 HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
112 if (FlushFileBuffers(hFile) == 0) {
113 LogError(
"FlushFileBuffers failed: %s", Win32ErrorString(GetLastError()));
116#elif defined(__APPLE__) && defined(F_FULLFSYNC)
117 if (fcntl(fileno(file), F_FULLFSYNC, 0) == -1) {
122 if (fdatasync(fileno(file)) != 0 && errno != EINVAL) {
127 if (fsync(fileno(file)) != 0 && errno != EINVAL) {
149 return _chsize(_fileno(file), length) == 0;
151 return ftruncate(fileno(file), length) == 0;
164 struct rlimit limitFD;
165 if (getrlimit(RLIMIT_NOFILE, &limitFD) != -1) {
166 if (limitFD.rlim_cur < (rlim_t)nMinFD) {
167 limitFD.rlim_cur = nMinFD;
168 if (limitFD.rlim_cur > limitFD.rlim_max)
169 limitFD.rlim_cur = limitFD.rlim_max;
170 setrlimit(RLIMIT_NOFILE, &limitFD);
171 getrlimit(RLIMIT_NOFILE, &limitFD);
173 return limitFD.rlim_cur;
187 HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
188 LARGE_INTEGER nFileSize;
189 int64_t nEndPos = (int64_t)offset + length;
190 nFileSize.u.LowPart = nEndPos & 0xFFFFFFFF;
191 nFileSize.u.HighPart = nEndPos >> 32;
192 SetFilePointerEx(hFile, nFileSize, 0, FILE_BEGIN);
194#elif defined(__APPLE__)
199 fst.fst_flags = F_ALLOCATECONTIG;
200 fst.fst_posmode = F_PEOFPOSMODE;
202 fst.fst_length = length;
203 fst.fst_bytesalloc = 0;
204 if (fcntl(fileno(file), F_PREALLOCATE, &fst) == -1) {
205 fst.fst_flags = F_ALLOCATEALL;
206 fcntl(fileno(file), F_PREALLOCATE, &fst);
208 ftruncate(fileno(file),
static_cast<off_t
>(offset) + length);
210#if defined(HAVE_POSIX_FALLOCATE)
212 off_t nEndPos = (off_t)offset + length;
213 if (0 == posix_fallocate(fileno(file), 0, nEndPos))
return;
217 static const char buf[65536] = {};
218 if (fseek(file, offset, SEEK_SET)) {
222 unsigned int now = 65536;
225 fwrite(buf, 1, now, file);
232fs::path GetSpecialFolderPath(
int nFolder,
bool fCreate)
236 if (SHGetSpecialFolderPathW(
nullptr, pszPath, nFolder, fCreate)) {
237 return fs::path(pszPath);
240 LogError(
"SHGetSpecialFolderPathW() failed, could not obtain requested path.");
247 std::error_code error;
248 fs::rename(src, dest, error);
260 return fs::create_directories(p);
261 }
catch (
const fs::filesystem_error&) {
272 std::string perm_str(9,
'-');
274 auto set_perm = [&](
size_t pos, fs::perms required_perm,
char letter) {
275 if ((p & required_perm) != fs::perms::none) {
276 perm_str[pos] = letter;
280 set_perm(0, fs::perms::owner_read,
'r');
281 set_perm(1, fs::perms::owner_write,
'w');
282 set_perm(2, fs::perms::owner_exec,
'x');
283 set_perm(3, fs::perms::group_read,
'r');
284 set_perm(4, fs::perms::group_write,
'w');
285 set_perm(5, fs::perms::group_exec,
'x');
286 set_perm(6, fs::perms::others_read,
'r');
287 set_perm(7, fs::perms::others_write,
'w');
288 set_perm(8, fs::perms::others_exec,
'x');
296 return fs::perms::owner_read | fs::perms::owner_write;
297 }
else if (
s ==
"group") {
298 return fs::perms::owner_read | fs::perms::owner_write |
299 fs::perms::group_read;
300 }
else if (
s ==
"all") {
301 return fs::perms::owner_read | fs::perms::owner_write |
302 fs::perms::group_read |
303 fs::perms::others_read;
310FSType GetFilesystemType(
const fs::path& path)
312 if (
struct statfs fs_info; statfs(path.c_str(), &fs_info)) {
313 return FSType::ERROR;
314 }
else if (std::string_view{fs_info.f_fstypename} ==
"exfat") {
315 return FSType::EXFAT;
317 return FSType::OTHER;
Different type to mark Mutex at global scope.
static bool exists(const path &p)
static std::string PathToString(const path &path)
Convert path object to a byte string.
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)
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.