Skip to content

Commit 1c25e49

Browse files
committed
mingw: emulate write(2) that fails with a SIGPIPE
On Windows, when writing to a pipe fails, errno is always EINVAL. However, Git expects it to be SIGPIPE. While write() can set EINVAL on Windows e.g. when the buffer pointer is NULL, this is not the case for Git's source code, therefore we *know* that an EINVAL on a pipe is actually an EPIPE. See https://msdn.microsoft.com/en-us/library/1570wh78.aspx for more details. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent b20c46e commit 1c25e49

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

compat/mingw.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,24 @@ FILE *mingw_freopen (const char *filename, const char *otype, FILE *stream);
225225
int mingw_fflush(FILE *stream);
226226
#define fflush mingw_fflush
227227

228+
static inline ssize_t mingw_write(int fd, const void *buf, size_t len)
229+
{
230+
ssize_t result = write(fd, buf, len);
231+
232+
if (result < 0 && errno == EINVAL) {
233+
/* check if fd is a pipe */
234+
HANDLE h = (HANDLE) _get_osfhandle(fd);
235+
if (GetFileType(h) == FILE_TYPE_PIPE)
236+
errno = EPIPE;
237+
else
238+
errno = EINVAL;
239+
}
240+
241+
return result;
242+
}
243+
244+
#define write mingw_write
245+
228246
int mingw_access(const char *filename, int mode);
229247
#undef access
230248
#define access mingw_access

0 commit comments

Comments
 (0)