All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] mingw: emulate write(2) that fails with a EPIPE
@ 2015-12-16 12:14 Johannes Schindelin
  2015-12-16 18:35 ` Junio C Hamano
                   ` (2 more replies)
  0 siblings, 3 replies; 13+ messages in thread
From: Johannes Schindelin @ 2015-12-16 12:14 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, Johannes Sixt

On Windows, when writing to a pipe fails, errno is always
EINVAL. However, Git expects it to be EPIPE.

According to the documentation, there are two cases in which write()
triggers EINVAL: the buffer is NULL, or the length is odd but the mode
is 16-bit Unicode (the broken pipe is not mentioned as possible cause).
Git never sets the file mode to anything but binary, therefore we know
that errno should actually be EPIPE if it is EINVAL and the buffer is
not NULL.

See https://msdn.microsoft.com/en-us/library/1570wh78.aspx for more
details.

This works around t5571.11 failing with v2.6.4 on Windows.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 compat/mingw.h | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/compat/mingw.h b/compat/mingw.h
index 738865c..2aca347 100644
--- a/compat/mingw.h
+++ b/compat/mingw.h
@@ -210,6 +210,24 @@ FILE *mingw_freopen (const char *filename, const char *otype, FILE *stream);
 int mingw_fflush(FILE *stream);
 #define fflush mingw_fflush
 
+static inline ssize_t mingw_write(int fd, const void *buf, size_t len)
+{
+	ssize_t result = write(fd, buf, len);
+
+	if (result < 0 && errno == EINVAL && buf) {
+		/* check if fd is a pipe */
+		HANDLE h = (HANDLE) _get_osfhandle(fd);
+		if (GetFileType(h) == FILE_TYPE_PIPE)
+			errno = EPIPE;
+		else
+			errno = EINVAL;
+	}
+
+	return result;
+}
+
+#define write mingw_write
+
 int mingw_access(const char *filename, int mode);
 #undef access
 #define access mingw_access
-- 
2.6.3.windows.1.300.g1c25e49

^ permalink raw reply related	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2015-12-21 17:00 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-12-16 12:14 [PATCH] mingw: emulate write(2) that fails with a EPIPE Johannes Schindelin
2015-12-16 18:35 ` Junio C Hamano
2015-12-17  9:39   ` Johannes Schindelin
2015-12-17 16:40     ` Junio C Hamano
2015-12-17 17:08       ` Johannes Schindelin
2015-12-16 19:47 ` Eric Sunshine
2015-12-16 20:36   ` Junio C Hamano
2015-12-17  9:37   ` Johannes Schindelin
2015-12-17 17:08 ` [PATCH v2] " Johannes Schindelin
2015-12-17 19:22   ` Junio C Hamano
2015-12-18 16:10     ` Johannes Schindelin
2015-12-18 20:57   ` Johannes Sixt
2015-12-21 16:59     ` Junio C Hamano

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.