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

map EBADF to error values for read and write
This commit is contained in:
Andrew Kelley
2020-08-19 23:46:22 -04:00
committed by GitHub
5 changed files with 27 additions and 17 deletions

View File

@@ -558,6 +558,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;

View File

@@ -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,
@@ -1251,7 +1253,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),
}
}

View File

@@ -857,6 +857,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,

View File

@@ -742,6 +742,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 {
@@ -807,6 +808,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();

View File

@@ -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,
@@ -611,6 +612,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,
@@ -666,6 +669,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,