authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-10-15 16:45:43-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-10-15 16:45:43-04:00
logfcef7c4bb2f8705774b9d53c8f81a8e74cbcb630
tree0cc9202dac3d1715d1cd4f02ae899d7a579422a1
parentbb169a7b36a658ed463787655df54e3f59133d98

fix std.io.InStream for windows

now we handle PIPE_BROKEN as an EOF also set up framework for debugging unexpected posix/windows errors

6 files changed, 95 insertions(+), 58 deletions(-)

std/debug.zig+1
...@@ -11,6 +11,7 @@ error MissingDebugInfo;...@@ -11,6 +11,7 @@ error MissingDebugInfo;
11error InvalidDebugInfo;11error InvalidDebugInfo;
12error UnsupportedDebugInfo;12error UnsupportedDebugInfo;
1313
14
14pub fn assert(ok: bool) {15pub fn assert(ok: bool) {
15 if (!ok) {16 if (!ok) {
16 // In ReleaseFast test mode, we still want assert(false) to crash, so17 // In ReleaseFast test mode, we still want assert(false) to crash, so
std/io.zig+17-16
...@@ -45,17 +45,12 @@ pub var stderr = OutStream {...@@ -45,17 +45,12 @@ pub var stderr = OutStream {
45/// bug in the program that called the function.45/// bug in the program that called the function.
46error Invalid;46error Invalid;
4747
48/// When an Unexpected error occurs, code that emitted the error likely needs
49/// a patch to recognize the unexpected case so that it can handle it and emit
50/// a more specific error.
51error Unexpected;
52
53error DiskQuota;48error DiskQuota;
54error FileTooBig;49error FileTooBig;
55error Io;50error Io;
56error NoSpaceLeft;51error NoSpaceLeft;
57error BadPerm;52error BadPerm;
58error PipeFail;53error BrokenPipe;
59error BadFd;54error BadFd;
60error IsDir;55error IsDir;
61error NotDir;56error NotDir;
...@@ -207,7 +202,10 @@ pub const OutStream = struct {...@@ -207,7 +202,10 @@ pub const OutStream = struct {
207 if (self.handle) |handle| return handle;202 if (self.handle) |handle| return handle;
208 if (system.GetStdHandle(self.handle_id)) |handle| {203 if (system.GetStdHandle(self.handle_id)) |handle| {
209 if (handle == system.INVALID_HANDLE_VALUE) {204 if (handle == system.INVALID_HANDLE_VALUE) {
210 return error.Unexpected;205 const err = system.GetLastError();
206 return switch (err) {
207 else => os.unexpectedErrorWindows(err),
208 };
211 }209 }
212 self.handle = handle;210 self.handle = handle;
213 return handle;211 return handle;
...@@ -292,7 +290,7 @@ pub const InStream = struct {...@@ -292,7 +290,7 @@ pub const InStream = struct {
292 system.EFAULT => unreachable,290 system.EFAULT => unreachable,
293 system.EBADF => return error.BadFd,291 system.EBADF => return error.BadFd,
294 system.EIO => return error.Io,292 system.EIO => return error.Io,
295 else => return error.Unexpected,293 else => return os.unexpectedErrorPosix(read_err),
296 }294 }
297 }295 }
298 if (amt_read == 0) return index;296 if (amt_read == 0) return index;
...@@ -309,12 +307,12 @@ pub const InStream = struct {...@@ -309,12 +307,12 @@ pub const InStream = struct {
309 const err = system.GetLastError();307 const err = system.GetLastError();
310 return switch (err) {308 return switch (err) {
311 system.ERROR.OPERATION_ABORTED => continue,309 system.ERROR.OPERATION_ABORTED => continue,
312 system.ERROR.BROKEN_PIPE => error.PipeFail,310 system.ERROR.BROKEN_PIPE => return index,
313 else => error.Unexpected,311 else => os.unexpectedErrorWindows(err),
314 };312 };
315 }313 }
314 if (amt_read == 0) return index;
316 index += amt_read;315 index += amt_read;
317 if (amt_read < want_read_count) return index;
318 }316 }
319 return index;317 return index;
320 } else {318 } else {
...@@ -374,7 +372,7 @@ pub const InStream = struct {...@@ -374,7 +372,7 @@ pub const InStream = struct {
374 system.EOVERFLOW => error.Unseekable,372 system.EOVERFLOW => error.Unseekable,
375 system.ESPIPE => error.Unseekable,373 system.ESPIPE => error.Unseekable,
376 system.ENXIO => error.Unseekable,374 system.ENXIO => error.Unseekable,
377 else => error.Unexpected,375 else => os.unexpectedErrorPosix(err),
378 };376 };
379 }377 }
380 },378 },
...@@ -394,7 +392,7 @@ pub const InStream = struct {...@@ -394,7 +392,7 @@ pub const InStream = struct {
394 system.EOVERFLOW => error.Unseekable,392 system.EOVERFLOW => error.Unseekable,
395 system.ESPIPE => error.Unseekable,393 system.ESPIPE => error.Unseekable,
396 system.ENXIO => error.Unseekable,394 system.ENXIO => error.Unseekable,
397 else => error.Unexpected,395 else => os.unexpectedErrorPosix(err),
398 };396 };
399 }397 }
400 },398 },
...@@ -414,7 +412,7 @@ pub const InStream = struct {...@@ -414,7 +412,7 @@ pub const InStream = struct {
414 system.EOVERFLOW => error.Unseekable,412 system.EOVERFLOW => error.Unseekable,
415 system.ESPIPE => error.Unseekable,413 system.ESPIPE => error.Unseekable,
416 system.ENXIO => error.Unseekable,414 system.ENXIO => error.Unseekable,
417 else => error.Unexpected,415 else => os.unexpectedErrorPosix(err),
418 };416 };
419 }417 }
420 return result;418 return result;
...@@ -430,7 +428,7 @@ pub const InStream = struct {...@@ -430,7 +428,7 @@ pub const InStream = struct {
430 return switch (err) {428 return switch (err) {
431 system.EBADF => error.BadFd,429 system.EBADF => error.BadFd,
432 system.ENOMEM => error.OutOfMemory,430 system.ENOMEM => error.OutOfMemory,
433 else => error.Unexpected,431 else => os.unexpectedErrorPosix(err),
434 }432 }
435 }433 }
436434
...@@ -485,7 +483,10 @@ pub const InStream = struct {...@@ -485,7 +483,10 @@ pub const InStream = struct {
485 if (self.handle) |handle| return handle;483 if (self.handle) |handle| return handle;
486 if (system.GetStdHandle(self.handle_id)) |handle| {484 if (system.GetStdHandle(self.handle_id)) |handle| {
487 if (handle == system.INVALID_HANDLE_VALUE) {485 if (handle == system.INVALID_HANDLE_VALUE) {
488 return error.Unexpected;486 const err = system.GetLastError();
487 return switch (err) {
488 else => os.unexpectedErrorWindows(err),
489 };
489 }490 }
490 self.handle = handle;491 self.handle = handle;
491 return handle;492 return handle;
std/net.zig-1
...@@ -3,7 +3,6 @@ const assert = @import("debug.zig").assert;...@@ -3,7 +3,6 @@ const assert = @import("debug.zig").assert;
3const endian = @import("endian.zig");3const endian = @import("endian.zig");
44
5error SigInterrupt;5error SigInterrupt;
6error Unexpected;
7error Io;6error Io;
8error TimedOut;7error TimedOut;
9error ConnectionReset;8error ConnectionReset;
std/os/index.zig+64-32
...@@ -51,7 +51,6 @@ const ArrayList = @import("../array_list.zig").ArrayList;...@@ -51,7 +51,6 @@ const ArrayList = @import("../array_list.zig").ArrayList;
51const Buffer = @import("../buffer.zig").Buffer;51const Buffer = @import("../buffer.zig").Buffer;
52const math = @import("../index.zig").math;52const math = @import("../index.zig").math;
5353
54error Unexpected;
55error SystemResources;54error SystemResources;
56error AccessDenied;55error AccessDenied;
57error InvalidExe;56error InvalidExe;
...@@ -81,7 +80,7 @@ pub fn getRandomBytes(buf: []u8) -> %void {...@@ -81,7 +80,7 @@ pub fn getRandomBytes(buf: []u8) -> %void {
81 posix.EINVAL => unreachable,80 posix.EINVAL => unreachable,
82 posix.EFAULT => unreachable,81 posix.EFAULT => unreachable,
83 posix.EINTR => continue,82 posix.EINTR => continue,
84 else => error.Unexpected,83 else => unexpectedErrorPosix(err),
85 }84 }
86 }85 }
87 return;86 return;
...@@ -96,12 +95,18 @@ pub fn getRandomBytes(buf: []u8) -> %void {...@@ -96,12 +95,18 @@ pub fn getRandomBytes(buf: []u8) -> %void {
96 Os.windows => {95 Os.windows => {
97 var hCryptProv: windows.HCRYPTPROV = undefined;96 var hCryptProv: windows.HCRYPTPROV = undefined;
98 if (windows.CryptAcquireContextA(&hCryptProv, null, null, windows.PROV_RSA_FULL, 0) == 0) {97 if (windows.CryptAcquireContextA(&hCryptProv, null, null, windows.PROV_RSA_FULL, 0) == 0) {
99 return error.Unexpected;98 const err = windows.GetLastError();
99 return switch (err) {
100 else => unexpectedErrorWindows(err),
101 };
100 }102 }
101 defer _ = windows.CryptReleaseContext(hCryptProv, 0);103 defer _ = windows.CryptReleaseContext(hCryptProv, 0);
102104
103 if (windows.CryptGenRandom(hCryptProv, windows.DWORD(buf.len), buf.ptr) == 0) {105 if (windows.CryptGenRandom(hCryptProv, windows.DWORD(buf.len), buf.ptr) == 0) {
104 return error.Unexpected;106 const err = windows.GetLastError();
107 return switch (err) {
108 else => unexpectedErrorWindows(err),
109 };
105 }110 }
106 },111 },
107 else => @compileError("Unsupported OS"),112 else => @compileError("Unsupported OS"),
...@@ -187,8 +192,8 @@ pub fn posixRead(fd: i32, buf: []u8) -> %void {...@@ -187,8 +192,8 @@ pub fn posixRead(fd: i32, buf: []u8) -> %void {
187 posix.EIO => error.InputOutput,192 posix.EIO => error.InputOutput,
188 posix.EISDIR => error.IsDir,193 posix.EISDIR => error.IsDir,
189 posix.ENOBUFS, posix.ENOMEM => error.SystemResources,194 posix.ENOBUFS, posix.ENOMEM => error.SystemResources,
190 else => return error.Unexpected,195 else => unexpectedErrorPosix(err),
191 }196 };
192 }197 }
193 index += amt_written;198 index += amt_written;
194 }199 }
...@@ -202,7 +207,6 @@ error FileTooBig;...@@ -202,7 +207,6 @@ error FileTooBig;
202error InputOutput;207error InputOutput;
203error NoSpaceLeft;208error NoSpaceLeft;
204error BrokenPipe;209error BrokenPipe;
205error Unexpected;
206210
207/// Calls POSIX write, and keeps trying if it gets interrupted.211/// Calls POSIX write, and keeps trying if it gets interrupted.
208pub fn posixWrite(fd: i32, bytes: []const u8) -> %void {212pub fn posixWrite(fd: i32, bytes: []const u8) -> %void {
...@@ -222,8 +226,8 @@ pub fn posixWrite(fd: i32, bytes: []const u8) -> %void {...@@ -222,8 +226,8 @@ pub fn posixWrite(fd: i32, bytes: []const u8) -> %void {
222 posix.ENOSPC => error.NoSpaceLeft,226 posix.ENOSPC => error.NoSpaceLeft,
223 posix.EPERM => error.AccessDenied,227 posix.EPERM => error.AccessDenied,
224 posix.EPIPE => error.BrokenPipe,228 posix.EPIPE => error.BrokenPipe,
225 else => error.Unexpected,229 else => unexpectedErrorPosix(write_err),
226 }230 };
227 }231 }
228 return;232 return;
229 }233 }
...@@ -277,7 +281,7 @@ pub fn posixOpen(file_path: []const u8, flags: u32, perm: usize, allocator: ?&Al...@@ -277,7 +281,7 @@ pub fn posixOpen(file_path: []const u8, flags: u32, perm: usize, allocator: ?&Al
277 posix.ENOTDIR => error.NotDir,281 posix.ENOTDIR => error.NotDir,
278 posix.EPERM => error.AccessDenied,282 posix.EPERM => error.AccessDenied,
279 posix.EEXIST => error.PathAlreadyExists,283 posix.EEXIST => error.PathAlreadyExists,
280 else => error.Unexpected,284 else => unexpectedErrorPosix(err),
281 }285 }
282 }286 }
283 return i32(result);287 return i32(result);
...@@ -292,7 +296,7 @@ pub fn posixDup2(old_fd: i32, new_fd: i32) -> %void {...@@ -292,7 +296,7 @@ pub fn posixDup2(old_fd: i32, new_fd: i32) -> %void {
292 posix.EBUSY, posix.EINTR => continue,296 posix.EBUSY, posix.EINTR => continue,
293 posix.EMFILE => error.ProcessFdQuotaExceeded,297 posix.EMFILE => error.ProcessFdQuotaExceeded,
294 posix.EINVAL => unreachable,298 posix.EINVAL => unreachable,
295 else => error.Unexpected,299 else => unexpectedErrorPosix(err),
296 };300 };
297 }301 }
298 return;302 return;
...@@ -404,7 +408,7 @@ fn posixExecveErrnoToErr(err: usize) -> error {...@@ -404,7 +408,7 @@ fn posixExecveErrnoToErr(err: usize) -> error {
404 posix.ENOENT => error.FileNotFound,408 posix.ENOENT => error.FileNotFound,
405 posix.ENOTDIR => error.NotDir,409 posix.ENOTDIR => error.NotDir,
406 posix.ETXTBSY => error.FileBusy,410 posix.ETXTBSY => error.FileBusy,
407 else => error.Unexpected,411 else => unexpectedErrorPosix(err),
408 };412 };
409}413}
410414
...@@ -491,7 +495,7 @@ pub fn getEnvVarOwned(allocator: &mem.Allocator, key: []const u8) -> %[]u8 {...@@ -491,7 +495,7 @@ pub fn getEnvVarOwned(allocator: &mem.Allocator, key: []const u8) -> %[]u8 {
491 const err = windows.GetLastError();495 const err = windows.GetLastError();
492 return switch (err) {496 return switch (err) {
493 windows.ERROR.ENVVAR_NOT_FOUND => error.EnvironmentVariableNotFound,497 windows.ERROR.ENVVAR_NOT_FOUND => error.EnvironmentVariableNotFound,
494 else => error.Unexpected,498 else => unexpectedErrorWindows(err),
495 };499 };
496 }500 }
497501
...@@ -519,7 +523,10 @@ pub fn getCwd(allocator: &Allocator) -> %[]u8 {...@@ -519,7 +523,10 @@ pub fn getCwd(allocator: &Allocator) -> %[]u8 {
519 const result = windows.GetCurrentDirectoryA(windows.WORD(buf.len), buf.ptr);523 const result = windows.GetCurrentDirectoryA(windows.WORD(buf.len), buf.ptr);
520524
521 if (result == 0) {525 if (result == 0) {
522 return error.Unexpected;526 const err = windows.GetLastError();
527 return switch (err) {
528 else => unexpectedErrorWindows(err),
529 };
523 }530 }
524531
525 if (result > buf.len) {532 if (result > buf.len) {
...@@ -539,7 +546,7 @@ pub fn getCwd(allocator: &Allocator) -> %[]u8 {...@@ -539,7 +546,7 @@ pub fn getCwd(allocator: &Allocator) -> %[]u8 {
539 buf = %return allocator.realloc(u8, buf, buf.len * 2);546 buf = %return allocator.realloc(u8, buf, buf.len * 2);
540 continue;547 continue;
541 } else if (err > 0) {548 } else if (err > 0) {
542 return error.Unexpected;549 return unexpectedErrorPosix(err);
543 }550 }
544551
545 return allocator.shrink(u8, buf, cstr.len(buf.ptr));552 return allocator.shrink(u8, buf, cstr.len(buf.ptr));
...@@ -570,7 +577,7 @@ pub fn symLinkWindows(allocator: &Allocator, existing_path: []const u8, new_path...@@ -570,7 +577,7 @@ pub fn symLinkWindows(allocator: &Allocator, existing_path: []const u8, new_path
570 if (windows.CreateSymbolicLinkA(existing_with_null.ptr, new_with_null.ptr, 0) == 0) {577 if (windows.CreateSymbolicLinkA(existing_with_null.ptr, new_with_null.ptr, 0) == 0) {
571 const err = windows.GetLastError();578 const err = windows.GetLastError();
572 return switch (err) {579 return switch (err) {
573 else => error.Unexpected,580 else => unexpectedErrorWindows(err),
574 };581 };
575 }582 }
576}583}
...@@ -602,7 +609,7 @@ pub fn symLinkPosix(allocator: &Allocator, existing_path: []const u8, new_path:...@@ -602,7 +609,7 @@ pub fn symLinkPosix(allocator: &Allocator, existing_path: []const u8, new_path:
602 posix.ENOMEM => error.SystemResources,609 posix.ENOMEM => error.SystemResources,
603 posix.ENOSPC => error.NoSpaceLeft,610 posix.ENOSPC => error.NoSpaceLeft,
604 posix.EROFS => error.ReadOnlyFileSystem,611 posix.EROFS => error.ReadOnlyFileSystem,
605 else => error.Unexpected,612 else => unexpectedErrorPosix(err),
606 };613 };
607 }614 }
608}615}
...@@ -663,7 +670,7 @@ pub fn deleteFileWindows(allocator: &Allocator, file_path: []const u8) -> %void...@@ -663,7 +670,7 @@ pub fn deleteFileWindows(allocator: &Allocator, file_path: []const u8) -> %void
663 windows.ERROR.FILE_NOT_FOUND => error.FileNotFound,670 windows.ERROR.FILE_NOT_FOUND => error.FileNotFound,
664 windows.ERROR.ACCESS_DENIED => error.AccessDenied,671 windows.ERROR.ACCESS_DENIED => error.AccessDenied,
665 windows.ERROR.FILENAME_EXCED_RANGE, windows.ERROR.INVALID_PARAMETER => error.NameTooLong,672 windows.ERROR.FILENAME_EXCED_RANGE, windows.ERROR.INVALID_PARAMETER => error.NameTooLong,
666 else => error.Unexpected,673 else => unexpectedErrorWindows(err),
667 }674 }
668 }675 }
669}676}
...@@ -689,7 +696,7 @@ pub fn deleteFilePosix(allocator: &Allocator, file_path: []const u8) -> %void {...@@ -689,7 +696,7 @@ pub fn deleteFilePosix(allocator: &Allocator, file_path: []const u8) -> %void {
689 posix.ENOTDIR => error.NotDir,696 posix.ENOTDIR => error.NotDir,
690 posix.ENOMEM => error.SystemResources,697 posix.ENOMEM => error.SystemResources,
691 posix.EROFS => error.ReadOnlyFileSystem,698 posix.EROFS => error.ReadOnlyFileSystem,
692 else => error.Unexpected,699 else => unexpectedErrorPosix(err),
693 };700 };
694 }701 }
695}702}
...@@ -743,7 +750,7 @@ pub fn rename(allocator: &Allocator, old_path: []const u8, new_path: []const u8)...@@ -743,7 +750,7 @@ pub fn rename(allocator: &Allocator, old_path: []const u8, new_path: []const u8)
743 if (windows.MoveFileExA(old_buf.ptr, new_buf.ptr, flags) == 0) {750 if (windows.MoveFileExA(old_buf.ptr, new_buf.ptr, flags) == 0) {
744 const err = windows.GetLastError();751 const err = windows.GetLastError();
745 return switch (err) {752 return switch (err) {
746 else => return error.Unexpected,753 else => unexpectedErrorWindows(err),
747 };754 };
748 }755 }
749 } else {756 } else {
...@@ -765,7 +772,7 @@ pub fn rename(allocator: &Allocator, old_path: []const u8, new_path: []const u8)...@@ -765,7 +772,7 @@ pub fn rename(allocator: &Allocator, old_path: []const u8, new_path: []const u8)
765 posix.EEXIST, posix.ENOTEMPTY => error.PathAlreadyExists,772 posix.EEXIST, posix.ENOTEMPTY => error.PathAlreadyExists,
766 posix.EROFS => error.ReadOnlyFileSystem,773 posix.EROFS => error.ReadOnlyFileSystem,
767 posix.EXDEV => error.RenameAcrossMountPoints,774 posix.EXDEV => error.RenameAcrossMountPoints,
768 else => error.Unexpected,775 else => unexpectedErrorPosix(err),
769 };776 };
770 }777 }
771 }778 }
...@@ -788,7 +795,7 @@ pub fn makeDirWindows(allocator: &Allocator, dir_path: []const u8) -> %void {...@@ -788,7 +795,7 @@ pub fn makeDirWindows(allocator: &Allocator, dir_path: []const u8) -> %void {
788 return switch (err) {795 return switch (err) {
789 windows.ERROR.ALREADY_EXISTS => error.PathAlreadyExists,796 windows.ERROR.ALREADY_EXISTS => error.PathAlreadyExists,
790 windows.ERROR.PATH_NOT_FOUND => error.FileNotFound,797 windows.ERROR.PATH_NOT_FOUND => error.FileNotFound,
791 else => error.Unexpected,798 else => unexpectedErrorWindows(err),
792 };799 };
793 }800 }
794}801}
...@@ -812,7 +819,7 @@ pub fn makeDirPosix(allocator: &Allocator, dir_path: []const u8) -> %void {...@@ -812,7 +819,7 @@ pub fn makeDirPosix(allocator: &Allocator, dir_path: []const u8) -> %void {
812 posix.ENOSPC => error.NoSpaceLeft,819 posix.ENOSPC => error.NoSpaceLeft,
813 posix.ENOTDIR => error.NotDir,820 posix.ENOTDIR => error.NotDir,
814 posix.EROFS => error.ReadOnlyFileSystem,821 posix.EROFS => error.ReadOnlyFileSystem,
815 else => error.Unexpected,822 else => unexpectedErrorPosix(err),
816 };823 };
817 }824 }
818}825}
...@@ -877,7 +884,7 @@ pub fn deleteDir(allocator: &Allocator, dir_path: []const u8) -> %void {...@@ -877,7 +884,7 @@ pub fn deleteDir(allocator: &Allocator, dir_path: []const u8) -> %void {
877 posix.ENOTDIR => error.NotDir,884 posix.ENOTDIR => error.NotDir,
878 posix.EEXIST, posix.ENOTEMPTY => error.DirNotEmpty,885 posix.EEXIST, posix.ENOTEMPTY => error.DirNotEmpty,
879 posix.EROFS => error.ReadOnlyFileSystem,886 posix.EROFS => error.ReadOnlyFileSystem,
880 else => error.Unexpected,887 else => unexpectedErrorPosix(err),
881 };888 };
882 }889 }
883}890}
...@@ -988,7 +995,7 @@ pub const Dir = struct {...@@ -988,7 +995,7 @@ pub const Dir = struct {
988 self.buf = %return self.allocator.realloc(u8, self.buf, self.buf.len * 2);995 self.buf = %return self.allocator.realloc(u8, self.buf, self.buf.len * 2);
989 continue;996 continue;
990 },997 },
991 else => return error.Unexpected,998 else => return unexpectedErrorPosix(err),
992 };999 };
993 }1000 }
994 if (result == 0)1001 if (result == 0)
...@@ -1045,7 +1052,7 @@ pub fn changeCurDir(allocator: &Allocator, dir_path: []const u8) -> %void {...@@ -1045,7 +1052,7 @@ pub fn changeCurDir(allocator: &Allocator, dir_path: []const u8) -> %void {
1045 posix.ENOENT => error.FileNotFound,1052 posix.ENOENT => error.FileNotFound,
1046 posix.ENOMEM => error.SystemResources,1053 posix.ENOMEM => error.SystemResources,
1047 posix.ENOTDIR => error.NotDir,1054 posix.ENOTDIR => error.NotDir,
1048 else => error.Unexpected,1055 else => unexpectedErrorPosix(err),
1049 };1056 };
1050 }1057 }
1051}1058}
...@@ -1073,7 +1080,7 @@ pub fn readLink(allocator: &Allocator, pathname: []const u8) -> %[]u8 {...@@ -1073,7 +1080,7 @@ pub fn readLink(allocator: &Allocator, pathname: []const u8) -> %[]u8 {
1073 posix.ENOENT => error.FileNotFound,1080 posix.ENOENT => error.FileNotFound,
1074 posix.ENOMEM => error.SystemResources,1081 posix.ENOMEM => error.SystemResources,
1075 posix.ENOTDIR => error.NotDir,1082 posix.ENOTDIR => error.NotDir,
1076 else => error.Unexpected,1083 else => unexpectedErrorPosix(err),
1077 };1084 };
1078 }1085 }
1079 if (ret_val == result_buf.len) {1086 if (ret_val == result_buf.len) {
...@@ -1132,7 +1139,6 @@ test "os.sleep" {...@@ -1132,7 +1139,6 @@ test "os.sleep" {
1132error ResourceLimitReached;1139error ResourceLimitReached;
1133error InvalidUserId;1140error InvalidUserId;
1134error PermissionDenied;1141error PermissionDenied;
1135error Unexpected;
11361142
1137pub fn posix_setuid(uid: u32) -> %void {1143pub fn posix_setuid(uid: u32) -> %void {
1138 const err = posix.getErrno(posix.setuid(uid));1144 const err = posix.getErrno(posix.setuid(uid));
...@@ -1141,7 +1147,7 @@ pub fn posix_setuid(uid: u32) -> %void {...@@ -1141,7 +1147,7 @@ pub fn posix_setuid(uid: u32) -> %void {
1141 posix.EAGAIN => error.ResourceLimitReached,1147 posix.EAGAIN => error.ResourceLimitReached,
1142 posix.EINVAL => error.InvalidUserId,1148 posix.EINVAL => error.InvalidUserId,
1143 posix.EPERM => error.PermissionDenied,1149 posix.EPERM => error.PermissionDenied,
1144 else => error.Unexpected,1150 else => unexpectedErrorPosix(err),
1145 };1151 };
1146}1152}
11471153
...@@ -1152,7 +1158,7 @@ pub fn posix_setreuid(ruid: u32, euid: u32) -> %void {...@@ -1152,7 +1158,7 @@ pub fn posix_setreuid(ruid: u32, euid: u32) -> %void {
1152 posix.EAGAIN => error.ResourceLimitReached,1158 posix.EAGAIN => error.ResourceLimitReached,
1153 posix.EINVAL => error.InvalidUserId,1159 posix.EINVAL => error.InvalidUserId,
1154 posix.EPERM => error.PermissionDenied,1160 posix.EPERM => error.PermissionDenied,
1155 else => error.Unexpected,1161 else => unexpectedErrorPosix(err),
1156 };1162 };
1157}1163}
11581164
...@@ -1163,7 +1169,7 @@ pub fn posix_setgid(gid: u32) -> %void {...@@ -1163,7 +1169,7 @@ pub fn posix_setgid(gid: u32) -> %void {
1163 posix.EAGAIN => error.ResourceLimitReached,1169 posix.EAGAIN => error.ResourceLimitReached,
1164 posix.EINVAL => error.InvalidUserId,1170 posix.EINVAL => error.InvalidUserId,
1165 posix.EPERM => error.PermissionDenied,1171 posix.EPERM => error.PermissionDenied,
1166 else => error.Unexpected,1172 else => unexpectedErrorPosix(err),
1167 };1173 };
1168}1174}
11691175
...@@ -1174,7 +1180,7 @@ pub fn posix_setregid(rgid: u32, egid: u32) -> %void {...@@ -1174,7 +1180,7 @@ pub fn posix_setregid(rgid: u32, egid: u32) -> %void {
1174 posix.EAGAIN => error.ResourceLimitReached,1180 posix.EAGAIN => error.ResourceLimitReached,
1175 posix.EINVAL => error.InvalidUserId,1181 posix.EINVAL => error.InvalidUserId,
1176 posix.EPERM => error.PermissionDenied,1182 posix.EPERM => error.PermissionDenied,
1177 else => error.Unexpected,1183 else => unexpectedErrorPosix(err),
1178 };1184 };
1179}1185}
11801186
...@@ -1417,3 +1423,29 @@ test "std.os" {...@@ -1417,3 +1423,29 @@ test "std.os" {
1417 _ = @import("path.zig");1423 _ = @import("path.zig");
1418 _ = @import("windows/index.zig");1424 _ = @import("windows/index.zig");
1419}1425}
1426
1427
1428error Unexpected;
1429
1430// TODO make this a build variable that you can set
1431const unexpected_error_tracing = false;
1432
1433/// Call this when you made a syscall or something that sets errno
1434/// and you get an unexpected error.
1435pub fn unexpectedErrorPosix(errno: c_int) -> error {
1436 if (unexpected_error_tracing) {
1437 io.stderr.printf("unexpected errno: {}\n", errno) %% return;
1438 debug.printStackTrace() %% return;
1439 }
1440 return error.Unexpected;
1441}
1442
1443/// Call this when you made a windows DLL call or something that does SetLastError
1444/// and you get an unexpected error.
1445pub fn unexpectedErrorWindows(err: windows.DWORD) -> error {
1446 if (unexpected_error_tracing) {
1447 io.stderr.printf("unexpected GetLastError(): {}\n", err) %% return;
1448 debug.printStackTrace() %% return;
1449 }
1450 return error.Unexpected;
1451}
std/os/path.zig+3-4
...@@ -915,7 +915,6 @@ error NotDir;...@@ -915,7 +915,6 @@ error NotDir;
915error NameTooLong;915error NameTooLong;
916error SymLinkLoop;916error SymLinkLoop;
917error InputOutput;917error InputOutput;
918error Unexpected;
919/// Return the canonicalized absolute pathname.918/// Return the canonicalized absolute pathname.
920/// Expands all symbolic links and resolves references to `.`, `..`, and919/// Expands all symbolic links and resolves references to `.`, `..`, and
921/// extra `/` characters in ::pathname.920/// extra `/` characters in ::pathname.
...@@ -938,7 +937,7 @@ pub fn real(allocator: &Allocator, pathname: []const u8) -> %[]u8 {...@@ -938,7 +937,7 @@ pub fn real(allocator: &Allocator, pathname: []const u8) -> %[]u8 {
938 windows.ERROR.FILE_NOT_FOUND => error.FileNotFound,937 windows.ERROR.FILE_NOT_FOUND => error.FileNotFound,
939 windows.ERROR.ACCESS_DENIED => error.AccessDenied,938 windows.ERROR.ACCESS_DENIED => error.AccessDenied,
940 windows.ERROR.FILENAME_EXCED_RANGE => error.NameTooLong,939 windows.ERROR.FILENAME_EXCED_RANGE => error.NameTooLong,
941 else => error.Unexpected,940 else => os.unexpectedErrorWindows(err),
942 };941 };
943 }942 }
944 defer os.windowsClose(h_file);943 defer os.windowsClose(h_file);
...@@ -954,7 +953,7 @@ pub fn real(allocator: &Allocator, pathname: []const u8) -> %[]u8 {...@@ -954,7 +953,7 @@ pub fn real(allocator: &Allocator, pathname: []const u8) -> %[]u8 {
954 windows.ERROR.PATH_NOT_FOUND => error.FileNotFound,953 windows.ERROR.PATH_NOT_FOUND => error.FileNotFound,
955 windows.ERROR.NOT_ENOUGH_MEMORY => error.OutOfMemory,954 windows.ERROR.NOT_ENOUGH_MEMORY => error.OutOfMemory,
956 windows.ERROR.INVALID_PARAMETER => unreachable,955 windows.ERROR.INVALID_PARAMETER => unreachable,
957 else => error.Unexpected,956 else => os.unexpectedErrorWindows(err),
958 };957 };
959 }958 }
960959
...@@ -1003,7 +1002,7 @@ pub fn real(allocator: &Allocator, pathname: []const u8) -> %[]u8 {...@@ -1003,7 +1002,7 @@ pub fn real(allocator: &Allocator, pathname: []const u8) -> %[]u8 {
1003 posix.ENAMETOOLONG => error.NameTooLong,1002 posix.ENAMETOOLONG => error.NameTooLong,
1004 posix.ELOOP => error.SymLinkLoop,1003 posix.ELOOP => error.SymLinkLoop,
1005 posix.EIO => error.InputOutput,1004 posix.EIO => error.InputOutput,
1006 else => error.Unexpected,1005 else => os.unexpectedErrorPosix(err),
1007 };1006 };
1008 }1007 }
1009 return allocator.shrink(u8, result_buf, cstr.len(result_buf.ptr));1008 return allocator.shrink(u8, result_buf, cstr.len(result_buf.ptr));
std/os/windows/util.zig+10-5
...@@ -7,6 +7,7 @@ const BufMap = std.BufMap;...@@ -7,6 +7,7 @@ const BufMap = std.BufMap;
77
8error WaitAbandoned;8error WaitAbandoned;
9error WaitTimeOut;9error WaitTimeOut;
10error Unexpected;
1011
11pub fn windowsWaitSingle(handle: windows.HANDLE, milliseconds: windows.DWORD) -> %void {12pub fn windowsWaitSingle(handle: windows.HANDLE, milliseconds: windows.DWORD) -> %void {
12 const result = windows.WaitForSingleObject(handle, milliseconds);13 const result = windows.WaitForSingleObject(handle, milliseconds);
...@@ -14,8 +15,11 @@ pub fn windowsWaitSingle(handle: windows.HANDLE, milliseconds: windows.DWORD) ->...@@ -14,8 +15,11 @@ pub fn windowsWaitSingle(handle: windows.HANDLE, milliseconds: windows.DWORD) ->
14 windows.WAIT_ABANDONED => error.WaitAbandoned,15 windows.WAIT_ABANDONED => error.WaitAbandoned,
15 windows.WAIT_OBJECT_0 => {},16 windows.WAIT_OBJECT_0 => {},
16 windows.WAIT_TIMEOUT => error.WaitTimeOut,17 windows.WAIT_TIMEOUT => error.WaitTimeOut,
17 windows.WAIT_FAILED => switch (windows.GetLastError()) {18 windows.WAIT_FAILED => {
18 else => error.Unexpected,19 const err = windows.GetLastError();
20 switch (err) {
21 else => os.unexpectedErrorWindows(err),
22 }
19 },23 },
20 else => error.Unexpected,24 else => error.Unexpected,
21 };25 };
...@@ -32,14 +36,15 @@ error BrokenPipe;...@@ -32,14 +36,15 @@ error BrokenPipe;
3236
33pub fn windowsWrite(handle: windows.HANDLE, bytes: []const u8) -> %void {37pub fn windowsWrite(handle: windows.HANDLE, bytes: []const u8) -> %void {
34 if (windows.WriteFile(handle, @ptrCast(&const c_void, bytes.ptr), u32(bytes.len), null, null) == 0) {38 if (windows.WriteFile(handle, @ptrCast(&const c_void, bytes.ptr), u32(bytes.len), null, null) == 0) {
35 return switch (windows.GetLastError()) {39 const err = windows.GetLastError();
40 return switch (err) {
36 windows.ERROR.INVALID_USER_BUFFER => error.SystemResources,41 windows.ERROR.INVALID_USER_BUFFER => error.SystemResources,
37 windows.ERROR.NOT_ENOUGH_MEMORY => error.SystemResources,42 windows.ERROR.NOT_ENOUGH_MEMORY => error.SystemResources,
38 windows.ERROR.OPERATION_ABORTED => error.OperationAborted,43 windows.ERROR.OPERATION_ABORTED => error.OperationAborted,
39 windows.ERROR.NOT_ENOUGH_QUOTA => error.SystemResources,44 windows.ERROR.NOT_ENOUGH_QUOTA => error.SystemResources,
40 windows.ERROR.IO_PENDING => error.IoPending,45 windows.ERROR.IO_PENDING => error.IoPending,
41 windows.ERROR.BROKEN_PIPE => error.BrokenPipe,46 windows.ERROR.BROKEN_PIPE => error.BrokenPipe,
42 else => error.Unexpected,47 else => os.unexpectedErrorWindows(err),
43 };48 };
44 }49 }
45}50}
...@@ -106,7 +111,7 @@ pub fn windowsOpen(file_path: []const u8, desired_access: windows.DWORD, share_m...@@ -106,7 +111,7 @@ pub fn windowsOpen(file_path: []const u8, desired_access: windows.DWORD, share_m
106 windows.ERROR.FILE_NOT_FOUND => error.FileNotFound,111 windows.ERROR.FILE_NOT_FOUND => error.FileNotFound,
107 windows.ERROR.ACCESS_DENIED => error.AccessDenied,112 windows.ERROR.ACCESS_DENIED => error.AccessDenied,
108 windows.ERROR.PIPE_BUSY => error.PipeBusy,113 windows.ERROR.PIPE_BUSY => error.PipeBusy,
109 else => error.Unexpected,114 else => os.unexpectedErrorWindows(err),
110 };115 };
111 }116 }
112117