zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit b4831403c929d429c7710d5004ec32fd0dfbd0a4 (tree)
parent d08098861f47fb0974066fce8cc3af7d5709d12f
Author: Ryan Liptak <squeek502@hotmail.com>
Date:   Thu,  8 Jan 2026 16:51:20 -0800

fileRead functions: handle INVALID_FUNCTION on Windows and map it to error.IsDir

INVALID_FUNCTION may be possible in other scenarios as well, but it is verifiably returned when the handle refers to a directory.

Diffstat:
Mlib/std/Io/Threaded.zig | 6++++++
Mlib/std/fs/test.zig | 11++++++++++-
2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/lib/std/Io/Threaded.zig b/lib/std/Io/Threaded.zig @@ -7987,6 +7987,9 @@ fn fileReadStreamingWindows(userdata: ?*anyopaque, file: File, data: []const []u .LOCK_VIOLATION => return syscall.fail(error.LockViolation), .ACCESS_DENIED => return syscall.fail(error.AccessDenied), .INVALID_HANDLE => return syscall.fail(error.NotOpenForReading), + // TODO: Determine if INVALID_FUNCTION is possible in more scenarios than just passing + // a handle to a directory. + .INVALID_FUNCTION => return syscall.fail(error.IsDir), else => |err| { syscall.finish(); return windows.unexpectedError(err); @@ -8144,6 +8147,9 @@ fn fileReadPositionalWindows(userdata: ?*anyopaque, file: File, data: []const [] .LOCK_VIOLATION => return syscall.fail(error.LockViolation), .ACCESS_DENIED => return syscall.fail(error.AccessDenied), .INVALID_HANDLE => return syscall.fail(error.NotOpenForReading), + // TODO: Determine if INVALID_FUNCTION is possible in more scenarios than just passing + // a handle to a directory. + .INVALID_FUNCTION => return syscall.fail(error.IsDir), else => |err| { syscall.finish(); return windows.unexpectedError(err); diff --git a/lib/std/fs/test.zig b/lib/std/fs/test.zig @@ -847,7 +847,16 @@ test "file operations on directories" { { const handle = try ctx.dir.openFile(io, test_dir_name, .{ .allow_directory = true, .mode = .read_only }); - handle.close(io); + defer handle.close(io); + + // Reading from the handle should fail + const expected_err = switch (native_os) { + .wasi => error.NotOpenForReading, + else => error.IsDir, + }; + var buf: [1]u8 = undefined; + try expectError(expected_err, handle.readStreaming(io, &.{&buf})); + try expectError(expected_err, handle.readPositional(io, &.{&buf}, 0)); } try expectError(error.IsDir, ctx.dir.openFile(io, test_dir_name, .{ .allow_directory = false, .mode = .read_only }));