compate/clipped-write.c: large write(2) fails on Mac OS X/XNU

Due to a bug in the Darwin kernel, write(2) calls have a maximum size of INT_MAX bytes. Introduce a new compat function, clipped_write(), that only writes at most INT_MAX bytes and returns the number of bytes written, as a substitute for write(2), and allow platforms that need this to enable it from the build mechanism with NEEDS_CLIPPED_WRITE. Set it for Mac OS X by default. It may be necessary to include this function on Windows, too. Signed-off-by: Filipe Cabecinhas <filcab+git@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Filipe Cabecinhas committed May 10, 2013 at 15:24 UTC 6c642a878688adf46b226903858b53e2d31ac5c3
4 files changed +27
Makefile
+8
@@ -69,6 +69,9 @@ all::
69 # Define NO_MSGFMT_EXTENDED_OPTIONS if your implementation of msgfmt
70 # doesn't support GNU extensions like --check and --statistics
71 #
72 +# Define NEEDS_CLIPPED_WRITE if your write(2) cannot write more than
73 +# INT_MAX bytes at once (e.g. MacOS X).
74 +#
75 # Define HAVE_PATHS_H if you have paths.h and want to use the default PATH
76 # it specifies.
77 #
@@ -1463,6 +1466,11 @@ ifndef NO_MSGFMT_EXTENDED_OPTIONS
1466 MSGFMT += --check --statistics
1467 endif
1468
1469 +ifdef NEEDS_CLIPPED_WRITE
1470 + BASIC_CFLAGS += -DNEEDS_CLIPPED_WRITE
1471 + COMPAT_OBJS += compat/clipped-write.o
1472 +endif
1473 +
1474 ifneq (,$(XDL_FAST_HASH))
1475 BASIC_CFLAGS += -DXDL_FAST_HASH
1476 endif
compat/clipped-write.c new
+13
@@ -0,0 +1,13 @@
1 +#include "../git-compat-util.h"
2 +#undef write
3 +
4 +/*
5 + * Version of write that will write at most INT_MAX bytes.
6 + * Workaround a xnu bug on Mac OS X
7 + */
8 +ssize_t clipped_write(int fildes, const void *buf, size_t nbyte)
9 +{
10 + if (nbyte > INT_MAX)
11 + nbyte = INT_MAX;
12 + return write(fildes, buf, nbyte);
13 +}
config.mak.uname
+1
@@ -95,6 +95,7 @@ ifeq ($(uname_S),Darwin)
95 NO_MEMMEM = YesPlease
96 USE_ST_TIMESPEC = YesPlease
97 HAVE_DEV_TTY = YesPlease
98 + NEEDS_CLIPPED_WRITE = YesPlease
99 COMPAT_OBJS += compat/precompose_utf8.o
100 BASIC_CFLAGS += -DPRECOMPOSE_UNICODE
101 endif
git-compat-util.h
+5
@@ -181,6 +181,11 @@ typedef unsigned long uintptr_t;
181 #define probe_utf8_pathname_composition(a,b)
182 #endif
183
184 +#ifdef NEEDS_CLIPPED_WRITE
185 +ssize_t clipped_write(int fildes, const void *buf, size_t nbyte);
186 +#define write(x,y,z) clipped_write((x),(y),(z))
187 +#endif
188 +
189 #ifdef MKDIR_WO_TRAILING_SLASH
190 #define mkdir(a,b) compat_mkdir_wo_trailing_slash((a),(b))
191 extern int compat_mkdir_wo_trailing_slash(const char*, mode_t);