authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-01-10 00:45:09-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-01-10 00:45:09-07:00
log537618464abd11352c67fa1f761b28d9349eae21
treef29b6f458ca4f164bc427aca4c9aa5f5d8ea2ed4
parentb9ea086812a080458446af94685a8b23eb3f3bc8

Revert "std.os: Fix std.os.chdir for WASI"

This reverts commit fff7f15fb88f6683f9d73565d8d59593ecf7461a. This commit was not intended to be cherry-picked into the 0.10.x branch.

3 files changed, 22 insertions(+), 38 deletions(-)

lib/std/os.zig+2-6
......@@ -3005,19 +3005,15 @@ pub fn chdir(dir_path: []const u8) ChangeCurDirError!void {
30053005 if (builtin.os.tag == .wasi and !builtin.link_libc) {
30063006 var buf: [MAX_PATH_BYTES]u8 = undefined;
30073007 var alloc = std.heap.FixedBufferAllocator.init(&buf);
3008 const path = fs.path.resolve(alloc.allocator(), &.{ wasi_cwd.cwd, dir_path }) catch |err| switch (err) {
3009 error.OutOfMemory => return error.NameTooLong,
3010 else => |e| return e,
3011 };
3008 const path = try fs.resolve(alloc.allocator(), &.{ wasi_cwd.cwd, dir_path });
30123009
30133010 const dirinfo = try fstatat(AT.FDCWD, path, 0);
30143011 if (dirinfo.filetype != .DIRECTORY) {
30153012 return error.NotDir;
30163013 }
30173014
3018 // This copy is guaranteed to succeed, since buf and path_buffer are the same size.
30193015 var cwd_alloc = std.heap.FixedBufferAllocator.init(&wasi_cwd.path_buffer);
3020 wasi_cwd.cwd = cwd_alloc.allocator().dupe(u8, path) catch unreachable;
3016 wasi_cwd.cwd = try cwd_alloc.allocator().dupe(u8, path);
30213017 return;
30223018 } else if (builtin.os.tag == .windows) {
30233019 var utf16_dir_path: [windows.PATH_MAX_WIDE]u16 = undefined;
lib/std/os/test.zig+3-30
......@@ -22,8 +22,7 @@ const Dir = std.fs.Dir;
2222const ArenaAllocator = std.heap.ArenaAllocator;
2323
2424test "chdir smoke test" {
25 if (native_os == .wasi and builtin.link_libc) return error.SkipZigTest;
26 if (native_os == .wasi and !builtin.link_libc) try os.initPreopensWasi(std.heap.page_allocator, "/preopens/cwd");
25 if (native_os == .wasi) return error.SkipZigTest; // WASI doesn't allow navigating outside of a preopen
2726
2827 // Get current working directory path
2928 var old_cwd_buf: [fs.MAX_PATH_BYTES]u8 = undefined;
......@@ -36,42 +35,16 @@ test "chdir smoke test" {
3635 const new_cwd = try os.getcwd(new_cwd_buf[0..]);
3736 try expect(mem.eql(u8, old_cwd, new_cwd));
3837 }
39
40 // Next, change current working directory to one level above
41 if (native_os != .wasi) { // WASI does not support navigating outside of Preopens
38 {
39 // Next, change current working directory to one level above
4240 const parent = fs.path.dirname(old_cwd) orelse unreachable; // old_cwd should be absolute
4341 try os.chdir(parent);
44
4542 // Restore cwd because process may have other tests that do not tolerate chdir.
4643 defer os.chdir(old_cwd) catch unreachable;
47
4844 var new_cwd_buf: [fs.MAX_PATH_BYTES]u8 = undefined;
4945 const new_cwd = try os.getcwd(new_cwd_buf[0..]);
5046 try expect(mem.eql(u8, parent, new_cwd));
5147 }
52
53 // Next, change current working directory to a temp directory one level below
54 {
55 // Create a tmp directory
56 var tmp_dir_buf: [fs.MAX_PATH_BYTES]u8 = undefined;
57 var tmp_dir_path = path: {
58 var allocator = std.heap.FixedBufferAllocator.init(&tmp_dir_buf);
59 break :path try fs.path.resolve(allocator.allocator(), &[_][]const u8{ old_cwd, "zig-test-tmp" });
60 };
61 var tmp_dir = try fs.cwd().makeOpenPath("zig-test-tmp", .{});
62
63 // Change current working directory to tmp directory
64 try os.chdir("zig-test-tmp");
65
66 var new_cwd_buf: [fs.MAX_PATH_BYTES]u8 = undefined;
67 const new_cwd = try os.getcwd(new_cwd_buf[0..]);
68 try expect(mem.eql(u8, tmp_dir_path, new_cwd));
69
70 // Restore cwd because process may have other tests that do not tolerate chdir.
71 tmp_dir.close();
72 os.chdir(old_cwd) catch unreachable;
73 try fs.cwd().deleteDir("zig-test-tmp");
74 }
7548}
7649
7750test "open smoke test" {
lib/std/testing.zig+17-2
......@@ -379,13 +379,28 @@ pub const TmpIterableDir = struct {
379379 }
380380};
381381
382fn getCwdOrWasiPreopen() std.fs.Dir {
383 if (builtin.os.tag == .wasi and !builtin.link_libc) {
384 var preopens = std.fs.wasi.PreopenList.init(allocator);
385 defer preopens.deinit();
386 preopens.populate(null) catch
387 @panic("unable to make tmp dir for testing: unable to populate preopens");
388 const preopen = preopens.find(std.fs.wasi.PreopenType{ .Dir = "." }) orelse
389 @panic("unable to make tmp dir for testing: didn't find '.' in the preopens");
390
391 return std.fs.Dir{ .fd = preopen.fd };
392 } else {
393 return std.fs.cwd();
394 }
395}
396
382397pub fn tmpDir(opts: std.fs.Dir.OpenDirOptions) TmpDir {
383398 var random_bytes: [TmpDir.random_bytes_count]u8 = undefined;
384399 std.crypto.random.bytes(&random_bytes);
385400 var sub_path: [TmpDir.sub_path_len]u8 = undefined;
386401 _ = std.fs.base64_encoder.encode(&sub_path, &random_bytes);
387402
388 var cwd = std.fs.cwd();
403 var cwd = getCwdOrWasiPreopen();
389404 var cache_dir = cwd.makeOpenPath("zig-cache", .{}) catch
390405 @panic("unable to make tmp dir for testing: unable to make and open zig-cache dir");
391406 defer cache_dir.close();
......@@ -407,7 +422,7 @@ pub fn tmpIterableDir(opts: std.fs.Dir.OpenDirOptions) TmpIterableDir {
407422 var sub_path: [TmpIterableDir.sub_path_len]u8 = undefined;
408423 _ = std.fs.base64_encoder.encode(&sub_path, &random_bytes);
409424
410 var cwd = std.fs.cwd();
425 var cwd = getCwdOrWasiPreopen();
411426 var cache_dir = cwd.makeOpenPath("zig-cache", .{}) catch
412427 @panic("unable to make tmp dir for testing: unable to make and open zig-cache dir");
413428 defer cache_dir.close();