authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-04-30 01:03:38-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-04-30 01:03:38-04:00
loge14db2366160840e0c25f3a467ff984304831e4c
tree28db45a4b1230bd48325fe6ac715b42b4a0f1049
parent54987c3d8f358e4ded9113a9ee5fa04dc1372a56

run zig fmt on std/os/index.zig


1 files changed, 202 insertions(+), 126 deletions(-)

std/os/index.zig+202-126
......@@ -3,7 +3,8 @@ const builtin = @import("builtin");
33const Os = builtin.Os;
44const is_windows = builtin.os == Os.windows;
55const is_posix = switch (builtin.os) {
6 builtin.Os.linux, builtin.Os.macosx => true,
6 builtin.Os.linux,
7 builtin.Os.macosx => true,
78 else => false,
89};
910const os = this;
......@@ -24,9 +25,10 @@ pub const windows = @import("windows/index.zig");
2425pub const darwin = @import("darwin.zig");
2526pub const linux = @import("linux/index.zig");
2627pub const zen = @import("zen.zig");
27pub const posix = switch(builtin.os) {
28pub const posix = switch (builtin.os) {
2829 Os.linux => linux,
29 Os.macosx, Os.ios => darwin,
30 Os.macosx,
31 Os.ios => darwin,
3032 Os.zen => zen,
3133 else => @compileError("Unsupported OS"),
3234};
......@@ -58,7 +60,7 @@ pub const windowsWrite = windows_util.windowsWrite;
5860pub const windowsIsCygwinPty = windows_util.windowsIsCygwinPty;
5961pub const windowsOpen = windows_util.windowsOpen;
6062pub const windowsLoadDll = windows_util.windowsLoadDll;
61pub const windowsUnloadDll = windows_util.windowsUnloadDll;
63pub const windowsUnloadDll = windows_util.windowsUnloadDll;
6264pub const createWindowsEnvBlock = windows_util.createWindowsEnvBlock;
6365
6466pub const WindowsWaitError = windows_util.WaitError;
......@@ -97,9 +99,9 @@ pub fn getRandomBytes(buf: []u8) !void {
9799 switch (err) {
98100 posix.EINVAL => unreachable,
99101 posix.EFAULT => unreachable,
100 posix.EINTR => continue,
102 posix.EINTR => continue,
101103 posix.ENOSYS => {
102 const fd = try posixOpenC(c"/dev/urandom", posix.O_RDONLY|posix.O_CLOEXEC, 0);
104 const fd = try posixOpenC(c"/dev/urandom", posix.O_RDONLY | posix.O_CLOEXEC, 0);
103105 defer close(fd);
104106
105107 try posixRead(fd, buf);
......@@ -110,8 +112,9 @@ pub fn getRandomBytes(buf: []u8) !void {
110112 }
111113 return;
112114 },
113 Os.macosx, Os.ios => {
114 const fd = try posixOpenC(c"/dev/urandom", posix.O_RDONLY|posix.O_CLOEXEC, 0);
115 Os.macosx,
116 Os.ios => {
117 const fd = try posixOpenC(c"/dev/urandom", posix.O_RDONLY | posix.O_CLOEXEC, 0);
115118 defer close(fd);
116119
117120 try posixRead(fd, buf);
......@@ -134,7 +137,20 @@ pub fn getRandomBytes(buf: []u8) !void {
134137 }
135138 },
136139 Os.zen => {
137 const randomness = []u8 {42, 1, 7, 12, 22, 17, 99, 16, 26, 87, 41, 45};
140 const randomness = []u8 {
141 42,
142 1,
143 7,
144 12,
145 22,
146 17,
147 99,
148 16,
149 26,
150 87,
151 41,
152 45,
153 };
138154 var i: usize = 0;
139155 while (i < buf.len) : (i += 1) {
140156 if (i > randomness.len) return error.Unknown;
......@@ -159,7 +175,9 @@ pub fn abort() noreturn {
159175 c.abort();
160176 }
161177 switch (builtin.os) {
162 Os.linux, Os.macosx, Os.ios => {
178 Os.linux,
179 Os.macosx,
180 Os.ios => {
163181 _ = posix.raise(posix.SIGABRT);
164182 _ = posix.raise(posix.SIGKILL);
165183 while (true) {}
......@@ -181,7 +199,9 @@ pub fn exit(status: u8) noreturn {
181199 c.exit(status);
182200 }
183201 switch (builtin.os) {
184 Os.linux, Os.macosx, Os.ios => {
202 Os.linux,
203 Os.macosx,
204 Os.ios => {
185205 posix.exit(status);
186206 },
187207 Os.windows => {
......@@ -230,12 +250,14 @@ pub fn posixRead(fd: i32, buf: []u8) !void {
230250 if (err > 0) {
231251 return switch (err) {
232252 posix.EINTR => continue,
233 posix.EINVAL, posix.EFAULT => unreachable,
253 posix.EINVAL,
254 posix.EFAULT => unreachable,
234255 posix.EAGAIN => error.WouldBlock,
235256 posix.EBADF => error.FileClosed,
236257 posix.EIO => error.InputOutput,
237258 posix.EISDIR => error.IsDir,
238 posix.ENOBUFS, posix.ENOMEM => error.SystemResources,
259 posix.ENOBUFS,
260 posix.ENOMEM => error.SystemResources,
239261 else => unexpectedErrorPosix(err),
240262 };
241263 }
......@@ -269,18 +291,19 @@ pub fn posixWrite(fd: i32, bytes: []const u8) !void {
269291 const write_err = posix.getErrno(rc);
270292 if (write_err > 0) {
271293 return switch (write_err) {
272 posix.EINTR => continue,
273 posix.EINVAL, posix.EFAULT => unreachable,
294 posix.EINTR => continue,
295 posix.EINVAL,
296 posix.EFAULT => unreachable,
274297 posix.EAGAIN => PosixWriteError.WouldBlock,
275298 posix.EBADF => PosixWriteError.FileClosed,
276299 posix.EDESTADDRREQ => PosixWriteError.DestinationAddressRequired,
277300 posix.EDQUOT => PosixWriteError.DiskQuota,
278 posix.EFBIG => PosixWriteError.FileTooBig,
279 posix.EIO => PosixWriteError.InputOutput,
301 posix.EFBIG => PosixWriteError.FileTooBig,
302 posix.EIO => PosixWriteError.InputOutput,
280303 posix.ENOSPC => PosixWriteError.NoSpaceLeft,
281 posix.EPERM => PosixWriteError.AccessDenied,
282 posix.EPIPE => PosixWriteError.BrokenPipe,
283 else => unexpectedErrorPosix(write_err),
304 posix.EPERM => PosixWriteError.AccessDenied,
305 posix.EPIPE => PosixWriteError.BrokenPipe,
306 else => unexpectedErrorPosix(write_err),
284307 };
285308 }
286309 index += rc;
......@@ -326,7 +349,8 @@ pub fn posixOpenC(file_path: &const u8, flags: u32, perm: usize) !i32 {
326349 posix.EFAULT => unreachable,
327350 posix.EINVAL => unreachable,
328351 posix.EACCES => return PosixOpenError.AccessDenied,
329 posix.EFBIG, posix.EOVERFLOW => return PosixOpenError.FileTooBig,
352 posix.EFBIG,
353 posix.EOVERFLOW => return PosixOpenError.FileTooBig,
330354 posix.EISDIR => return PosixOpenError.IsDir,
331355 posix.ELOOP => return PosixOpenError.SymLinkLoop,
332356 posix.EMFILE => return PosixOpenError.ProcessFdQuotaExceeded,
......@@ -351,7 +375,8 @@ pub fn posixDup2(old_fd: i32, new_fd: i32) !void {
351375 const err = posix.getErrno(posix.dup2(old_fd, new_fd));
352376 if (err > 0) {
353377 return switch (err) {
354 posix.EBUSY, posix.EINTR => continue,
378 posix.EBUSY,
379 posix.EINTR => continue,
355380 posix.EMFILE => error.ProcessFdQuotaExceeded,
356381 posix.EINVAL => unreachable,
357382 else => unexpectedErrorPosix(err),
......@@ -386,7 +411,7 @@ pub fn createNullDelimitedEnvMap(allocator: &Allocator, env_map: &const BufMap)
386411
387412pub fn freeNullDelimitedEnvMap(allocator: &Allocator, envp_buf: []?&u8) void {
388413 for (envp_buf) |env| {
389 const env_buf = if (env) |ptr| ptr[0 .. cstr.len(ptr) + 1] else break;
414 const env_buf = if (env) |ptr| ptr[0..cstr.len(ptr) + 1] else break;
390415 allocator.free(env_buf);
391416 }
392417 allocator.free(envp_buf);
......@@ -397,9 +422,7 @@ pub fn freeNullDelimitedEnvMap(allocator: &Allocator, envp_buf: []?&u8) void {
397422/// pointers after the args and after the environment variables.
398423/// `argv[0]` is the executable path.
399424/// This function also uses the PATH environment variable to get the full path to the executable.
400pub fn posixExecve(argv: []const []const u8, env_map: &const BufMap,
401 allocator: &Allocator) !void
402{
425pub fn posixExecve(argv: []const []const u8, env_map: &const BufMap, allocator: &Allocator) !void {
403426 const argv_buf = try allocator.alloc(?&u8, argv.len + 1);
404427 mem.set(?&u8, argv_buf, null);
405428 defer {
......@@ -438,7 +461,7 @@ pub fn posixExecve(argv: []const []const u8, env_map: &const BufMap,
438461 while (it.next()) |search_path| {
439462 mem.copy(u8, path_buf, search_path);
440463 path_buf[search_path.len] = '/';
441 mem.copy(u8, path_buf[search_path.len + 1 ..], exe_path);
464 mem.copy(u8, path_buf[search_path.len + 1..], exe_path);
442465 path_buf[search_path.len + exe_path.len + 1] = 0;
443466 err = posix.getErrno(posix.execve(path_buf.ptr, argv_buf.ptr, envp_buf.ptr));
444467 assert(err > 0);
......@@ -470,10 +493,17 @@ fn posixExecveErrnoToErr(err: usize) PosixExecveError {
470493 assert(err > 0);
471494 return switch (err) {
472495 posix.EFAULT => unreachable,
473 posix.E2BIG, posix.EMFILE, posix.ENAMETOOLONG, posix.ENFILE, posix.ENOMEM => error.SystemResources,
474 posix.EACCES, posix.EPERM => error.AccessDenied,
475 posix.EINVAL, posix.ENOEXEC => error.InvalidExe,
476 posix.EIO, posix.ELOOP => error.FileSystem,
496 posix.E2BIG,
497 posix.EMFILE,
498 posix.ENAMETOOLONG,
499 posix.ENFILE,
500 posix.ENOMEM => error.SystemResources,
501 posix.EACCES,
502 posix.EPERM => error.AccessDenied,
503 posix.EINVAL,
504 posix.ENOEXEC => error.InvalidExe,
505 posix.EIO,
506 posix.ELOOP => error.FileSystem,
477507 posix.EISDIR => error.IsDir,
478508 posix.ENOENT => error.FileNotFound,
479509 posix.ENOTDIR => error.NotDir,
......@@ -482,7 +512,7 @@ fn posixExecveErrnoToErr(err: usize) PosixExecveError {
482512 };
483513}
484514
485pub var linux_aux_raw = []usize{0} ** 38;
515pub var linux_aux_raw = []usize {0} ** 38;
486516pub var posix_environ_raw: []&u8 = undefined;
487517
488518/// Caller must free result when done.
......@@ -496,8 +526,7 @@ pub fn getEnvMap(allocator: &Allocator) !BufMap {
496526
497527 var i: usize = 0;
498528 while (true) {
499 if (ptr[i] == 0)
500 return result;
529 if (ptr[i] == 0) return result;
501530
502531 const key_start = i;
503532
......@@ -535,8 +564,7 @@ pub fn getEnvPosix(key: []const u8) ?[]const u8 {
535564 var line_i: usize = 0;
536565 while (ptr[line_i] != 0 and ptr[line_i] != '=') : (line_i += 1) {}
537566 const this_key = ptr[0..line_i];
538 if (!mem.eql(u8, key, this_key))
539 continue;
567 if (!mem.eql(u8, key, this_key)) continue;
540568
541569 var end_i: usize = line_i;
542570 while (ptr[end_i] != 0) : (end_i += 1) {}
......@@ -689,8 +717,10 @@ pub fn symLinkPosix(allocator: &Allocator, existing_path: []const u8, new_path:
689717 const err = posix.getErrno(posix.symlink(existing_buf.ptr, new_buf.ptr));
690718 if (err > 0) {
691719 return switch (err) {
692 posix.EFAULT, posix.EINVAL => unreachable,
693 posix.EACCES, posix.EPERM => error.AccessDenied,
720 posix.EFAULT,
721 posix.EINVAL => unreachable,
722 posix.EACCES,
723 posix.EPERM => error.AccessDenied,
694724 posix.EDQUOT => error.DiskQuota,
695725 posix.EEXIST => error.PathAlreadyExists,
696726 posix.EIO => error.FileSystem,
......@@ -707,9 +737,7 @@ pub fn symLinkPosix(allocator: &Allocator, existing_path: []const u8, new_path:
707737}
708738
709739// here we replace the standard +/ with -_ so that it can be used in a file name
710const b64_fs_encoder = base64.Base64Encoder.init(
711 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",
712 base64.standard_pad_char);
740const b64_fs_encoder = base64.Base64Encoder.init("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_", base64.standard_pad_char);
713741
714742pub fn atomicSymLink(allocator: &Allocator, existing_path: []const u8, new_path: []const u8) !void {
715743 if (symLink(allocator, existing_path, new_path)) {
......@@ -728,7 +756,7 @@ pub fn atomicSymLink(allocator: &Allocator, existing_path: []const u8, new_path:
728756 tmp_path[dirname.len] = os.path.sep;
729757 while (true) {
730758 try getRandomBytes(rand_buf[0..]);
731 b64_fs_encoder.encode(tmp_path[dirname.len + 1 ..], rand_buf);
759 b64_fs_encoder.encode(tmp_path[dirname.len + 1..], rand_buf);
732760
733761 if (symLink(allocator, existing_path, tmp_path)) {
734762 return rename(allocator, tmp_path, new_path);
......@@ -737,7 +765,6 @@ pub fn atomicSymLink(allocator: &Allocator, existing_path: []const u8, new_path:
737765 else => return err, // TODO zig should know this set does not include PathAlreadyExists
738766 }
739767 }
740
741768}
742769
743770pub fn deleteFile(allocator: &Allocator, file_path: []const u8) !void {
......@@ -760,7 +787,8 @@ pub fn deleteFileWindows(allocator: &Allocator, file_path: []const u8) !void {
760787 return switch (err) {
761788 windows.ERROR.FILE_NOT_FOUND => error.FileNotFound,
762789 windows.ERROR.ACCESS_DENIED => error.AccessDenied,
763 windows.ERROR.FILENAME_EXCED_RANGE, windows.ERROR.INVALID_PARAMETER => error.NameTooLong,
790 windows.ERROR.FILENAME_EXCED_RANGE,
791 windows.ERROR.INVALID_PARAMETER => error.NameTooLong,
764792 else => unexpectedErrorWindows(err),
765793 };
766794 }
......@@ -776,9 +804,11 @@ pub fn deleteFilePosix(allocator: &Allocator, file_path: []const u8) !void {
776804 const err = posix.getErrno(posix.unlink(buf.ptr));
777805 if (err > 0) {
778806 return switch (err) {
779 posix.EACCES, posix.EPERM => error.AccessDenied,
807 posix.EACCES,
808 posix.EPERM => error.AccessDenied,
780809 posix.EBUSY => error.FileBusy,
781 posix.EFAULT, posix.EINVAL => unreachable,
810 posix.EFAULT,
811 posix.EINVAL => unreachable,
782812 posix.EIO => error.FileSystem,
783813 posix.EISDIR => error.IsDir,
784814 posix.ELOOP => error.SymLinkLoop,
......@@ -856,7 +886,7 @@ pub const AtomicFile = struct {
856886
857887 while (true) {
858888 try getRandomBytes(rand_buf[0..]);
859 b64_fs_encoder.encode(tmp_path[dirname.len + 1 ..], rand_buf);
889 b64_fs_encoder.encode(tmp_path[dirname.len + 1..], rand_buf);
860890
861891 const file = os.File.openWriteNoClobber(allocator, tmp_path, mode) catch |err| switch (err) {
862892 error.PathAlreadyExists => continue,
......@@ -907,7 +937,7 @@ pub fn rename(allocator: &Allocator, old_path: []const u8, new_path: []const u8)
907937 new_buf[new_path.len] = 0;
908938
909939 if (is_windows) {
910 const flags = windows.MOVEFILE_REPLACE_EXISTING|windows.MOVEFILE_WRITE_THROUGH;
940 const flags = windows.MOVEFILE_REPLACE_EXISTING | windows.MOVEFILE_WRITE_THROUGH;
911941 if (windows.MoveFileExA(old_buf.ptr, new_buf.ptr, flags) == 0) {
912942 const err = windows.GetLastError();
913943 return switch (err) {
......@@ -918,10 +948,12 @@ pub fn rename(allocator: &Allocator, old_path: []const u8, new_path: []const u8)
918948 const err = posix.getErrno(posix.rename(old_buf.ptr, new_buf.ptr));
919949 if (err > 0) {
920950 return switch (err) {
921 posix.EACCES, posix.EPERM => error.AccessDenied,
951 posix.EACCES,
952 posix.EPERM => error.AccessDenied,
922953 posix.EBUSY => error.FileBusy,
923954 posix.EDQUOT => error.DiskQuota,
924 posix.EFAULT, posix.EINVAL => unreachable,
955 posix.EFAULT,
956 posix.EINVAL => unreachable,
925957 posix.EISDIR => error.IsDir,
926958 posix.ELOOP => error.SymLinkLoop,
927959 posix.EMLINK => error.LinkQuotaExceeded,
......@@ -930,7 +962,8 @@ pub fn rename(allocator: &Allocator, old_path: []const u8, new_path: []const u8)
930962 posix.ENOTDIR => error.NotDir,
931963 posix.ENOMEM => error.SystemResources,
932964 posix.ENOSPC => error.NoSpaceLeft,
933 posix.EEXIST, posix.ENOTEMPTY => error.PathAlreadyExists,
965 posix.EEXIST,
966 posix.ENOTEMPTY => error.PathAlreadyExists,
934967 posix.EROFS => error.ReadOnlyFileSystem,
935968 posix.EXDEV => error.RenameAcrossMountPoints,
936969 else => unexpectedErrorPosix(err),
......@@ -968,7 +1001,8 @@ pub fn makeDirPosix(allocator: &Allocator, dir_path: []const u8) !void {
9681001 const err = posix.getErrno(posix.mkdir(path_buf.ptr, 0o755));
9691002 if (err > 0) {
9701003 return switch (err) {
971 posix.EACCES, posix.EPERM => error.AccessDenied,
1004 posix.EACCES,
1005 posix.EPERM => error.AccessDenied,
9721006 posix.EDQUOT => error.DiskQuota,
9731007 posix.EEXIST => error.PathAlreadyExists,
9741008 posix.EFAULT => unreachable,
......@@ -998,27 +1032,23 @@ pub fn makePath(allocator: &Allocator, full_path: []const u8) !void {
9981032 // TODO stat the file and return an error if it's not a directory
9991033 // this is important because otherwise a dangling symlink
10001034 // could cause an infinite loop
1001 if (end_index == resolved_path.len)
1002 return;
1035 if (end_index == resolved_path.len) return;
10031036 } else if (err == error.FileNotFound) {
10041037 // march end_index backward until next path component
10051038 while (true) {
10061039 end_index -= 1;
1007 if (os.path.isSep(resolved_path[end_index]))
1008 break;
1040 if (os.path.isSep(resolved_path[end_index])) break;
10091041 }
10101042 continue;
10111043 } else {
10121044 return err;
10131045 }
10141046 };
1015 if (end_index == resolved_path.len)
1016 return;
1047 if (end_index == resolved_path.len) return;
10171048 // march end_index forward until next path component
10181049 while (true) {
10191050 end_index += 1;
1020 if (end_index == resolved_path.len or os.path.isSep(resolved_path[end_index]))
1021 break;
1051 if (end_index == resolved_path.len or os.path.isSep(resolved_path[end_index])) break;
10221052 }
10231053 }
10241054}
......@@ -1035,15 +1065,18 @@ pub fn deleteDir(allocator: &Allocator, dir_path: []const u8) !void {
10351065 const err = posix.getErrno(posix.rmdir(path_buf.ptr));
10361066 if (err > 0) {
10371067 return switch (err) {
1038 posix.EACCES, posix.EPERM => error.AccessDenied,
1068 posix.EACCES,
1069 posix.EPERM => error.AccessDenied,
10391070 posix.EBUSY => error.FileBusy,
1040 posix.EFAULT, posix.EINVAL => unreachable,
1071 posix.EFAULT,
1072 posix.EINVAL => unreachable,
10411073 posix.ELOOP => error.SymLinkLoop,
10421074 posix.ENAMETOOLONG => error.NameTooLong,
10431075 posix.ENOENT => error.FileNotFound,
10441076 posix.ENOMEM => error.SystemResources,
10451077 posix.ENOTDIR => error.NotDir,
1046 posix.EEXIST, posix.ENOTEMPTY => error.DirNotEmpty,
1078 posix.EEXIST,
1079 posix.ENOTEMPTY => error.DirNotEmpty,
10471080 posix.EROFS => error.ReadOnlyFileSystem,
10481081 else => unexpectedErrorPosix(err),
10491082 };
......@@ -1053,7 +1086,7 @@ pub fn deleteDir(allocator: &Allocator, dir_path: []const u8) !void {
10531086/// Whether ::full_path describes a symlink, file, or directory, this function
10541087/// removes it. If it cannot be removed because it is a non-empty directory,
10551088/// this function recursively removes its entries and then tries again.
1056// TODO non-recursive implementation
1089/// TODO non-recursive implementation
10571090const DeleteTreeError = error {
10581091 OutOfMemory,
10591092 AccessDenied,
......@@ -1095,8 +1128,7 @@ pub fn deleteTree(allocator: &Allocator, full_path: []const u8) DeleteTreeError!
10951128 error.NotDir,
10961129 error.FileSystem,
10971130 error.FileBusy,
1098 error.Unexpected
1099 => return err,
1131 error.Unexpected => return err,
11001132 }
11011133 {
11021134 var dir = Dir.open(allocator, full_path) catch |err| switch (err) {
......@@ -1120,8 +1152,7 @@ pub fn deleteTree(allocator: &Allocator, full_path: []const u8) DeleteTreeError!
11201152 error.SystemResources,
11211153 error.NoSpaceLeft,
11221154 error.PathAlreadyExists,
1123 error.Unexpected
1124 => return err,
1155 error.Unexpected => return err,
11251156 };
11261157 defer dir.close();
11271158
......@@ -1151,7 +1182,8 @@ pub const Dir = struct {
11511182 end_index: usize,
11521183
11531184 const darwin_seek_t = switch (builtin.os) {
1154 Os.macosx, Os.ios => i64,
1185 Os.macosx,
1186 Os.ios => i64,
11551187 else => void,
11561188 };
11571189
......@@ -1175,12 +1207,14 @@ pub const Dir = struct {
11751207 pub fn open(allocator: &Allocator, dir_path: []const u8) !Dir {
11761208 const fd = switch (builtin.os) {
11771209 Os.windows => @compileError("TODO support Dir.open for windows"),
1178 Os.linux => try posixOpen(allocator, dir_path, posix.O_RDONLY|posix.O_DIRECTORY|posix.O_CLOEXEC, 0),
1179 Os.macosx, Os.ios => try posixOpen(allocator, dir_path, posix.O_RDONLY|posix.O_NONBLOCK|posix.O_DIRECTORY|posix.O_CLOEXEC, 0),
1210 Os.linux => try posixOpen(allocator, dir_path, posix.O_RDONLY | posix.O_DIRECTORY | posix.O_CLOEXEC, 0),
1211 Os.macosx,
1212 Os.ios => try posixOpen(allocator, dir_path, posix.O_RDONLY | posix.O_NONBLOCK | posix.O_DIRECTORY | posix.O_CLOEXEC, 0),
11801213 else => @compileError("Dir.open is not supported for this platform"),
11811214 };
11821215 const darwin_seek_init = switch (builtin.os) {
1183 Os.macosx, Os.ios => 0,
1216 Os.macosx,
1217 Os.ios => 0,
11841218 else => {},
11851219 };
11861220 return Dir {
......@@ -1203,7 +1237,8 @@ pub const Dir = struct {
12031237 pub fn next(self: &Dir) !?Entry {
12041238 switch (builtin.os) {
12051239 Os.linux => return self.nextLinux(),
1206 Os.macosx, Os.ios => return self.nextDarwin(),
1240 Os.macosx,
1241 Os.ios => return self.nextDarwin(),
12071242 Os.windows => return self.nextWindows(),
12081243 else => @compileError("Dir.next not supported on " ++ @tagName(builtin.os)),
12091244 }
......@@ -1217,12 +1252,13 @@ pub const Dir = struct {
12171252 }
12181253
12191254 while (true) {
1220 const result = posix.getdirentries64(self.fd, self.buf.ptr, self.buf.len,
1221 &self.darwin_seek);
1255 const result = posix.getdirentries64(self.fd, self.buf.ptr, self.buf.len, &self.darwin_seek);
12221256 const err = posix.getErrno(result);
12231257 if (err > 0) {
12241258 switch (err) {
1225 posix.EBADF, posix.EFAULT, posix.ENOTDIR => unreachable,
1259 posix.EBADF,
1260 posix.EFAULT,
1261 posix.ENOTDIR => unreachable,
12261262 posix.EINVAL => {
12271263 self.buf = try self.allocator.realloc(u8, self.buf, self.buf.len * 2);
12281264 continue;
......@@ -1230,14 +1266,13 @@ pub const Dir = struct {
12301266 else => return unexpectedErrorPosix(err),
12311267 }
12321268 }
1233 if (result == 0)
1234 return null;
1269 if (result == 0) return null;
12351270 self.index = 0;
12361271 self.end_index = result;
12371272 break;
12381273 }
12391274 }
1240 const darwin_entry = @ptrCast(& align(1) posix.dirent, &self.buf[self.index]);
1275 const darwin_entry = @ptrCast(&align(1) posix.dirent, &self.buf[self.index]);
12411276 const next_index = self.index + darwin_entry.d_reclen;
12421277 self.index = next_index;
12431278
......@@ -1282,7 +1317,9 @@ pub const Dir = struct {
12821317 const err = posix.getErrno(result);
12831318 if (err > 0) {
12841319 switch (err) {
1285 posix.EBADF, posix.EFAULT, posix.ENOTDIR => unreachable,
1320 posix.EBADF,
1321 posix.EFAULT,
1322 posix.ENOTDIR => unreachable,
12861323 posix.EINVAL => {
12871324 self.buf = try self.allocator.realloc(u8, self.buf, self.buf.len * 2);
12881325 continue;
......@@ -1290,14 +1327,13 @@ pub const Dir = struct {
12901327 else => return unexpectedErrorPosix(err),
12911328 }
12921329 }
1293 if (result == 0)
1294 return null;
1330 if (result == 0) return null;
12951331 self.index = 0;
12961332 self.end_index = result;
12971333 break;
12981334 }
12991335 }
1300 const linux_entry = @ptrCast(& align(1) posix.dirent, &self.buf[self.index]);
1336 const linux_entry = @ptrCast(&align(1) posix.dirent, &self.buf[self.index]);
13011337 const next_index = self.index + linux_entry.d_reclen;
13021338 self.index = next_index;
13031339
......@@ -1366,7 +1402,8 @@ pub fn readLink(allocator: &Allocator, pathname: []const u8) ![]u8 {
13661402 if (err > 0) {
13671403 return switch (err) {
13681404 posix.EACCES => error.AccessDenied,
1369 posix.EFAULT, posix.EINVAL => unreachable,
1405 posix.EFAULT,
1406 posix.EINVAL => unreachable,
13701407 posix.EIO => error.FileSystem,
13711408 posix.ELOOP => error.SymLinkLoop,
13721409 posix.ENAMETOOLONG => error.NameTooLong,
......@@ -1459,8 +1496,7 @@ pub const ArgIteratorPosix = struct {
14591496 }
14601497
14611498 pub fn next(self: &ArgIteratorPosix) ?[]const u8 {
1462 if (self.index == self.count)
1463 return null;
1499 if (self.index == self.count) return null;
14641500
14651501 const s = raw[self.index];
14661502 self.index += 1;
......@@ -1468,8 +1504,7 @@ pub const ArgIteratorPosix = struct {
14681504 }
14691505
14701506 pub fn skip(self: &ArgIteratorPosix) bool {
1471 if (self.index == self.count)
1472 return false;
1507 if (self.index == self.count) return false;
14731508
14741509 self.index += 1;
14751510 return true;
......@@ -1487,7 +1522,9 @@ pub const ArgIteratorWindows = struct {
14871522 quote_count: usize,
14881523 seen_quote_count: usize,
14891524
1490 pub const NextError = error{OutOfMemory};
1525 pub const NextError = error {
1526 OutOfMemory,
1527 };
14911528
14921529 pub fn init() ArgIteratorWindows {
14931530 return initWithCmdLine(windows.GetCommandLineA());
......@@ -1510,7 +1547,8 @@ pub const ArgIteratorWindows = struct {
15101547 const byte = self.cmd_line[self.index];
15111548 switch (byte) {
15121549 0 => return null,
1513 ' ', '\t' => continue,
1550 ' ',
1551 '\t' => continue,
15141552 else => break,
15151553 }
15161554 }
......@@ -1524,7 +1562,8 @@ pub const ArgIteratorWindows = struct {
15241562 const byte = self.cmd_line[self.index];
15251563 switch (byte) {
15261564 0 => return false,
1527 ' ', '\t' => continue,
1565 ' ',
1566 '\t' => continue,
15281567 else => break,
15291568 }
15301569 }
......@@ -1543,7 +1582,8 @@ pub const ArgIteratorWindows = struct {
15431582 '\\' => {
15441583 backslash_count += 1;
15451584 },
1546 ' ', '\t' => {
1585 ' ',
1586 '\t' => {
15471587 if (self.seen_quote_count % 2 == 0 or self.seen_quote_count == self.quote_count) {
15481588 return true;
15491589 }
......@@ -1583,7 +1623,8 @@ pub const ArgIteratorWindows = struct {
15831623 '\\' => {
15841624 backslash_count += 1;
15851625 },
1586 ' ', '\t' => {
1626 ' ',
1627 '\t' => {
15871628 try self.emitBackslashes(&buf, backslash_count);
15881629 backslash_count = 0;
15891630 if (self.seen_quote_count % 2 == 1 and self.seen_quote_count != self.quote_count) {
......@@ -1627,7 +1668,6 @@ pub const ArgIteratorWindows = struct {
16271668 }
16281669 }
16291670 }
1630
16311671};
16321672
16331673pub const ArgIterator = struct {
......@@ -1642,7 +1682,7 @@ pub const ArgIterator = struct {
16421682 }
16431683
16441684 pub const NextError = ArgIteratorWindows.NextError;
1645
1685
16461686 /// You must free the returned memory when done.
16471687 pub fn next(self: &ArgIterator, allocator: &Allocator) ?(NextError![]u8) {
16481688 if (builtin.os == Os.windows) {
......@@ -1717,15 +1757,47 @@ pub fn argsFree(allocator: &mem.Allocator, args_alloc: []const []u8) void {
17171757}
17181758
17191759test "windows arg parsing" {
1720 testWindowsCmdLine(c"a b\tc d", [][]const u8{"a", "b", "c", "d"});
1721 testWindowsCmdLine(c"\"abc\" d e", [][]const u8{"abc", "d", "e"});
1722 testWindowsCmdLine(c"a\\\\\\b d\"e f\"g h", [][]const u8{"a\\\\\\b", "de fg", "h"});
1723 testWindowsCmdLine(c"a\\\\\\\"b c d", [][]const u8{"a\\\"b", "c", "d"});
1724 testWindowsCmdLine(c"a\\\\\\\\\"b c\" d e", [][]const u8{"a\\\\b c", "d", "e"});
1725 testWindowsCmdLine(c"a b\tc \"d f", [][]const u8{"a", "b", "c", "\"d", "f"});
1726
1727 testWindowsCmdLine(c"\".\\..\\zig-cache\\build\" \"bin\\zig.exe\" \".\\..\" \".\\..\\zig-cache\" \"--help\"",
1728 [][]const u8{".\\..\\zig-cache\\build", "bin\\zig.exe", ".\\..", ".\\..\\zig-cache", "--help"});
1760 testWindowsCmdLine(c"a b\tc d", [][]const u8 {
1761 "a",
1762 "b",
1763 "c",
1764 "d",
1765 });
1766 testWindowsCmdLine(c"\"abc\" d e", [][]const u8 {
1767 "abc",
1768 "d",
1769 "e",
1770 });
1771 testWindowsCmdLine(c"a\\\\\\b d\"e f\"g h", [][]const u8 {
1772 "a\\\\\\b",
1773 "de fg",
1774 "h",
1775 });
1776 testWindowsCmdLine(c"a\\\\\\\"b c d", [][]const u8 {
1777 "a\\\"b",
1778 "c",
1779 "d",
1780 });
1781 testWindowsCmdLine(c"a\\\\\\\\\"b c\" d e", [][]const u8 {
1782 "a\\\\b c",
1783 "d",
1784 "e",
1785 });
1786 testWindowsCmdLine(c"a b\tc \"d f", [][]const u8 {
1787 "a",
1788 "b",
1789 "c",
1790 "\"d",
1791 "f",
1792 });
1793
1794 testWindowsCmdLine(c"\".\\..\\zig-cache\\build\" \"bin\\zig.exe\" \".\\..\" \".\\..\\zig-cache\" \"--help\"", [][]const u8 {
1795 ".\\..\\zig-cache\\build",
1796 "bin\\zig.exe",
1797 ".\\..",
1798 ".\\..\\zig-cache",
1799 "--help",
1800 });
17291801}
17301802
17311803fn testWindowsCmdLine(input_cmd_line: &const u8, expected_args: []const []const u8) void {
......@@ -1772,7 +1844,8 @@ pub fn openSelfExe() !os.File {
17721844 var fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]);
17731845 return os.File.openRead(&fixed_allocator.allocator, proc_file_path);
17741846 },
1775 Os.macosx, Os.ios => {
1847 Os.macosx,
1848 Os.ios => {
17761849 var fixed_buffer_mem: [darwin.PATH_MAX * 2]u8 = undefined;
17771850 var fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]);
17781851 const self_exe_path = try selfExePath(&fixed_allocator.allocator);
......@@ -1784,8 +1857,10 @@ pub fn openSelfExe() !os.File {
17841857
17851858test "openSelfExe" {
17861859 switch (builtin.os) {
1787 Os.linux, Os.macosx, Os.ios => (try openSelfExe()).close(),
1788 else => return, // Unsupported OS.
1860 Os.linux,
1861 Os.macosx,
1862 Os.ios => (try openSelfExe()).close(),
1863 else => return, // Unsupported OS.
17891864 }
17901865}
17911866
......@@ -1822,7 +1897,8 @@ pub fn selfExePath(allocator: &mem.Allocator) ![]u8 {
18221897 try out_path.resize(new_len);
18231898 }
18241899 },
1825 Os.macosx, Os.ios => {
1900 Os.macosx,
1901 Os.ios => {
18261902 var u32_len: u32 = 0;
18271903 const ret1 = c._NSGetExecutablePath(undefined, &u32_len);
18281904 assert(ret1 != 0);
......@@ -1850,7 +1926,9 @@ pub fn selfExeDirPath(allocator: &mem.Allocator) ![]u8 {
18501926 const dir = path.dirname(full_exe_path);
18511927 return allocator.shrink(u8, full_exe_path, dir.len);
18521928 },
1853 Os.windows, Os.macosx, Os.ios => {
1929 Os.windows,
1930 Os.macosx,
1931 Os.ios => {
18541932 const self_exe_path = try selfExePath(allocator);
18551933 errdefer allocator.free(self_exe_path);
18561934 const dirname = os.path.dirname(self_exe_path);
......@@ -1907,7 +1985,8 @@ pub fn posixSocket(domain: u32, socket_type: u32, protocol: u32) !i32 {
19071985 posix.EINVAL => return PosixSocketError.ProtocolFamilyNotAvailable,
19081986 posix.EMFILE => return PosixSocketError.ProcessFdQuotaExceeded,
19091987 posix.ENFILE => return PosixSocketError.SystemFdQuotaExceeded,
1910 posix.ENOBUFS, posix.ENOMEM => return PosixSocketError.SystemResources,
1988 posix.ENOBUFS,
1989 posix.ENOMEM => return PosixSocketError.SystemResources,
19111990 posix.EPROTONOSUPPORT => return PosixSocketError.ProtocolNotSupported,
19121991 else => return unexpectedErrorPosix(err),
19131992 }
......@@ -1938,7 +2017,7 @@ pub const PosixBindError = error {
19382017
19392018 /// A nonexistent interface was requested or the requested address was not local.
19402019 AddressNotAvailable,
1941
2020
19422021 /// addr points outside the user's accessible address space.
19432022 PageFault,
19442023
......@@ -2027,7 +2106,7 @@ pub const PosixAcceptError = error {
20272106 FileDescriptorClosed,
20282107
20292108 ConnectionAborted,
2030
2109
20312110 /// The addr argument is not in a writable part of the user address space.
20322111 PageFault,
20332112
......@@ -2040,7 +2119,7 @@ pub const PosixAcceptError = error {
20402119
20412120 /// The system-wide limit on the total number of open files has been reached.
20422121 SystemFdQuotaExceeded,
2043
2122
20442123 /// Not enough free memory. This often means that the memory allocation is limited
20452124 /// by the socket buffer limits, not by the system memory.
20462125 SystemResources,
......@@ -2076,7 +2155,8 @@ pub fn posixAccept(fd: i32, addr: &posix.sockaddr, flags: u32) PosixAcceptError!
20762155 posix.EINVAL => return PosixAcceptError.InvalidSyscall,
20772156 posix.EMFILE => return PosixAcceptError.ProcessFdQuotaExceeded,
20782157 posix.ENFILE => return PosixAcceptError.SystemFdQuotaExceeded,
2079 posix.ENOBUFS, posix.ENOMEM => return PosixAcceptError.SystemResources,
2158 posix.ENOBUFS,
2159 posix.ENOMEM => return PosixAcceptError.SystemResources,
20802160 posix.ENOTSOCK => return PosixAcceptError.FileDescriptorNotASocket,
20812161 posix.EOPNOTSUPP => return PosixAcceptError.OperationNotSupported,
20822162 posix.EPROTO => return PosixAcceptError.ProtocolFailure,
......@@ -2287,7 +2367,8 @@ pub fn posixConnectAsync(sockfd: i32, sockaddr: &const posix.sockaddr) PosixConn
22872367 const rc = posix.connect(sockfd, sockaddr, @sizeOf(posix.sockaddr));
22882368 const err = posix.getErrno(rc);
22892369 switch (err) {
2290 0, posix.EINPROGRESS => return,
2370 0,
2371 posix.EINPROGRESS => return,
22912372 else => return unexpectedErrorPosix(err),
22922373
22932374 posix.EACCES => return PosixConnectError.PermissionDenied,
......@@ -2351,9 +2432,9 @@ pub const Thread = struct {
23512432
23522433 pub const use_pthreads = is_posix and builtin.link_libc;
23532434 const Data = if (use_pthreads) struct {
2354 handle: c.pthread_t,
2355 stack_addr: usize,
2356 stack_len: usize,
2435 handle: c.pthread_t,
2436 stack_addr: usize,
2437 stack_len: usize,
23572438 } else switch (builtin.os) {
23582439 builtin.Os.linux => struct {
23592440 pid: i32,
......@@ -2467,9 +2548,7 @@ pub fn spawnThread(context: var, comptime startFn: var) SpawnThreadError!&Thread
24672548 outer_context.thread.data.alloc_start = bytes_ptr;
24682549
24692550 const parameter = if (@sizeOf(Context) == 0) null else @ptrCast(&c_void, &outer_context.inner);
2470 outer_context.thread.data.handle = windows.CreateThread(null, default_stack_size, WinThread.threadMain,
2471 parameter, 0, null) ??
2472 {
2551 outer_context.thread.data.handle = windows.CreateThread(null, default_stack_size, WinThread.threadMain, parameter, 0, null) ?? {
24732552 const err = windows.GetLastError();
24742553 return switch (err) {
24752554 else => os.unexpectedErrorWindows(err),
......@@ -2500,8 +2579,7 @@ pub fn spawnThread(context: var, comptime startFn: var) SpawnThreadError!&Thread
25002579 const MAP_GROWSDOWN = if (builtin.os == builtin.Os.linux) linux.MAP_GROWSDOWN else 0;
25012580
25022581 const mmap_len = default_stack_size;
2503 const stack_addr = posix.mmap(null, mmap_len, posix.PROT_READ|posix.PROT_WRITE,
2504 posix.MAP_PRIVATE|posix.MAP_ANONYMOUS|MAP_GROWSDOWN, -1, 0);
2582 const stack_addr = posix.mmap(null, mmap_len, posix.PROT_READ | posix.PROT_WRITE, posix.MAP_PRIVATE | posix.MAP_ANONYMOUS | MAP_GROWSDOWN, -1, 0);
25052583 if (stack_addr == posix.MAP_FAILED) return error.OutOfMemory;
25062584 errdefer assert(posix.munmap(stack_addr, mmap_len) == 0);
25072585
......@@ -2547,9 +2625,7 @@ pub fn spawnThread(context: var, comptime startFn: var) SpawnThreadError!&Thread
25472625 }
25482626 } else if (builtin.os == builtin.Os.linux) {
25492627 // use linux API directly. TODO use posix.CLONE_SETTLS and initialize thread local storage correctly
2550 const flags = posix.CLONE_VM | posix.CLONE_FS | posix.CLONE_FILES | posix.CLONE_SIGHAND
2551 | posix.CLONE_THREAD | posix.CLONE_SYSVSEM
2552 | posix.CLONE_PARENT_SETTID | posix.CLONE_CHILD_CLEARTID | posix.CLONE_DETACHED;
2628 const flags = posix.CLONE_VM | posix.CLONE_FS | posix.CLONE_FILES | posix.CLONE_SIGHAND | posix.CLONE_THREAD | posix.CLONE_SYSVSEM | posix.CLONE_PARENT_SETTID | posix.CLONE_CHILD_CLEARTID | posix.CLONE_DETACHED;
25532629 const newtls: usize = 0;
25542630 const rc = posix.clone(MainFuncs.linuxThreadMain, stack_end, flags, arg, &thread_ptr.data.pid, newtls, &thread_ptr.data.pid);
25552631 const err = posix.getErrno(rc);