Skip to content

Commit

Permalink
fix(windows): fix win32 errors (oven-sh#8569)
Browse files Browse the repository at this point in the history
* translate eexist

* use SystemErrno translation

* update
  • Loading branch information
dylan-conway authored and sroussey committed Feb 2, 2024
1 parent 4baba21 commit 3ce2ea7
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 45 deletions.
4 changes: 4 additions & 0 deletions src/bun.js/api/bun/subprocess.zig
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,10 @@ pub const Subprocess = struct {
if (comptime Environment.isWindows) {
if (std.os.windows.kernel32.TerminateProcess(this.pid.process_handle, @intCast(sig)) == 0) {
const err = bun.windows.getLastErrno();
if (comptime Environment.allow_assert) {
std.debug.assert(err != .UNKNOWN);
}

// if the process was already killed don't throw
//
// "After a process has terminated, call to TerminateProcess with open
Expand Down
19 changes: 16 additions & 3 deletions src/bun.js/node/node_fs.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5990,7 +5990,16 @@ pub const NodeFS = struct {
}

const flags = os.O.DIRECTORY | os.O.RDONLY;
const fd = switch (Syscall.openatOSPath(bun.toFD((std.fs.cwd().fd)), src, flags, 0)) {
var wbuf: if (Environment.isWindows) bun.WPathBuffer else void = undefined;
const fd = switch (Syscall.openatOSPath(
bun.toFD((std.fs.cwd().fd)),
if (Environment.isWindows and std.fs.path.isAbsoluteWindowsWTF16(src))
bun.strings.addNTPathPrefixIfNeeded(&wbuf, src)
else
src,
flags,
0,
)) {
.err => |err| {
return .{ .err = err.withPath(this.osPathIntoSyncErrorBuf(src)) };
},
Expand Down Expand Up @@ -6327,9 +6336,13 @@ pub const NodeFS = struct {

if (Environment.isWindows) {
const result = windows.CopyFileW(src, dest, @intFromBool(mode.shouldntOverwrite()));
if (Maybe(Return.CopyFile).errnoSysP(result, .copyfile, this.osPathIntoSyncErrorBuf(src))) |e| {
return e;
if (result == bun.windows.FALSE) {
if (Maybe(Return.CopyFile).errnoSysP(result, .copyfile, this.osPathIntoSyncErrorBuf(src))) |e| {
return e;
}
}

return ret.success;
}

return ret.todo();
Expand Down
10 changes: 9 additions & 1 deletion src/bun.js/node/types.zig
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ pub fn Maybe(comptime ResultType: type) type {
}

pub inline fn errnoSys(rc: anytype, syscall: Syscall.Tag) ?@This() {
if (comptime Environment.isWindows) {
if (rc != 0) return null;
}
return switch (Syscall.getErrno(rc)) {
.SUCCESS => null,
else => |err| @This(){
Expand All @@ -164,6 +167,9 @@ pub fn Maybe(comptime ResultType: type) type {
}

pub inline fn errnoSysFd(rc: anytype, syscall: Syscall.Tag, fd: bun.FileDescriptor) ?@This() {
if (comptime Environment.isWindows) {
if (rc != 0) return null;
}
return switch (Syscall.getErrno(rc)) {
.SUCCESS => null,
else => |err| @This(){
Expand All @@ -181,6 +187,9 @@ pub fn Maybe(comptime ResultType: type) type {
if (std.meta.Child(@TypeOf(path)) == u16) {
@compileError("Do not pass WString path to errnoSysP, it needs the path encoded as utf8");
}
if (comptime Environment.isWindows) {
if (rc != 0) return null;
}
return switch (Syscall.getErrno(rc)) {
.SUCCESS => null,
else => |err| @This(){
Expand All @@ -198,7 +207,6 @@ pub fn Maybe(comptime ResultType: type) type {

fn translateToErrInt(err: anytype) bun.sys.Error.Int {
return switch (@TypeOf(err)) {
bun.windows.Win32Error => @intFromEnum(bun.windows.translateWinErrorToErrno(err)),
bun.windows.NTSTATUS => @intFromEnum(bun.windows.translateNTStatusToErrno(err)),
else => @truncate(@intFromEnum(err)),
};
Expand Down
29 changes: 14 additions & 15 deletions src/sys.zig
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,11 @@ pub fn mkdir(file_path: [:0]const u8, flags: bun.Mode) Maybe(void) {

.windows => {
var wbuf: bun.WPathBuffer = undefined;
const rc = kernel32.CreateDirectoryW(bun.strings.toWPath(&wbuf, file_path).ptr, null);
return if (rc != 0)
Maybe(void).success
else
Maybe(void).errnoSys(rc, .mkdir) orelse Maybe(void).success;
return Maybe(void).errnoSysP(
kernel32.CreateDirectoryW(bun.strings.toWPath(&wbuf, file_path).ptr, null),
.mkdir,
file_path,
) orelse Maybe(void).success;
},

else => @compileError("mkdir is not implemented on this platform"),
Expand Down Expand Up @@ -303,23 +303,22 @@ pub fn mkdirA(file_path: []const u8, flags: bun.Mode) Maybe(void) {

if (comptime Environment.isWindows) {
var wbuf: bun.WPathBuffer = undefined;
const rc = kernel32.CreateDirectoryW(bun.strings.toWPath(&wbuf, file_path).ptr, null);
return if (rc != 0)
Maybe(void).success
else
Maybe(void).errnoSys(rc, .mkdir) orelse Maybe(void).success;
return Maybe(void).errnoSysP(
kernel32.CreateDirectoryW(bun.strings.toWPath(&wbuf, file_path).ptr, null),
.mkdir,
file_path,
) orelse Maybe(void).success;
}
}

pub fn mkdirOSPath(file_path: bun.OSPathSliceZ, flags: bun.Mode) Maybe(void) {
return switch (Environment.os) {
else => mkdir(file_path, flags),
.windows => {
const rc = kernel32.CreateDirectoryW(file_path, null);
return if (rc != 0)
Maybe(void).success
else
Maybe(void).errnoSys(rc, .mkdir) orelse Maybe(void).success;
return Maybe(void).errnoSys(
kernel32.CreateDirectoryW(file_path, null),
.mkdir,
) orelse Maybe(void).success;
},
};
}
Expand Down
25 changes: 2 additions & 23 deletions src/windows.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2938,7 +2938,7 @@ pub const Win32Error = enum(u16) {
}

pub fn toSystemErrno(this: Win32Error) ?SystemErrno {
return SystemErrno.init(@intFromEnum(translateWinErrorToErrno(@enumFromInt(@intFromEnum(this)))));
return SystemErrno.init(this);
}

pub fn fromNTStatus(status: win32.NTSTATUS) Win32Error {
Expand Down Expand Up @@ -2985,28 +2985,7 @@ pub extern "kernel32" fn SetFileInformationByHandle(
) BOOL;

pub fn getLastErrno() bun.C.E {
return translateWinErrorToErrno(bun.windows.kernel32.GetLastError());
}

pub fn translateWinErrorToErrno(err: win32.Win32Error) bun.C.E {
return switch (err) {
.SUCCESS => .SUCCESS,
.FILE_NOT_FOUND => .NOENT,
.PATH_NOT_FOUND => .NOENT,
.TOO_MANY_OPEN_FILES => .NOMEM,
.ACCESS_DENIED => .PERM,
.INVALID_HANDLE => .BADF,
.NOT_ENOUGH_MEMORY => .NOMEM,
.OUTOFMEMORY => .NOMEM,
.INVALID_PARAMETER => .INVAL,

else => |t| {
// if (bun.Environment.isDebug) {
bun.Output.warn("Called translateWinErrorToErrno with {s} which does not have a mapping to errno.", .{@tagName(t)});
// }
return .UNKNOWN;
},
};
return (bun.C.SystemErrno.init(bun.windows.kernel32.GetLastError()) orelse SystemErrno.EUNKNOWN).toE();
}

pub fn translateNTStatusToErrno(err: win32.NTSTATUS) bun.C.E {
Expand Down
6 changes: 3 additions & 3 deletions src/windows_c.zig
Original file line number Diff line number Diff line change
Expand Up @@ -701,8 +701,8 @@ pub const SystemErrno = enum(u16) {
}
}

if (comptime @TypeOf(code) == Win32Error) {
return switch (code) {
if (comptime @TypeOf(code) == Win32Error or @TypeOf(code) == std.os.windows.Win32Error) {
return switch (@as(Win32Error, @enumFromInt(@intFromEnum(code)))) {
Win32Error.NOACCESS => SystemErrno.EACCES,
Win32Error.WSAEACCES => SystemErrno.EACCES,
Win32Error.ELEVATION_REQUIRED => SystemErrno.EACCES,
Expand Down Expand Up @@ -803,7 +803,7 @@ pub const SystemErrno = enum(u16) {
Win32Error.META_EXPANSION_TOO_LONG => SystemErrno.E2BIG,
Win32Error.WSAESOCKTNOSUPPORT => SystemErrno.ESOCKTNOSUPPORT,
Win32Error.DELETE_PENDING => SystemErrno.EBUSY,
else => return null,
else => null,
};
}

Expand Down

0 comments on commit 3ce2ea7

Please sign in to comment.