Commit bd132046b04875f928e52d16363fb73f8e85dded

Edward Thomson 2019-02-22T20:10:52

p_fallocate: compatibility fixes for macOS On macOS, fcntl(..., F_PREALLOCATE, ...) will only succeed when followed by an ftruncate(), even when it reports success. However, that syscall will fail when the file already exists. Thus, we must ignore the error code and simply let ftruncate extend the size of the file itself (albeit slowly). By calling ftruncate, we also need to prevent against file shrinkage, for compatibility with posix_ftruncate, which will only extend files, never shrink them.

diff --git a/src/posix.c b/src/posix.c
index 242fda8..b5dfac5 100644
--- a/src/posix.c
+++ b/src/posix.c
@@ -159,6 +159,20 @@ int p_fallocate(int fd, off_t offset, off_t len)
 {
 #ifdef __APPLE__
 	fstore_t prealloc;
+	struct stat st;
+	size_t newsize;
+	int error;
+
+	if ((error = p_fstat(fd, &st)) < 0)
+		return error;
+
+	if (git__add_sizet_overflow(&newsize, offset, len)) {
+		errno = EINVAL;
+		return -1;
+	}
+
+	if (newsize < (unsigned long long)st.st_size)
+		return 0;
 
 	memset(&prealloc, 0, sizeof(prealloc));
 	prealloc.fst_flags  = F_ALLOCATEALL;
@@ -166,7 +180,14 @@ int p_fallocate(int fd, off_t offset, off_t len)
 	prealloc.fst_offset = offset;
 	prealloc.fst_length = len;
 
-	return fcntl(fd, F_PREALLOCATE, &prealloc);
+	/*
+	 * fcntl will often error when the file already exists; ignore
+	 * this error since ftruncate will also resize the file (although
+	 * likely slower).
+	 */
+	fcntl(fd, F_PREALLOCATE, &prealloc);
+
+	return ftruncate(fd, (offset + len));
 #else
 	return posix_fallocate(fd, offset, len);
 #endif