authorgravatar for pat.github@tullmann.orgPat Tullmann <pat.github@tullmann.org> 2025-02-18 17:12:51-08:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-03-24 16:20:45+01:00
logf304d8e50afac12ac209f850ced4f9ddf3bcff3b
tree0db34e8f6056820f4802f97ef29dedeafd7ded3e
parent972cab5bb027dce3d4f700e381bec3e1724c9504

windows: Use AccessDenied for ACCESS_DENIED on Windows

Windows defines an `ACCESS_DENIED` error code. There is no PERMISSION_DENIED (or its equivalent) which seems to only exist on POSIX systems. Fix a couple Windows calls code to return `error.AccessDenied` for `ACCESS_DENIED` and to stop mapping AccessDenied into PermissionDenied.

4 files changed, 9 insertions(+), 21 deletions(-)

lib/std/fs/Dir.zig+2-8
...@@ -2447,10 +2447,7 @@ pub const AccessError = posix.AccessError;...@@ -2447,10 +2447,7 @@ pub const AccessError = posix.AccessError;
2447/// open it and handle the error for file not found.2447/// open it and handle the error for file not found.
2448pub fn access(self: Dir, sub_path: []const u8, flags: File.OpenFlags) AccessError!void {2448pub fn access(self: Dir, sub_path: []const u8, flags: File.OpenFlags) AccessError!void {
2449 if (native_os == .windows) {2449 if (native_os == .windows) {
2450 const sub_path_w = windows.sliceToPrefixedFileW(self.fd, sub_path) catch |err| switch (err) {2450 const sub_path_w = try windows.sliceToPrefixedFileW(self.fd, sub_path);
2451 error.AccessDenied => return error.PermissionDenied,
2452 else => |e| return e,
2453 };
2454 return self.accessW(sub_path_w.span().ptr, flags);2451 return self.accessW(sub_path_w.span().ptr, flags);
2455 }2452 }
2456 const path_c = try posix.toPosixPath(sub_path);2453 const path_c = try posix.toPosixPath(sub_path);
...@@ -2460,10 +2457,7 @@ pub fn access(self: Dir, sub_path: []const u8, flags: File.OpenFlags) AccessErro...@@ -2460,10 +2457,7 @@ pub fn access(self: Dir, sub_path: []const u8, flags: File.OpenFlags) AccessErro
2460/// Same as `access` except the path parameter is null-terminated.2457/// Same as `access` except the path parameter is null-terminated.
2461pub fn accessZ(self: Dir, sub_path: [*:0]const u8, flags: File.OpenFlags) AccessError!void {2458pub fn accessZ(self: Dir, sub_path: [*:0]const u8, flags: File.OpenFlags) AccessError!void {
2462 if (native_os == .windows) {2459 if (native_os == .windows) {
2463 const sub_path_w = windows.cStrToPrefixedFileW(self.fd, sub_path) catch |err| switch (err) {2460 const sub_path_w = try windows.cStrToPrefixedFileW(self.fd, sub_path);
2464 error.AccessDenied => return error.PermissionDenied,
2465 else => |e| return e,
2466 };
2467 return self.accessW(sub_path_w.span().ptr, flags);2461 return self.accessW(sub_path_w.span().ptr, flags);
2468 }2462 }
2469 const os_mode = switch (flags.mode) {2463 const os_mode = switch (flags.mode) {
lib/std/os/windows.zig+4-4
...@@ -1517,7 +1517,7 @@ pub fn GetFileSizeEx(hFile: HANDLE) GetFileSizeError!u64 {...@@ -1517,7 +1517,7 @@ pub fn GetFileSizeEx(hFile: HANDLE) GetFileSizeError!u64 {
15171517
1518pub const GetFileAttributesError = error{1518pub const GetFileAttributesError = error{
1519 FileNotFound,1519 FileNotFound,
1520 PermissionDenied,1520 AccessDenied,
1521 Unexpected,1521 Unexpected,
1522};1522};
15231523
...@@ -1532,7 +1532,7 @@ pub fn GetFileAttributesW(lpFileName: [*:0]const u16) GetFileAttributesError!DWO...@@ -1532,7 +1532,7 @@ pub fn GetFileAttributesW(lpFileName: [*:0]const u16) GetFileAttributesError!DWO
1532 switch (GetLastError()) {1532 switch (GetLastError()) {
1533 .FILE_NOT_FOUND => return error.FileNotFound,1533 .FILE_NOT_FOUND => return error.FileNotFound,
1534 .PATH_NOT_FOUND => return error.FileNotFound,1534 .PATH_NOT_FOUND => return error.FileNotFound,
1535 .ACCESS_DENIED => return error.PermissionDenied,1535 .ACCESS_DENIED => return error.AccessDenied,
1536 else => |err| return unexpectedError(err),1536 else => |err| return unexpectedError(err),
1537 }1537 }
1538 }1538 }
...@@ -1747,12 +1747,12 @@ pub fn GetModuleFileNameW(hModule: ?HMODULE, buf_ptr: [*]u16, buf_len: DWORD) Ge...@@ -1747,12 +1747,12 @@ pub fn GetModuleFileNameW(hModule: ?HMODULE, buf_ptr: [*]u16, buf_len: DWORD) Ge
1747 return buf_ptr[0..rc :0];1747 return buf_ptr[0..rc :0];
1748}1748}
17491749
1750pub const TerminateProcessError = error{ PermissionDenied, Unexpected };1750pub const TerminateProcessError = error{ AccessDenied, Unexpected };
17511751
1752pub fn TerminateProcess(hProcess: HANDLE, uExitCode: UINT) TerminateProcessError!void {1752pub fn TerminateProcess(hProcess: HANDLE, uExitCode: UINT) TerminateProcessError!void {
1753 if (kernel32.TerminateProcess(hProcess, uExitCode) == 0) {1753 if (kernel32.TerminateProcess(hProcess, uExitCode) == 0) {
1754 switch (GetLastError()) {1754 switch (GetLastError()) {
1755 Win32Error.ACCESS_DENIED => return error.PermissionDenied,1755 Win32Error.ACCESS_DENIED => return error.AccessDenied,
1756 else => |err| return unexpectedError(err),1756 else => |err| return unexpectedError(err),
1757 }1757 }
1758 }1758 }
lib/std/posix.zig+2-8
...@@ -4896,10 +4896,7 @@ pub const AccessError = error{...@@ -4896,10 +4896,7 @@ pub const AccessError = error{
4896/// Windows. See `fs` for the cross-platform file system API.4896/// Windows. See `fs` for the cross-platform file system API.
4897pub fn access(path: []const u8, mode: u32) AccessError!void {4897pub fn access(path: []const u8, mode: u32) AccessError!void {
4898 if (native_os == .windows) {4898 if (native_os == .windows) {
4899 const path_w = windows.sliceToPrefixedFileW(null, path) catch |err| switch (err) {4899 const path_w = try windows.sliceToPrefixedFileW(null, path);
4900 error.AccessDenied => return error.PermissionDenied,
4901 else => |e| return e,
4902 };
4903 _ = try windows.GetFileAttributesW(path_w.span().ptr);4900 _ = try windows.GetFileAttributesW(path_w.span().ptr);
4904 return;4901 return;
4905 } else if (native_os == .wasi and !builtin.link_libc) {4902 } else if (native_os == .wasi and !builtin.link_libc) {
...@@ -4912,10 +4909,7 @@ pub fn access(path: []const u8, mode: u32) AccessError!void {...@@ -4912,10 +4909,7 @@ pub fn access(path: []const u8, mode: u32) AccessError!void {
4912/// Same as `access` except `path` is null-terminated.4909/// Same as `access` except `path` is null-terminated.
4913pub fn accessZ(path: [*:0]const u8, mode: u32) AccessError!void {4910pub fn accessZ(path: [*:0]const u8, mode: u32) AccessError!void {
4914 if (native_os == .windows) {4911 if (native_os == .windows) {
4915 const path_w = windows.cStrToPrefixedFileW(null, path) catch |err| switch (err) {4912 const path_w = try windows.cStrToPrefixedFileW(null, path);
4916 error.AccessDenied => return error.PermissionDenied,
4917 else => |e| return e,
4918 };
4919 _ = try windows.GetFileAttributesW(path_w.span().ptr);4913 _ = try windows.GetFileAttributesW(path_w.span().ptr);
4920 return;4914 return;
4921 } else if (native_os == .wasi and !builtin.link_libc) {4915 } else if (native_os == .wasi and !builtin.link_libc) {
lib/std/process/Child.zig+1-1
...@@ -269,7 +269,7 @@ pub fn killWindows(self: *ChildProcess, exit_code: windows.UINT) !Term {...@@ -269,7 +269,7 @@ pub fn killWindows(self: *ChildProcess, exit_code: windows.UINT) !Term {
269 }269 }
270270
271 windows.TerminateProcess(self.id, exit_code) catch |err| switch (err) {271 windows.TerminateProcess(self.id, exit_code) catch |err| switch (err) {
272 error.PermissionDenied => {272 error.AccessDenied => {
273 // Usually when TerminateProcess triggers a ACCESS_DENIED error, it273 // Usually when TerminateProcess triggers a ACCESS_DENIED error, it
274 // indicates that the process has already exited, but there may be274 // indicates that the process has already exited, but there may be
275 // some rare edge cases where our process handle no longer has the275 // some rare edge cases where our process handle no longer has the