Skip to content

Commit 75163f4

Browse files
committed
bug-fix macos: give free bytes to F_PREALLOCATE
The macos manpage for fcntl (for F_PEOFPOSMODE) states: > Allocate from the physical end of file. In this case, fst_length indicates the number of newly allocated bytes desired.
1 parent 8164641 commit 75163f4

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/util/system.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -974,17 +974,19 @@ void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length) {
974974
SetEndOfFile(hFile);
975975
#elif defined(MAC_OSX)
976976
// OSX specific version
977+
// NOTE: Contrary to other OS versions, the OSX version assumes that
978+
// NOTE: offset is the size of the file.
977979
fstore_t fst;
978980
fst.fst_flags = F_ALLOCATECONTIG;
979981
fst.fst_posmode = F_PEOFPOSMODE;
980982
fst.fst_offset = 0;
981-
fst.fst_length = (off_t)offset + length;
983+
fst.fst_length = length; // mac os fst_length takes the # of free bytes to allocate, not desired file size
982984
fst.fst_bytesalloc = 0;
983985
if (fcntl(fileno(file), F_PREALLOCATE, &fst) == -1) {
984986
fst.fst_flags = F_ALLOCATEALL;
985987
fcntl(fileno(file), F_PREALLOCATE, &fst);
986988
}
987-
ftruncate(fileno(file), fst.fst_length);
989+
ftruncate(fileno(file), static_cast<off_t>(offset) + length);
988990
#else
989991
#if defined(__linux__)
990992
// Version using posix_fallocate

0 commit comments

Comments
 (0)