diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2025-10-28 06:33:30 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2025-10-29 06:20:52 -0700 |
| commit | b39f3d294da3ce9075bbc08651965615839219f2 (patch) | |
| tree | 89096136184f6d491bba689ba559541a8750745b /lib/std | |
| parent | 4114392369c28a2455a508e4da67d945ebad2b44 (diff) | |
| download | zig-b39f3d294da3ce9075bbc08651965615839219f2.tar.gz zig-b39f3d294da3ce9075bbc08651965615839219f2.zip | |
std.Io.Threaded: implement dirMakeOpenPath for WASI
and fix error code when file operation occurs on director handle
Diffstat (limited to 'lib/std')
| -rw-r--r-- | lib/std/Io.zig | 2 | ||||
| -rw-r--r-- | lib/std/Io/File.zig | 48 | ||||
| -rw-r--r-- | lib/std/Io/Kqueue.zig | 2 | ||||
| -rw-r--r-- | lib/std/Io/Threaded.zig | 23 | ||||
| -rw-r--r-- | lib/std/posix.zig | 2 |
5 files changed, 40 insertions, 37 deletions
diff --git a/lib/std/Io.zig b/lib/std/Io.zig index 6ab366d484..cf07d8526f 100644 --- a/lib/std/Io.zig +++ b/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, diff --git a/lib/std/Io/File.zig b/lib/std/Io/File.zig index 22a904cc6a..ada57344e4 100644 --- a/lib/std/Io/File.zig +++ b/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. diff --git a/lib/std/Io/Kqueue.zig b/lib/std/Io/Kqueue.zig index fd5baaddde..87ef302fb5 100644 --- a/lib/std/Io/Kqueue.zig +++ b/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; diff --git a/lib/std/Io/Threaded.zig b/lib/std/Io/Threaded.zig index 36b53f1a4d..38a94f352d 100644 --- a/lib/std/Io/Threaded.zig +++ b/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, diff --git a/lib/std/posix.zig b/lib/std/posix.zig index 56c2bad3c3..7952727e9a 100644 --- a/lib/std/posix.zig +++ b/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. |
