Skip to content

Commit c4ce0d1

Browse files
laanwjthelazier
authored andcommitted
Merge bitcoin#17887: bug-fix macos: give free bytes to F_PREALLOCATE
75163f4 bug-fix macos: give free bytes to F_PREALLOCATE (Karl-Johan Alm) Pull request description: 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. This would result in the rev files being essentially pre-allocating 2x their necessary size (this is the case for block files as well, but these are flushed down to their right sizes every time) as they would pre-allocate `pos + length` **free** bytes, rather than allocating `length` bytes after `pos`, as expected. Fixes bitcoin#17827. ACKs for top commit: eriknylund: ACK 75163f4 built locally. All tests passing. Manual test as per my previous comment above on an older commit, using an APFS unencrypted disk image with 3 GB. laanwj: code review ACK 75163f4 Tree-SHA512: 105c8d56c20acad8febdf0583f1e5721b63376ace325a7a62c2e4b15a442c7131404ed604c32c0cda716791d7ca5aa9f5b6a774ff86e39838bc7e87ca3c42760
1 parent 092bb8a commit c4ce0d1

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
@@ -1159,17 +1159,19 @@ void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length) {
11591159
SetEndOfFile(hFile);
11601160
#elif defined(MAC_OSX)
11611161
// OSX specific version
1162+
// NOTE: Contrary to other OS versions, the OSX version assumes that
1163+
// NOTE: offset is the size of the file.
11621164
fstore_t fst;
11631165
fst.fst_flags = F_ALLOCATECONTIG;
11641166
fst.fst_posmode = F_PEOFPOSMODE;
11651167
fst.fst_offset = 0;
1166-
fst.fst_length = (off_t)offset + length;
1168+
fst.fst_length = length; // mac os fst_length takes the # of free bytes to allocate, not desired file size
11671169
fst.fst_bytesalloc = 0;
11681170
if (fcntl(fileno(file), F_PREALLOCATE, &fst) == -1) {
11691171
fst.fst_flags = F_ALLOCATEALL;
11701172
fcntl(fileno(file), F_PREALLOCATE, &fst);
11711173
}
1172-
ftruncate(fileno(file), fst.fst_length);
1174+
ftruncate(fileno(file), static_cast<off_t>(offset) + length);
11731175
#else
11741176
#if defined(__linux__)
11751177
// Version using posix_fallocate

0 commit comments

Comments
 (0)