From a1304637c5b67e19a7062b4fa0333d046b7e5e00 Mon Sep 17 00:00:00 2001 From: kbkpbot Date: Fri, 21 Mar 2025 17:03:03 +0800 Subject: [PATCH] os: implement Process.is_pending() on windows (fix #23990) (#23993) --- vlib/os/process.c.v | 2 +- vlib/os/process_nix.c.v | 4 ++++ vlib/os/process_windows.c.v | 26 ++++++++++++++++++++++++-- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/vlib/os/process.c.v b/vlib/os/process.c.v index 9ccb9e452d..d146a4b8bc 100644 --- a/vlib/os/process.c.v +++ b/vlib/os/process.c.v @@ -248,7 +248,7 @@ fn (mut p Process) _write_to(pkind ChildProcessPipeKind, s string) { // _is_pending should be called only from is_pending() fn (mut p Process) _is_pending(pkind ChildProcessPipeKind) bool { $if windows { - // TODO + return p.win_is_pending(int(pkind)) } $else { return fd_is_pending(p.stdio_fd[pkind]) } diff --git a/vlib/os/process_nix.c.v b/vlib/os/process_nix.c.v index 72806ad595..76be7414e0 100644 --- a/vlib/os/process_nix.c.v +++ b/vlib/os/process_nix.c.v @@ -154,6 +154,10 @@ fn (mut p Process) win_read_string(_idx int, _maxbytes int) (string, int) { return '', 0 } +fn (mut p Process) win_is_pending(idx int) bool { + return false +} + fn (mut p Process) win_slurp(_idx int) string { return '' } diff --git a/vlib/os/process_windows.c.v b/vlib/os/process_windows.c.v index 4894feb5e6..e2adc79a64 100644 --- a/vlib/os/process_windows.c.v +++ b/vlib/os/process_windows.c.v @@ -88,13 +88,13 @@ fn (mut p Process) win_spawn_process() int { sa.n_length = sizeof(C.SECURITY_ATTRIBUTES) sa.b_inherit_handle = true create_pipe_ok1 := C.CreatePipe(voidptr(&wdata.child_stdout_read), voidptr(&wdata.child_stdout_write), - voidptr(&sa), 0) + voidptr(&sa), 65536) failed_cfn_report_error(create_pipe_ok1, 'CreatePipe stdout') set_handle_info_ok1 := C.SetHandleInformation(wdata.child_stdout_read, C.HANDLE_FLAG_INHERIT, 0) failed_cfn_report_error(set_handle_info_ok1, 'SetHandleInformation') create_pipe_ok2 := C.CreatePipe(voidptr(&wdata.child_stderr_read), voidptr(&wdata.child_stderr_write), - voidptr(&sa), 0) + voidptr(&sa), 65536) failed_cfn_report_error(create_pipe_ok2, 'CreatePipe stderr') set_handle_info_ok2 := C.SetHandleInformation(wdata.child_stderr_read, C.HANDLE_FLAG_INHERIT, 0) @@ -233,6 +233,28 @@ fn (mut p Process) win_read_string(idx int, _maxbytes int) (string, int) { return buf[..bytes_read].bytestr(), bytes_read } +fn (mut p Process) win_is_pending(idx int) bool { + mut wdata := unsafe { &WProcess(p.wdata) } + if unsafe { wdata == 0 } { + return false + } + mut rhandle := &u32(0) + if idx == 1 { + rhandle = wdata.child_stdout_read + } + if idx == 2 { + rhandle = wdata.child_stderr_read + } + if rhandle == 0 { + return false + } + mut bytes_avail := int(0) + if C.PeekNamedPipe(rhandle, 0, 0, 0, &bytes_avail, 0) { + return bytes_avail > 0 + } + return false +} + fn (mut p Process) win_slurp(idx int) string { mut wdata := unsafe { &WProcess(p.wdata) } if unsafe { wdata == 0 } {