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 {
558558 error.InputOutput => return error.FileSystem,
559559 error.Unexpected => return error.Unexpected,
560560 error.WouldBlock => return error.Unexpected,
561 error.NotOpenForReading => return error.Unexpected,
561562 error.AccessDenied => return error.Unexpected,
562563 };
563564 if (len == 0) return error.UnexpectedEndOfFile;
lib/std/os.zig+19-17
......@@ -296,6 +296,7 @@ pub const ReadError = error{
296296 BrokenPipe,
297297 ConnectionResetByPeer,
298298 ConnectionTimedOut,
299 NotOpenForReading,
299300
300301 /// This error occurs when no global event loop is configured,
301302 /// and reading from the file descriptor would block.
......@@ -332,7 +333,7 @@ pub fn read(fd: fd_t, buf: []u8) ReadError!usize {
332333 wasi.EINVAL => unreachable,
333334 wasi.EFAULT => unreachable,
334335 wasi.EAGAIN => unreachable,
335 wasi.EBADF => unreachable, // Always a race condition.
336 wasi.EBADF => return error.NotOpenForReading, // Can be a race condition.
336337 wasi.EIO => return error.InputOutput,
337338 wasi.EISDIR => return error.IsDir,
338339 wasi.ENOBUFS => return error.SystemResources,
......@@ -364,7 +365,7 @@ pub fn read(fd: fd_t, buf: []u8) ReadError!usize {
364365 } else {
365366 return error.WouldBlock;
366367 },
367 EBADF => unreachable, // Always a race condition.
368 EBADF => return error.NotOpenForReading, // Can be a race condition.
368369 EIO => return error.InputOutput,
369370 EISDIR => return error.IsDir,
370371 ENOBUFS => return error.SystemResources,
......@@ -402,7 +403,7 @@ pub fn readv(fd: fd_t, iov: []const iovec) ReadError!usize {
402403 wasi.EINVAL => unreachable,
403404 wasi.EFAULT => unreachable,
404405 wasi.EAGAIN => unreachable, // currently not support in WASI
405 wasi.EBADF => unreachable, // always a race condition
406 wasi.EBADF => return error.NotOpenForReading, // can be a race condition
406407 wasi.EIO => return error.InputOutput,
407408 wasi.EISDIR => return error.IsDir,
408409 wasi.ENOBUFS => return error.SystemResources,
......@@ -426,7 +427,7 @@ pub fn readv(fd: fd_t, iov: []const iovec) ReadError!usize {
426427 } else {
427428 return error.WouldBlock;
428429 },
429 EBADF => unreachable, // always a race condition
430 EBADF => return error.NotOpenForReading, // can be a race condition
430431 EIO => return error.InputOutput,
431432 EISDIR => return error.IsDir,
432433 ENOBUFS => return error.SystemResources,
......@@ -463,7 +464,7 @@ pub fn pread(fd: fd_t, buf: []u8, offset: u64) PReadError!usize {
463464 wasi.EINVAL => unreachable,
464465 wasi.EFAULT => unreachable,
465466 wasi.EAGAIN => unreachable,
466 wasi.EBADF => unreachable, // Always a race condition.
467 wasi.EBADF => return error.NotOpenForReading, // Can be a race condition.
467468 wasi.EIO => return error.InputOutput,
468469 wasi.EISDIR => return error.IsDir,
469470 wasi.ENOBUFS => return error.SystemResources,
......@@ -490,7 +491,7 @@ pub fn pread(fd: fd_t, buf: []u8, offset: u64) PReadError!usize {
490491 } else {
491492 return error.WouldBlock;
492493 },
493 EBADF => unreachable, // Always a race condition.
494 EBADF => return error.NotOpenForReading, // Can be a race condition.
494495 EIO => return error.InputOutput,
495496 EISDIR => return error.IsDir,
496497 ENOBUFS => return error.SystemResources,
......@@ -607,7 +608,7 @@ pub fn preadv(fd: fd_t, iov: []const iovec, offset: u64) PReadError!usize {
607608 wasi.EINVAL => unreachable,
608609 wasi.EFAULT => unreachable,
609610 wasi.EAGAIN => unreachable,
610 wasi.EBADF => unreachable, // always a race condition
611 wasi.EBADF => return error.NotOpenForReading, // can be a race condition
611612 wasi.EIO => return error.InputOutput,
612613 wasi.EISDIR => return error.IsDir,
613614 wasi.ENOBUFS => return error.SystemResources,
......@@ -635,7 +636,7 @@ pub fn preadv(fd: fd_t, iov: []const iovec, offset: u64) PReadError!usize {
635636 } else {
636637 return error.WouldBlock;
637638 },
638 EBADF => unreachable, // always a race condition
639 EBADF => return error.NotOpenForReading, // can be a race condition
639640 EIO => return error.InputOutput,
640641 EISDIR => return error.IsDir,
641642 ENOBUFS => return error.SystemResources,
......@@ -660,6 +661,7 @@ pub const WriteError = error{
660661 BrokenPipe,
661662 SystemResources,
662663 OperationAborted,
664 NotOpenForWriting,
663665
664666 /// This error occurs when no global event loop is configured,
665667 /// and reading from the file descriptor would block.
......@@ -704,7 +706,7 @@ pub fn write(fd: fd_t, bytes: []const u8) WriteError!usize {
704706 wasi.EINVAL => unreachable,
705707 wasi.EFAULT => unreachable,
706708 wasi.EAGAIN => unreachable,
707 wasi.EBADF => unreachable, // Always a race condition.
709 wasi.EBADF => return error.NotOpenForWriting, // can be a race condition.
708710 wasi.EDESTADDRREQ => unreachable, // `connect` was never called.
709711 wasi.EDQUOT => return error.DiskQuota,
710712 wasi.EFBIG => return error.FileTooBig,
......@@ -736,7 +738,7 @@ pub fn write(fd: fd_t, bytes: []const u8) WriteError!usize {
736738 } else {
737739 return error.WouldBlock;
738740 },
739 EBADF => unreachable, // Always a race condition.
741 EBADF => return error.NotOpenForWriting, // can be a race condition.
740742 EDESTADDRREQ => unreachable, // `connect` was never called.
741743 EDQUOT => return error.DiskQuota,
742744 EFBIG => return error.FileTooBig,
......@@ -782,7 +784,7 @@ pub fn writev(fd: fd_t, iov: []const iovec_const) WriteError!usize {
782784 wasi.EINVAL => unreachable,
783785 wasi.EFAULT => unreachable,
784786 wasi.EAGAIN => unreachable,
785 wasi.EBADF => unreachable, // Always a race condition.
787 wasi.EBADF => return error.NotOpenForWriting, // can be a race condition.
786788 wasi.EDESTADDRREQ => unreachable, // `connect` was never called.
787789 wasi.EDQUOT => return error.DiskQuota,
788790 wasi.EFBIG => return error.FileTooBig,
......@@ -809,7 +811,7 @@ pub fn writev(fd: fd_t, iov: []const iovec_const) WriteError!usize {
809811 } else {
810812 return error.WouldBlock;
811813 },
812 EBADF => unreachable, // Always a race condition.
814 EBADF => return error.NotOpenForWriting, // Can be a race condition.
813815 EDESTADDRREQ => unreachable, // `connect` was never called.
814816 EDQUOT => return error.DiskQuota,
815817 EFBIG => return error.FileTooBig,
......@@ -862,7 +864,7 @@ pub fn pwrite(fd: fd_t, bytes: []const u8, offset: u64) PWriteError!usize {
862864 wasi.EINVAL => unreachable,
863865 wasi.EFAULT => unreachable,
864866 wasi.EAGAIN => unreachable,
865 wasi.EBADF => unreachable, // Always a race condition.
867 wasi.EBADF => return error.NotOpenForWriting, // can be a race condition.
866868 wasi.EDESTADDRREQ => unreachable, // `connect` was never called.
867869 wasi.EDQUOT => return error.DiskQuota,
868870 wasi.EFBIG => return error.FileTooBig,
......@@ -898,7 +900,7 @@ pub fn pwrite(fd: fd_t, bytes: []const u8, offset: u64) PWriteError!usize {
898900 } else {
899901 return error.WouldBlock;
900902 },
901 EBADF => unreachable, // Always a race condition.
903 EBADF => return error.NotOpenForWriting, // Can be a race condition.
902904 EDESTADDRREQ => unreachable, // `connect` was never called.
903905 EDQUOT => return error.DiskQuota,
904906 EFBIG => return error.FileTooBig,
......@@ -956,7 +958,7 @@ pub fn pwritev(fd: fd_t, iov: []const iovec_const, offset: u64) PWriteError!usiz
956958 wasi.EINVAL => unreachable,
957959 wasi.EFAULT => unreachable,
958960 wasi.EAGAIN => unreachable,
959 wasi.EBADF => unreachable, // Always a race condition.
961 wasi.EBADF => return error.NotOpenForWriting, // Can be a race condition.
960962 wasi.EDESTADDRREQ => unreachable, // `connect` was never called.
961963 wasi.EDQUOT => return error.DiskQuota,
962964 wasi.EFBIG => return error.FileTooBig,
......@@ -986,7 +988,7 @@ pub fn pwritev(fd: fd_t, iov: []const iovec_const, offset: u64) PWriteError!usiz
986988 } else {
987989 return error.WouldBlock;
988990 },
989 EBADF => unreachable, // Always a race condition.
991 EBADF => return error.NotOpenForWriting, // Can be a race condition.
990992 EDESTADDRREQ => unreachable, // `connect` was never called.
991993 EDQUOT => return error.DiskQuota,
992994 EFBIG => return error.FileTooBig,
......@@ -1251,7 +1253,7 @@ pub fn dup2(old_fd: fd_t, new_fd: fd_t) !void {
12511253 EBUSY, EINTR => continue,
12521254 EMFILE => return error.ProcessFdQuotaExceeded,
12531255 EINVAL => unreachable, // invalid parameters passed to dup2
1254 EBADF => unreachable, // always a race condition
1256 EBADF => unreachable, // invalid file descriptor
12551257 else => |err| return unexpectedErrno(err),
12561258 }
12571259 }
lib/std/zig/system.zig+1
......@@ -857,6 +857,7 @@ pub const NativeTargetInfo = struct {
857857 const len = file.pread(buf[i .. buf.len - i], offset + i) catch |err| switch (err) {
858858 error.OperationAborted => unreachable, // Windows-only
859859 error.WouldBlock => unreachable, // Did not request blocking mode
860 error.NotOpenForReading => unreachable,
860861 error.SystemResources => return error.SystemResources,
861862 error.IsDir => return error.UnableToReadElfFile,
862863 error.BrokenPipe => return error.UnableToReadElfFile,
src-self-hosted/main.zig+2
......@@ -742,6 +742,7 @@ const FmtError = error{
742742 LinkQuotaExceeded,
743743 FileBusy,
744744 EndOfStream,
745 NotOpenForWriting,
745746} || fs.File.OpenError;
746747
747748fn 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(
807808 const source_code = source_file.readAllAlloc(fmt.gpa, stat.size, max_src_size) catch |err| switch (err) {
808809 error.ConnectionResetByPeer => unreachable,
809810 error.ConnectionTimedOut => unreachable,
811 error.NotOpenForReading => unreachable,
810812 else => |e| return e,
811813 };
812814 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 {
153153 const c_out_stream = std.io.cOutStream(output_file);
154154 _ = std.zig.render(std.heap.c_allocator, c_out_stream, tree) catch |e| switch (e) {
155155 error.WouldBlock => unreachable, // stage1 opens stuff in exclusively blocking mode
156 error.NotOpenForWriting => unreachable,
156157 error.SystemResources => return .SystemResources,
157158 error.OperationAborted => return .OperationAborted,
158159 error.BrokenPipe => return .BrokenPipe,
......@@ -611,6 +612,8 @@ export fn stage2_libc_parse(stage1_libc: *Stage2LibCInstallation, libc_file_z: [
611612 error.SystemResources => return .SystemResources,
612613 error.OperationAborted => return .OperationAborted,
613614 error.WouldBlock => unreachable,
615 error.NotOpenForWriting => unreachable,
616 error.NotOpenForReading => unreachable,
614617 error.Unexpected => return .Unexpected,
615618 error.EndOfStream => return .EndOfFile,
616619 error.IsDir => return .IsDir,
......@@ -666,6 +669,7 @@ export fn stage2_libc_render(stage1_libc: *Stage2LibCInstallation, output_file:
666669 const c_out_stream = std.io.cOutStream(output_file);
667670 libc.render(c_out_stream) catch |err| switch (err) {
668671 error.WouldBlock => unreachable, // stage1 opens stuff in exclusively blocking mode
672 error.NotOpenForWriting => unreachable,
669673 error.SystemResources => return .SystemResources,
670674 error.OperationAborted => return .OperationAborted,
671675 error.BrokenPipe => return .BrokenPipe,