mirror of
https://github.com/ziglang/zig.git
synced 2025-12-28 17:13:19 +00:00
std.Io.Threaded: implement dirMakeOpenPath for WASI
and fix error code when file operation occurs on director handle
This commit is contained in:
parent
4114392369
commit
b39f3d294d
@ -684,7 +684,7 @@ pub const VTable = struct {
|
|||||||
fileWriteStreaming: *const fn (?*anyopaque, File, buffer: [][]const u8) File.WriteStreamingError!usize,
|
fileWriteStreaming: *const fn (?*anyopaque, File, buffer: [][]const u8) File.WriteStreamingError!usize,
|
||||||
fileWritePositional: *const fn (?*anyopaque, File, buffer: [][]const u8, offset: u64) File.WritePositionalError!usize,
|
fileWritePositional: *const fn (?*anyopaque, File, buffer: [][]const u8, offset: u64) File.WritePositionalError!usize,
|
||||||
/// Returns 0 on end of stream.
|
/// 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.
|
/// Returns 0 on end of stream.
|
||||||
fileReadPositional: *const fn (?*anyopaque, File, data: [][]u8, offset: u64) File.ReadPositionalError!usize,
|
fileReadPositional: *const fn (?*anyopaque, File, data: [][]u8, offset: u64) File.ReadPositionalError!usize,
|
||||||
fileSeekBy: *const fn (?*anyopaque, File, relative_offset: i64) File.SeekError!void,
|
fileSeekBy: *const fn (?*anyopaque, File, relative_offset: i64) File.SeekError!void,
|
||||||
|
|||||||
@ -213,29 +213,7 @@ pub fn openSelfExe(io: Io, flags: OpenFlags) OpenSelfExeError!File {
|
|||||||
return io.vtable.openSelfExe(io.userdata, flags);
|
return io.vtable.openSelfExe(io.userdata, flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const ReadStreamingError = error{
|
pub const ReadPositionalError = Reader.Error || error{Unseekable};
|
||||||
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 fn readPositional(file: File, io: Io, buffer: []u8, offset: u64) ReadPositionalError!usize {
|
pub fn readPositional(file: File, io: Io, buffer: []u8, offset: u64) ReadPositionalError!usize {
|
||||||
return io.vtable.fileReadPositional(io.userdata, file, buffer, offset);
|
return io.vtable.fileReadPositional(io.userdata, file, buffer, offset);
|
||||||
@ -301,7 +279,29 @@ pub const Reader = struct {
|
|||||||
seek_err: ?Reader.SeekError = null,
|
seek_err: ?Reader.SeekError = null,
|
||||||
interface: Io.Reader,
|
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{
|
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.
|
/// Occurs if, for example, the file handle is a network socket and therefore does not have a size.
|
||||||
|
|||||||
@ -1226,7 +1226,7 @@ fn fileWritePositional(userdata: ?*anyopaque, file: File, buffer: [][]const u8,
|
|||||||
_ = offset;
|
_ = offset;
|
||||||
@panic("TODO");
|
@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));
|
const k: *Kqueue = @ptrCast(@alignCast(userdata));
|
||||||
_ = k;
|
_ = k;
|
||||||
_ = file;
|
_ = file;
|
||||||
|
|||||||
@ -1219,14 +1219,17 @@ fn dirMakeOpenPathWasi(
|
|||||||
userdata: ?*anyopaque,
|
userdata: ?*anyopaque,
|
||||||
dir: Io.Dir,
|
dir: Io.Dir,
|
||||||
sub_path: []const u8,
|
sub_path: []const u8,
|
||||||
mode: Io.Dir.OpenOptions,
|
options: Io.Dir.OpenOptions,
|
||||||
) Io.Dir.MakeOpenPathError!Io.Dir {
|
) Io.Dir.MakeOpenPathError!Io.Dir {
|
||||||
const t: *Threaded = @ptrCast(@alignCast(userdata));
|
const t: *Threaded = @ptrCast(@alignCast(userdata));
|
||||||
_ = t;
|
const t_io = ioBasic(t);
|
||||||
_ = dir;
|
return dirOpenDirWasi(t, dir, sub_path, options) catch |err| switch (err) {
|
||||||
_ = sub_path;
|
error.FileNotFound => {
|
||||||
_ = mode;
|
try dir.makePath(t_io, sub_path);
|
||||||
@panic("TODO implement dirMakeOpenPathWasi");
|
return dirOpenDirWasi(t, dir, sub_path, options);
|
||||||
|
},
|
||||||
|
else => |e| return e,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn dirStat(userdata: ?*anyopaque, dir: Io.Dir) Io.Dir.StatError!Io.Dir.Stat {
|
fn dirStat(userdata: ?*anyopaque, dir: Io.Dir) Io.Dir.StatError!Io.Dir.Stat {
|
||||||
@ -2498,7 +2501,7 @@ const fileReadStreaming = switch (native_os) {
|
|||||||
else => fileReadStreamingPosix,
|
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));
|
const t: *Threaded = @ptrCast(@alignCast(userdata));
|
||||||
|
|
||||||
var iovecs_buffer: [max_iovecs_len]posix.iovec = undefined;
|
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),
|
.INVAL => |err| return errnoBug(err),
|
||||||
.FAULT => |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,
|
.IO => return error.InputOutput,
|
||||||
.ISDIR => return error.IsDir,
|
.ISDIR => return error.IsDir,
|
||||||
.NOBUFS => return error.SystemResources,
|
.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 t: *Threaded = @ptrCast(@alignCast(userdata));
|
||||||
|
|
||||||
const DWORD = windows.DWORD;
|
const DWORD = windows.DWORD;
|
||||||
@ -2617,7 +2620,7 @@ fn fileReadPositionalPosix(userdata: ?*anyopaque, file: Io.File, data: [][]u8, o
|
|||||||
.INVAL => |err| return errnoBug(err),
|
.INVAL => |err| return errnoBug(err),
|
||||||
.FAULT => |err| return errnoBug(err),
|
.FAULT => |err| return errnoBug(err),
|
||||||
.AGAIN => |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,
|
.IO => return error.InputOutput,
|
||||||
.ISDIR => return error.IsDir,
|
.ISDIR => return error.IsDir,
|
||||||
.NOBUFS => return error.SystemResources,
|
.NOBUFS => return error.SystemResources,
|
||||||
|
|||||||
@ -814,7 +814,7 @@ pub fn exit(status: u8) noreturn {
|
|||||||
system.exit(status);
|
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
|
/// Returns the number of bytes that were read, which can be less than
|
||||||
/// buf.len. If 0 bytes were read, that means EOF.
|
/// buf.len. If 0 bytes were read, that means EOF.
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user