From 750795b2a5665cc9bf860d1b57c65f204e3cac53 Mon Sep 17 00:00:00 2001 From: Dylan Conway Date: Tue, 21 Jan 2025 03:46:35 -0800 Subject: [PATCH] Revert "fix" This reverts commit ea93b0893eb125bc81c685c7919b36154dd0a94f. --- src/deps/libuv.zig | 5 +- src/sys.zig | 14 +++-- src/windows.zig | 2 +- src/windows_c.zig | 129 ++++++++++++++++++++++++++++++++++++++++++--- 4 files changed, 132 insertions(+), 18 deletions(-) diff --git a/src/deps/libuv.zig b/src/deps/libuv.zig index c135d2b4948af8..a07f095a2615ec 100644 --- a/src/deps/libuv.zig +++ b/src/deps/libuv.zig @@ -2076,10 +2076,7 @@ pub const uv_fs_poll_cb = ?*const fn ([*c]uv_fs_poll_t, c_int, [*c]const uv_stat pub const UV_LEAVE_GROUP: c_int = 0; pub const UV_JOIN_GROUP: c_int = 1; pub const uv_membership = c_uint; -extern fn uv_translate_sys_error(sys_errno: c_int) c_int; -pub fn translateWin32ErrorToUV(err: bun.windows.Win32Error) c_int { - return uv_translate_sys_error(@intFromEnum(err)); -} +pub extern fn uv_translate_sys_error(sys_errno: c_int) c_int; pub extern fn uv_strerror(err: c_int) [*c]const u8; pub extern fn uv_strerror_r(err: c_int, buf: [*]u8, buflen: usize) [*]u8; pub extern fn uv_err_name(err: c_int) [*c]const u8; diff --git a/src/sys.zig b/src/sys.zig index 5b012d5850b768..8992246febf82d 100644 --- a/src/sys.zig +++ b/src/sys.zig @@ -437,11 +437,15 @@ pub const Error = struct { return .{ @tagName(system_errno), system_errno }; } } else { - const system_errno: C.SystemErrno = @enumFromInt(@intFromEnum( - bun.windows.libuv.translateUVErrorToE( - @as(c_int, err.errno) * -1, - ), - )); + const system_errno: C.SystemErrno = brk: { + // setRuntimeSafety(false) because we use tagName function, which will be null on invalid enum value. + @setRuntimeSafety(false); + if (err.from_libuv) { + break :brk @enumFromInt(@intFromEnum(bun.windows.libuv.translateUVErrorToE(@as(c_int, err.errno) * -1))); + } + + break :brk @enumFromInt(err.errno); + }; if (bun.tagName(bun.C.SystemErrno, system_errno)) |errname| { return .{ errname, system_errno }; } diff --git a/src/windows.zig b/src/windows.zig index b70e87cb4d19db..8f6cbc5f5e326b 100644 --- a/src/windows.zig +++ b/src/windows.zig @@ -2974,7 +2974,7 @@ pub const Win32Error = enum(u16) { } pub fn toSystemErrno(this: Win32Error) ?SystemErrno { - return SystemErrno.init(libuv.translateWin32ErrorToUV(this)); + return SystemErrno.init(this); } pub fn fromNTStatus(status: win32.NTSTATUS) Win32Error { diff --git a/src/windows_c.zig b/src/windows_c.zig index 3a68a335691432..f4f6e08c3d6857 100644 --- a/src/windows_c.zig +++ b/src/windows_c.zig @@ -726,16 +726,129 @@ pub const SystemErrno = enum(u16) { } pub fn init(code: anytype) ?SystemErrno { - if (@TypeOf(code) == Win32Error) - return code.toSystemErrno(); - if (@TypeOf(code) == std.os.windows.Win32Error) - return @as(Win32Error, @enumFromInt(@intFromEnum(code))).toSystemErrno(); - if (@TypeOf(code) == SystemErrno) - return code; + if (@TypeOf(code) == u16 or (@TypeOf(code) == c_int and code > 0)) { + // Win32Error and WSA Error codes + if (code <= @intFromEnum(Win32Error.IO_REISSUE_AS_CACHED) or (code >= @intFromEnum(Win32Error.WSAEINTR) and code <= @intFromEnum(Win32Error.WSA_QOS_RESERVED_PETYPE))) { + return init(@as(Win32Error, @enumFromInt(code))); + } else { + if (comptime bun.Environment.allow_assert) + bun.Output.debugWarn("Unknown error code: {any}\n", .{code}); + + return null; + } + } + + 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, + Win32Error.CANT_ACCESS_FILE => SystemErrno.EACCES, + Win32Error.ADDRESS_ALREADY_ASSOCIATED => SystemErrno.EADDRINUSE, + Win32Error.WSAEADDRINUSE => SystemErrno.EADDRINUSE, + Win32Error.WSAEADDRNOTAVAIL => SystemErrno.EADDRNOTAVAIL, + Win32Error.WSAEAFNOSUPPORT => SystemErrno.EAFNOSUPPORT, + Win32Error.WSAEWOULDBLOCK => SystemErrno.EAGAIN, + Win32Error.WSAEALREADY => SystemErrno.EALREADY, + Win32Error.INVALID_FLAGS => SystemErrno.EBADF, + Win32Error.INVALID_HANDLE => SystemErrno.EBADF, + Win32Error.LOCK_VIOLATION => SystemErrno.EBUSY, + Win32Error.PIPE_BUSY => SystemErrno.EBUSY, + Win32Error.SHARING_VIOLATION => SystemErrno.EBUSY, + Win32Error.OPERATION_ABORTED => SystemErrno.ECANCELED, + Win32Error.WSAEINTR => SystemErrno.ECANCELED, + Win32Error.NO_UNICODE_TRANSLATION => SystemErrno.ECHARSET, + Win32Error.CONNECTION_ABORTED => SystemErrno.ECONNABORTED, + Win32Error.WSAECONNABORTED => SystemErrno.ECONNABORTED, + Win32Error.CONNECTION_REFUSED => SystemErrno.ECONNREFUSED, + Win32Error.WSAECONNREFUSED => SystemErrno.ECONNREFUSED, + Win32Error.NETNAME_DELETED => SystemErrno.ECONNRESET, + Win32Error.WSAECONNRESET => SystemErrno.ECONNRESET, + Win32Error.ALREADY_EXISTS => SystemErrno.EEXIST, + Win32Error.FILE_EXISTS => SystemErrno.EEXIST, + Win32Error.BUFFER_OVERFLOW => SystemErrno.EFAULT, + Win32Error.WSAEFAULT => SystemErrno.EFAULT, + Win32Error.HOST_UNREACHABLE => SystemErrno.EHOSTUNREACH, + Win32Error.WSAEHOSTUNREACH => SystemErrno.EHOSTUNREACH, + Win32Error.INSUFFICIENT_BUFFER => SystemErrno.EINVAL, + Win32Error.INVALID_DATA => SystemErrno.EINVAL, + Win32Error.INVALID_PARAMETER => SystemErrno.EINVAL, + Win32Error.SYMLINK_NOT_SUPPORTED => SystemErrno.EINVAL, + Win32Error.WSAEINVAL => SystemErrno.EINVAL, + Win32Error.WSAEPFNOSUPPORT => SystemErrno.EINVAL, + Win32Error.BEGINNING_OF_MEDIA => SystemErrno.EIO, + Win32Error.BUS_RESET => SystemErrno.EIO, + Win32Error.CRC => SystemErrno.EIO, + Win32Error.DEVICE_DOOR_OPEN => SystemErrno.EIO, + Win32Error.DEVICE_REQUIRES_CLEANING => SystemErrno.EIO, + Win32Error.DISK_CORRUPT => SystemErrno.EIO, + Win32Error.EOM_OVERFLOW => SystemErrno.EIO, + Win32Error.FILEMARK_DETECTED => SystemErrno.EIO, + Win32Error.GEN_FAILURE => SystemErrno.EIO, + Win32Error.INVALID_BLOCK_LENGTH => SystemErrno.EIO, + Win32Error.IO_DEVICE => SystemErrno.EIO, + Win32Error.NO_DATA_DETECTED => SystemErrno.EIO, + Win32Error.NO_SIGNAL_SENT => SystemErrno.EIO, + Win32Error.OPEN_FAILED => SystemErrno.EIO, + Win32Error.SETMARK_DETECTED => SystemErrno.EIO, + Win32Error.SIGNAL_REFUSED => SystemErrno.EIO, + Win32Error.WSAEISCONN => SystemErrno.EISCONN, + Win32Error.CANT_RESOLVE_FILENAME => SystemErrno.ELOOP, + Win32Error.TOO_MANY_OPEN_FILES => SystemErrno.EMFILE, + Win32Error.WSAEMFILE => SystemErrno.EMFILE, + Win32Error.WSAEMSGSIZE => SystemErrno.EMSGSIZE, + Win32Error.FILENAME_EXCED_RANGE => SystemErrno.ENAMETOOLONG, + Win32Error.NETWORK_UNREACHABLE => SystemErrno.ENETUNREACH, + Win32Error.WSAENETUNREACH => SystemErrno.ENETUNREACH, + Win32Error.WSAENOBUFS => SystemErrno.ENOBUFS, + Win32Error.BAD_PATHNAME => SystemErrno.ENOENT, + Win32Error.DIRECTORY => SystemErrno.ENOTDIR, + Win32Error.ENVVAR_NOT_FOUND => SystemErrno.ENOENT, + Win32Error.FILE_NOT_FOUND => SystemErrno.ENOENT, + Win32Error.INVALID_NAME => SystemErrno.ENOENT, + Win32Error.INVALID_DRIVE => SystemErrno.ENOENT, + Win32Error.INVALID_REPARSE_DATA => SystemErrno.ENOENT, + Win32Error.MOD_NOT_FOUND => SystemErrno.ENOENT, + Win32Error.PATH_NOT_FOUND => SystemErrno.ENOENT, + Win32Error.WSAHOST_NOT_FOUND => SystemErrno.ENOENT, + Win32Error.WSANO_DATA => SystemErrno.ENOENT, + Win32Error.NOT_ENOUGH_MEMORY => SystemErrno.ENOMEM, + Win32Error.OUTOFMEMORY => SystemErrno.ENOMEM, + Win32Error.CANNOT_MAKE => SystemErrno.ENOSPC, + Win32Error.DISK_FULL => SystemErrno.ENOSPC, + Win32Error.EA_TABLE_FULL => SystemErrno.ENOSPC, + Win32Error.END_OF_MEDIA => SystemErrno.ENOSPC, + Win32Error.HANDLE_DISK_FULL => SystemErrno.ENOSPC, + Win32Error.NOT_CONNECTED => SystemErrno.ENOTCONN, + Win32Error.WSAENOTCONN => SystemErrno.ENOTCONN, + Win32Error.DIR_NOT_EMPTY => SystemErrno.ENOTEMPTY, + Win32Error.WSAENOTSOCK => SystemErrno.ENOTSOCK, + Win32Error.NOT_SUPPORTED => SystemErrno.ENOTSUP, + Win32Error.BROKEN_PIPE => SystemErrno.EPIPE, + Win32Error.ACCESS_DENIED => SystemErrno.EPERM, + Win32Error.PRIVILEGE_NOT_HELD => SystemErrno.EPERM, + Win32Error.BAD_PIPE => SystemErrno.EPIPE, + Win32Error.NO_DATA => SystemErrno.EPIPE, + Win32Error.PIPE_NOT_CONNECTED => SystemErrno.EPIPE, + Win32Error.WSAESHUTDOWN => SystemErrno.EPIPE, + Win32Error.WSAEPROTONOSUPPORT => SystemErrno.EPROTONOSUPPORT, + Win32Error.WRITE_PROTECT => SystemErrno.EROFS, + Win32Error.SEM_TIMEOUT => SystemErrno.ETIMEDOUT, + Win32Error.WSAETIMEDOUT => SystemErrno.ETIMEDOUT, + Win32Error.NOT_SAME_DEVICE => SystemErrno.EXDEV, + Win32Error.INVALID_FUNCTION => SystemErrno.EISDIR, + Win32Error.META_EXPANSION_TOO_LONG => SystemErrno.E2BIG, + Win32Error.WSAESOCKTNOSUPPORT => SystemErrno.ESOCKTNOSUPPORT, + Win32Error.DELETE_PENDING => SystemErrno.EBUSY, + else => null, + }; + } + if (code < 0) return init(-code); - if (code > std.math.maxInt(u16)) return null; - return @enumFromInt(code); + + if (code >= max) return null; + return @as(SystemErrno, @enumFromInt(code)); } };