8#include <bitcoin-build-config.h>
21#include <system_error>
26#include <sys/resource.h>
45 fs::path pathLockFile = directory / lockfile_name;
58 auto lock = std::make_unique<fsbridge::FileLock>(pathLockFile);
59 if (!lock->TryLock()) {
84 constexpr uint64_t min_disk_space = 52428800;
86 uint64_t free_bytes_available = fs::space(dir).available;
87 return free_bytes_available >= min_disk_space + additional_bytes;
90std::streampos
GetFileSize(
const char* path, std::streamsize max)
92 std::ifstream file{path, std::ios::binary};
99 if (fflush(file) != 0) {
104 HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
105 if (FlushFileBuffers(hFile) == 0) {
106 LogPrintf(
"FlushFileBuffers failed: %s\n", Win32ErrorString(GetLastError()));
109#elif defined(__APPLE__) && defined(F_FULLFSYNC)
110 if (fcntl(fileno(file), F_FULLFSYNC, 0) == -1) {
115 if (fdatasync(fileno(file)) != 0 && errno != EINVAL) {
120 if (fsync(fileno(file)) != 0 && errno != EINVAL) {
142 return _chsize(_fileno(file), length) == 0;
144 return ftruncate(fileno(file), length) == 0;
157 struct rlimit limitFD;
158 if (getrlimit(RLIMIT_NOFILE, &limitFD) != -1) {
159 if (limitFD.rlim_cur < (rlim_t)nMinFD) {
160 limitFD.rlim_cur = nMinFD;
161 if (limitFD.rlim_cur > limitFD.rlim_max)
162 limitFD.rlim_cur = limitFD.rlim_max;
163 setrlimit(RLIMIT_NOFILE, &limitFD);
164 getrlimit(RLIMIT_NOFILE, &limitFD);
166 return limitFD.rlim_cur;
180 HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
181 LARGE_INTEGER nFileSize;
182 int64_t nEndPos = (int64_t)offset + length;
183 nFileSize.u.LowPart = nEndPos & 0xFFFFFFFF;
184 nFileSize.u.HighPart = nEndPos >> 32;
185 SetFilePointerEx(hFile, nFileSize, 0, FILE_BEGIN);
187#elif defined(__APPLE__)
192 fst.fst_flags = F_ALLOCATECONTIG;
193 fst.fst_posmode = F_PEOFPOSMODE;
195 fst.fst_length = length;
196 fst.fst_bytesalloc = 0;
197 if (fcntl(fileno(file), F_PREALLOCATE, &fst) == -1) {
198 fst.fst_flags = F_ALLOCATEALL;
199 fcntl(fileno(file), F_PREALLOCATE, &fst);
201 ftruncate(fileno(file),
static_cast<off_t
>(offset) + length);
203#if defined(HAVE_POSIX_FALLOCATE)
205 off_t nEndPos = (off_t)offset + length;
206 if (0 == posix_fallocate(fileno(file), 0, nEndPos))
return;
210 static const char buf[65536] = {};
211 if (fseek(file, offset, SEEK_SET)) {
215 unsigned int now = 65536;
218 fwrite(buf, 1, now, file);
225fs::path GetSpecialFolderPath(
int nFolder,
bool fCreate)
229 if (SHGetSpecialFolderPathW(
nullptr, pszPath, nFolder, fCreate)) {
230 return fs::path(pszPath);
233 LogPrintf(
"SHGetSpecialFolderPathW() failed, could not obtain requested path.\n");
240 std::error_code error;
241 fs::rename(src, dest, error);
254 }
catch (
const fs::filesystem_error&) {
265 std::string perm_str(9,
'-');
267 auto set_perm = [&](
size_t pos, fs::perms required_perm,
char letter) {
268 if ((p & required_perm) != fs::perms::none) {
269 perm_str[pos] = letter;
273 set_perm(0, fs::perms::owner_read,
'r');
274 set_perm(1, fs::perms::owner_write,
'w');
275 set_perm(2, fs::perms::owner_exec,
'x');
276 set_perm(3, fs::perms::group_read,
'r');
277 set_perm(4, fs::perms::group_write,
'w');
278 set_perm(5, fs::perms::group_exec,
'x');
279 set_perm(6, fs::perms::others_read,
'r');
280 set_perm(7, fs::perms::others_write,
'w');
281 set_perm(8, fs::perms::others_exec,
'x');
289 return fs::perms::owner_read | fs::perms::owner_write;
290 }
else if (
s ==
"group") {
291 return fs::perms::owner_read | fs::perms::owner_write |
292 fs::perms::group_read;
293 }
else if (
s ==
"all") {
294 return fs::perms::owner_read | fs::perms::owner_write |
295 fs::perms::group_read |
296 fs::perms::others_read;
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.