Commit b39f3d294d

Andrew Kelley <andrew@ziglang.org>
2025-10-28 14:33:30
std.Io.Threaded: implement dirMakeOpenPath for WASI
and fix error code when file operation occurs on director handle
1 parent 4114392
lib/std/Io/File.zig
@@ -213,29 +213,7 @@ pub fn openSelfExe(io: Io, flags: OpenFlags) OpenSelfExeError!File {
     return io.vtable.openSelfExe(io.userdata, flags);
 }
 
-pub const ReadStreamingError = error{
-    InputOutput,
-    SystemResources,
-    IsDir,
-    BrokenPipe,
-    ConnectionResetByPeer,
-    Timeout,
-    NotOpenForReading,
-    SocketUnconnected,
-    /// This error occurs when no global event loop is configured,
-    /// and reading from the file descriptor would block.
-    WouldBlock,
-    /// In WASI, this error occurs when the file descriptor does
-    /// not hold the required rights to read from it.
-    AccessDenied,
-    /// This error occurs in Linux if the process to be read from
-    /// no longer exists.
-    ProcessNotFound,
-    /// Unable to read file due to lock.
-    LockViolation,
-} || Io.Cancelable || Io.UnexpectedError;
-
-pub const ReadPositionalError = ReadStreamingError || error{Unseekable};
+pub const ReadPositionalError = Reader.Error || error{Unseekable};
 
 pub fn readPositional(file: File, io: Io, buffer: []u8, offset: u64) ReadPositionalError!usize {
     return io.vtable.fileReadPositional(io.userdata, file, buffer, offset);
@@ -301,7 +279,29 @@ pub const Reader = struct {
     seek_err: ?Reader.SeekError = null,
     interface: Io.Reader,
 
-    pub const Error = std.posix.ReadError || Io.Cancelable;
+    pub const Error = error{
+        InputOutput,
+        SystemResources,
+        IsDir,
+        BrokenPipe,
+        ConnectionResetByPeer,
+        Timeout,
+        /// In WASI, EBADF is mapped to this error because it is returned when
+        /// trying to read a directory file descriptor as if it were a file.
+        NotOpenForReading,
+        SocketUnconnected,
+        /// This error occurs when no global event loop is configured,
+        /// and reading from the file descriptor would block.
+        WouldBlock,
+        /// In WASI, this error occurs when the file descriptor does
+        /// not hold the required rights to read from it.
+        AccessDenied,
+        /// This error occurs in Linux if the process to be read from
+        /// no longer exists.
+        ProcessNotFound,
+        /// Unable to read file due to lock.
+        LockViolation,
+    } || Io.Cancelable || Io.UnexpectedError;
 
     pub const SizeError = std.os.windows.GetFileSizeError || StatError || error{
         /// Occurs if, for example, the file handle is a network socket and therefore does not have a size.
lib/std/Io/Kqueue.zig
@@ -1226,7 +1226,7 @@ fn fileWritePositional(userdata: ?*anyopaque, file: File, buffer: [][]const u8,
     _ = offset;
     @panic("TODO");
 }
-fn fileReadStreaming(userdata: ?*anyopaque, file: File, data: [][]u8) File.ReadStreamingError!usize {
+fn fileReadStreaming(userdata: ?*anyopaque, file: File, data: [][]u8) File.Reader.Error!usize {
     const k: *Kqueue = @ptrCast(@alignCast(userdata));
     _ = k;
     _ = file;
lib/std/Io/Threaded.zig
@@ -1219,14 +1219,17 @@ fn dirMakeOpenPathWasi(
     userdata: ?*anyopaque,
     dir: Io.Dir,
     sub_path: []const u8,
-    mode: Io.Dir.OpenOptions,
+    options: Io.Dir.OpenOptions,
 ) Io.Dir.MakeOpenPathError!Io.Dir {
     const t: *Threaded = @ptrCast(@alignCast(userdata));
-    _ = t;
-    _ = dir;
-    _ = sub_path;
-    _ = mode;
-    @panic("TODO implement dirMakeOpenPathWasi");
+    const t_io = ioBasic(t);
+    return dirOpenDirWasi(t, dir, sub_path, options) catch |err| switch (err) {
+        error.FileNotFound => {
+            try dir.makePath(t_io, sub_path);
+            return dirOpenDirWasi(t, dir, sub_path, options);
+        },
+        else => |e| return e,
+    };
 }
 
 fn dirStat(userdata: ?*anyopaque, dir: Io.Dir) Io.Dir.StatError!Io.Dir.Stat {
@@ -2498,7 +2501,7 @@ const fileReadStreaming = switch (native_os) {
     else => fileReadStreamingPosix,
 };
 
-fn fileReadStreamingPosix(userdata: ?*anyopaque, file: Io.File, data: [][]u8) Io.File.ReadStreamingError!usize {
+fn fileReadStreamingPosix(userdata: ?*anyopaque, file: Io.File, data: [][]u8) Io.File.Reader.Error!usize {
     const t: *Threaded = @ptrCast(@alignCast(userdata));
 
     var iovecs_buffer: [max_iovecs_len]posix.iovec = undefined;
@@ -2523,7 +2526,7 @@ fn fileReadStreamingPosix(userdata: ?*anyopaque, file: Io.File, data: [][]u8) Io
 
             .INVAL => |err| return errnoBug(err),
             .FAULT => |err| return errnoBug(err),
-            .BADF => |err| return errnoBug(err), // File descriptor used after closed.
+            .BADF => return error.NotOpenForReading, // File operation on directory.
             .IO => return error.InputOutput,
             .ISDIR => return error.IsDir,
             .NOBUFS => return error.SystemResources,
@@ -2561,7 +2564,7 @@ fn fileReadStreamingPosix(userdata: ?*anyopaque, file: Io.File, data: [][]u8) Io
     }
 }
 
-fn fileReadStreamingWindows(userdata: ?*anyopaque, file: Io.File, data: [][]u8) Io.File.ReadStreamingError!usize {
+fn fileReadStreamingWindows(userdata: ?*anyopaque, file: Io.File, data: [][]u8) Io.File.Reader.Error!usize {
     const t: *Threaded = @ptrCast(@alignCast(userdata));
 
     const DWORD = windows.DWORD;
@@ -2617,7 +2620,7 @@ fn fileReadPositionalPosix(userdata: ?*anyopaque, file: Io.File, data: [][]u8, o
             .INVAL => |err| return errnoBug(err),
             .FAULT => |err| return errnoBug(err),
             .AGAIN => |err| return errnoBug(err),
-            .BADF => |err| return errnoBug(err), // File descriptor used after closed.
+            .BADF => return error.NotOpenForReading, // File operation on directory.
             .IO => return error.InputOutput,
             .ISDIR => return error.IsDir,
             .NOBUFS => return error.SystemResources,
lib/std/Io.zig
@@ -684,7 +684,7 @@ pub const VTable = struct {
     fileWriteStreaming: *const fn (?*anyopaque, File, buffer: [][]const u8) File.WriteStreamingError!usize,
     fileWritePositional: *const fn (?*anyopaque, File, buffer: [][]const u8, offset: u64) File.WritePositionalError!usize,
     /// Returns 0 on end of stream.
-    fileReadStreaming: *const fn (?*anyopaque, File, data: [][]u8) File.ReadStreamingError!usize,
+    fileReadStreaming: *const fn (?*anyopaque, File, data: [][]u8) File.Reader.Error!usize,
     /// Returns 0 on end of stream.
     fileReadPositional: *const fn (?*anyopaque, File, data: [][]u8, offset: u64) File.ReadPositionalError!usize,
     fileSeekBy: *const fn (?*anyopaque, File, relative_offset: i64) File.SeekError!void,
lib/std/posix.zig
@@ -814,7 +814,7 @@ pub fn exit(status: u8) noreturn {
     system.exit(status);
 }
 
-pub const ReadError = std.Io.File.ReadStreamingError;
+pub const ReadError = std.Io.File.Reader.Error;
 
 /// Returns the number of bytes that were read, which can be less than
 /// buf.len. If 0 bytes were read, that means EOF.