6#include <bitcoin-build-config.h>
26#include <system_error>
31#include <sys/resource.h>
69 auto lock = std::make_unique<fsbridge::FileLock>(
pathLockFile);
70 if (!lock->TryLock()) {
103 std::ifstream file{path, std::ios::binary};
105 return file.gcount();
120#elif defined(__APPLE__) && defined(F_FULLFSYNC)
170 static_assert(std::in_range<rlim_t>(std::numeric_limits<int>::max()));
187 std::cmp_greater_equal(
limitFD.rlim_cur, std::numeric_limits<int>::max())) {
188 return std::numeric_limits<int>::max();
190 return static_cast<int>(
limitFD.rlim_cur);
211#elif defined(__APPLE__)
219 fst.fst_length = length;
220 fst.fst_bytesalloc = 0;
227#if defined(HAVE_POSIX_FALLOCATE)
234 static const char buf[65536] = {};
239 unsigned int now = 65536;
242 fwrite(buf, 1, now, file);
257 LogError(
"SHGetSpecialFolderPathW() failed, could not obtain requested path.");
264 std::error_code error;
265 fs::rename(
src, dest, error);
277 return fs::create_directories(
p);
278 }
catch (
const fs::filesystem_error&) {
297 set_perm(0, fs::perms::owner_read,
'r');
298 set_perm(1, fs::perms::owner_write,
'w');
299 set_perm(2, fs::perms::owner_exec,
'x');
300 set_perm(3, fs::perms::group_read,
'r');
301 set_perm(4, fs::perms::group_write,
'w');
302 set_perm(5, fs::perms::group_exec,
'x');
303 set_perm(6, fs::perms::others_read,
'r');
304 set_perm(7, fs::perms::others_write,
'w');
305 set_perm(8, fs::perms::others_exec,
'x');
313 return fs::perms::owner_read | fs::perms::owner_write;
314 }
else if (s ==
"group") {
315 return fs::perms::owner_read | fs::perms::owner_write |
316 fs::perms::group_read;
317 }
else if (s ==
"all") {
318 return fs::perms::owner_read | fs::perms::owner_write |
319 fs::perms::group_read |
320 fs::perms::others_read;
334 std::fclose(created);
346 return FSType::ERROR;
347 }
else if (std::string_view{
fs_info.f_fstypename} ==
"exfat") {
348 return FSType::EXFAT;
350 return FSType::OTHER;
#define Assert(val)
Identity function.
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.
bool TruncateFile(FILE *file, uint64_t length)
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.
void DirectoryCommit(const fs::path &dirname)
Sync directory contents.
int RaiseFileDescriptorLimit(int min_fd)
Try to raise the file descriptor limit to the requested number.
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.
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.
void AllocateFileRange(FILE *file, uint64_t offset, unsigned int length)
this function tries to make a particular range of a file allocated (corresponding to disk space) it i...
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)
int FileSeek64(FILE *file, int64_t offset, int origin)
64-bit-clean file positioning.
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.
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.