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;
24472447/// open it and handle the error for file not found.
24482448pub fn access(self: Dir, sub_path: []const u8, flags: File.OpenFlags) AccessError!void {
24492449 if (native_os == .windows) {
2450 const sub_path_w = windows.sliceToPrefixedFileW(self.fd, sub_path) catch |err| switch (err) {
2451 error.AccessDenied => return error.PermissionDenied,
2452 else => |e| return e,
2453 };
2450 const sub_path_w = try windows.sliceToPrefixedFileW(self.fd, sub_path);
24542451 return self.accessW(sub_path_w.span().ptr, flags);
24552452 }
24562453 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
24602457/// Same as `access` except the path parameter is null-terminated.
24612458pub fn accessZ(self: Dir, sub_path: [*:0]const u8, flags: File.OpenFlags) AccessError!void {
24622459 if (native_os == .windows) {
2463 const sub_path_w = windows.cStrToPrefixedFileW(self.fd, sub_path) catch |err| switch (err) {
2464 error.AccessDenied => return error.PermissionDenied,
2465 else => |e| return e,
2466 };
2460 const sub_path_w = try windows.cStrToPrefixedFileW(self.fd, sub_path);
24672461 return self.accessW(sub_path_w.span().ptr, flags);
24682462 }
24692463 const os_mode = switch (flags.mode) {
lib/std/os/windows.zig+4-4
......@@ -1517,7 +1517,7 @@ pub fn GetFileSizeEx(hFile: HANDLE) GetFileSizeError!u64 {
15171517
15181518pub const GetFileAttributesError = error{
15191519 FileNotFound,
1520 PermissionDenied,
1520 AccessDenied,
15211521 Unexpected,
15221522};
15231523
......@@ -1532,7 +1532,7 @@ pub fn GetFileAttributesW(lpFileName: [*:0]const u16) GetFileAttributesError!DWO
15321532 switch (GetLastError()) {
15331533 .FILE_NOT_FOUND => return error.FileNotFound,
15341534 .PATH_NOT_FOUND => return error.FileNotFound,
1535 .ACCESS_DENIED => return error.PermissionDenied,
1535 .ACCESS_DENIED => return error.AccessDenied,
15361536 else => |err| return unexpectedError(err),
15371537 }
15381538 }
......@@ -1747,12 +1747,12 @@ pub fn GetModuleFileNameW(hModule: ?HMODULE, buf_ptr: [*]u16, buf_len: DWORD) Ge
17471747 return buf_ptr[0..rc :0];
17481748}
17491749
1750pub const TerminateProcessError = error{ PermissionDenied, Unexpected };
1750pub const TerminateProcessError = error{ AccessDenied, Unexpected };
17511751
17521752pub fn TerminateProcess(hProcess: HANDLE, uExitCode: UINT) TerminateProcessError!void {
17531753 if (kernel32.TerminateProcess(hProcess, uExitCode) == 0) {
17541754 switch (GetLastError()) {
1755 Win32Error.ACCESS_DENIED => return error.PermissionDenied,
1755 Win32Error.ACCESS_DENIED => return error.AccessDenied,
17561756 else => |err| return unexpectedError(err),
17571757 }
17581758 }
lib/std/posix.zig+2-8
......@@ -4896,10 +4896,7 @@ pub const AccessError = error{
48964896/// Windows. See `fs` for the cross-platform file system API.
48974897pub fn access(path: []const u8, mode: u32) AccessError!void {
48984898 if (native_os == .windows) {
4899 const path_w = windows.sliceToPrefixedFileW(null, path) catch |err| switch (err) {
4900 error.AccessDenied => return error.PermissionDenied,
4901 else => |e| return e,
4902 };
4899 const path_w = try windows.sliceToPrefixedFileW(null, path);
49034900 _ = try windows.GetFileAttributesW(path_w.span().ptr);
49044901 return;
49054902 } else if (native_os == .wasi and !builtin.link_libc) {
......@@ -4912,10 +4909,7 @@ pub fn access(path: []const u8, mode: u32) AccessError!void {
49124909/// Same as `access` except `path` is null-terminated.
49134910pub fn accessZ(path: [*:0]const u8, mode: u32) AccessError!void {
49144911 if (native_os == .windows) {
4915 const path_w = windows.cStrToPrefixedFileW(null, path) catch |err| switch (err) {
4916 error.AccessDenied => return error.PermissionDenied,
4917 else => |e| return e,
4918 };
4912 const path_w = try windows.cStrToPrefixedFileW(null, path);
49194913 _ = try windows.GetFileAttributesW(path_w.span().ptr);
49204914 return;
49214915 } 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 {
269269 }
270270
271271 windows.TerminateProcess(self.id, exit_code) catch |err| switch (err) {
272 error.PermissionDenied => {
272 error.AccessDenied => {
273273 // Usually when TerminateProcess triggers a ACCESS_DENIED error, it
274274 // indicates that the process has already exited, but there may be
275275 // some rare edge cases where our process handle no longer has the