From f5f71e56e1c8e96f2690a45e2e2f58fda2fb8ac5 Mon Sep 17 00:00:00 2001 From: cirospaciari Date: Sat, 27 Jan 2024 13:24:54 -0300 Subject: [PATCH] add .path support so we can use Bun.file as stdin/stdout/stderr --- src/bun.js/api/bun/subprocess.zig | 53 +++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 16 deletions(-) diff --git a/src/bun.js/api/bun/subprocess.zig b/src/bun.js/api/bun/subprocess.zig index 314ee764e57845..0ef151fb623075 100644 --- a/src/bun.js/api/bun/subprocess.zig +++ b/src/bun.js/api/bun/subprocess.zig @@ -3223,22 +3223,43 @@ pub const Subprocess = struct { .data = .{ .fd = bun.uvfdcast(_fd) }, }, .path => |pathlike| { - _ = pathlike; - @panic("TODO"); - // var file_path: [bun.MAX_PATH_BYTES]u8 = undefined; - // const path_fd = try bun.sys.open( - // pathlike.path.sliceZ(&file_path), - // std.os.O.WRONLY | std.os.O.CREAT | std.os.O.NONBLOCK, - // 0o664, - // ).unwrap(); - - // try pipe.init(uv.Loop.get(), false).unwrap(); - // try pipe.open(bun.uvfdcast(path_fd)).unwrap(); - - // return uv.uv_stdio_container_s{ - // .flags = @intCast(uv.UV_INHERIT_STREAM), - // .data = .{ .stream = @ptrCast(pipe) }, - // }; + var path_buf: [bun.MAX_PATH_BYTES]u8 = undefined; + var resolver = bun.path.PosixToWinNormalizer{}; + const flag: i32 = (if (isReadable) os.O.RDONLY else std.os.O.WRONLY); + + var joined_buf: [bun.MAX_PATH_BYTES]u8 = undefined; + var slice = pathlike.sliceZ(&path_buf); + if (!std.fs.path.isAbsoluteWindows(slice)) { + // we need the absolute path so we can open + var cwd_buf: [bun.MAX_PATH_BYTES + 1]u8 = undefined; + + var parts = [_]string{ + slice, + }; + + const application_cwd = try bun.getcwd(&cwd_buf); + cwd_buf[application_cwd.len] = std.fs.path.sep; + const file_path = bun.path.joinAbsStringBuf( + cwd_buf[0 .. application_cwd.len + 1], + &joined_buf, + &parts, + .auto, + ); + + joined_buf[file_path.len] = 0; + slice = joined_buf[0..file_path.len :0]; + } + + const path_fd = try bun.sys.open( + try resolver.resolveCWDZ(slice), + flag | std.os.O.CREAT | std.os.O.NONBLOCK, + 0o664, + ).unwrap(); + + return uv.uv_stdio_container_s{ + .flags = uv.UV_INHERIT_FD, + .data = .{ .fd = bun.uvfdcast(path_fd) }, + }; }, .inherit => uv.uv_stdio_container_s{ .flags = uv.UV_INHERIT_FD,