Skip to content

Commit 24b56ae

Browse files
rscharfegitster
authored andcommitted
nonblock: support Windows
Implement enable_pipe_nonblock() using the Windows API. This works only for pipes, but that is sufficient for this limited interface. Despite the API calls used, it handles both "named" and anonymous pipes from our pipe() emulation. Signed-off-by: René Scharfe <[email protected]> Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 10f7433 commit 24b56ae

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

compat/nonblock.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,33 @@ int enable_pipe_nonblock(int fd)
1212
return fcntl(fd, F_SETFL, flags);
1313
}
1414

15+
#elif defined(GIT_WINDOWS_NATIVE)
16+
17+
#include "win32.h"
18+
19+
int enable_pipe_nonblock(int fd)
20+
{
21+
HANDLE h = (HANDLE)_get_osfhandle(fd);
22+
DWORD mode;
23+
DWORD type = GetFileType(h);
24+
if (type == FILE_TYPE_UNKNOWN && GetLastError() != NO_ERROR) {
25+
errno = EBADF;
26+
return -1;
27+
}
28+
if (type != FILE_TYPE_PIPE)
29+
BUG("unsupported file type: %lu", type);
30+
if (!GetNamedPipeHandleState(h, &mode, NULL, NULL, NULL, NULL, 0)) {
31+
errno = err_win_to_posix(GetLastError());
32+
return -1;
33+
}
34+
mode |= PIPE_NOWAIT;
35+
if (!SetNamedPipeHandleState(h, &mode, NULL, NULL)) {
36+
errno = err_win_to_posix(GetLastError());
37+
return -1;
38+
}
39+
return 0;
40+
}
41+
1542
#else
1643

1744
int enable_pipe_nonblock(int fd)

0 commit comments

Comments
 (0)