authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-05-05 17:23:49+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-05-18 16:09:49+02:00
logd43c08a3e517f29b3dcaf5aa7860ed185815a305
tree9e7728c7f34c872f5ce032b131db6fcaa6077307
parentfeade9ef0010b1b47d7216e786ed964d09612c2b

Add/fix missing WASI functionality to pass libstd tests

This rather large commit adds/fixes missing WASI functionality in `libstd` needed to pass the `libstd` tests. As such, now by default tests targeting `wasm32-wasi` target are enabled in `test/tests.zig` module. However, they can be disabled by passing the `-Dskip-wasi=true` flag when invoking the `zig build test` command. When the flag is set to `false`, i.e., when WASI tests are included, `wasmtime` with `--dir=.` is used as the default testing command. Since the majority of `libstd` tests were relying on `fs.cwd()` call to get current working directory handle wrapped in `Dir` struct, in order to make the tests WASI-friendly, `fs.cwd()` call was replaced with `testing.getTestDir()` function which resolved to either `fs.cwd()` for non-WASI targets, or tries to fetch the preopen list from the WASI runtime and extract a preopen for '.' path. The summary of changes introduced by this commit: * implement `Dir.makeDir` and `Dir.openDir` targeting WASI * implement `Dir.deleteFile` and `Dir.deleteDir` targeting WASI * fix `os.close` and map errors in `unlinkat` * move WASI-specific `mkdirat` and `unlinkat` from `std.fs.wasi` to `std.os` module * implement `lseek_{SET, CUR, END}` targeting WASI * implement `futimens` targeting WASI * implement `ftruncate` targeting WASI * implement `readv`, `writev`, `pread{v}`, `pwrite{v}` targeting WASI * make sure ANSI escape codes are _not_ used in stderr or stdout in WASI, as WASI always sanitizes stderr, and sanitizes stdout if fd is a TTY * fix specifying WASI rights when opening/creating files/dirs * tweak `AtomicFile` to be WASI-compatible * implement `os.renameatWasi` for WASI-compliant `os.renameat` function * implement sleep() targeting WASI * fix `process.getEnvMap` targeting WASI

20 files changed, 797 insertions(+), 130 deletions(-)

build.zig+6-4
......@@ -60,6 +60,7 @@ pub fn build(b: *Builder) !void {
6060 const skip_release_safe = b.option(bool, "skip-release-safe", "Main test suite skips release-safe builds") orelse skip_release;
6161 const skip_non_native = b.option(bool, "skip-non-native", "Main test suite skips non-native builds") orelse false;
6262 const skip_libc = b.option(bool, "skip-libc", "Main test suite skips tests that link libc") orelse false;
63 const skip_wasi = b.option(bool, "skip-wasi", "Main test suite skips WASI build") orelse false;
6364
6465 const only_install_lib_files = b.option(bool, "lib-files-only", "Only install library files") orelse false;
6566 const enable_llvm = b.option(bool, "enable-llvm", "Build self-hosted compiler with LLVM backend enabled") orelse false;
......@@ -115,11 +116,12 @@ pub fn build(b: *Builder) !void {
115116 const fmt_step = b.step("test-fmt", "Run zig fmt against build.zig to make sure it works");
116117 fmt_step.dependOn(&fmt_build_zig.step);
117118
118 test_step.dependOn(tests.addPkgTests(b, test_filter, "test/stage1/behavior.zig", "behavior", "Run the behavior tests", modes, false, skip_non_native, skip_libc, is_wine_enabled, is_qemu_enabled, glibc_multi_dir));
119 // TODO for the moment, skip wasm32-wasi until bugs are sorted out.
120 test_step.dependOn(tests.addPkgTests(b, test_filter, "test/stage1/behavior.zig", "behavior", "Run the behavior tests", modes, false, skip_non_native, skip_libc, true, is_wine_enabled, is_qemu_enabled, glibc_multi_dir));
119121
120 test_step.dependOn(tests.addPkgTests(b, test_filter, "lib/std/std.zig", "std", "Run the standard library tests", modes, false, skip_non_native, skip_libc, is_wine_enabled, is_qemu_enabled, glibc_multi_dir));
122 test_step.dependOn(tests.addPkgTests(b, test_filter, "lib/std/std.zig", "std", "Run the standard library tests", modes, false, skip_non_native, skip_libc, skip_wasi, is_wine_enabled, is_qemu_enabled, glibc_multi_dir));
121123
122 test_step.dependOn(tests.addPkgTests(b, test_filter, "lib/std/special/compiler_rt.zig", "compiler-rt", "Run the compiler_rt tests", modes, true, skip_non_native, true, is_wine_enabled, is_qemu_enabled, glibc_multi_dir));
124 test_step.dependOn(tests.addPkgTests(b, test_filter, "lib/std/special/compiler_rt.zig", "compiler-rt", "Run the compiler_rt tests", modes, true, skip_non_native, true, skip_wasi, is_wine_enabled, is_qemu_enabled, glibc_multi_dir));
123125
124126 test_step.dependOn(tests.addCompareOutputTests(b, test_filter, modes));
125127 test_step.dependOn(tests.addStandaloneTests(b, test_filter, modes));
......@@ -130,7 +132,7 @@ pub fn build(b: *Builder) !void {
130132 test_step.dependOn(tests.addTranslateCTests(b, test_filter));
131133 test_step.dependOn(tests.addRunTranslatedCTests(b, test_filter));
132134 // tests for this feature are disabled until we have the self-hosted compiler available
133 //test_step.dependOn(tests.addGenHTests(b, test_filter));
135 // test_step.dependOn(tests.addGenHTests(b, test_filter));
134136 test_step.dependOn(tests.addCompileErrorTests(b, test_filter, modes));
135137 test_step.dependOn(docs_step);
136138}
lib/std/build.zig+2
......@@ -2089,6 +2089,8 @@ pub const LibExeObjStep = struct {
20892089 .wasmtime => |bin_name| if (self.enable_wasmtime) {
20902090 try zig_args.append("--test-cmd");
20912091 try zig_args.append(bin_name);
2092 try zig_args.append("--test-cmd");
2093 try zig_args.append("--dir=.");
20922094 try zig_args.append("--test-cmd-bin");
20932095 },
20942096 }
lib/std/fmt.zig+5-2
......@@ -1684,12 +1684,15 @@ test "vector" {
16841684 // https://github.com/ziglang/zig/issues/4486
16851685 return error.SkipZigTest;
16861686 }
1687 if (builtin.arch != .wasm32) {
1688 // TODO investigate why this fails on wasm32
1689 const vbool: std.meta.Vector(4, bool) = [_]bool{ true, false, true, false };
1690 try testFmt("{ true, false, true, false }", "{}", .{vbool});
1691 }
16871692
1688 const vbool: std.meta.Vector(4, bool) = [_]bool{ true, false, true, false };
16891693 const vi64: std.meta.Vector(4, i64) = [_]i64{ -2, -1, 0, 1 };
16901694 const vu64: std.meta.Vector(4, u64) = [_]u64{ 1000, 2000, 3000, 4000 };
16911695
1692 try testFmt("{ true, false, true, false }", "{}", .{vbool});
16931696 try testFmt("{ -2, -1, 0, 1 }", "{}", .{vi64});
16941697 try testFmt("{ - 2, - 1, + 0, + 1 }", "{d:5}", .{vi64});
16951698 try testFmt("{ 1000, 2000, 3000, 4000 }", "{}", .{vu64});
lib/std/fs.zig+151-36
......@@ -44,6 +44,8 @@ pub const MAX_PATH_BYTES = switch (builtin.os.tag) {
4444 // pair in the UTF-16LE, and we (over)account 3 bytes for it that way.
4545 // +1 for the null byte at the end, which can be encoded in 1 byte.
4646 .windows => os.windows.PATH_MAX_WIDE * 3 + 1,
47 // TODO work out what a reasonable value we should use here
48 .wasi => 4096,
4749 else => @compileError("Unsupported OS"),
4850};
4951
......@@ -155,7 +157,7 @@ pub const AtomicFile = struct {
155157 try crypto.randomBytes(rand_buf[0..]);
156158 base64_encoder.encode(&tmp_path_buf, &rand_buf);
157159
158 const file = dir.createFileZ(
160 const file = dir.createFile(
159161 &tmp_path_buf,
160162 .{ .mode = mode, .exclusive = true },
161163 ) catch |err| switch (err) {
......@@ -182,7 +184,7 @@ pub const AtomicFile = struct {
182184 self.file_open = false;
183185 }
184186 if (self.file_exists) {
185 self.dir.deleteFileZ(&self.tmp_path_buf) catch {};
187 self.dir.deleteFile(&self.tmp_path_buf) catch {};
186188 self.file_exists = false;
187189 }
188190 if (self.close_dir_on_deinit) {
......@@ -197,16 +199,8 @@ pub const AtomicFile = struct {
197199 self.file.close();
198200 self.file_open = false;
199201 }
200 if (std.Target.current.os.tag == .windows) {
201 const dest_path_w = try os.windows.sliceToPrefixedFileW(self.dest_basename);
202 const tmp_path_w = try os.windows.cStrToPrefixedFileW(&self.tmp_path_buf);
203 try os.renameatW(self.dir.fd, tmp_path_w.span(), self.dir.fd, dest_path_w.span(), os.windows.TRUE);
204 self.file_exists = false;
205 } else {
206 const dest_path_c = try os.toPosixPath(self.dest_basename);
207 try os.renameatZ(self.dir.fd, &self.tmp_path_buf, self.dir.fd, &dest_path_c);
208 self.file_exists = false;
209 }
202 try os.renameat(self.dir.fd, self.tmp_path_buf[0..], self.dir.fd, self.dest_basename);
203 self.file_exists = false;
210204 }
211205};
212206
......@@ -522,6 +516,66 @@ pub const Dir = struct {
522516 }
523517 }
524518 },
519 .wasi => struct {
520 dir: Dir,
521 buf: [8192]u8, // TODO align(@alignOf(os.wasi.dirent_t)),
522 cookie: u64,
523 index: usize,
524 end_index: usize,
525
526 const Self = @This();
527
528 pub const Error = IteratorError;
529
530 /// Memory such as file names referenced in this returned entry becomes invalid
531 /// with subsequent calls to `next`, as well as when this `Dir` is deinitialized.
532 pub fn next(self: *Self) Error!?Entry {
533 const w = os.wasi;
534 start_over: while (true) {
535 if (self.index >= self.end_index) {
536 var bufused: usize = undefined;
537 switch (w.fd_readdir(self.dir.fd, &self.buf, self.buf.len, self.cookie, &bufused)) {
538 w.ESUCCESS => {},
539 w.EBADF => unreachable, // Dir is invalid or was opened without iteration ability
540 w.EFAULT => unreachable,
541 w.ENOTDIR => unreachable,
542 w.EINVAL => unreachable,
543 else => |err| return os.unexpectedErrno(err),
544 }
545 if (bufused == 0) return null;
546 self.index = 0;
547 self.end_index = bufused;
548 }
549 const entry = @ptrCast(*align(1) os.wasi.dirent_t, &self.buf[self.index]);
550 const entry_size = @sizeOf(os.wasi.dirent_t);
551 const name_index = self.index + entry_size;
552 const name = mem.span(self.buf[name_index .. name_index + entry.d_namlen]);
553
554 const next_index = name_index + entry.d_namlen;
555 self.index = next_index;
556 self.cookie = entry.d_next;
557
558 // skip . and .. entries
559 if (mem.eql(u8, name, ".") or mem.eql(u8, name, "..")) {
560 continue :start_over;
561 }
562
563 const entry_kind = switch (entry.d_type) {
564 wasi.FILETYPE_BLOCK_DEVICE => Entry.Kind.BlockDevice,
565 wasi.FILETYPE_CHARACTER_DEVICE => Entry.Kind.CharacterDevice,
566 wasi.FILETYPE_DIRECTORY => Entry.Kind.Directory,
567 wasi.FILETYPE_SYMBOLIC_LINK => Entry.Kind.SymLink,
568 wasi.FILETYPE_REGULAR_FILE => Entry.Kind.File,
569 wasi.FILETYPE_SOCKET_STREAM, wasi.FILETYPE_SOCKET_DGRAM => Entry.Kind.UnixDomainSocket,
570 else => Entry.Kind.Unknown,
571 };
572 return Entry{
573 .name = name,
574 .kind = entry_kind,
575 };
576 }
577 }
578 },
525579 else => @compileError("unimplemented"),
526580 };
527581
......@@ -548,6 +602,13 @@ pub const Dir = struct {
548602 .buf = undefined,
549603 .name_data = undefined,
550604 },
605 .wasi => return Iterator{
606 .dir = self,
607 .cookie = os.wasi.DIRCOOKIE_START,
608 .index = 0,
609 .end_index = 0,
610 .buf = undefined,
611 },
551612 else => @compileError("unimplemented"),
552613 }
553614 }
......@@ -595,24 +656,25 @@ pub const Dir = struct {
595656 /// Save as `openFile` but WASI only.
596657 pub fn openFileWasi(self: Dir, sub_path: []const u8, flags: File.OpenFlags) File.OpenError!File {
597658 const w = os.wasi;
598 var fdflags: w.fdflag_t = 0x0;
599 var rights: w.rights_t = 0x0;
659 var fdflags: w.fdflags_t = 0x0;
660 var base: w.rights_t = 0x0;
600661 if (flags.read) {
601 rights |= w.FD_READ | w.FD_TELL | w.FD_FILESTAT_GET;
662 base |= w.RIGHT_FD_READ | w.RIGHT_FD_TELL | w.RIGHT_FD_SEEK | w.RIGHT_FD_FILESTAT_GET;
602663 }
603664 if (flags.write) {
604665 fdflags |= w.FDFLAG_APPEND;
605 rights |= w.FD_WRITE |
606 w.FD_DATASYNC |
607 w.FD_SEEK |
608 w.FD_FDSTAT_SET_FLAGS |
609 w.FD_SYNC |
610 w.FD_ALLOCATE |
611 w.FD_ADVISE |
612 w.FD_FILESTAT_SET_TIMES |
613 w.FD_FILESTAT_SET_SIZE;
666 base |= w.RIGHT_FD_WRITE |
667 w.RIGHT_FD_TELL |
668 w.RIGHT_FD_SEEK |
669 w.RIGHT_FD_DATASYNC |
670 w.RIGHT_FD_FDSTAT_SET_FLAGS |
671 w.RIGHT_FD_SYNC |
672 w.RIGHT_FD_ALLOCATE |
673 w.RIGHT_FD_ADVISE |
674 w.RIGHT_FD_FILESTAT_SET_TIMES |
675 w.RIGHT_FD_FILESTAT_SET_SIZE;
614676 }
615 const fd = try wasi.openat(self.fd, sub_path, 0x0, fdflags, rights);
677 const fd = try os.openatWasi(self.fd, sub_path, 0x0, fdflags, base, 0x0);
616678 return File{ .handle = fd };
617679 }
618680
......@@ -712,17 +774,19 @@ pub const Dir = struct {
712774 pub fn createFileWasi(self: Dir, sub_path: []const u8, flags: File.CreateFlags) File.OpenError!File {
713775 const w = os.wasi;
714776 var oflags = w.O_CREAT;
715 var rights = w.RIGHT_FD_WRITE |
777 var base: w.rights_t = w.RIGHT_FD_WRITE |
716778 w.RIGHT_FD_DATASYNC |
717779 w.RIGHT_FD_SEEK |
780 w.RIGHT_FD_TELL |
718781 w.RIGHT_FD_FDSTAT_SET_FLAGS |
719782 w.RIGHT_FD_SYNC |
720783 w.RIGHT_FD_ALLOCATE |
721784 w.RIGHT_FD_ADVISE |
722785 w.RIGHT_FD_FILESTAT_SET_TIMES |
723 w.RIGHT_FD_FILESTAT_SET_SIZE;
786 w.RIGHT_FD_FILESTAT_SET_SIZE |
787 w.RIGHT_FD_FILESTAT_GET;
724788 if (flags.read) {
725 rights |= w.RIGHT_FD_READ | w.RIGHT_FD_TELL | w.RIGHT_FD_FILESTAT_GET;
789 base |= w.RIGHT_FD_READ;
726790 }
727791 if (flags.truncate) {
728792 oflags |= w.O_TRUNC;
......@@ -730,7 +794,7 @@ pub const Dir = struct {
730794 if (flags.exclusive) {
731795 oflags |= w.O_EXCL;
732796 }
733 const fd = try wasi.openat(self.fd, sub_path, oflags, 0x0, rights);
797 const fd = try os.openatWasi(self.fd, sub_path, oflags, 0x0, base, 0x0);
734798 return File{ .handle = fd };
735799 }
736800
......@@ -877,6 +941,9 @@ pub const Dir = struct {
877941 /// Not all targets support this. For example, WASI does not have the concept
878942 /// of a current working directory.
879943 pub fn setAsCwd(self: Dir) !void {
944 if (builtin.os.tag == .wasi) {
945 @compileError("changing cwd is not currently possible in WASI");
946 }
880947 try os.fchdir(self.fd);
881948 }
882949
......@@ -899,6 +966,8 @@ pub const Dir = struct {
899966 if (builtin.os.tag == .windows) {
900967 const sub_path_w = try os.windows.sliceToPrefixedFileW(sub_path);
901968 return self.openDirW(sub_path_w.span().ptr, args);
969 } else if (builtin.os.tag == .wasi) {
970 return self.openDirWasi(sub_path, args);
902971 } else {
903972 const sub_path_c = try os.toPosixPath(sub_path);
904973 return self.openDirZ(&sub_path_c, args);
......@@ -907,6 +976,44 @@ pub const Dir = struct {
907976
908977 pub const openDirC = @compileError("deprecated: renamed to openDirZ");
909978
979 /// Same as `openDir` except only WASI.
980 pub fn openDirWasi(self: Dir, sub_path: []const u8, args: OpenDirOptions) OpenError!Dir {
981 const w = os.wasi;
982 var base: w.rights_t = w.RIGHT_FD_FILESTAT_GET | w.RIGHT_FD_FDSTAT_SET_FLAGS | w.RIGHT_FD_FILESTAT_SET_TIMES;
983 if (args.iterate) {
984 base |= w.RIGHT_FD_READDIR;
985 }
986 if (args.access_sub_paths) {
987 base |= w.RIGHT_PATH_CREATE_DIRECTORY |
988 w.RIGHT_PATH_CREATE_FILE |
989 w.RIGHT_PATH_LINK_SOURCE |
990 w.RIGHT_PATH_LINK_TARGET |
991 w.RIGHT_PATH_OPEN |
992 w.RIGHT_PATH_READLINK |
993 w.RIGHT_PATH_RENAME_SOURCE |
994 w.RIGHT_PATH_RENAME_TARGET |
995 w.RIGHT_PATH_FILESTAT_GET |
996 w.RIGHT_PATH_FILESTAT_SET_SIZE |
997 w.RIGHT_PATH_FILESTAT_SET_TIMES |
998 w.RIGHT_PATH_SYMLINK |
999 w.RIGHT_PATH_REMOVE_DIRECTORY |
1000 w.RIGHT_PATH_UNLINK_FILE;
1001 }
1002 // TODO do we really need all the rights here?
1003 const inheriting: w.rights_t = w.RIGHT_ALL ^ w.RIGHT_SOCK_SHUTDOWN;
1004
1005 const result = os.openatWasi(self.fd, sub_path, w.O_DIRECTORY, 0x0, base, inheriting);
1006 const fd = result catch |err| switch (err) {
1007 error.FileTooBig => unreachable, // can't happen for directories
1008 error.IsDir => unreachable, // we're providing O_DIRECTORY
1009 error.NoSpaceLeft => unreachable, // not providing O_CREAT
1010 error.PathAlreadyExists => unreachable, // not providing O_CREAT
1011 error.FileLocksNotSupported => unreachable, // locking folders is not supported
1012 else => |e| return e,
1013 };
1014 return Dir{ .fd = fd };
1015 }
1016
9101017 /// Same as `openDir` except the parameter is null-terminated.
9111018 pub fn openDirZ(self: Dir, sub_path_c: [*:0]const u8, args: OpenDirOptions) OpenError!Dir {
9121019 if (builtin.os.tag == .windows) {
......@@ -1054,9 +1161,15 @@ pub const Dir = struct {
10541161 if (builtin.os.tag == .windows) {
10551162 const sub_path_w = try os.windows.sliceToPrefixedFileW(sub_path);
10561163 return self.deleteDirW(sub_path_w.span().ptr);
1164 } else if (builtin.os.tag == .wasi) {
1165 os.unlinkat(self.fd, sub_path, os.AT_REMOVEDIR) catch |err| switch (err) {
1166 error.IsDir => unreachable, // not possible since we pass AT_REMOVEDIR
1167 else => |e| return e,
1168 };
1169 } else {
1170 const sub_path_c = try os.toPosixPath(sub_path);
1171 return self.deleteDirZ(&sub_path_c);
10571172 }
1058 const sub_path_c = try os.toPosixPath(sub_path);
1059 return self.deleteDirZ(&sub_path_c);
10601173 }
10611174
10621175 /// Same as `deleteDir` except the parameter is null-terminated.
......@@ -1448,7 +1561,7 @@ pub fn cwd() Dir {
14481561 if (builtin.os.tag == .windows) {
14491562 return Dir{ .fd = os.windows.peb().ProcessParameters.CurrentDirectory.Handle };
14501563 } else if (builtin.os.tag == .wasi) {
1451 @compileError("WASI doesn't have a concept of cwd; use TODO instead");
1564 @compileError("WASI doesn't have a concept of cwd(); use std.fs.wasi.PreopenList to get available Dir handles instead");
14521565 } else {
14531566 return Dir{ .fd = os.AT_FDCWD };
14541567 }
......@@ -1754,10 +1867,12 @@ pub fn realpathAlloc(allocator: *Allocator, pathname: []const u8) ![]u8 {
17541867}
17551868
17561869test "" {
1757 _ = makeDirAbsolute;
1758 _ = makeDirAbsoluteZ;
1759 _ = copyFileAbsolute;
1760 _ = updateFileAbsolute;
1870 if (builtin.os.tag != .wasi) {
1871 _ = makeDirAbsolute;
1872 _ = makeDirAbsoluteZ;
1873 _ = copyFileAbsolute;
1874 _ = updateFileAbsolute;
1875 }
17611876 _ = Dir.copyFile;
17621877 _ = @import("fs/test.zig");
17631878 _ = @import("fs/path.zig");
lib/std/fs/file.zig+10-1
......@@ -140,6 +140,14 @@ pub const File = struct {
140140 if (builtin.os.tag == .windows) {
141141 return os.isCygwinPty(self.handle);
142142 }
143 if (builtin.os.tag == .wasi) {
144 // WASI sanitizes stdout when fd is a tty so ANSI escape codes
145 // will not be interpreted as actual cursor commands.
146 if (self.handle == os.STDOUT_FILENO and self.isTty()) return false;
147 // stderr is always sanitized.
148 if (self.handle == os.STDERR_FILENO) return false;
149 return true;
150 }
143151 if (self.isTty()) {
144152 if (self.handle == os.STDOUT_FILENO or self.handle == os.STDERR_FILENO) {
145153 // Use getenvC to workaround https://github.com/ziglang/zig/issues/3511
......@@ -259,10 +267,11 @@ pub const File = struct {
259267 const atime = st.atime();
260268 const mtime = st.mtime();
261269 const ctime = st.ctime();
270 const m = if (builtin.os.tag == .wasi) 0 else st.mode;
262271 return Stat{
263272 .inode = st.ino,
264273 .size = @bitCast(u64, st.size),
265 .mode = st.mode,
274 .mode = m,
266275 .atime = @as(i64, atime.tv_sec) * std.time.ns_per_s + atime.tv_nsec,
267276 .mtime = @as(i64, mtime.tv_sec) * std.time.ns_per_s + mtime.tv_nsec,
268277 .ctime = @as(i64, ctime.tv_sec) * std.time.ns_per_s + ctime.tv_nsec,
lib/std/fs/get_app_data_dir.zig+2
......@@ -56,6 +56,8 @@ pub fn getAppDataDir(allocator: *mem.Allocator, appname: []const u8) GetAppDataD
5656}
5757
5858test "getAppDataDir" {
59 if (builtin.os.tag == .wasi) return error.SkipZigTest;
60
5961 // We can't actually validate the result
6062 const dir = getAppDataDir(std.testing.allocator, "zig") catch return;
6163 defer std.testing.allocator.free(dir);
lib/std/fs/path.zig+9-2
......@@ -653,6 +653,8 @@ pub fn resolvePosix(allocator: *Allocator, paths: []const []const u8) ![]u8 {
653653}
654654
655655test "resolve" {
656 if (builtin.os.tag == .wasi) return error.SkipZigTest;
657
656658 const cwd = try process.getCwdAlloc(testing.allocator);
657659 defer testing.allocator.free(cwd);
658660 if (builtin.os.tag == .windows) {
......@@ -667,10 +669,11 @@ test "resolve" {
667669}
668670
669671test "resolveWindows" {
670 if (@import("builtin").arch == .aarch64) {
672 if (builtin.arch == .aarch64) {
671673 // TODO https://github.com/ziglang/zig/issues/3288
672674 return error.SkipZigTest;
673675 }
676 if (builtin.os.tag == .wasi) return error.SkipZigTest;
674677 if (builtin.os.tag == .windows) {
675678 const cwd = try process.getCwdAlloc(testing.allocator);
676679 defer testing.allocator.free(cwd);
......@@ -715,6 +718,8 @@ test "resolveWindows" {
715718}
716719
717720test "resolvePosix" {
721 if (builtin.os.tag == .wasi) return error.SkipZigTest;
722
718723 try testResolvePosix(&[_][]const u8{ "/a/b", "c" }, "/a/b/c");
719724 try testResolvePosix(&[_][]const u8{ "/a/b", "c", "//d", "e///" }, "/d/e");
720725 try testResolvePosix(&[_][]const u8{ "/a/b/c", "..", "../" }, "/a");
......@@ -1116,10 +1121,12 @@ pub fn relativePosix(allocator: *Allocator, from: []const u8, to: []const u8) ![
11161121}
11171122
11181123test "relative" {
1119 if (@import("builtin").arch == .aarch64) {
1124 if (builtin.arch == .aarch64) {
11201125 // TODO https://github.com/ziglang/zig/issues/3288
11211126 return error.SkipZigTest;
11221127 }
1128 if (builtin.os.tag == .wasi) return error.SkipZigTest;
1129
11231130 try testRelativeWindows("c:/blah\\blah", "d:/games", "D:\\games");
11241131 try testRelativeWindows("c:/aaaa/bbbb", "c:/aaaa", "..");
11251132 try testRelativeWindows("c:/aaaa/bbbb", "c:/cccc", "..\\..\\cccc");
lib/std/fs/test.zig+6
......@@ -4,6 +4,8 @@ const fs = std.fs;
44const File = std.fs.File;
55
66test "openSelfExe" {
7 if (builtin.os.tag == .wasi) return error.SkipZigTest;
8
79 const self_exe_file = try std.fs.openSelfExe();
810 self_exe_file.close();
911}
......@@ -11,6 +13,8 @@ test "openSelfExe" {
1113const FILE_LOCK_TEST_SLEEP_TIME = 5 * std.time.millisecond;
1214
1315test "open file with exclusive nonblocking lock twice" {
16 if (builtin.os.tag == .wasi) return error.SkipZigTest;
17
1418 const dir = fs.cwd();
1519 const filename = "file_nonblocking_lock_test.txt";
1620
......@@ -111,6 +115,8 @@ test "create file, lock and read from multiple process at once" {
111115}
112116
113117test "open file with exclusive nonblocking lock twice (absolute paths)" {
118 if (builtin.os.tag == .wasi) return error.SkipZigTest;
119
114120 const allocator = std.testing.allocator;
115121
116122 const file_paths: [1][]const u8 = .{"zig-test-absolute-paths.txt"};
lib/std/fs/wasi.zig-11
......@@ -138,14 +138,3 @@ pub const PreopenList = struct {
138138 return self.buffer.toOwnedSlice();
139139 }
140140};
141
142/// Convenience wrapper for `std.os.wasi.path_open` syscall.
143pub fn openat(dir_fd: fd_t, file_path: []const u8, oflags: oflags_t, fdflags: fdflags_t, rights: rights_t) os.OpenError!fd_t {
144 var fd: fd_t = undefined;
145 switch (path_open(dir_fd, 0x0, file_path.ptr, file_path.len, oflags, rights, 0x0, fdflags, &fd)) {
146 0 => {},
147 // TODO map errors
148 else => |err| return std.os.unexpectedErrno(err),
149 }
150 return fd;
151}
lib/std/io/test.zig+20-14
......@@ -11,15 +11,17 @@ const mem = std.mem;
1111const fs = std.fs;
1212const File = std.fs.File;
1313
14const getTestDir = std.testing.getTestDir;
15
1416test "write a file, read it, then delete it" {
15 const cwd = fs.cwd();
17 const test_dir = getTestDir();
1618
1719 var data: [1024]u8 = undefined;
1820 var prng = DefaultPrng.init(1234);
1921 prng.random.bytes(data[0..]);
2022 const tmp_file_name = "temp_test_file.txt";
2123 {
22 var file = try cwd.createFile(tmp_file_name, .{});
24 var file = try test_dir.createFile(tmp_file_name, .{});
2325 defer file.close();
2426
2527 var buf_stream = io.bufferedOutStream(file.outStream());
......@@ -32,7 +34,7 @@ test "write a file, read it, then delete it" {
3234
3335 {
3436 // Make sure the exclusive flag is honored.
35 if (cwd.createFile(tmp_file_name, .{ .exclusive = true })) |file| {
37 if (test_dir.createFile(tmp_file_name, .{ .exclusive = true })) |file| {
3638 unreachable;
3739 } else |err| {
3840 std.debug.assert(err == File.OpenError.PathAlreadyExists);
......@@ -40,7 +42,7 @@ test "write a file, read it, then delete it" {
4042 }
4143
4244 {
43 var file = try cwd.openFile(tmp_file_name, .{});
45 var file = try test_dir.openFile(tmp_file_name, .{});
4446 defer file.close();
4547
4648 const file_size = try file.getEndPos();
......@@ -56,13 +58,14 @@ test "write a file, read it, then delete it" {
5658 expect(mem.eql(u8, contents["begin".len .. contents.len - "end".len], &data));
5759 expect(mem.eql(u8, contents[contents.len - "end".len ..], "end"));
5860 }
59 try cwd.deleteFile(tmp_file_name);
61 try test_dir.deleteFile(tmp_file_name);
6062}
6163
6264test "BitStreams with File Stream" {
65 var test_dir = getTestDir();
6366 const tmp_file_name = "temp_test_file.txt";
6467 {
65 var file = try fs.cwd().createFile(tmp_file_name, .{});
68 var file = try test_dir.createFile(tmp_file_name, .{});
6669 defer file.close();
6770
6871 var bit_stream = io.bitOutStream(builtin.endian, file.outStream());
......@@ -76,7 +79,7 @@ test "BitStreams with File Stream" {
7679 try bit_stream.flushBits();
7780 }
7881 {
79 var file = try fs.cwd().openFile(tmp_file_name, .{});
82 var file = try test_dir.openFile(tmp_file_name, .{});
8083 defer file.close();
8184
8285 var bit_stream = io.bitInStream(builtin.endian, file.inStream());
......@@ -98,15 +101,16 @@ test "BitStreams with File Stream" {
98101
99102 expectError(error.EndOfStream, bit_stream.readBitsNoEof(u1, 1));
100103 }
101 try fs.cwd().deleteFile(tmp_file_name);
104 try test_dir.deleteFile(tmp_file_name);
102105}
103106
104107test "File seek ops" {
108 var test_dir = getTestDir();
105109 const tmp_file_name = "temp_test_file.txt";
106 var file = try fs.cwd().createFile(tmp_file_name, .{});
110 var file = try test_dir.createFile(tmp_file_name, .{});
107111 defer {
108112 file.close();
109 fs.cwd().deleteFile(tmp_file_name) catch {};
113 test_dir.deleteFile(tmp_file_name) catch {};
110114 }
111115
112116 try file.writeAll(&([_]u8{0x55} ** 8192));
......@@ -129,11 +133,12 @@ test "setEndPos" {
129133 // https://github.com/ziglang/zig/issues/5127
130134 if (std.Target.current.cpu.arch == .mips) return error.SkipZigTest;
131135
136 var test_dir = getTestDir();
132137 const tmp_file_name = "temp_test_file.txt";
133 var file = try fs.cwd().createFile(tmp_file_name, .{});
138 var file = try test_dir.createFile(tmp_file_name, .{});
134139 defer {
135140 file.close();
136 fs.cwd().deleteFile(tmp_file_name) catch {};
141 test_dir.deleteFile(tmp_file_name) catch {};
137142 }
138143
139144 // Verify that the file size changes and the file offset is not moved
......@@ -152,11 +157,12 @@ test "setEndPos" {
152157}
153158
154159test "updateTimes" {
160 var test_dir = getTestDir();
155161 const tmp_file_name = "just_a_temporary_file.txt";
156 var file = try fs.cwd().createFile(tmp_file_name, .{ .read = true });
162 var file = try test_dir.createFile(tmp_file_name, .{ .read = true });
157163 defer {
158164 file.close();
159 std.fs.cwd().deleteFile(tmp_file_name) catch {};
165 test_dir.deleteFile(tmp_file_name) catch {};
160166 }
161167 var stat_old = try file.stat();
162168 // Set atime and mtime to 5s before
lib/std/net/test.zig+10-1
......@@ -1,9 +1,12 @@
11const std = @import("../std.zig");
2const builtin = std.builtin;
23const net = std.net;
34const mem = std.mem;
45const testing = std.testing;
56
67test "parse and render IPv6 addresses" {
8 if (builtin.os.tag == .wasi) return error.SkipZigTest;
9
710 var buffer: [100]u8 = undefined;
811 const ips = [_][]const u8{
912 "FF01:0:0:0:0:0:0:FB",
......@@ -42,6 +45,8 @@ test "parse and render IPv6 addresses" {
4245}
4346
4447test "parse and render IPv4 addresses" {
48 if (builtin.os.tag == .wasi) return error.SkipZigTest;
49
4550 var buffer: [18]u8 = undefined;
4651 for ([_][]const u8{
4752 "0.0.0.0",
......@@ -63,7 +68,7 @@ test "parse and render IPv4 addresses" {
6368}
6469
6570test "resolve DNS" {
66 if (std.builtin.os.tag == .windows) {
71 if (builtin.os.tag == .windows or builtin.os.tag == .wasi) {
6772 // DNS resolution not implemented on Windows yet.
6873 return error.SkipZigTest;
6974 }
......@@ -101,6 +106,8 @@ test "listen on a port, send bytes, receive bytes" {
101106}
102107
103108fn testClient(addr: net.Address) anyerror!void {
109 if (builtin.os.tag == .wasi) return error.SkipZigTest;
110
104111 const socket_file = try net.tcpConnectToAddress(addr);
105112 defer socket_file.close();
106113
......@@ -111,6 +118,8 @@ fn testClient(addr: net.Address) anyerror!void {
111118}
112119
113120fn testServer(server: *net.StreamServer) anyerror!void {
121 if (builtin.os.tag == .wasi) return error.SkipZigTest;
122
114123 var client = try server.accept();
115124
116125 const stream = client.file.outStream();
lib/std/os.zig+376-8
......@@ -98,6 +98,7 @@ pub fn close(fd: fd_t) void {
9898 }
9999 if (builtin.os.tag == .wasi) {
100100 _ = wasi.fd_close(fd);
101 return;
101102 }
102103 if (comptime std.Target.current.isDarwin()) {
103104 // This avoids the EINTR problem.
......@@ -312,7 +313,6 @@ pub fn read(fd: fd_t, buf: []u8) ReadError!usize {
312313 if (builtin.os.tag == .windows) {
313314 return windows.ReadFile(fd, buf, null, std.io.default_mode);
314315 }
315
316316 if (builtin.os.tag == .wasi and !builtin.link_libc) {
317317 const iovs = [1]iovec{iovec{
318318 .iov_base = buf.ptr,
......@@ -321,7 +321,18 @@ pub fn read(fd: fd_t, buf: []u8) ReadError!usize {
321321
322322 var nread: usize = undefined;
323323 switch (wasi.fd_read(fd, &iovs, iovs.len, &nread)) {
324 0 => return nread,
324 wasi.ESUCCESS => return nread,
325 wasi.EINTR => unreachable,
326 wasi.EINVAL => unreachable,
327 wasi.EFAULT => unreachable,
328 wasi.EAGAIN => unreachable,
329 wasi.EBADF => unreachable, // Always a race condition.
330 wasi.EIO => return error.InputOutput,
331 wasi.EISDIR => return error.IsDir,
332 wasi.ENOBUFS => return error.SystemResources,
333 wasi.ENOMEM => return error.SystemResources,
334 wasi.ECONNRESET => return error.ConnectionResetByPeer,
335 wasi.ETIMEDOUT => return error.ConnectionTimedOut,
325336 else => |err| return unexpectedErrno(err),
326337 }
327338 }
......@@ -376,6 +387,22 @@ pub fn readv(fd: fd_t, iov: []const iovec) ReadError!usize {
376387 const first = iov[0];
377388 return read(fd, first.iov_base[0..first.iov_len]);
378389 }
390 if (builtin.os.tag == .wasi) {
391 var nread: usize = undefined;
392 switch (wasi.fd_read(fd, iov.ptr, iov.len, &nread)) {
393 wasi.ESUCCESS => return nread,
394 wasi.EINTR => unreachable,
395 wasi.EINVAL => unreachable,
396 wasi.EFAULT => unreachable,
397 wasi.EAGAIN => unreachable, // currently not support in WASI
398 wasi.EBADF => unreachable, // always a race condition
399 wasi.EIO => return error.InputOutput,
400 wasi.EISDIR => return error.IsDir,
401 wasi.ENOBUFS => return error.SystemResources,
402 wasi.ENOMEM => return error.SystemResources,
403 else => |err| return unexpectedErrno(err),
404 }
405 }
379406
380407 const iov_count = math.cast(u31, iov.len) catch math.maxInt(u31);
381408 while (true) {
......@@ -416,6 +443,31 @@ pub fn pread(fd: fd_t, buf: []u8, offset: u64) PReadError!usize {
416443 if (builtin.os.tag == .windows) {
417444 return windows.ReadFile(fd, buf, offset, std.io.default_mode);
418445 }
446 if (builtin.os.tag == .wasi) {
447 const iovs = [1]iovec{iovec{
448 .iov_base = buf.ptr,
449 .iov_len = buf.len,
450 }};
451
452 var nread: usize = undefined;
453 switch (wasi.fd_pread(fd, &iovs, iovs.len, offset, &nread)) {
454 wasi.ESUCCESS => return nread,
455 wasi.EINTR => unreachable,
456 wasi.EINVAL => unreachable,
457 wasi.EFAULT => unreachable,
458 wasi.EAGAIN => unreachable,
459 wasi.EBADF => unreachable, // Always a race condition.
460 wasi.EIO => return error.InputOutput,
461 wasi.EISDIR => return error.IsDir,
462 wasi.ENOBUFS => return error.SystemResources,
463 wasi.ENOMEM => return error.SystemResources,
464 wasi.ECONNRESET => return error.ConnectionResetByPeer,
465 wasi.ENXIO => return error.Unseekable,
466 wasi.ESPIPE => return error.Unseekable,
467 wasi.EOVERFLOW => return error.Unseekable,
468 else => |err| return unexpectedErrno(err),
469 }
470 }
419471
420472 while (true) {
421473 const rc = system.pread(fd, buf.ptr, buf.len, offset);
......@@ -442,7 +494,6 @@ pub fn pread(fd: fd_t, buf: []u8, offset: u64) PReadError!usize {
442494 else => |err| return unexpectedErrno(err),
443495 }
444496 }
445 return index;
446497}
447498
448499pub const TruncateError = error{
......@@ -474,6 +525,19 @@ pub fn ftruncate(fd: fd_t, length: u64) TruncateError!void {
474525 else => return windows.unexpectedStatus(rc),
475526 }
476527 }
528 if (std.Target.current.os.tag == .wasi) {
529 switch (wasi.fd_filestat_set_size(fd, length)) {
530 wasi.ESUCCESS => return,
531 wasi.EINTR => unreachable,
532 wasi.EFBIG => return error.FileTooBig,
533 wasi.EIO => return error.InputOutput,
534 wasi.EPERM => return error.CannotTruncate,
535 wasi.ETXTBSY => return error.FileBusy,
536 wasi.EBADF => unreachable, // Handle not open for writing
537 wasi.EINVAL => unreachable, // Handle not open for writing
538 else => |err| return unexpectedErrno(err),
539 }
540 }
477541
478542 while (true) {
479543 const rc = if (builtin.link_libc)
......@@ -523,6 +587,25 @@ pub fn preadv(fd: fd_t, iov: []const iovec, offset: u64) PReadError!usize {
523587 const first = iov[0];
524588 return pread(fd, first.iov_base[0..first.iov_len], offset);
525589 }
590 if (builtin.os.tag == .wasi) {
591 var nread: usize = undefined;
592 switch (wasi.fd_pread(fd, iov.ptr, iov.len, offset, &nread)) {
593 wasi.ESUCCESS => return nread,
594 wasi.EINTR => unreachable,
595 wasi.EINVAL => unreachable,
596 wasi.EFAULT => unreachable,
597 wasi.EAGAIN => unreachable,
598 wasi.EBADF => unreachable, // always a race condition
599 wasi.EIO => return error.InputOutput,
600 wasi.EISDIR => return error.IsDir,
601 wasi.ENOBUFS => return error.SystemResources,
602 wasi.ENOMEM => return error.SystemResources,
603 wasi.ENXIO => return error.Unseekable,
604 wasi.ESPIPE => return error.Unseekable,
605 wasi.EOVERFLOW => return error.Unseekable,
606 else => |err| return unexpectedErrno(err),
607 }
608 }
526609
527610 const iov_count = math.cast(u31, iov.len) catch math.maxInt(u31);
528611
......@@ -594,13 +677,25 @@ pub fn write(fd: fd_t, bytes: []const u8) WriteError!usize {
594677 }
595678
596679 if (builtin.os.tag == .wasi and !builtin.link_libc) {
597 const ciovs = [1]iovec_const{iovec_const{
680 const ciovs = [_]iovec_const{iovec_const{
598681 .iov_base = bytes.ptr,
599682 .iov_len = bytes.len,
600683 }};
601684 var nwritten: usize = undefined;
602685 switch (wasi.fd_write(fd, &ciovs, ciovs.len, &nwritten)) {
603 0 => return nwritten,
686 wasi.ESUCCESS => return nwritten,
687 wasi.EINTR => unreachable,
688 wasi.EINVAL => unreachable,
689 wasi.EFAULT => unreachable,
690 wasi.EAGAIN => unreachable,
691 wasi.EBADF => unreachable, // Always a race condition.
692 wasi.EDESTADDRREQ => unreachable, // `connect` was never called.
693 wasi.EDQUOT => return error.DiskQuota,
694 wasi.EFBIG => return error.FileTooBig,
695 wasi.EIO => return error.InputOutput,
696 wasi.ENOSPC => return error.NoSpaceLeft,
697 wasi.EPERM => return error.AccessDenied,
698 wasi.EPIPE => return error.BrokenPipe,
604699 else => |err| return unexpectedErrno(err),
605700 }
606701 }
......@@ -662,6 +757,25 @@ pub fn writev(fd: fd_t, iov: []const iovec_const) WriteError!usize {
662757 const first = iov[0];
663758 return write(fd, first.iov_base[0..first.iov_len]);
664759 }
760 if (builtin.os.tag == .wasi) {
761 var nwritten: usize = undefined;
762 switch (wasi.fd_write(fd, iov.ptr, iov.len, &nwritten)) {
763 wasi.ESUCCESS => return nwritten,
764 wasi.EINTR => unreachable,
765 wasi.EINVAL => unreachable,
766 wasi.EFAULT => unreachable,
767 wasi.EAGAIN => unreachable,
768 wasi.EBADF => unreachable, // Always a race condition.
769 wasi.EDESTADDRREQ => unreachable, // `connect` was never called.
770 wasi.EDQUOT => return error.DiskQuota,
771 wasi.EFBIG => return error.FileTooBig,
772 wasi.EIO => return error.InputOutput,
773 wasi.ENOSPC => return error.NoSpaceLeft,
774 wasi.EPERM => return error.AccessDenied,
775 wasi.EPIPE => return error.BrokenPipe,
776 else => |err| return unexpectedErrno(err),
777 }
778 }
665779
666780 const iov_count = math.cast(u31, iov.len) catch math.maxInt(u31);
667781 while (true) {
......@@ -717,6 +831,33 @@ pub fn pwrite(fd: fd_t, bytes: []const u8, offset: u64) PWriteError!usize {
717831 if (std.Target.current.os.tag == .windows) {
718832 return windows.WriteFile(fd, bytes, offset, std.io.default_mode);
719833 }
834 if (builtin.os.tag == .wasi) {
835 const ciovs = [1]iovec_const{iovec_const{
836 .iov_base = bytes.ptr,
837 .iov_len = bytes.len,
838 }};
839
840 var nwritten: usize = undefined;
841 switch (wasi.fd_pwrite(fd, &ciovs, ciovs.len, offset, &nwritten)) {
842 wasi.ESUCCESS => return nwritten,
843 wasi.EINTR => unreachable,
844 wasi.EINVAL => unreachable,
845 wasi.EFAULT => unreachable,
846 wasi.EAGAIN => unreachable,
847 wasi.EBADF => unreachable, // Always a race condition.
848 wasi.EDESTADDRREQ => unreachable, // `connect` was never called.
849 wasi.EDQUOT => return error.DiskQuota,
850 wasi.EFBIG => return error.FileTooBig,
851 wasi.EIO => return error.InputOutput,
852 wasi.ENOSPC => return error.NoSpaceLeft,
853 wasi.EPERM => return error.AccessDenied,
854 wasi.EPIPE => return error.BrokenPipe,
855 wasi.ENXIO => return error.Unseekable,
856 wasi.ESPIPE => return error.Unseekable,
857 wasi.EOVERFLOW => return error.Unseekable,
858 else => |err| return unexpectedErrno(err),
859 }
860 }
720861
721862 // Prevent EINVAL.
722863 const max_count = switch (std.Target.current.os.tag) {
......@@ -788,6 +929,28 @@ pub fn pwritev(fd: fd_t, iov: []const iovec_const, offset: u64) PWriteError!usiz
788929 const first = iov[0];
789930 return pwrite(fd, first.iov_base[0..first.iov_len], offset);
790931 }
932 if (builtin.os.tag == .wasi) {
933 var nwritten: usize = undefined;
934 switch (wasi.fd_pwrite(fd, iov.ptr, iov.len, offset, &nwritten)) {
935 wasi.ESUCCESS => return nwritten,
936 wasi.EINTR => unreachable,
937 wasi.EINVAL => unreachable,
938 wasi.EFAULT => unreachable,
939 wasi.EAGAIN => unreachable,
940 wasi.EBADF => unreachable, // Always a race condition.
941 wasi.EDESTADDRREQ => unreachable, // `connect` was never called.
942 wasi.EDQUOT => return error.DiskQuota,
943 wasi.EFBIG => return error.FileTooBig,
944 wasi.EIO => return error.InputOutput,
945 wasi.ENOSPC => return error.NoSpaceLeft,
946 wasi.EPERM => return error.AccessDenied,
947 wasi.EPIPE => return error.BrokenPipe,
948 wasi.ENXIO => return error.Unseekable,
949 wasi.ESPIPE => return error.Unseekable,
950 wasi.EOVERFLOW => return error.Unseekable,
951 else => |err| return unexpectedErrno(err),
952 }
953 }
791954
792955 const iov_count = math.cast(u31, iov.len) catch math.maxInt(u31);
793956 while (true) {
......@@ -923,6 +1086,37 @@ pub fn openat(dir_fd: fd_t, file_path: []const u8, flags: u32, mode: mode_t) Ope
9231086 return openatZ(dir_fd, &file_path_c, flags, mode);
9241087}
9251088
1089/// Open and possibly create a file in WASI.
1090pub fn openatWasi(dir_fd: fd_t, file_path: []const u8, oflags: oflags_t, fdflags: fdflags_t, base: rights_t, inheriting: rights_t) OpenError!fd_t {
1091 while (true) {
1092 var fd: fd_t = undefined;
1093 switch (wasi.path_open(dir_fd, 0x0, file_path.ptr, file_path.len, oflags, base, inheriting, fdflags, &fd)) {
1094 wasi.ESUCCESS => return fd,
1095 wasi.EINTR => continue,
1096
1097 wasi.EFAULT => unreachable,
1098 wasi.EINVAL => unreachable,
1099 wasi.EACCES => return error.AccessDenied,
1100 wasi.EFBIG => return error.FileTooBig,
1101 wasi.EOVERFLOW => return error.FileTooBig,
1102 wasi.EISDIR => return error.IsDir,
1103 wasi.ELOOP => return error.SymLinkLoop,
1104 wasi.EMFILE => return error.ProcessFdQuotaExceeded,
1105 wasi.ENAMETOOLONG => return error.NameTooLong,
1106 wasi.ENFILE => return error.SystemFdQuotaExceeded,
1107 wasi.ENODEV => return error.NoDevice,
1108 wasi.ENOENT => return error.FileNotFound,
1109 wasi.ENOMEM => return error.SystemResources,
1110 wasi.ENOSPC => return error.NoSpaceLeft,
1111 wasi.ENOTDIR => return error.NotDir,
1112 wasi.EPERM => return error.AccessDenied,
1113 wasi.EEXIST => return error.PathAlreadyExists,
1114 wasi.EBUSY => return error.DeviceBusy,
1115 else => |err| return unexpectedErrno(err),
1116 }
1117 }
1118}
1119
9261120pub const openatC = @compileError("deprecated: renamed to openatZ");
9271121
9281122/// Open and possibly create a file. Keeps trying if it gets interrupted.
......@@ -1282,6 +1476,9 @@ pub fn getcwd(out_buffer: []u8) GetCwdError![]u8 {
12821476 if (builtin.os.tag == .windows) {
12831477 return windows.GetCurrentDirectory(out_buffer);
12841478 }
1479 if (builtin.os.tag == .wasi) {
1480 @compileError("WASI doesn't have a concept of cwd(); use std.fs.wasi.PreopenList to get available Dir handles instead");
1481 }
12851482
12861483 const err = if (builtin.link_libc) blk: {
12871484 break :blk if (std.c.getcwd(out_buffer.ptr, out_buffer.len)) |_| 0 else std.c._errno().*;
......@@ -1460,13 +1657,45 @@ pub fn unlinkat(dirfd: fd_t, file_path: []const u8, flags: u32) UnlinkatError!vo
14601657 if (builtin.os.tag == .windows) {
14611658 const file_path_w = try windows.sliceToPrefixedFileW(file_path);
14621659 return unlinkatW(dirfd, file_path_w.span().ptr, flags);
1660 } else if (builtin.os.tag == .wasi) {
1661 return unlinkatWasi(dirfd, file_path, flags);
1662 } else {
1663 const file_path_c = try toPosixPath(file_path);
1664 return unlinkatZ(dirfd, &file_path_c, flags);
14631665 }
1464 const file_path_c = try toPosixPath(file_path);
1465 return unlinkatZ(dirfd, &file_path_c, flags);
14661666}
14671667
14681668pub const unlinkatC = @compileError("deprecated: renamed to unlinkatZ");
14691669
1670pub fn unlinkatWasi(dirfd: fd_t, file_path: []const u8, flags: u32) UnlinkatError!void {
1671 const remove_dir = (flags & AT_REMOVEDIR) != 0;
1672 const res = if (remove_dir)
1673 wasi.path_remove_directory(dirfd, file_path.ptr, file_path.len)
1674 else
1675 wasi.path_unlink_file(dirfd, file_path.ptr, file_path.len);
1676 switch (res) {
1677 wasi.ESUCCESS => return,
1678 wasi.EACCES => return error.AccessDenied,
1679 wasi.EPERM => return error.AccessDenied,
1680 wasi.EBUSY => return error.FileBusy,
1681 wasi.EFAULT => unreachable,
1682 wasi.EIO => return error.FileSystem,
1683 wasi.EISDIR => return error.IsDir,
1684 wasi.ELOOP => return error.SymLinkLoop,
1685 wasi.ENAMETOOLONG => return error.NameTooLong,
1686 wasi.ENOENT => return error.FileNotFound,
1687 wasi.ENOTDIR => return error.NotDir,
1688 wasi.ENOMEM => return error.SystemResources,
1689 wasi.EROFS => return error.ReadOnlyFileSystem,
1690 wasi.ENOTEMPTY => return error.DirNotEmpty,
1691
1692 wasi.EINVAL => unreachable, // invalid flags, or pathname has . as last component
1693 wasi.EBADF => unreachable, // always a race condition
1694
1695 else => |err| return unexpectedErrno(err),
1696 }
1697}
1698
14701699/// Same as `unlinkat` but `file_path` is a null-terminated string.
14711700pub fn unlinkatZ(dirfd: fd_t, file_path_c: [*:0]const u8, flags: u32) UnlinkatError!void {
14721701 if (builtin.os.tag == .windows) {
......@@ -1645,6 +1874,8 @@ pub fn renameat(
16451874 const old_path_w = try windows.sliceToPrefixedFileW(old_path);
16461875 const new_path_w = try windows.sliceToPrefixedFileW(new_path);
16471876 return renameatW(old_dir_fd, old_path_w.span(), new_dir_fd, new_path_w.span(), windows.TRUE);
1877 } else if (builtin.os.tag == .wasi) {
1878 return renameatWasi(old_dir_fd, old_path, new_dir_fd, new_path);
16481879 } else {
16491880 const old_path_c = try toPosixPath(old_path);
16501881 const new_path_c = try toPosixPath(new_path);
......@@ -1652,6 +1883,32 @@ pub fn renameat(
16521883 }
16531884}
16541885
1886/// Same as `renameat` expect only WASI.
1887pub fn renameatWasi(old_dir_fd: fd_t, old_path: []const u8, new_dir_fd: fd_t, new_path: []const u8) RenameError!void {
1888 switch (wasi.path_rename(old_dir_fd, old_path.ptr, old_path.len, new_dir_fd, new_path.ptr, new_path.len)) {
1889 wasi.ESUCCESS => return,
1890 wasi.EACCES => return error.AccessDenied,
1891 wasi.EPERM => return error.AccessDenied,
1892 wasi.EBUSY => return error.FileBusy,
1893 wasi.EDQUOT => return error.DiskQuota,
1894 wasi.EFAULT => unreachable,
1895 wasi.EINVAL => unreachable,
1896 wasi.EISDIR => return error.IsDir,
1897 wasi.ELOOP => return error.SymLinkLoop,
1898 wasi.EMLINK => return error.LinkQuotaExceeded,
1899 wasi.ENAMETOOLONG => return error.NameTooLong,
1900 wasi.ENOENT => return error.FileNotFound,
1901 wasi.ENOTDIR => return error.NotDir,
1902 wasi.ENOMEM => return error.SystemResources,
1903 wasi.ENOSPC => return error.NoSpaceLeft,
1904 wasi.EEXIST => return error.PathAlreadyExists,
1905 wasi.ENOTEMPTY => return error.PathAlreadyExists,
1906 wasi.EROFS => return error.ReadOnlyFileSystem,
1907 wasi.EXDEV => return error.RenameAcrossMountPoints,
1908 else => |err| return unexpectedErrno(err),
1909 }
1910}
1911
16551912/// Same as `renameat` except the parameters are null-terminated byte arrays.
16561913pub fn renameatZ(
16571914 old_dir_fd: fd_t,
......@@ -1767,6 +2024,8 @@ pub fn mkdirat(dir_fd: fd_t, sub_dir_path: []const u8, mode: u32) MakeDirError!v
17672024 if (builtin.os.tag == .windows) {
17682025 const sub_dir_path_w = try windows.sliceToPrefixedFileW(sub_dir_path);
17692026 return mkdiratW(dir_fd, sub_dir_path_w.span().ptr, mode);
2027 } else if (builtin.os.tag == .wasi) {
2028 return mkdiratWasi(dir_fd, sub_dir_path, mode);
17702029 } else {
17712030 const sub_dir_path_c = try toPosixPath(sub_dir_path);
17722031 return mkdiratZ(dir_fd, &sub_dir_path_c, mode);
......@@ -1775,6 +2034,27 @@ pub fn mkdirat(dir_fd: fd_t, sub_dir_path: []const u8, mode: u32) MakeDirError!v
17752034
17762035pub const mkdiratC = @compileError("deprecated: renamed to mkdiratZ");
17772036
2037pub fn mkdiratWasi(dir_fd: fd_t, sub_dir_path: []const u8, mode: u32) MakeDirError!void {
2038 switch (wasi.path_create_directory(dir_fd, sub_dir_path.ptr, sub_dir_path.len)) {
2039 wasi.ESUCCESS => return,
2040 wasi.EACCES => return error.AccessDenied,
2041 wasi.EBADF => unreachable,
2042 wasi.EPERM => return error.AccessDenied,
2043 wasi.EDQUOT => return error.DiskQuota,
2044 wasi.EEXIST => return error.PathAlreadyExists,
2045 wasi.EFAULT => unreachable,
2046 wasi.ELOOP => return error.SymLinkLoop,
2047 wasi.EMLINK => return error.LinkQuotaExceeded,
2048 wasi.ENAMETOOLONG => return error.NameTooLong,
2049 wasi.ENOENT => return error.FileNotFound,
2050 wasi.ENOMEM => return error.SystemResources,
2051 wasi.ENOSPC => return error.NoSpaceLeft,
2052 wasi.ENOTDIR => return error.NotDir,
2053 wasi.EROFS => return error.ReadOnlyFileSystem,
2054 else => |err| return unexpectedErrno(err),
2055 }
2056}
2057
17782058pub fn mkdiratZ(dir_fd: fd_t, sub_dir_path: [*:0]const u8, mode: u32) MakeDirError!void {
17792059 if (builtin.os.tag == .windows) {
17802060 const sub_dir_path_w = try windows.cStrToPrefixedFileW(sub_dir_path);
......@@ -2623,8 +2903,19 @@ pub const FStatError = error{
26232903} || UnexpectedError;
26242904
26252905pub fn fstat(fd: fd_t) FStatError!Stat {
2626 var stat: Stat = undefined;
2906 if (builtin.os.tag == .wasi) {
2907 var stat: Stat = undefined;
2908 switch (wasi.fd_filestat_get(fd, &stat)) {
2909 wasi.ESUCCESS => return stat,
2910 wasi.EINVAL => unreachable,
2911 wasi.EBADF => unreachable, // Always a race condition.
2912 wasi.ENOMEM => return error.SystemResources,
2913 wasi.EACCES => return error.AccessDenied,
2914 else => |err| return unexpectedErrno(err),
2915 }
2916 }
26272917
2918 var stat: Stat = undefined;
26282919 switch (errno(system.fstat(fd, &stat))) {
26292920 0 => return stat,
26302921 EINVAL => unreachable,
......@@ -3097,6 +3388,10 @@ pub fn sysctl(
30973388 newp: ?*c_void,
30983389 newlen: usize,
30993390) SysCtlError!void {
3391 if (builtin.os.tag == .wasi) {
3392 @panic("unsupported");
3393 }
3394
31003395 const name_len = math.cast(c_uint, name.len) catch return error.NameTooLong;
31013396 switch (errno(system.sysctl(name.ptr, name_len, oldp, oldlenp, newp, newlen))) {
31023397 0 => return,
......@@ -3117,6 +3412,10 @@ pub fn sysctlbynameZ(
31173412 newp: ?*c_void,
31183413 newlen: usize,
31193414) SysCtlError!void {
3415 if (builtin.os.tag == .wasi) {
3416 @panic("unsupported");
3417 }
3418
31203419 switch (errno(system.sysctlbyname(name, oldp, oldlenp, newp, newlen))) {
31213420 0 => return,
31223421 EFAULT => unreachable,
......@@ -3154,6 +3453,18 @@ pub fn lseek_SET(fd: fd_t, offset: u64) SeekError!void {
31543453 if (builtin.os.tag == .windows) {
31553454 return windows.SetFilePointerEx_BEGIN(fd, offset);
31563455 }
3456 if (builtin.os.tag == .wasi) {
3457 var new_offset: wasi.filesize_t = undefined;
3458 switch (wasi.fd_seek(fd, @bitCast(wasi.filedelta_t, offset), wasi.WHENCE_SET, &new_offset)) {
3459 wasi.ESUCCESS => return,
3460 wasi.EBADF => unreachable, // always a race condition
3461 wasi.EINVAL => return error.Unseekable,
3462 wasi.EOVERFLOW => return error.Unseekable,
3463 wasi.ESPIPE => return error.Unseekable,
3464 wasi.ENXIO => return error.Unseekable,
3465 else => |err| return unexpectedErrno(err),
3466 }
3467 }
31573468 const ipos = @bitCast(i64, offset); // the OS treats this as unsigned
31583469 switch (errno(system.lseek(fd, ipos, SEEK_SET))) {
31593470 0 => return,
......@@ -3183,6 +3494,18 @@ pub fn lseek_CUR(fd: fd_t, offset: i64) SeekError!void {
31833494 if (builtin.os.tag == .windows) {
31843495 return windows.SetFilePointerEx_CURRENT(fd, offset);
31853496 }
3497 if (builtin.os.tag == .wasi) {
3498 var new_offset: wasi.filesize_t = undefined;
3499 switch (wasi.fd_seek(fd, offset, wasi.WHENCE_CUR, &new_offset)) {
3500 wasi.ESUCCESS => return,
3501 wasi.EBADF => unreachable, // always a race condition
3502 wasi.EINVAL => return error.Unseekable,
3503 wasi.EOVERFLOW => return error.Unseekable,
3504 wasi.ESPIPE => return error.Unseekable,
3505 wasi.ENXIO => return error.Unseekable,
3506 else => |err| return unexpectedErrno(err),
3507 }
3508 }
31863509 switch (errno(system.lseek(fd, offset, SEEK_CUR))) {
31873510 0 => return,
31883511 EBADF => unreachable, // always a race condition
......@@ -3211,6 +3534,18 @@ pub fn lseek_END(fd: fd_t, offset: i64) SeekError!void {
32113534 if (builtin.os.tag == .windows) {
32123535 return windows.SetFilePointerEx_END(fd, offset);
32133536 }
3537 if (builtin.os.tag == .wasi) {
3538 var new_offset: wasi.filesize_t = undefined;
3539 switch (wasi.fd_seek(fd, offset, wasi.WHENCE_END, &new_offset)) {
3540 wasi.ESUCCESS => return,
3541 wasi.EBADF => unreachable, // always a race condition
3542 wasi.EINVAL => return error.Unseekable,
3543 wasi.EOVERFLOW => return error.Unseekable,
3544 wasi.ESPIPE => return error.Unseekable,
3545 wasi.ENXIO => return error.Unseekable,
3546 else => |err| return unexpectedErrno(err),
3547 }
3548 }
32143549 switch (errno(system.lseek(fd, offset, SEEK_END))) {
32153550 0 => return,
32163551 EBADF => unreachable, // always a race condition
......@@ -3239,6 +3574,18 @@ pub fn lseek_CUR_get(fd: fd_t) SeekError!u64 {
32393574 if (builtin.os.tag == .windows) {
32403575 return windows.SetFilePointerEx_CURRENT_get(fd);
32413576 }
3577 if (builtin.os.tag == .wasi) {
3578 var new_offset: wasi.filesize_t = undefined;
3579 switch (wasi.fd_seek(fd, 0, wasi.WHENCE_CUR, &new_offset)) {
3580 wasi.ESUCCESS => return new_offset,
3581 wasi.EBADF => unreachable, // always a race condition
3582 wasi.EINVAL => return error.Unseekable,
3583 wasi.EOVERFLOW => return error.Unseekable,
3584 wasi.ESPIPE => return error.Unseekable,
3585 wasi.ENXIO => return error.Unseekable,
3586 else => |err| return unexpectedErrno(err),
3587 }
3588 }
32423589 const rc = system.lseek(fd, 0, SEEK_CUR);
32433590 switch (errno(rc)) {
32443591 0 => return @bitCast(u64, rc),
......@@ -3365,6 +3712,9 @@ pub fn realpath(pathname: []const u8, out_buffer: *[MAX_PATH_BYTES]u8) RealPathE
33653712 const pathname_w = try windows.sliceToPrefixedFileW(pathname);
33663713 return realpathW(pathname_w.span().ptr, out_buffer);
33673714 }
3715 if (builtin.os.tag == .wasi) {
3716 @compileError("Use std.fs.wasi.PreopenList to obtain valid Dir handles instead of using absolute paths");
3717 }
33683718 const pathname_c = try toPosixPath(pathname);
33693719 return realpathZ(&pathname_c, out_buffer);
33703720}
......@@ -3679,6 +4029,24 @@ pub const FutimensError = error{
36794029} || UnexpectedError;
36804030
36814031pub fn futimens(fd: fd_t, times: *const [2]timespec) FutimensError!void {
4032 if (builtin.os.tag == .wasi) {
4033 // TODO WASI encodes `wasi.fstflags` to signify magic values
4034 // similar to UTIME_NOW and UTIME_OMIT. Currently, we ignore
4035 // this here, but we should really handle it somehow.
4036 const atim = times[0].toTimestamp();
4037 const mtim = times[1].toTimestamp();
4038 switch (wasi.fd_filestat_set_times(fd, atim, mtim, wasi.FILESTAT_SET_ATIM | wasi.FILESTAT_SET_MTIM)) {
4039 wasi.ESUCCESS => return,
4040 wasi.EACCES => return error.AccessDenied,
4041 wasi.EPERM => return error.PermissionDenied,
4042 wasi.EBADF => unreachable, // always a race condition
4043 wasi.EFAULT => unreachable,
4044 wasi.EINVAL => unreachable,
4045 wasi.EROFS => return error.ReadOnlyFileSystem,
4046 else => |err| return unexpectedErrno(err),
4047 }
4048 }
4049
36824050 switch (errno(system.futimens(fd, times))) {
36834051 0 => return,
36844052 EACCES => return error.AccessDenied,
lib/std/os/bits/wasi.zig+68-9
......@@ -10,8 +10,26 @@ pub const time_t = i64; // match https://github.com/CraneStation/wasi-libc
1010pub const timespec = extern struct {
1111 tv_sec: time_t,
1212 tv_nsec: isize,
13
14 pub fn fromTimestamp(tm: timestamp_t) timespec {
15 const tv_sec: timestamp_t = tm / 1_000_000_000;
16 const tv_nsec = tm - tv_sec * 1_000_000_000;
17 return timespec{
18 .tv_sec = @intCast(time_t, tv_sec),
19 .tv_nsec = @intCast(isize, tv_nsec),
20 };
21 }
22
23 pub fn toTimestamp(ts: timespec) timestamp_t {
24 const tm = @intCast(timestamp_t, ts.tv_sec * 1_000_000_000) + @intCast(timestamp_t, ts.tv_nsec);
25 return tm;
26 }
1327};
1428
29pub const Stat = filestat_t;
30
31pub const AT_REMOVEDIR: u32 = 1; // there's no AT_REMOVEDIR in WASI, but we simulate here to match other OSes
32
1533// As defined in the wasi_snapshot_preview1 spec file:
1634// https://github.com/WebAssembly/WASI/blob/master/phases/snapshot/witx/typenames.witx
1735pub const advice_t = u8;
......@@ -164,14 +182,26 @@ pub const filedelta_t = i64;
164182pub const filesize_t = u64;
165183
166184pub const filestat_t = extern struct {
167 st_dev: device_t,
168 st_ino: inode_t,
169 st_filetype: filetype_t,
170 st_nlink: linkcount_t,
171 st_size: filesize_t,
172 st_atim: timestamp_t,
173 st_mtim: timestamp_t,
174 st_ctim: timestamp_t,
185 dev: device_t,
186 ino: inode_t,
187 filetype: filetype_t,
188 nlink: linkcount_t,
189 size: filesize_t,
190 atim: timestamp_t,
191 mtim: timestamp_t,
192 ctim: timestamp_t,
193
194 pub fn atime(self: filestat_t) timespec {
195 return timespec.fromTimestamp(self.atim);
196 }
197
198 pub fn mtime(self: filestat_t) timespec {
199 return timespec.fromTimestamp(self.mtim);
200 }
201
202 pub fn ctime(self: filestat_t) timespec {
203 return timespec.fromTimestamp(self.ctim);
204 }
175205};
176206
177207pub const filetype_t = u8;
......@@ -254,6 +284,35 @@ pub const RIGHT_PATH_REMOVE_DIRECTORY: rights_t = 0x0000000002000000;
254284pub const RIGHT_PATH_UNLINK_FILE: rights_t = 0x0000000004000000;
255285pub const RIGHT_POLL_FD_READWRITE: rights_t = 0x0000000008000000;
256286pub const RIGHT_SOCK_SHUTDOWN: rights_t = 0x0000000010000000;
287pub const RIGHT_ALL: rights_t = RIGHT_FD_DATASYNC |
288 RIGHT_FD_READ |
289 RIGHT_FD_SEEK |
290 RIGHT_FD_FDSTAT_SET_FLAGS |
291 RIGHT_FD_SYNC |
292 RIGHT_FD_TELL |
293 RIGHT_FD_WRITE |
294 RIGHT_FD_ADVISE |
295 RIGHT_FD_ALLOCATE |
296 RIGHT_PATH_CREATE_DIRECTORY |
297 RIGHT_PATH_CREATE_FILE |
298 RIGHT_PATH_LINK_SOURCE |
299 RIGHT_PATH_LINK_TARGET |
300 RIGHT_PATH_OPEN |
301 RIGHT_FD_READDIR |
302 RIGHT_PATH_READLINK |
303 RIGHT_PATH_RENAME_SOURCE |
304 RIGHT_PATH_RENAME_TARGET |
305 RIGHT_PATH_FILESTAT_GET |
306 RIGHT_PATH_FILESTAT_SET_SIZE |
307 RIGHT_PATH_FILESTAT_SET_TIMES |
308 RIGHT_FD_FILESTAT_GET |
309 RIGHT_FD_FILESTAT_SET_SIZE |
310 RIGHT_FD_FILESTAT_SET_TIMES |
311 RIGHT_PATH_SYMLINK |
312 RIGHT_PATH_REMOVE_DIRECTORY |
313 RIGHT_PATH_UNLINK_FILE |
314 RIGHT_POLL_FD_READWRITE |
315 RIGHT_SOCK_SHUTDOWN;
257316
258317pub const roflags_t = u16;
259318pub const SOCK_RECV_DATA_TRUNCATED: roflags_t = 0x0001;
......@@ -306,7 +365,7 @@ pub const subscription_t = extern struct {
306365};
307366
308367pub const subscription_clock_t = extern struct {
309 id: clock_id_t,
368 id: clockid_t,
310369 timeout: timestamp_t,
311370 precision: timestamp_t,
312371 flags: subclockflags_t,
lib/std/os/test.zig+43-30
......@@ -15,13 +15,15 @@ const a = std.testing.allocator;
1515const builtin = @import("builtin");
1616const AtomicRmwOp = builtin.AtomicRmwOp;
1717const AtomicOrder = builtin.AtomicOrder;
18const getTestDir = std.testing.getTestDir;
1819
1920test "makePath, put some files in it, deleteTree" {
20 try fs.cwd().makePath("os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "c");
21 try fs.cwd().writeFile("os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "c" ++ fs.path.sep_str ++ "file.txt", "nonsense");
22 try fs.cwd().writeFile("os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "file2.txt", "blah");
23 try fs.cwd().deleteTree("os_test_tmp");
24 if (fs.cwd().openDir("os_test_tmp", .{})) |dir| {
21 var test_dir = getTestDir();
22 try test_dir.makePath("os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "c");
23 try test_dir.writeFile("os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "c" ++ fs.path.sep_str ++ "file.txt", "nonsense");
24 try test_dir.writeFile("os_test_tmp" ++ fs.path.sep_str ++ "b" ++ fs.path.sep_str ++ "file2.txt", "blah");
25 try test_dir.deleteTree("os_test_tmp");
26 if (test_dir.openDir("os_test_tmp", .{})) |dir| {
2527 @panic("expected error");
2628 } else |err| {
2729 expect(err == error.FileNotFound);
......@@ -29,16 +31,19 @@ test "makePath, put some files in it, deleteTree" {
2931}
3032
3133test "access file" {
32 try fs.cwd().makePath("os_test_tmp");
33 if (fs.cwd().access("os_test_tmp" ++ fs.path.sep_str ++ "file.txt", .{})) |ok| {
34 if (builtin.os.tag == .wasi) return error.SkipZigTest;
35
36 var test_dir = getTestDir();
37 try test_dir.makePath("os_test_tmp");
38 if (test_dir.access("os_test_tmp" ++ fs.path.sep_str ++ "file.txt", .{})) |ok| {
3439 @panic("expected error");
3540 } else |err| {
3641 expect(err == error.FileNotFound);
3742 }
3843
39 try fs.cwd().writeFile("os_test_tmp" ++ fs.path.sep_str ++ "file.txt", "");
40 try fs.cwd().access("os_test_tmp" ++ fs.path.sep_str ++ "file.txt", .{});
41 try fs.cwd().deleteTree("os_test_tmp");
44 try test_dir.writeFile("os_test_tmp" ++ fs.path.sep_str ++ "file.txt", "");
45 try test_dir.access("os_test_tmp" ++ fs.path.sep_str ++ "file.txt", .{});
46 try test_dir.deleteTree("os_test_tmp");
4247}
4348
4449fn testThreadIdFn(thread_id: *Thread.Id) void {
......@@ -46,10 +51,11 @@ fn testThreadIdFn(thread_id: *Thread.Id) void {
4651}
4752
4853test "sendfile" {
49 try fs.cwd().makePath("os_test_tmp");
50 defer fs.cwd().deleteTree("os_test_tmp") catch {};
54 var test_dir = getTestDir();
55 try test_dir.makePath("os_test_tmp");
56 defer test_dir.deleteTree("os_test_tmp") catch {};
5157
52 var dir = try fs.cwd().openDir("os_test_tmp", .{});
58 var dir = try test_dir.openDir("os_test_tmp", .{});
5359 defer dir.close();
5460
5561 const line1 = "line1\n";
......@@ -65,12 +71,12 @@ test "sendfile" {
6571 },
6672 };
6773
68 var src_file = try dir.createFileZ("sendfile1.txt", .{ .read = true });
74 var src_file = try dir.createFile("sendfile1.txt", .{ .read = true });
6975 defer src_file.close();
7076
7177 try src_file.writevAll(&vecs);
7278
73 var dest_file = try dir.createFileZ("sendfile2.txt", .{ .read = true });
79 var dest_file = try dir.createFile("sendfile2.txt", .{ .read = true });
7480 defer dest_file.close();
7581
7682 const header1 = "header1\n";
......@@ -113,23 +119,23 @@ test "fs.copyFile" {
113119 const dest_file = "tmp_test_copy_file2.txt";
114120 const dest_file2 = "tmp_test_copy_file3.txt";
115121
116 const cwd = fs.cwd();
122 const test_dir = getTestDir();
117123
118 try cwd.writeFile(src_file, data);
119 defer cwd.deleteFile(src_file) catch {};
124 try test_dir.writeFile(src_file, data);
125 defer test_dir.deleteFile(src_file) catch {};
120126
121 try cwd.copyFile(src_file, cwd, dest_file, .{});
122 defer cwd.deleteFile(dest_file) catch {};
127 try test_dir.copyFile(src_file, test_dir, dest_file, .{});
128 defer test_dir.deleteFile(dest_file) catch {};
123129
124 try cwd.copyFile(src_file, cwd, dest_file2, .{ .override_mode = File.default_mode });
125 defer cwd.deleteFile(dest_file2) catch {};
130 try test_dir.copyFile(src_file, test_dir, dest_file2, .{ .override_mode = File.default_mode });
131 defer test_dir.deleteFile(dest_file2) catch {};
126132
127133 try expectFileContents(dest_file, data);
128134 try expectFileContents(dest_file2, data);
129135}
130136
131137fn expectFileContents(file_path: []const u8, data: []const u8) !void {
132 const contents = try fs.cwd().readFileAlloc(testing.allocator, file_path, 1000);
138 const contents = try getTestDir().readFileAlloc(testing.allocator, file_path, 1000);
133139 defer testing.allocator.free(contents);
134140
135141 testing.expectEqualSlices(u8, data, contents);
......@@ -181,6 +187,8 @@ fn start2(ctx: *i32) u8 {
181187}
182188
183189test "cpu count" {
190 if (builtin.os.tag == .wasi) return error.SkipZigTest;
191
184192 const cpu_count = try Thread.cpuCount();
185193 expect(cpu_count >= 1);
186194}
......@@ -191,17 +199,18 @@ test "AtomicFile" {
191199 \\ hello!
192200 \\ this is a test file
193201 ;
202 var test_dir = getTestDir();
194203 {
195 var af = try fs.cwd().atomicFile(test_out_file, .{});
204 var af = try test_dir.atomicFile(test_out_file, .{});
196205 defer af.deinit();
197206 try af.file.writeAll(test_content);
198207 try af.finish();
199208 }
200 const content = try fs.cwd().readFileAlloc(testing.allocator, test_out_file, 9999);
209 const content = try test_dir.readFileAlloc(testing.allocator, test_out_file, 9999);
201210 defer testing.allocator.free(content);
202211 expect(mem.eql(u8, content, test_content));
203212
204 try fs.cwd().deleteFile(test_out_file);
213 try test_dir.deleteFile(test_out_file);
205214}
206215
207216test "thread local storage" {
......@@ -231,12 +240,16 @@ test "getrandom" {
231240}
232241
233242test "getcwd" {
243 if (builtin.os.tag == .wasi) return error.SkipZigTest;
244
234245 // at least call it so it gets compiled
235246 var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined;
236247 _ = os.getcwd(&buf) catch undefined;
237248}
238249
239250test "realpath" {
251 if (builtin.os.tag == .wasi) return error.SkipZigTest;
252
240253 var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined;
241254 testing.expectError(error.FileNotFound, fs.realpath("definitely_bogus_does_not_exist1234", &buf));
242255}
......@@ -304,7 +317,7 @@ test "dl_iterate_phdr" {
304317}
305318
306319test "gethostname" {
307 if (builtin.os.tag == .windows)
320 if (builtin.os.tag == .windows or builtin.os.tag == .wasi)
308321 return error.SkipZigTest;
309322
310323 var buf: [os.HOST_NAME_MAX]u8 = undefined;
......@@ -313,7 +326,7 @@ test "gethostname" {
313326}
314327
315328test "pipe" {
316 if (builtin.os.tag == .windows)
329 if (builtin.os.tag == .windows or builtin.os.tag == .wasi)
317330 return error.SkipZigTest;
318331
319332 var fds = try os.pipe();
......@@ -349,7 +362,7 @@ test "memfd_create" {
349362}
350363
351364test "mmap" {
352 if (builtin.os.tag == .windows)
365 if (builtin.os.tag == .windows or builtin.os.tag == .wasi)
353366 return error.SkipZigTest;
354367
355368 // Simple mmap() call with non page-aligned size
......@@ -451,7 +464,7 @@ test "getenv" {
451464}
452465
453466test "fcntl" {
454 if (builtin.os.tag == .windows)
467 if (builtin.os.tag == .windows or builtin.os.tag == .wasi)
455468 return error.SkipZigTest;
456469
457470 const test_out_file = "os_tmp_test";
lib/std/packed_int_array.zig+6
......@@ -314,6 +314,9 @@ pub fn PackedIntSliceEndian(comptime Int: type, comptime endian: builtin.Endian)
314314}
315315
316316test "PackedIntArray" {
317 // TODO @setEvalBranchQuota generates panics in wasm32. Investigate.
318 if (builtin.arch == .wasm32) return error.SkipZigTest;
319
317320 @setEvalBranchQuota(10000);
318321 const max_bits = 256;
319322 const int_count = 19;
......@@ -357,6 +360,9 @@ test "PackedIntArray init" {
357360}
358361
359362test "PackedIntSlice" {
363 // TODO @setEvalBranchQuota generates panics in wasm32. Investigate.
364 if (builtin.arch == .wasm32) return error.SkipZigTest;
365
360366 @setEvalBranchQuota(10000);
361367 const max_bits = 256;
362368 const int_count = 19;
lib/std/process.zig+8-10
......@@ -26,6 +26,8 @@ pub fn getCwdAlloc(allocator: *Allocator) ![]u8 {
2626}
2727
2828test "getCwdAlloc" {
29 if (builtin.os.tag == .wasi) return error.SkipZigTest;
30
2931 const cwd = try getCwdAlloc(testing.allocator);
3032 testing.allocator.free(cwd);
3133}
......@@ -69,9 +71,7 @@ pub fn getEnvMap(allocator: *Allocator) !BufMap {
6971 return os.unexpectedErrno(environ_sizes_get_ret);
7072 }
7173
72 // TODO: Verify that the documentation is incorrect
73 // https://github.com/WebAssembly/WASI/issues/27
74 var environ = try allocator.alloc(?[*:0]u8, environ_count + 1);
74 var environ = try allocator.alloc([*:0]u8, environ_count);
7575 defer allocator.free(environ);
7676 var environ_buf = try allocator.alloc(u8, environ_buf_size);
7777 defer allocator.free(environ_buf);
......@@ -82,13 +82,11 @@ pub fn getEnvMap(allocator: *Allocator) !BufMap {
8282 }
8383
8484 for (environ) |env| {
85 if (env) |ptr| {
86 const pair = mem.spanZ(ptr);
87 var parts = mem.split(pair, "=");
88 const key = parts.next().?;
89 const value = parts.next().?;
90 try result.set(key, value);
91 }
85 const pair = mem.spanZ(env);
86 var parts = mem.split(pair, "=");
87 const key = parts.next().?;
88 const value = parts.next().?;
89 try result.set(key, value);
9290 }
9391 return result;
9492 } else if (builtin.link_libc) {
lib/std/std.zig+6-1
......@@ -75,5 +75,10 @@ comptime {
7575}
7676
7777test "" {
78 meta.refAllDecls(@This());
78 // TODO is there a way around this? When enabled for WASI, we pick up functions
79 // which generate compile error. Perhaps semantic analyser should skip those
80 // if running in test mode?
81 if (builtin.os.tag != .wasi) {
82 meta.refAllDecls(@This());
83 }
7984}
lib/std/testing.zig+15
......@@ -14,6 +14,21 @@ pub var failing_allocator_instance = FailingAllocator.init(&base_allocator_insta
1414pub var base_allocator_instance = std.heap.ThreadSafeFixedBufferAllocator.init(allocator_mem[0..]);
1515var allocator_mem: [2 * 1024 * 1024]u8 = undefined;
1616
17/// This function is intended to be used only in tests. It should be used in any testcase
18/// where we intend to test WASI and should be used a replacement for `std.fs.cwd()` in WASI.
19pub fn getTestDir() std.fs.Dir {
20 if (@import("builtin").os.tag == .wasi) {
21 var preopens = std.fs.wasi.PreopenList.init(allocator);
22 defer preopens.deinit();
23 preopens.populate() catch unreachable;
24
25 const preopen = preopens.find(".") orelse unreachable;
26 return std.fs.Dir{ .fd = preopen.fd };
27 } else {
28 return std.fs.cwd();
29 }
30}
31
1732/// This function is intended to be used only in tests. It prints diagnostics to stderr
1833/// and then aborts when actual_error_union is not expected_error.
1934pub fn expectError(expected_error: anyerror, actual_error_union: var) void {
lib/std/time.zig+34-1
......@@ -19,6 +19,39 @@ pub fn sleep(nanoseconds: u64) void {
1919 os.windows.kernel32.Sleep(ms);
2020 return;
2121 }
22 if (builtin.os.tag == .wasi) {
23 const w = std.os.wasi;
24 const userdata: w.userdata_t = 0x0123_45678;
25 const clock = w.subscription_clock_t{
26 .id = w.CLOCK_MONOTONIC,
27 .timeout = nanoseconds,
28 .precision = 0,
29 .flags = 0,
30 };
31 const in = w.subscription_t{
32 .userdata = userdata,
33 .u = w.subscription_u_t{
34 .tag = w.EVENTTYPE_CLOCK,
35 .u = w.subscription_u_u_t{
36 .clock = clock,
37 },
38 },
39 };
40
41 var event: w.event_t = undefined;
42 var nevents: usize = undefined;
43 switch (w.poll_oneoff(&in, &event, 1, &nevents)) {
44 w.ESUCCESS => {},
45 else => |err| @panic("unexpected error of poll_oneoff"),
46 }
47
48 if (nevents == 1 and event.userdata == userdata and event.@"error" == w.ESUCCESS and event.@"type" == w.EVENTTYPE_CLOCK) {
49 return;
50 }
51
52 @panic("unexpected result of poll_oneoff");
53 }
54
2255 const s = nanoseconds / ns_per_s;
2356 const ns = nanoseconds % ns_per_s;
2457 std.os.nanosleep(s, ns);
......@@ -202,7 +235,7 @@ test "sleep" {
202235
203236test "timestamp" {
204237 const ns_per_ms = (ns_per_s / ms_per_s);
205 const margin = 50;
238 const margin = ns_per_ms * 50;
206239
207240 const time_0 = milliTimestamp();
208241 sleep(ns_per_ms);
test/tests.zig+20
......@@ -50,6 +50,15 @@ const test_targets = blk: {
5050 .single_threaded = true,
5151 },
5252
53 TestTarget{
54 .target = .{
55 .cpu_arch = .wasm32,
56 .os_tag = .wasi,
57 },
58 .link_libc = false,
59 .single_threaded = true,
60 },
61
5362 TestTarget{
5463 .target = .{
5564 .cpu_arch = .x86_64,
......@@ -463,6 +472,7 @@ pub fn addPkgTests(
463472 skip_single_threaded: bool,
464473 skip_non_native: bool,
465474 skip_libc: bool,
475 skip_wasi: bool,
466476 is_wine_enabled: bool,
467477 is_qemu_enabled: bool,
468478 glibc_dir: ?[]const u8,
......@@ -473,6 +483,15 @@ pub fn addPkgTests(
473483 if (skip_non_native and !test_target.target.isNative())
474484 continue;
475485
486 if (skip_wasi) {
487 if (test_target.target.os_tag) |tag| {
488 if (tag == .wasi) {
489 warn("Skipping {} on wasm32-wasi.\n", .{root_src});
490 continue;
491 }
492 }
493 }
494
476495 if (skip_libc and test_target.link_libc)
477496 continue;
478497
......@@ -525,6 +544,7 @@ pub fn addPkgTests(
525544 these_tests.overrideZigLibDir("lib");
526545 these_tests.enable_wine = is_wine_enabled;
527546 these_tests.enable_qemu = is_qemu_enabled;
547 these_tests.enable_wasmtime = !skip_wasi;
528548 these_tests.glibc_multi_install_dir = glibc_dir;
529549
530550 step.dependOn(&these_tests.step);