Skip to content

Commit

Permalink
fix std.io.InStream for windows
Browse files Browse the repository at this point in the history
now we handle PIPE_BROKEN as an EOF

also set up framework for debugging unexpected posix/windows errors
  • Loading branch information
andrewrk committed Oct 15, 2017
1 parent bb169a7 commit fcef7c4
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 58 deletions.
1 change: 1 addition & 0 deletions std/debug.zig
Expand Up @@ -11,6 +11,7 @@ error MissingDebugInfo;
error InvalidDebugInfo;
error UnsupportedDebugInfo;


pub fn assert(ok: bool) {
if (!ok) {
// In ReleaseFast test mode, we still want assert(false) to crash, so
Expand Down
33 changes: 17 additions & 16 deletions std/io.zig
Expand Up @@ -45,17 +45,12 @@ pub var stderr = OutStream {
/// bug in the program that called the function.
error Invalid;

/// When an Unexpected error occurs, code that emitted the error likely needs
/// a patch to recognize the unexpected case so that it can handle it and emit
/// a more specific error.
error Unexpected;

error DiskQuota;
error FileTooBig;
error Io;
error NoSpaceLeft;
error BadPerm;
error PipeFail;
error BrokenPipe;
error BadFd;
error IsDir;
error NotDir;
Expand Down Expand Up @@ -207,7 +202,10 @@ pub const OutStream = struct {
if (self.handle) |handle| return handle;
if (system.GetStdHandle(self.handle_id)) |handle| {
if (handle == system.INVALID_HANDLE_VALUE) {
return error.Unexpected;
const err = system.GetLastError();
return switch (err) {
else => os.unexpectedErrorWindows(err),
};
}
self.handle = handle;
return handle;
Expand Down Expand Up @@ -292,7 +290,7 @@ pub const InStream = struct {
system.EFAULT => unreachable,
system.EBADF => return error.BadFd,
system.EIO => return error.Io,
else => return error.Unexpected,
else => return os.unexpectedErrorPosix(read_err),
}
}
if (amt_read == 0) return index;
Expand All @@ -309,12 +307,12 @@ pub const InStream = struct {
const err = system.GetLastError();
return switch (err) {
system.ERROR.OPERATION_ABORTED => continue,
system.ERROR.BROKEN_PIPE => error.PipeFail,
else => error.Unexpected,
system.ERROR.BROKEN_PIPE => return index,
else => os.unexpectedErrorWindows(err),
};
}
if (amt_read == 0) return index;
index += amt_read;
if (amt_read < want_read_count) return index;
}
return index;
} else {
Expand Down Expand Up @@ -374,7 +372,7 @@ pub const InStream = struct {
system.EOVERFLOW => error.Unseekable,
system.ESPIPE => error.Unseekable,
system.ENXIO => error.Unseekable,
else => error.Unexpected,
else => os.unexpectedErrorPosix(err),
};
}
},
Expand All @@ -394,7 +392,7 @@ pub const InStream = struct {
system.EOVERFLOW => error.Unseekable,
system.ESPIPE => error.Unseekable,
system.ENXIO => error.Unseekable,
else => error.Unexpected,
else => os.unexpectedErrorPosix(err),
};
}
},
Expand All @@ -414,7 +412,7 @@ pub const InStream = struct {
system.EOVERFLOW => error.Unseekable,
system.ESPIPE => error.Unseekable,
system.ENXIO => error.Unseekable,
else => error.Unexpected,
else => os.unexpectedErrorPosix(err),
};
}
return result;
Expand All @@ -430,7 +428,7 @@ pub const InStream = struct {
return switch (err) {
system.EBADF => error.BadFd,
system.ENOMEM => error.OutOfMemory,
else => error.Unexpected,
else => os.unexpectedErrorPosix(err),
}
}

Expand Down Expand Up @@ -485,7 +483,10 @@ pub const InStream = struct {
if (self.handle) |handle| return handle;
if (system.GetStdHandle(self.handle_id)) |handle| {
if (handle == system.INVALID_HANDLE_VALUE) {
return error.Unexpected;
const err = system.GetLastError();
return switch (err) {
else => os.unexpectedErrorWindows(err),
};
}
self.handle = handle;
return handle;
Expand Down
1 change: 0 additions & 1 deletion std/net.zig
Expand Up @@ -3,7 +3,6 @@ const assert = @import("debug.zig").assert;
const endian = @import("endian.zig");

error SigInterrupt;
error Unexpected;
error Io;
error TimedOut;
error ConnectionReset;
Expand Down

0 comments on commit fcef7c4

Please sign in to comment.