authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-08-19 23:46:22-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-08-19 23:46:22-04:00
logb1c7334355b345a6c244f617c2d4fae9e9f82c5a
treec97071917e07990ef415e4bd948c0f3c08c4141c
parenta30950706f858e052376fb2d526895970f59d0ca
parente2cfc65909d49d1102fa440759f5475ced0c0c15
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #5745 from lun-4/ebadf-error

map EBADF to error values for read and write

5 files changed, 27 insertions(+), 17 deletions(-)

lib/std/elf.zig+1
...@@ -558,6 +558,7 @@ fn preadNoEof(file: std.fs.File, buf: []u8, offset: u64) !void {...@@ -558,6 +558,7 @@ fn preadNoEof(file: std.fs.File, buf: []u8, offset: u64) !void {
558 error.InputOutput => return error.FileSystem,558 error.InputOutput => return error.FileSystem,
559 error.Unexpected => return error.Unexpected,559 error.Unexpected => return error.Unexpected,
560 error.WouldBlock => return error.Unexpected,560 error.WouldBlock => return error.Unexpected,
561 error.NotOpenForReading => return error.Unexpected,
561 error.AccessDenied => return error.Unexpected,562 error.AccessDenied => return error.Unexpected,
562 };563 };
563 if (len == 0) return error.UnexpectedEndOfFile;564 if (len == 0) return error.UnexpectedEndOfFile;
lib/std/os.zig+19-17
...@@ -296,6 +296,7 @@ pub const ReadError = error{...@@ -296,6 +296,7 @@ pub const ReadError = error{
296 BrokenPipe,296 BrokenPipe,
297 ConnectionResetByPeer,297 ConnectionResetByPeer,
298 ConnectionTimedOut,298 ConnectionTimedOut,
299 NotOpenForReading,
299300
300 /// This error occurs when no global event loop is configured,301 /// This error occurs when no global event loop is configured,
301 /// and reading from the file descriptor would block.302 /// and reading from the file descriptor would block.
...@@ -332,7 +333,7 @@ pub fn read(fd: fd_t, buf: []u8) ReadError!usize {...@@ -332,7 +333,7 @@ pub fn read(fd: fd_t, buf: []u8) ReadError!usize {
332 wasi.EINVAL => unreachable,333 wasi.EINVAL => unreachable,
333 wasi.EFAULT => unreachable,334 wasi.EFAULT => unreachable,
334 wasi.EAGAIN => unreachable,335 wasi.EAGAIN => unreachable,
335 wasi.EBADF => unreachable, // Always a race condition.336 wasi.EBADF => return error.NotOpenForReading, // Can be a race condition.
336 wasi.EIO => return error.InputOutput,337 wasi.EIO => return error.InputOutput,
337 wasi.EISDIR => return error.IsDir,338 wasi.EISDIR => return error.IsDir,
338 wasi.ENOBUFS => return error.SystemResources,339 wasi.ENOBUFS => return error.SystemResources,
...@@ -364,7 +365,7 @@ pub fn read(fd: fd_t, buf: []u8) ReadError!usize {...@@ -364,7 +365,7 @@ pub fn read(fd: fd_t, buf: []u8) ReadError!usize {
364 } else {365 } else {
365 return error.WouldBlock;366 return error.WouldBlock;
366 },367 },
367 EBADF => unreachable, // Always a race condition.368 EBADF => return error.NotOpenForReading, // Can be a race condition.
368 EIO => return error.InputOutput,369 EIO => return error.InputOutput,
369 EISDIR => return error.IsDir,370 EISDIR => return error.IsDir,
370 ENOBUFS => return error.SystemResources,371 ENOBUFS => return error.SystemResources,
...@@ -402,7 +403,7 @@ pub fn readv(fd: fd_t, iov: []const iovec) ReadError!usize {...@@ -402,7 +403,7 @@ pub fn readv(fd: fd_t, iov: []const iovec) ReadError!usize {
402 wasi.EINVAL => unreachable,403 wasi.EINVAL => unreachable,
403 wasi.EFAULT => unreachable,404 wasi.EFAULT => unreachable,
404 wasi.EAGAIN => unreachable, // currently not support in WASI405 wasi.EAGAIN => unreachable, // currently not support in WASI
405 wasi.EBADF => unreachable, // always a race condition406 wasi.EBADF => return error.NotOpenForReading, // can be a race condition
406 wasi.EIO => return error.InputOutput,407 wasi.EIO => return error.InputOutput,
407 wasi.EISDIR => return error.IsDir,408 wasi.EISDIR => return error.IsDir,
408 wasi.ENOBUFS => return error.SystemResources,409 wasi.ENOBUFS => return error.SystemResources,
...@@ -426,7 +427,7 @@ pub fn readv(fd: fd_t, iov: []const iovec) ReadError!usize {...@@ -426,7 +427,7 @@ pub fn readv(fd: fd_t, iov: []const iovec) ReadError!usize {
426 } else {427 } else {
427 return error.WouldBlock;428 return error.WouldBlock;
428 },429 },
429 EBADF => unreachable, // always a race condition430 EBADF => return error.NotOpenForReading, // can be a race condition
430 EIO => return error.InputOutput,431 EIO => return error.InputOutput,
431 EISDIR => return error.IsDir,432 EISDIR => return error.IsDir,
432 ENOBUFS => return error.SystemResources,433 ENOBUFS => return error.SystemResources,
...@@ -463,7 +464,7 @@ pub fn pread(fd: fd_t, buf: []u8, offset: u64) PReadError!usize {...@@ -463,7 +464,7 @@ pub fn pread(fd: fd_t, buf: []u8, offset: u64) PReadError!usize {
463 wasi.EINVAL => unreachable,464 wasi.EINVAL => unreachable,
464 wasi.EFAULT => unreachable,465 wasi.EFAULT => unreachable,
465 wasi.EAGAIN => unreachable,466 wasi.EAGAIN => unreachable,
466 wasi.EBADF => unreachable, // Always a race condition.467 wasi.EBADF => return error.NotOpenForReading, // Can be a race condition.
467 wasi.EIO => return error.InputOutput,468 wasi.EIO => return error.InputOutput,
468 wasi.EISDIR => return error.IsDir,469 wasi.EISDIR => return error.IsDir,
469 wasi.ENOBUFS => return error.SystemResources,470 wasi.ENOBUFS => return error.SystemResources,
...@@ -490,7 +491,7 @@ pub fn pread(fd: fd_t, buf: []u8, offset: u64) PReadError!usize {...@@ -490,7 +491,7 @@ pub fn pread(fd: fd_t, buf: []u8, offset: u64) PReadError!usize {
490 } else {491 } else {
491 return error.WouldBlock;492 return error.WouldBlock;
492 },493 },
493 EBADF => unreachable, // Always a race condition.494 EBADF => return error.NotOpenForReading, // Can be a race condition.
494 EIO => return error.InputOutput,495 EIO => return error.InputOutput,
495 EISDIR => return error.IsDir,496 EISDIR => return error.IsDir,
496 ENOBUFS => return error.SystemResources,497 ENOBUFS => return error.SystemResources,
...@@ -607,7 +608,7 @@ pub fn preadv(fd: fd_t, iov: []const iovec, offset: u64) PReadError!usize {...@@ -607,7 +608,7 @@ pub fn preadv(fd: fd_t, iov: []const iovec, offset: u64) PReadError!usize {
607 wasi.EINVAL => unreachable,608 wasi.EINVAL => unreachable,
608 wasi.EFAULT => unreachable,609 wasi.EFAULT => unreachable,
609 wasi.EAGAIN => unreachable,610 wasi.EAGAIN => unreachable,
610 wasi.EBADF => unreachable, // always a race condition611 wasi.EBADF => return error.NotOpenForReading, // can be a race condition
611 wasi.EIO => return error.InputOutput,612 wasi.EIO => return error.InputOutput,
612 wasi.EISDIR => return error.IsDir,613 wasi.EISDIR => return error.IsDir,
613 wasi.ENOBUFS => return error.SystemResources,614 wasi.ENOBUFS => return error.SystemResources,
...@@ -635,7 +636,7 @@ pub fn preadv(fd: fd_t, iov: []const iovec, offset: u64) PReadError!usize {...@@ -635,7 +636,7 @@ pub fn preadv(fd: fd_t, iov: []const iovec, offset: u64) PReadError!usize {
635 } else {636 } else {
636 return error.WouldBlock;637 return error.WouldBlock;
637 },638 },
638 EBADF => unreachable, // always a race condition639 EBADF => return error.NotOpenForReading, // can be a race condition
639 EIO => return error.InputOutput,640 EIO => return error.InputOutput,
640 EISDIR => return error.IsDir,641 EISDIR => return error.IsDir,
641 ENOBUFS => return error.SystemResources,642 ENOBUFS => return error.SystemResources,
...@@ -660,6 +661,7 @@ pub const WriteError = error{...@@ -660,6 +661,7 @@ pub const WriteError = error{
660 BrokenPipe,661 BrokenPipe,
661 SystemResources,662 SystemResources,
662 OperationAborted,663 OperationAborted,
664 NotOpenForWriting,
663665
664 /// This error occurs when no global event loop is configured,666 /// This error occurs when no global event loop is configured,
665 /// and reading from the file descriptor would block.667 /// and reading from the file descriptor would block.
...@@ -704,7 +706,7 @@ pub fn write(fd: fd_t, bytes: []const u8) WriteError!usize {...@@ -704,7 +706,7 @@ pub fn write(fd: fd_t, bytes: []const u8) WriteError!usize {
704 wasi.EINVAL => unreachable,706 wasi.EINVAL => unreachable,
705 wasi.EFAULT => unreachable,707 wasi.EFAULT => unreachable,
706 wasi.EAGAIN => unreachable,708 wasi.EAGAIN => unreachable,
707 wasi.EBADF => unreachable, // Always a race condition.709 wasi.EBADF => return error.NotOpenForWriting, // can be a race condition.
708 wasi.EDESTADDRREQ => unreachable, // `connect` was never called.710 wasi.EDESTADDRREQ => unreachable, // `connect` was never called.
709 wasi.EDQUOT => return error.DiskQuota,711 wasi.EDQUOT => return error.DiskQuota,
710 wasi.EFBIG => return error.FileTooBig,712 wasi.EFBIG => return error.FileTooBig,
...@@ -736,7 +738,7 @@ pub fn write(fd: fd_t, bytes: []const u8) WriteError!usize {...@@ -736,7 +738,7 @@ pub fn write(fd: fd_t, bytes: []const u8) WriteError!usize {
736 } else {738 } else {
737 return error.WouldBlock;739 return error.WouldBlock;
738 },740 },
739 EBADF => unreachable, // Always a race condition.741 EBADF => return error.NotOpenForWriting, // can be a race condition.
740 EDESTADDRREQ => unreachable, // `connect` was never called.742 EDESTADDRREQ => unreachable, // `connect` was never called.
741 EDQUOT => return error.DiskQuota,743 EDQUOT => return error.DiskQuota,
742 EFBIG => return error.FileTooBig,744 EFBIG => return error.FileTooBig,
...@@ -782,7 +784,7 @@ pub fn writev(fd: fd_t, iov: []const iovec_const) WriteError!usize {...@@ -782,7 +784,7 @@ pub fn writev(fd: fd_t, iov: []const iovec_const) WriteError!usize {
782 wasi.EINVAL => unreachable,784 wasi.EINVAL => unreachable,
783 wasi.EFAULT => unreachable,785 wasi.EFAULT => unreachable,
784 wasi.EAGAIN => unreachable,786 wasi.EAGAIN => unreachable,
785 wasi.EBADF => unreachable, // Always a race condition.787 wasi.EBADF => return error.NotOpenForWriting, // can be a race condition.
786 wasi.EDESTADDRREQ => unreachable, // `connect` was never called.788 wasi.EDESTADDRREQ => unreachable, // `connect` was never called.
787 wasi.EDQUOT => return error.DiskQuota,789 wasi.EDQUOT => return error.DiskQuota,
788 wasi.EFBIG => return error.FileTooBig,790 wasi.EFBIG => return error.FileTooBig,
...@@ -809,7 +811,7 @@ pub fn writev(fd: fd_t, iov: []const iovec_const) WriteError!usize {...@@ -809,7 +811,7 @@ pub fn writev(fd: fd_t, iov: []const iovec_const) WriteError!usize {
809 } else {811 } else {
810 return error.WouldBlock;812 return error.WouldBlock;
811 },813 },
812 EBADF => unreachable, // Always a race condition.814 EBADF => return error.NotOpenForWriting, // Can be a race condition.
813 EDESTADDRREQ => unreachable, // `connect` was never called.815 EDESTADDRREQ => unreachable, // `connect` was never called.
814 EDQUOT => return error.DiskQuota,816 EDQUOT => return error.DiskQuota,
815 EFBIG => return error.FileTooBig,817 EFBIG => return error.FileTooBig,
...@@ -862,7 +864,7 @@ pub fn pwrite(fd: fd_t, bytes: []const u8, offset: u64) PWriteError!usize {...@@ -862,7 +864,7 @@ pub fn pwrite(fd: fd_t, bytes: []const u8, offset: u64) PWriteError!usize {
862 wasi.EINVAL => unreachable,864 wasi.EINVAL => unreachable,
863 wasi.EFAULT => unreachable,865 wasi.EFAULT => unreachable,
864 wasi.EAGAIN => unreachable,866 wasi.EAGAIN => unreachable,
865 wasi.EBADF => unreachable, // Always a race condition.867 wasi.EBADF => return error.NotOpenForWriting, // can be a race condition.
866 wasi.EDESTADDRREQ => unreachable, // `connect` was never called.868 wasi.EDESTADDRREQ => unreachable, // `connect` was never called.
867 wasi.EDQUOT => return error.DiskQuota,869 wasi.EDQUOT => return error.DiskQuota,
868 wasi.EFBIG => return error.FileTooBig,870 wasi.EFBIG => return error.FileTooBig,
...@@ -898,7 +900,7 @@ pub fn pwrite(fd: fd_t, bytes: []const u8, offset: u64) PWriteError!usize {...@@ -898,7 +900,7 @@ pub fn pwrite(fd: fd_t, bytes: []const u8, offset: u64) PWriteError!usize {
898 } else {900 } else {
899 return error.WouldBlock;901 return error.WouldBlock;
900 },902 },
901 EBADF => unreachable, // Always a race condition.903 EBADF => return error.NotOpenForWriting, // Can be a race condition.
902 EDESTADDRREQ => unreachable, // `connect` was never called.904 EDESTADDRREQ => unreachable, // `connect` was never called.
903 EDQUOT => return error.DiskQuota,905 EDQUOT => return error.DiskQuota,
904 EFBIG => return error.FileTooBig,906 EFBIG => return error.FileTooBig,
...@@ -956,7 +958,7 @@ pub fn pwritev(fd: fd_t, iov: []const iovec_const, offset: u64) PWriteError!usiz...@@ -956,7 +958,7 @@ pub fn pwritev(fd: fd_t, iov: []const iovec_const, offset: u64) PWriteError!usiz
956 wasi.EINVAL => unreachable,958 wasi.EINVAL => unreachable,
957 wasi.EFAULT => unreachable,959 wasi.EFAULT => unreachable,
958 wasi.EAGAIN => unreachable,960 wasi.EAGAIN => unreachable,
959 wasi.EBADF => unreachable, // Always a race condition.961 wasi.EBADF => return error.NotOpenForWriting, // Can be a race condition.
960 wasi.EDESTADDRREQ => unreachable, // `connect` was never called.962 wasi.EDESTADDRREQ => unreachable, // `connect` was never called.
961 wasi.EDQUOT => return error.DiskQuota,963 wasi.EDQUOT => return error.DiskQuota,
962 wasi.EFBIG => return error.FileTooBig,964 wasi.EFBIG => return error.FileTooBig,
...@@ -986,7 +988,7 @@ pub fn pwritev(fd: fd_t, iov: []const iovec_const, offset: u64) PWriteError!usiz...@@ -986,7 +988,7 @@ pub fn pwritev(fd: fd_t, iov: []const iovec_const, offset: u64) PWriteError!usiz
986 } else {988 } else {
987 return error.WouldBlock;989 return error.WouldBlock;
988 },990 },
989 EBADF => unreachable, // Always a race condition.991 EBADF => return error.NotOpenForWriting, // Can be a race condition.
990 EDESTADDRREQ => unreachable, // `connect` was never called.992 EDESTADDRREQ => unreachable, // `connect` was never called.
991 EDQUOT => return error.DiskQuota,993 EDQUOT => return error.DiskQuota,
992 EFBIG => return error.FileTooBig,994 EFBIG => return error.FileTooBig,
...@@ -1251,7 +1253,7 @@ pub fn dup2(old_fd: fd_t, new_fd: fd_t) !void {...@@ -1251,7 +1253,7 @@ pub fn dup2(old_fd: fd_t, new_fd: fd_t) !void {
1251 EBUSY, EINTR => continue,1253 EBUSY, EINTR => continue,
1252 EMFILE => return error.ProcessFdQuotaExceeded,1254 EMFILE => return error.ProcessFdQuotaExceeded,
1253 EINVAL => unreachable, // invalid parameters passed to dup21255 EINVAL => unreachable, // invalid parameters passed to dup2
1254 EBADF => unreachable, // always a race condition1256 EBADF => unreachable, // invalid file descriptor
1255 else => |err| return unexpectedErrno(err),1257 else => |err| return unexpectedErrno(err),
1256 }1258 }
1257 }1259 }
lib/std/zig/system.zig+1
...@@ -857,6 +857,7 @@ pub const NativeTargetInfo = struct {...@@ -857,6 +857,7 @@ pub const NativeTargetInfo = struct {
857 const len = file.pread(buf[i .. buf.len - i], offset + i) catch |err| switch (err) {857 const len = file.pread(buf[i .. buf.len - i], offset + i) catch |err| switch (err) {
858 error.OperationAborted => unreachable, // Windows-only858 error.OperationAborted => unreachable, // Windows-only
859 error.WouldBlock => unreachable, // Did not request blocking mode859 error.WouldBlock => unreachable, // Did not request blocking mode
860 error.NotOpenForReading => unreachable,
860 error.SystemResources => return error.SystemResources,861 error.SystemResources => return error.SystemResources,
861 error.IsDir => return error.UnableToReadElfFile,862 error.IsDir => return error.UnableToReadElfFile,
862 error.BrokenPipe => return error.UnableToReadElfFile,863 error.BrokenPipe => return error.UnableToReadElfFile,
src-self-hosted/main.zig+2
...@@ -742,6 +742,7 @@ const FmtError = error{...@@ -742,6 +742,7 @@ const FmtError = error{
742 LinkQuotaExceeded,742 LinkQuotaExceeded,
743 FileBusy,743 FileBusy,
744 EndOfStream,744 EndOfStream,
745 NotOpenForWriting,
745} || fs.File.OpenError;746} || fs.File.OpenError;
746747
747fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool, dir: fs.Dir, sub_path: []const u8) FmtError!void {748fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool, dir: fs.Dir, sub_path: []const u8) FmtError!void {
...@@ -807,6 +808,7 @@ fn fmtPathFile(...@@ -807,6 +808,7 @@ fn fmtPathFile(
807 const source_code = source_file.readAllAlloc(fmt.gpa, stat.size, max_src_size) catch |err| switch (err) {808 const source_code = source_file.readAllAlloc(fmt.gpa, stat.size, max_src_size) catch |err| switch (err) {
808 error.ConnectionResetByPeer => unreachable,809 error.ConnectionResetByPeer => unreachable,
809 error.ConnectionTimedOut => unreachable,810 error.ConnectionTimedOut => unreachable,
811 error.NotOpenForReading => unreachable,
810 else => |e| return e,812 else => |e| return e,
811 };813 };
812 source_file.close();814 source_file.close();
src-self-hosted/stage2.zig+4
...@@ -153,6 +153,7 @@ export fn stage2_render_ast(tree: *ast.Tree, output_file: *FILE) Error {...@@ -153,6 +153,7 @@ export fn stage2_render_ast(tree: *ast.Tree, output_file: *FILE) Error {
153 const c_out_stream = std.io.cOutStream(output_file);153 const c_out_stream = std.io.cOutStream(output_file);
154 _ = std.zig.render(std.heap.c_allocator, c_out_stream, tree) catch |e| switch (e) {154 _ = std.zig.render(std.heap.c_allocator, c_out_stream, tree) catch |e| switch (e) {
155 error.WouldBlock => unreachable, // stage1 opens stuff in exclusively blocking mode155 error.WouldBlock => unreachable, // stage1 opens stuff in exclusively blocking mode
156 error.NotOpenForWriting => unreachable,
156 error.SystemResources => return .SystemResources,157 error.SystemResources => return .SystemResources,
157 error.OperationAborted => return .OperationAborted,158 error.OperationAborted => return .OperationAborted,
158 error.BrokenPipe => return .BrokenPipe,159 error.BrokenPipe => return .BrokenPipe,
...@@ -611,6 +612,8 @@ export fn stage2_libc_parse(stage1_libc: *Stage2LibCInstallation, libc_file_z: [...@@ -611,6 +612,8 @@ export fn stage2_libc_parse(stage1_libc: *Stage2LibCInstallation, libc_file_z: [
611 error.SystemResources => return .SystemResources,612 error.SystemResources => return .SystemResources,
612 error.OperationAborted => return .OperationAborted,613 error.OperationAborted => return .OperationAborted,
613 error.WouldBlock => unreachable,614 error.WouldBlock => unreachable,
615 error.NotOpenForWriting => unreachable,
616 error.NotOpenForReading => unreachable,
614 error.Unexpected => return .Unexpected,617 error.Unexpected => return .Unexpected,
615 error.EndOfStream => return .EndOfFile,618 error.EndOfStream => return .EndOfFile,
616 error.IsDir => return .IsDir,619 error.IsDir => return .IsDir,
...@@ -666,6 +669,7 @@ export fn stage2_libc_render(stage1_libc: *Stage2LibCInstallation, output_file:...@@ -666,6 +669,7 @@ export fn stage2_libc_render(stage1_libc: *Stage2LibCInstallation, output_file:
666 const c_out_stream = std.io.cOutStream(output_file);669 const c_out_stream = std.io.cOutStream(output_file);
667 libc.render(c_out_stream) catch |err| switch (err) {670 libc.render(c_out_stream) catch |err| switch (err) {
668 error.WouldBlock => unreachable, // stage1 opens stuff in exclusively blocking mode671 error.WouldBlock => unreachable, // stage1 opens stuff in exclusively blocking mode
672 error.NotOpenForWriting => unreachable,
669 error.SystemResources => return .SystemResources,673 error.SystemResources => return .SystemResources,
670 error.OperationAborted => return .OperationAborted,674 error.OperationAborted => return .OperationAborted,
671 error.BrokenPipe => return .BrokenPipe,675 error.BrokenPipe => return .BrokenPipe,