aboutsummaryrefslogtreecommitdiff
path: root/lib/std/os
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2022-12-06 18:52:39 -0500
committerGitHub <noreply@github.com>2022-12-06 18:52:39 -0500
commite7d28344fa3ee81d6ad7ca5ce1f83d50d8502118 (patch)
tree012b2556f2bda10ae663fab8efb235efe30e02f4 /lib/std/os
parent817cf6a82efa7ed274371a28621bbf88a723d9b7 (diff)
parent20d86d9c63476b6312b87dc5b0e4aa4822eb7717 (diff)
downloadzig-e7d28344fa3ee81d6ad7ca5ce1f83d50d8502118.tar.gz
zig-e7d28344fa3ee81d6ad7ca5ce1f83d50d8502118.zip
Merge pull request #13560 from ziglang/wasi-bootstrap
Nuke the C++ implementation of Zig from orbit using WASI
Diffstat (limited to 'lib/std/os')
-rw-r--r--lib/std/os/test.zig30
-rw-r--r--lib/std/os/wasi.zig8
2 files changed, 17 insertions, 21 deletions
diff --git a/lib/std/os/test.zig b/lib/std/os/test.zig
index ed86024dcd..44d0e0c808 100644
--- a/lib/std/os/test.zig
+++ b/lib/std/os/test.zig
@@ -22,8 +22,7 @@ const Dir = std.fs.Dir;
const ArenaAllocator = std.heap.ArenaAllocator;
test "chdir smoke test" {
- if (native_os == .wasi and builtin.link_libc) return error.SkipZigTest;
- if (native_os == .wasi and !builtin.link_libc) try os.initPreopensWasi(std.heap.page_allocator, "/preopens/cwd");
+ if (native_os == .wasi) return error.SkipZigTest;
// Get current working directory path
var old_cwd_buf: [fs.MAX_PATH_BYTES]u8 = undefined;
@@ -75,8 +74,7 @@ test "chdir smoke test" {
}
test "open smoke test" {
- if (native_os == .wasi and builtin.link_libc) return error.SkipZigTest;
- if (native_os == .wasi and !builtin.link_libc) try os.initPreopensWasi(std.heap.page_allocator, "/");
+ if (native_os == .wasi) return error.SkipZigTest;
// TODO verify file attributes using `fstat`
@@ -131,7 +129,6 @@ test "open smoke test" {
test "openat smoke test" {
if (native_os == .wasi and builtin.link_libc) return error.SkipZigTest;
- if (native_os == .wasi and !builtin.link_libc) try os.initPreopensWasi(std.heap.page_allocator, "/");
// TODO verify file attributes using `fstatat`
@@ -168,7 +165,6 @@ test "openat smoke test" {
test "symlink with relative paths" {
if (native_os == .wasi and builtin.link_libc) return error.SkipZigTest;
- if (native_os == .wasi and !builtin.link_libc) try os.initPreopensWasi(std.heap.page_allocator, "/");
const cwd = fs.cwd();
cwd.deleteFile("file.txt") catch {};
@@ -219,15 +215,10 @@ fn testReadlink(target_path: []const u8, symlink_path: []const u8) !void {
}
test "link with relative paths" {
+ if (native_os == .wasi and builtin.link_libc) return error.SkipZigTest;
+
switch (native_os) {
- .wasi => {
- if (builtin.link_libc) {
- return error.SkipZigTest;
- } else {
- try os.initPreopensWasi(std.heap.page_allocator, "/");
- }
- },
- .linux, .solaris => {},
+ .wasi, .linux, .solaris => {},
else => return error.SkipZigTest,
}
var cwd = fs.cwd();
@@ -263,9 +254,10 @@ test "link with relative paths" {
}
test "linkat with different directories" {
+ if (native_os == .wasi and builtin.link_libc) return error.SkipZigTest;
+
switch (native_os) {
- .wasi => if (!builtin.link_libc) try os.initPreopensWasi(std.heap.page_allocator, "/"),
- .linux, .solaris => {},
+ .wasi, .linux, .solaris => {},
else => return error.SkipZigTest,
}
var cwd = fs.cwd();
@@ -950,8 +942,7 @@ test "POSIX file locking with fcntl" {
}
test "rename smoke test" {
- if (native_os == .wasi and builtin.link_libc) return error.SkipZigTest;
- if (native_os == .wasi and !builtin.link_libc) try os.initPreopensWasi(std.heap.page_allocator, "/");
+ if (native_os == .wasi) return error.SkipZigTest;
var tmp = tmpDir(.{});
defer tmp.cleanup();
@@ -1007,8 +998,7 @@ test "rename smoke test" {
}
test "access smoke test" {
- if (native_os == .wasi and builtin.link_libc) return error.SkipZigTest;
- if (native_os == .wasi and !builtin.link_libc) try os.initPreopensWasi(std.heap.page_allocator, "/");
+ if (native_os == .wasi) return error.SkipZigTest;
var tmp = tmpDir(.{});
defer tmp.cleanup();
diff --git a/lib/std/os/wasi.zig b/lib/std/os/wasi.zig
index 308b6cc23d..711352e2fe 100644
--- a/lib/std/os/wasi.zig
+++ b/lib/std/os/wasi.zig
@@ -1,6 +1,7 @@
// wasi_snapshot_preview1 spec available (in witx format) here:
// * typenames -- https://github.com/WebAssembly/WASI/blob/master/phases/snapshot/witx/typenames.witx
// * module -- https://github.com/WebAssembly/WASI/blob/master/phases/snapshot/witx/wasi_snapshot_preview1.witx
+const builtin = @import("builtin");
const std = @import("std");
const assert = std.debug.assert;
@@ -157,7 +158,12 @@ pub const IOV_MAX = 1024;
pub const AT = struct {
pub const REMOVEDIR: u32 = 0x4;
- pub const FDCWD: fd_t = -2;
+ /// When linking libc, we follow their convention and use -2 for current working directory.
+ /// However, without libc, Zig does a different convention: it assumes the
+ /// current working directory is the first preopen. This behavior can be
+ /// overridden with a public function called `wasi_cwd` in the root source
+ /// file.
+ pub const FDCWD: fd_t = if (builtin.link_libc) -2 else 3;
};
// As defined in the wasi_snapshot_preview1 spec file: