6#include <bitcoin-build-config.h>
24#include <system_error>
29#include <sys/resource.h>
54 fs::path pathLockFile = directory / lockfile_name;
67 auto lock = std::make_unique<fsbridge::FileLock>(pathLockFile);
68 if (!lock->TryLock()) {
93 constexpr uint64_t min_disk_space{50_MiB};
95 uint64_t free_bytes_available = fs::space(dir).available;
96 return free_bytes_available >= min_disk_space + additional_bytes;
99std::streampos
GetFileSize(
const char* path, std::streamsize max)
101 std::ifstream file{path, std::ios::binary};
103 return file.gcount();
108 if (fflush(file) != 0) {
113 HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
114 if (FlushFileBuffers(hFile) == 0) {
115 LogError(
"FlushFileBuffers failed: %s", Win32ErrorString(GetLastError()));
118#elif defined(__APPLE__) && defined(F_FULLFSYNC)
119 if (fcntl(fileno(file), F_FULLFSYNC, 0) == -1) {
124 if (fdatasync(fileno(file)) != 0 && errno != EINVAL) {
129 if (fsync(fileno(file)) != 0 && errno != EINVAL) {
151 return _chsize(_fileno(file), length) == 0;
153 return ftruncate(fileno(file), length) == 0;
166 struct rlimit limitFD;
167 if (getrlimit(RLIMIT_NOFILE, &limitFD) != -1) {
168 if (limitFD.rlim_cur < (rlim_t)nMinFD) {
169 limitFD.rlim_cur = nMinFD;
170 if (limitFD.rlim_cur > limitFD.rlim_max)
171 limitFD.rlim_cur = limitFD.rlim_max;
172 setrlimit(RLIMIT_NOFILE, &limitFD);
173 getrlimit(RLIMIT_NOFILE, &limitFD);
175 return limitFD.rlim_cur;
189 HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
190 LARGE_INTEGER nFileSize;
191 int64_t nEndPos = (int64_t)offset + length;
192 nFileSize.u.LowPart = nEndPos & 0xFFFFFFFF;
193 nFileSize.u.HighPart = nEndPos >> 32;
194 SetFilePointerEx(hFile, nFileSize, 0, FILE_BEGIN);
196#elif defined(__APPLE__)
201 fst.fst_flags = F_ALLOCATECONTIG;
202 fst.fst_posmode = F_PEOFPOSMODE;
204 fst.fst_length = length;
205 fst.fst_bytesalloc = 0;
206 if (fcntl(fileno(file), F_PREALLOCATE, &fst) == -1) {
207 fst.fst_flags = F_ALLOCATEALL;
208 fcntl(fileno(file), F_PREALLOCATE, &fst);
210 ftruncate(fileno(file),
static_cast<off_t
>(offset) + length);
212#if defined(HAVE_POSIX_FALLOCATE)
214 off_t nEndPos = (off_t)offset + length;
215 if (0 == posix_fallocate(fileno(file), 0, nEndPos))
return;
219 static const char buf[65536] = {};
220 if (fseek(file, offset, SEEK_SET)) {
224 unsigned int now = 65536;
227 fwrite(buf, 1, now, file);
234fs::path GetSpecialFolderPath(
int nFolder,
bool fCreate)
238 if (SHGetSpecialFolderPathW(
nullptr, pszPath, nFolder, fCreate)) {
239 return fs::path(pszPath);
242 LogError(
"SHGetSpecialFolderPathW() failed, could not obtain requested path.");
249 std::error_code error;
250 fs::rename(src, dest, error);
262 return fs::create_directories(p);
263 }
catch (
const fs::filesystem_error&) {
274 std::string perm_str(9,
'-');
276 auto set_perm = [&](
size_t pos, fs::perms required_perm,
char letter) {
277 if ((p & required_perm) != fs::perms::none) {
278 perm_str[pos] = letter;
282 set_perm(0, fs::perms::owner_read,
'r');
283 set_perm(1, fs::perms::owner_write,
'w');
284 set_perm(2, fs::perms::owner_exec,
'x');
285 set_perm(3, fs::perms::group_read,
'r');
286 set_perm(4, fs::perms::group_write,
'w');
287 set_perm(5, fs::perms::group_exec,
'x');
288 set_perm(6, fs::perms::others_read,
'r');
289 set_perm(7, fs::perms::others_write,
'w');
290 set_perm(8, fs::perms::others_exec,
'x');
298 return fs::perms::owner_read | fs::perms::owner_write;
299 }
else if (
s ==
"group") {
300 return fs::perms::owner_read | fs::perms::owner_write |
301 fs::perms::group_read;
302 }
else if (
s ==
"all") {
303 return fs::perms::owner_read | fs::perms::owner_write |
304 fs::perms::group_read |
305 fs::perms::others_read;
314 if (!fs::is_directory(dir_path))
throw std::runtime_error(
strprintf(
"Path %s is not a directory",
fs::PathToString(dir_path)));
326 std::fclose(created);
335FSType GetFilesystemType(
const fs::path& path)
337 if (
struct statfs fs_info; statfs(path.c_str(), &fs_info)) {
338 return FSType::ERROR;
339 }
else if (std::string_view{fs_info.f_fstypename} ==
"exfat") {
340 return FSType::EXFAT;
342 return FSType::OTHER;
uint64_t rand64() noexcept
Generate a random 64-bit integer.
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 path PathFromString(const std::string &string)
Convert byte string to path object.
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 IsDirWritable(const fs::path &dir_path)
Check if a directory is writable by creating a temporary file on it.
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.