authorgravatar for git@l4.pmLuna <git@l4.pm> 2020-06-28 17:38:24-03:00
committergravatar for git@l4.pmLuna <git@l4.pm> 2020-06-28 18:40:43-03:00
logf47c6d3c6b5325ae682e50950765d352b2929126
treedc878c3eafb5cabf86620a4619547006883f7707
parent374e3e42e0de10d21406c077599cfc4a6a813497

std.os: make EBADF return error for read and write


5 files changed, 19 insertions(+), 9 deletions(-)

lib/std/elf.zig+1
......@@ -551,6 +551,7 @@ fn preadNoEof(file: std.fs.File, buf: []u8, offset: u64) !void {
551551 error.InputOutput => return error.FileSystem,
552552 error.Unexpected => return error.Unexpected,
553553 error.WouldBlock => return error.Unexpected,
554 error.NotOpenForReading => return error.Unexpected,
554555 };
555556 if (len == 0) return error.UnexpectedEndOfFile;
556557 i += len;
lib/std/os.zig+11-9
......@@ -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.
......@@ -359,7 +360,7 @@ pub fn read(fd: fd_t, buf: []u8) ReadError!usize {
359360 } else {
360361 return error.WouldBlock;
361362 },
362 EBADF => unreachable, // Always a race condition.
363 EBADF => return error.NotOpenForReading, // Can be a race condition.
363364 EIO => return error.InputOutput,
364365 EISDIR => return error.IsDir,
365366 ENOBUFS => return error.SystemResources,
......@@ -420,7 +421,7 @@ pub fn readv(fd: fd_t, iov: []const iovec) ReadError!usize {
420421 } else {
421422 return error.WouldBlock;
422423 },
423 EBADF => unreachable, // always a race condition
424 EBADF => return error.NotOpenForReading, // can be a race condition
424425 EIO => return error.InputOutput,
425426 EISDIR => return error.IsDir,
426427 ENOBUFS => return error.SystemResources,
......@@ -483,7 +484,7 @@ pub fn pread(fd: fd_t, buf: []u8, offset: u64) PReadError!usize {
483484 } else {
484485 return error.WouldBlock;
485486 },
486 EBADF => unreachable, // Always a race condition.
487 EBADF => return error.NotOpenForReading, // Can be a race condition.
487488 EIO => return error.InputOutput,
488489 EISDIR => return error.IsDir,
489490 ENOBUFS => return error.SystemResources,
......@@ -623,7 +624,7 @@ pub fn preadv(fd: fd_t, iov: []const iovec, offset: u64) PReadError!usize {
623624 } else {
624625 return error.WouldBlock;
625626 },
626 EBADF => unreachable, // always a race condition
627 EBADF => return error.NotOpenForReading, // can be a race condition
627628 EIO => return error.InputOutput,
628629 EISDIR => return error.IsDir,
629630 ENOBUFS => return error.SystemResources,
......@@ -645,6 +646,7 @@ pub const WriteError = error{
645646 BrokenPipe,
646647 SystemResources,
647648 OperationAborted,
649 NotOpenForWriting,
648650
649651 /// This error occurs when no global event loop is configured,
650652 /// and reading from the file descriptor would block.
......@@ -720,7 +722,7 @@ pub fn write(fd: fd_t, bytes: []const u8) WriteError!usize {
720722 } else {
721723 return error.WouldBlock;
722724 },
723 EBADF => unreachable, // Always a race condition.
725 EBADF => return error.NotOpenForWriting, // can be a race condition.
724726 EDESTADDRREQ => unreachable, // `connect` was never called.
725727 EDQUOT => return error.DiskQuota,
726728 EFBIG => return error.FileTooBig,
......@@ -792,7 +794,7 @@ pub fn writev(fd: fd_t, iov: []const iovec_const) WriteError!usize {
792794 } else {
793795 return error.WouldBlock;
794796 },
795 EBADF => unreachable, // Always a race condition.
797 EBADF => return error.NotOpenForWriting, // Can be a race condition.
796798 EDESTADDRREQ => unreachable, // `connect` was never called.
797799 EDQUOT => return error.DiskQuota,
798800 EFBIG => return error.FileTooBig,
......@@ -880,7 +882,7 @@ pub fn pwrite(fd: fd_t, bytes: []const u8, offset: u64) PWriteError!usize {
880882 } else {
881883 return error.WouldBlock;
882884 },
883 EBADF => unreachable, // Always a race condition.
885 EBADF => return error.NotOpenForWriting, // Can be a race condition.
884886 EDESTADDRREQ => unreachable, // `connect` was never called.
885887 EDQUOT => return error.DiskQuota,
886888 EFBIG => return error.FileTooBig,
......@@ -967,7 +969,7 @@ pub fn pwritev(fd: fd_t, iov: []const iovec_const, offset: u64) PWriteError!usiz
967969 } else {
968970 return error.WouldBlock;
969971 },
970 EBADF => unreachable, // Always a race condition.
972 EBADF => return error.NotOpenForWriting, // Can be a race condition.
971973 EDESTADDRREQ => unreachable, // `connect` was never called.
972974 EDQUOT => return error.DiskQuota,
973975 EFBIG => return error.FileTooBig,
......@@ -1162,7 +1164,7 @@ pub fn dup2(old_fd: fd_t, new_fd: fd_t) !void {
11621164 EBUSY, EINTR => continue,
11631165 EMFILE => return error.ProcessFdQuotaExceeded,
11641166 EINVAL => unreachable, // invalid parameters passed to dup2
1165 EBADF => unreachable, // always a race condition
1167 EBADF => unreachable, // invalid file descriptor
11661168 else => |err| return unexpectedErrno(err),
11671169 }
11681170 }
lib/std/zig/system.zig+1
......@@ -851,6 +851,7 @@ pub const NativeTargetInfo = struct {
851851 const len = file.pread(buf[i .. buf.len - i], offset + i) catch |err| switch (err) {
852852 error.OperationAborted => unreachable, // Windows-only
853853 error.WouldBlock => unreachable, // Did not request blocking mode
854 error.NotOpenForReading => unreachable,
854855 error.SystemResources => return error.SystemResources,
855856 error.IsDir => return error.UnableToReadElfFile,
856857 error.BrokenPipe => return error.UnableToReadElfFile,
src-self-hosted/main.zig+2
......@@ -696,6 +696,7 @@ const FmtError = error{
696696 LinkQuotaExceeded,
697697 FileBusy,
698698 EndOfStream,
699 NotOpenForWriting,
699700} || fs.File.OpenError;
700701
701702fn 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(
761762 const source_code = source_file.readAllAlloc(fmt.gpa, stat.size, max_src_size) catch |err| switch (err) {
762763 error.ConnectionResetByPeer => unreachable,
763764 error.ConnectionTimedOut => unreachable,
765 error.NotOpenForReading => unreachable,
764766 else => |e| return e,
765767 };
766768 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,
......@@ -585,6 +586,8 @@ export fn stage2_libc_parse(stage1_libc: *Stage2LibCInstallation, libc_file_z: [
585586 error.SystemResources => return .SystemResources,
586587 error.OperationAborted => return .OperationAborted,
587588 error.WouldBlock => unreachable,
589 error.NotOpenForWriting => unreachable,
590 error.NotOpenForReading => unreachable,
588591 error.Unexpected => return .Unexpected,
589592 error.EndOfStream => return .EndOfFile,
590593 error.IsDir => return .IsDir,
......@@ -640,6 +643,7 @@ export fn stage2_libc_render(stage1_libc: *Stage2LibCInstallation, output_file:
640643 const c_out_stream = std.io.cOutStream(output_file);
641644 libc.render(c_out_stream) catch |err| switch (err) {
642645 error.WouldBlock => unreachable, // stage1 opens stuff in exclusively blocking mode
646 error.NotOpenForWriting => unreachable,
643647 error.SystemResources => return .SystemResources,
644648 error.OperationAborted => return .OperationAborted,
645649 error.BrokenPipe => return .BrokenPipe,