diff --git a/lib/std/elf.zig b/lib/std/elf.zig index b6609d8b311f..6393c5342e19 100644 --- a/lib/std/elf.zig +++ b/lib/std/elf.zig @@ -551,6 +551,7 @@ fn preadNoEof(file: std.fs.File, buf: []u8, offset: u64) !void { error.InputOutput => return error.FileSystem, error.Unexpected => return error.Unexpected, error.WouldBlock => return error.Unexpected, + error.NotOpenForReading => return error.Unexpected, error.AccessDenied => return error.Unexpected, }; if (len == 0) return error.UnexpectedEndOfFile; diff --git a/lib/std/os.zig b/lib/std/os.zig index 1e1049ae51ce..887353545568 100644 --- a/lib/std/os.zig +++ b/lib/std/os.zig @@ -296,6 +296,7 @@ pub const ReadError = error{ BrokenPipe, ConnectionResetByPeer, ConnectionTimedOut, + NotOpenForReading, /// This error occurs when no global event loop is configured, /// and reading from the file descriptor would block. @@ -332,7 +333,7 @@ pub fn read(fd: fd_t, buf: []u8) ReadError!usize { wasi.EINVAL => unreachable, wasi.EFAULT => unreachable, wasi.EAGAIN => unreachable, - wasi.EBADF => unreachable, // Always a race condition. + wasi.EBADF => return error.NotOpenForReading, // Can be a race condition. wasi.EIO => return error.InputOutput, wasi.EISDIR => return error.IsDir, wasi.ENOBUFS => return error.SystemResources, @@ -364,7 +365,7 @@ pub fn read(fd: fd_t, buf: []u8) ReadError!usize { } else { return error.WouldBlock; }, - EBADF => unreachable, // Always a race condition. + EBADF => return error.NotOpenForReading, // Can be a race condition. EIO => return error.InputOutput, EISDIR => return error.IsDir, ENOBUFS => return error.SystemResources, @@ -402,7 +403,7 @@ pub fn readv(fd: fd_t, iov: []const iovec) ReadError!usize { wasi.EINVAL => unreachable, wasi.EFAULT => unreachable, wasi.EAGAIN => unreachable, // currently not support in WASI - wasi.EBADF => unreachable, // always a race condition + wasi.EBADF => return error.NotOpenForReading, // can be a race condition wasi.EIO => return error.InputOutput, wasi.EISDIR => return error.IsDir, wasi.ENOBUFS => return error.SystemResources, @@ -426,7 +427,7 @@ pub fn readv(fd: fd_t, iov: []const iovec) ReadError!usize { } else { return error.WouldBlock; }, - EBADF => unreachable, // always a race condition + EBADF => return error.NotOpenForReading, // can be a race condition EIO => return error.InputOutput, EISDIR => return error.IsDir, ENOBUFS => return error.SystemResources, @@ -463,7 +464,7 @@ pub fn pread(fd: fd_t, buf: []u8, offset: u64) PReadError!usize { wasi.EINVAL => unreachable, wasi.EFAULT => unreachable, wasi.EAGAIN => unreachable, - wasi.EBADF => unreachable, // Always a race condition. + wasi.EBADF => return error.NotOpenForReading, // Can be a race condition. wasi.EIO => return error.InputOutput, wasi.EISDIR => return error.IsDir, wasi.ENOBUFS => return error.SystemResources, @@ -490,7 +491,7 @@ pub fn pread(fd: fd_t, buf: []u8, offset: u64) PReadError!usize { } else { return error.WouldBlock; }, - EBADF => unreachable, // Always a race condition. + EBADF => return error.NotOpenForReading, // Can be a race condition. EIO => return error.InputOutput, EISDIR => return error.IsDir, ENOBUFS => return error.SystemResources, @@ -607,7 +608,7 @@ pub fn preadv(fd: fd_t, iov: []const iovec, offset: u64) PReadError!usize { wasi.EINVAL => unreachable, wasi.EFAULT => unreachable, wasi.EAGAIN => unreachable, - wasi.EBADF => unreachable, // always a race condition + wasi.EBADF => return error.NotOpenForReading, // can be a race condition wasi.EIO => return error.InputOutput, wasi.EISDIR => return error.IsDir, wasi.ENOBUFS => return error.SystemResources, @@ -635,7 +636,7 @@ pub fn preadv(fd: fd_t, iov: []const iovec, offset: u64) PReadError!usize { } else { return error.WouldBlock; }, - EBADF => unreachable, // always a race condition + EBADF => return error.NotOpenForReading, // can be a race condition EIO => return error.InputOutput, EISDIR => return error.IsDir, ENOBUFS => return error.SystemResources, @@ -660,6 +661,7 @@ pub const WriteError = error{ BrokenPipe, SystemResources, OperationAborted, + NotOpenForWriting, /// This error occurs when no global event loop is configured, /// and reading from the file descriptor would block. @@ -704,7 +706,7 @@ pub fn write(fd: fd_t, bytes: []const u8) WriteError!usize { wasi.EINVAL => unreachable, wasi.EFAULT => unreachable, wasi.EAGAIN => unreachable, - wasi.EBADF => unreachable, // Always a race condition. + wasi.EBADF => return error.NotOpenForWriting, // can be a race condition. wasi.EDESTADDRREQ => unreachable, // `connect` was never called. wasi.EDQUOT => return error.DiskQuota, wasi.EFBIG => return error.FileTooBig, @@ -736,7 +738,7 @@ pub fn write(fd: fd_t, bytes: []const u8) WriteError!usize { } else { return error.WouldBlock; }, - EBADF => unreachable, // Always a race condition. + EBADF => return error.NotOpenForWriting, // can be a race condition. EDESTADDRREQ => unreachable, // `connect` was never called. EDQUOT => return error.DiskQuota, EFBIG => return error.FileTooBig, @@ -782,7 +784,7 @@ pub fn writev(fd: fd_t, iov: []const iovec_const) WriteError!usize { wasi.EINVAL => unreachable, wasi.EFAULT => unreachable, wasi.EAGAIN => unreachable, - wasi.EBADF => unreachable, // Always a race condition. + wasi.EBADF => return error.NotOpenForWriting, // can be a race condition. wasi.EDESTADDRREQ => unreachable, // `connect` was never called. wasi.EDQUOT => return error.DiskQuota, wasi.EFBIG => return error.FileTooBig, @@ -809,7 +811,7 @@ pub fn writev(fd: fd_t, iov: []const iovec_const) WriteError!usize { } else { return error.WouldBlock; }, - EBADF => unreachable, // Always a race condition. + EBADF => return error.NotOpenForWriting, // Can be a race condition. EDESTADDRREQ => unreachable, // `connect` was never called. EDQUOT => return error.DiskQuota, EFBIG => return error.FileTooBig, @@ -862,7 +864,7 @@ pub fn pwrite(fd: fd_t, bytes: []const u8, offset: u64) PWriteError!usize { wasi.EINVAL => unreachable, wasi.EFAULT => unreachable, wasi.EAGAIN => unreachable, - wasi.EBADF => unreachable, // Always a race condition. + wasi.EBADF => return error.NotOpenForWriting, // can be a race condition. wasi.EDESTADDRREQ => unreachable, // `connect` was never called. wasi.EDQUOT => return error.DiskQuota, wasi.EFBIG => return error.FileTooBig, @@ -898,7 +900,7 @@ pub fn pwrite(fd: fd_t, bytes: []const u8, offset: u64) PWriteError!usize { } else { return error.WouldBlock; }, - EBADF => unreachable, // Always a race condition. + EBADF => return error.NotOpenForWriting, // Can be a race condition. EDESTADDRREQ => unreachable, // `connect` was never called. EDQUOT => return error.DiskQuota, EFBIG => return error.FileTooBig, @@ -956,7 +958,7 @@ pub fn pwritev(fd: fd_t, iov: []const iovec_const, offset: u64) PWriteError!usiz wasi.EINVAL => unreachable, wasi.EFAULT => unreachable, wasi.EAGAIN => unreachable, - wasi.EBADF => unreachable, // Always a race condition. + wasi.EBADF => return error.NotOpenForWriting, // Can be a race condition. wasi.EDESTADDRREQ => unreachable, // `connect` was never called. wasi.EDQUOT => return error.DiskQuota, wasi.EFBIG => return error.FileTooBig, @@ -986,7 +988,7 @@ pub fn pwritev(fd: fd_t, iov: []const iovec_const, offset: u64) PWriteError!usiz } else { return error.WouldBlock; }, - EBADF => unreachable, // Always a race condition. + EBADF => return error.NotOpenForWriting, // Can be a race condition. EDESTADDRREQ => unreachable, // `connect` was never called. EDQUOT => return error.DiskQuota, EFBIG => return error.FileTooBig, @@ -1184,7 +1186,7 @@ pub fn dup2(old_fd: fd_t, new_fd: fd_t) !void { EBUSY, EINTR => continue, EMFILE => return error.ProcessFdQuotaExceeded, EINVAL => unreachable, // invalid parameters passed to dup2 - EBADF => unreachable, // always a race condition + EBADF => unreachable, // invalid file descriptor else => |err| return unexpectedErrno(err), } } diff --git a/lib/std/zig/system.zig b/lib/std/zig/system.zig index 2b32e3962487..44a89459dd1f 100644 --- a/lib/std/zig/system.zig +++ b/lib/std/zig/system.zig @@ -851,6 +851,7 @@ pub const NativeTargetInfo = struct { const len = file.pread(buf[i .. buf.len - i], offset + i) catch |err| switch (err) { error.OperationAborted => unreachable, // Windows-only error.WouldBlock => unreachable, // Did not request blocking mode + error.NotOpenForReading => unreachable, error.SystemResources => return error.SystemResources, error.IsDir => return error.UnableToReadElfFile, error.BrokenPipe => return error.UnableToReadElfFile, diff --git a/src-self-hosted/main.zig b/src-self-hosted/main.zig index 3743b4f3342b..be1e941992d6 100644 --- a/src-self-hosted/main.zig +++ b/src-self-hosted/main.zig @@ -696,6 +696,7 @@ const FmtError = error{ LinkQuotaExceeded, FileBusy, EndOfStream, + NotOpenForWriting, } || fs.File.OpenError; fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool, dir: fs.Dir, sub_path: []const u8) FmtError!void { @@ -761,6 +762,7 @@ fn fmtPathFile( const source_code = source_file.readAllAlloc(fmt.gpa, stat.size, max_src_size) catch |err| switch (err) { error.ConnectionResetByPeer => unreachable, error.ConnectionTimedOut => unreachable, + error.NotOpenForReading => unreachable, else => |e| return e, }; source_file.close(); diff --git a/src-self-hosted/stage2.zig b/src-self-hosted/stage2.zig index bd24ffb399f1..33e9200902ea 100644 --- a/src-self-hosted/stage2.zig +++ b/src-self-hosted/stage2.zig @@ -153,6 +153,7 @@ export fn stage2_render_ast(tree: *ast.Tree, output_file: *FILE) Error { const c_out_stream = std.io.cOutStream(output_file); _ = std.zig.render(std.heap.c_allocator, c_out_stream, tree) catch |e| switch (e) { error.WouldBlock => unreachable, // stage1 opens stuff in exclusively blocking mode + error.NotOpenForWriting => unreachable, error.SystemResources => return .SystemResources, error.OperationAborted => return .OperationAborted, error.BrokenPipe => return .BrokenPipe, @@ -585,6 +586,8 @@ export fn stage2_libc_parse(stage1_libc: *Stage2LibCInstallation, libc_file_z: [ error.SystemResources => return .SystemResources, error.OperationAborted => return .OperationAborted, error.WouldBlock => unreachable, + error.NotOpenForWriting => unreachable, + error.NotOpenForReading => unreachable, error.Unexpected => return .Unexpected, error.EndOfStream => return .EndOfFile, error.IsDir => return .IsDir, @@ -640,6 +643,7 @@ export fn stage2_libc_render(stage1_libc: *Stage2LibCInstallation, output_file: const c_out_stream = std.io.cOutStream(output_file); libc.render(c_out_stream) catch |err| switch (err) { error.WouldBlock => unreachable, // stage1 opens stuff in exclusively blocking mode + error.NotOpenForWriting => unreachable, error.SystemResources => return .SystemResources, error.OperationAborted => return .OperationAborted, error.BrokenPipe => return .BrokenPipe,