6#include <bitcoin-build-config.h>
21#include <system_error>
26#include <sys/resource.h>
51 fs::path pathLockFile = directory / lockfile_name;
64 auto lock = std::make_unique<fsbridge::FileLock>(pathLockFile);
65 if (!lock->TryLock()) {
90 constexpr uint64_t min_disk_space = 52428800;
92 uint64_t free_bytes_available = fs::space(dir).available;
93 return free_bytes_available >= min_disk_space + additional_bytes;
96std::streampos
GetFileSize(
const char* path, std::streamsize max)
98 std::ifstream file{path, std::ios::binary};
100 return file.gcount();
105 if (fflush(file) != 0) {
110 HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
111 if (FlushFileBuffers(hFile) == 0) {
112 LogError(
"FlushFileBuffers failed: %s", Win32ErrorString(GetLastError()));
115#elif defined(__APPLE__) && defined(F_FULLFSYNC)
116 if (fcntl(fileno(file), F_FULLFSYNC, 0) == -1) {
121 if (fdatasync(fileno(file)) != 0 && errno != EINVAL) {
126 if (fsync(fileno(file)) != 0 && errno != EINVAL) {
148 return _chsize(_fileno(file), length) == 0;
150 return ftruncate(fileno(file), length) == 0;
163 struct rlimit limitFD;
164 if (getrlimit(RLIMIT_NOFILE, &limitFD) != -1) {
165 if (limitFD.rlim_cur < (rlim_t)nMinFD) {
166 limitFD.rlim_cur = nMinFD;
167 if (limitFD.rlim_cur > limitFD.rlim_max)
168 limitFD.rlim_cur = limitFD.rlim_max;
169 setrlimit(RLIMIT_NOFILE, &limitFD);
170 getrlimit(RLIMIT_NOFILE, &limitFD);
172 return limitFD.rlim_cur;
186 HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
187 LARGE_INTEGER nFileSize;
188 int64_t nEndPos = (int64_t)offset + length;
189 nFileSize.u.LowPart = nEndPos & 0xFFFFFFFF;
190 nFileSize.u.HighPart = nEndPos >> 32;
191 SetFilePointerEx(hFile, nFileSize, 0, FILE_BEGIN);
193#elif defined(__APPLE__)
198 fst.fst_flags = F_ALLOCATECONTIG;
199 fst.fst_posmode = F_PEOFPOSMODE;
201 fst.fst_length = length;
202 fst.fst_bytesalloc = 0;
203 if (fcntl(fileno(file), F_PREALLOCATE, &fst) == -1) {
204 fst.fst_flags = F_ALLOCATEALL;
205 fcntl(fileno(file), F_PREALLOCATE, &fst);
207 ftruncate(fileno(file),
static_cast<off_t
>(offset) + length);
209#if defined(HAVE_POSIX_FALLOCATE)
211 off_t nEndPos = (off_t)offset + length;
212 if (0 == posix_fallocate(fileno(file), 0, nEndPos))
return;
216 static const char buf[65536] = {};
217 if (fseek(file, offset, SEEK_SET)) {
221 unsigned int now = 65536;
224 fwrite(buf, 1, now, file);
231fs::path GetSpecialFolderPath(
int nFolder,
bool fCreate)
235 if (SHGetSpecialFolderPathW(
nullptr, pszPath, nFolder, fCreate)) {
236 return fs::path(pszPath);
239 LogError(
"SHGetSpecialFolderPathW() failed, could not obtain requested path.");
246 std::error_code error;
247 fs::rename(src, dest, error);
259 return fs::create_directories(p);
260 }
catch (
const fs::filesystem_error&) {
271 std::string perm_str(9,
'-');
273 auto set_perm = [&](
size_t pos, fs::perms required_perm,
char letter) {
274 if ((p & required_perm) != fs::perms::none) {
275 perm_str[pos] = letter;
279 set_perm(0, fs::perms::owner_read,
'r');
280 set_perm(1, fs::perms::owner_write,
'w');
281 set_perm(2, fs::perms::owner_exec,
'x');
282 set_perm(3, fs::perms::group_read,
'r');
283 set_perm(4, fs::perms::group_write,
'w');
284 set_perm(5, fs::perms::group_exec,
'x');
285 set_perm(6, fs::perms::others_read,
'r');
286 set_perm(7, fs::perms::others_write,
'w');
287 set_perm(8, fs::perms::others_exec,
'x');
295 return fs::perms::owner_read | fs::perms::owner_write;
296 }
else if (
s ==
"group") {
297 return fs::perms::owner_read | fs::perms::owner_write |
298 fs::perms::group_read;
299 }
else if (
s ==
"all") {
300 return fs::perms::owner_read | fs::perms::owner_write |
301 fs::perms::group_read |
302 fs::perms::others_read;
309FSType GetFilesystemType(
const fs::path& path)
311 if (
struct statfs fs_info; statfs(path.c_str(), &fs_info)) {
312 return FSType::ERROR;
313 }
else if (std::string_view{fs_info.f_fstypename} ==
"exfat") {
314 return FSType::EXFAT;
316 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.