8 #if defined(HAVE_CONFIG_H)
25 #include <system_error>
32 #ifdef _POSIX_C_SOURCE
33 #undef _POSIX_C_SOURCE
36 #define _POSIX_C_SOURCE 200112L
41 #include <sys/resource.h>
60 fs::path pathLockFile = directory / lockfile_name;
69 if (file) fclose(file);
70 auto lock = std::make_unique<fsbridge::FileLock>(pathLockFile);
71 if (!lock->TryLock()) {
72 return error(
"Error while attempting to lock directory %s: %s",
fs::PathToString(directory), lock->GetReason());
98 if (!file)
return false;
108 constexpr uint64_t min_disk_space = 52428800;
110 uint64_t free_bytes_available = fs::space(dir).available;
111 return free_bytes_available >= min_disk_space + additional_bytes;
116 std::ifstream file{path, std::ios::binary};
118 return file.gcount();
123 if (fflush(file) != 0) {
128 HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
129 if (FlushFileBuffers(hFile) == 0) {
130 LogPrintf(
"FlushFileBuffers failed: %s\n", Win32ErrorString(GetLastError()));
133 #elif defined(MAC_OSX) && defined(F_FULLFSYNC)
134 if (fcntl(fileno(file), F_FULLFSYNC, 0) == -1) {
139 if (fdatasync(fileno(file)) != 0 && errno != EINVAL) {
144 if (fsync(fileno(file)) != 0 && errno != EINVAL) {
166 return _chsize(_fileno(file), length) == 0;
168 return ftruncate(fileno(file), length) == 0;
181 struct rlimit limitFD;
182 if (getrlimit(RLIMIT_NOFILE, &limitFD) != -1) {
183 if (limitFD.rlim_cur < (rlim_t)nMinFD) {
184 limitFD.rlim_cur = nMinFD;
185 if (limitFD.rlim_cur > limitFD.rlim_max)
186 limitFD.rlim_cur = limitFD.rlim_max;
187 setrlimit(RLIMIT_NOFILE, &limitFD);
188 getrlimit(RLIMIT_NOFILE, &limitFD);
190 return limitFD.rlim_cur;
204 HANDLE hFile = (HANDLE)_get_osfhandle(_fileno(file));
205 LARGE_INTEGER nFileSize;
206 int64_t nEndPos = (int64_t)offset + length;
207 nFileSize.u.LowPart = nEndPos & 0xFFFFFFFF;
208 nFileSize.u.HighPart = nEndPos >> 32;
209 SetFilePointerEx(hFile, nFileSize, 0, FILE_BEGIN);
211 #elif defined(MAC_OSX)
216 fst.fst_flags = F_ALLOCATECONTIG;
217 fst.fst_posmode = F_PEOFPOSMODE;
219 fst.fst_length = length;
220 fst.fst_bytesalloc = 0;
221 if (fcntl(fileno(file), F_PREALLOCATE, &fst) == -1) {
222 fst.fst_flags = F_ALLOCATEALL;
223 fcntl(fileno(file), F_PREALLOCATE, &fst);
225 ftruncate(fileno(file),
static_cast<off_t
>(offset) + length);
227 #if defined(HAVE_POSIX_FALLOCATE)
229 off_t nEndPos = (off_t)offset + length;
230 if (0 == posix_fallocate(fileno(file), 0, nEndPos))
return;
234 static const char buf[65536] = {};
235 if (fseek(file, offset, SEEK_SET)) {
239 unsigned int now = 65536;
242 fwrite(buf, 1, now, file);
249 fs::path GetSpecialFolderPath(
int nFolder,
bool fCreate)
253 if (SHGetSpecialFolderPathW(
nullptr, pszPath, nFolder, fCreate)) {
257 LogPrintf(
"SHGetSpecialFolderPathW() failed, could not obtain requested path.\n");
271 return MoveFileExW(src.wstring().c_str(), dest.wstring().c_str(),
272 MOVEFILE_REPLACE_EXISTING) != 0;
274 std::error_code
error;
275 fs::rename(src, dest,
error);
289 }
catch (
const fs::filesystem_error&) {
Different type to mark Mutex at global scope.
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
bool LockDirectory(const fs::path &directory, const fs::path &lockfile_name, bool probe_only)
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.
static GlobalMutex cs_dir_locks
Mutex to protect dir_locks.
bool DirIsWritable(const fs::path &directory)
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)
bool TruncateFile(FILE *file, unsigned int length)
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)
fs::path GetUniquePath(const fs::path &base)
Helper function for getting a unique path.
bool error(const char *fmt, const Args &... args)
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.
FILE * fopen(const fs::path &p, const char *mode)
std::string SysErrorString(int err)
Return system error string from errno value.