Jeff King | 10f7433 | 2022-08-17 02:04:55 -0400 | [diff] [blame] | 1 | #include "git-compat-util.h" |
| 2 | #include "nonblock.h" |
| 3 | |
| 4 | #ifdef O_NONBLOCK |
| 5 | |
| 6 | int enable_pipe_nonblock(int fd) |
| 7 | { |
| 8 | int flags = fcntl(fd, F_GETFL); |
| 9 | if (flags < 0) |
| 10 | return -1; |
| 11 | flags |= O_NONBLOCK; |
| 12 | return fcntl(fd, F_SETFL, flags); |
| 13 | } |
| 14 | |
René Scharfe | 24b56ae | 2022-08-17 02:05:25 -0400 | [diff] [blame] | 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 | |
Jeff King | 10f7433 | 2022-08-17 02:04:55 -0400 | [diff] [blame] | 42 | #else |
| 43 | |
Jeff King | 808e919 | 2022-10-17 21:05:12 -0400 | [diff] [blame] | 44 | int enable_pipe_nonblock(int fd UNUSED) |
Jeff King | 10f7433 | 2022-08-17 02:04:55 -0400 | [diff] [blame] | 45 | { |
| 46 | errno = ENOSYS; |
| 47 | return -1; |
| 48 | } |
| 49 | |
| 50 | #endif |