8#include <bitcoin-build-config.h>
21#include <system_error>
26#include <sys/resource.h>
50 fs::path pathLockFile = directory / lockfile_name;
63 auto lock = std::make_unique<fsbridge::FileLock>(pathLockFile);
64 if (!lock->TryLock()) {
89 constexpr uint64_t min_disk_space = 52428800;
91 uint64_t free_bytes_available = fs::space(dir).available;
92 return free_bytes_available >= min_disk_space + additional_bytes;
95std::streampos
GetFileSize(
const char* path, std::streamsize max)
97 std::ifstream file{path, std::ios::binary};
104 if (fflush(file) != 0) {
109 HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
110 if (FlushFileBuffers(hFile) == 0) {
111 LogPrintf(
"FlushFileBuffers failed: %s\n", Win32ErrorString(GetLastError()));
114#elif defined(__APPLE__) && defined(F_FULLFSYNC)
115 if (fcntl(fileno(file), F_FULLFSYNC, 0) == -1) {
120 if (fdatasync(fileno(file)) != 0 && errno != EINVAL) {
125 if (fsync(fileno(file)) != 0 && errno != EINVAL) {
147 return _chsize(_fileno(file), length) == 0;
149 return ftruncate(fileno(file), length) == 0;
162 struct rlimit limitFD;
163 if (getrlimit(RLIMIT_NOFILE, &limitFD) != -1) {
164 if (limitFD.rlim_cur < (rlim_t)nMinFD) {
165 limitFD.rlim_cur = nMinFD;
166 if (limitFD.rlim_cur > limitFD.rlim_max)
167 limitFD.rlim_cur = limitFD.rlim_max;
168 setrlimit(RLIMIT_NOFILE, &limitFD);
169 getrlimit(RLIMIT_NOFILE, &limitFD);
171 return limitFD.rlim_cur;
185 HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
186 LARGE_INTEGER nFileSize;
187 int64_t nEndPos = (int64_t)offset + length;
188 nFileSize.u.LowPart = nEndPos & 0xFFFFFFFF;
189 nFileSize.u.HighPart = nEndPos >> 32;
190 SetFilePointerEx(hFile, nFileSize, 0, FILE_BEGIN);
192#elif defined(__APPLE__)
197 fst.fst_flags = F_ALLOCATECONTIG;
198 fst.fst_posmode = F_PEOFPOSMODE;
200 fst.fst_length = length;
201 fst.fst_bytesalloc = 0;
202 if (fcntl(fileno(file), F_PREALLOCATE, &fst) == -1) {
203 fst.fst_flags = F_ALLOCATEALL;
204 fcntl(fileno(file), F_PREALLOCATE, &fst);
206 ftruncate(fileno(file),
static_cast<off_t
>(offset) + length);
208#if defined(HAVE_POSIX_FALLOCATE)
210 off_t nEndPos = (off_t)offset + length;
211 if (0 == posix_fallocate(fileno(file), 0, nEndPos))
return;
215 static const char buf[65536] = {};
216 if (fseek(file, offset, SEEK_SET)) {
220 unsigned int now = 65536;
223 fwrite(buf, 1, now, file);
230fs::path GetSpecialFolderPath(
int nFolder,
bool fCreate)
234 if (SHGetSpecialFolderPathW(
nullptr, pszPath, nFolder, fCreate)) {
235 return fs::path(pszPath);
238 LogPrintf(
"SHGetSpecialFolderPathW() failed, could not obtain requested path.\n");
245 std::error_code error;
246 fs::rename(src, dest, error);
259 }
catch (
const fs::filesystem_error&) {
270 std::string perm_str(9,
'-');
272 auto set_perm = [&](
size_t pos, fs::perms required_perm,
char letter) {
273 if ((p & required_perm) != fs::perms::none) {
274 perm_str[pos] = letter;
278 set_perm(0, fs::perms::owner_read,
'r');
279 set_perm(1, fs::perms::owner_write,
'w');
280 set_perm(2, fs::perms::owner_exec,
'x');
281 set_perm(3, fs::perms::group_read,
'r');
282 set_perm(4, fs::perms::group_write,
'w');
283 set_perm(5, fs::perms::group_exec,
'x');
284 set_perm(6, fs::perms::others_read,
'r');
285 set_perm(7, fs::perms::others_write,
'w');
286 set_perm(8, fs::perms::others_exec,
'x');
294 return fs::perms::owner_read | fs::perms::owner_write;
295 }
else if (
s ==
"group") {
296 return fs::perms::owner_read | fs::perms::owner_write |
297 fs::perms::group_read;
298 }
else if (
s ==
"all") {
299 return fs::perms::owner_read | fs::perms::owner_write |
300 fs::perms::group_read |
301 fs::perms::others_read;
308FSType GetFilesystemType(
const fs::path& path)
310 if (
struct statfs fs_info; statfs(path.c_str(), &fs_info)) {
311 return FSType::ERROR;
312 }
else if (std::string_view{fs_info.f_fstypename} ==
"exfat") {
313 return FSType::EXFAT;
315 return FSType::OTHER;
Different type to mark Mutex at global scope.
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.
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.