| author | |
| committer | |
| log | 537618464abd11352c67fa1f761b28d9349eae21 |
| tree | f29b6f458ca4f164bc427aca4c9aa5f5d8ea2ed4 |
| parent | b9ea086812a080458446af94685a8b23eb3f3bc8 |
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 { |
| 3005 | 3005 | if (builtin.os.tag == .wasi and !builtin.link_libc) { |
| 3006 | 3006 | var buf: [MAX_PATH_BYTES]u8 = undefined; |
| 3007 | 3007 | 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 }); | |
| 3012 | 3009 | |
| 3013 | 3010 | const dirinfo = try fstatat(AT.FDCWD, path, 0); |
| 3014 | 3011 | if (dirinfo.filetype != .DIRECTORY) { |
| 3015 | 3012 | return error.NotDir; |
| 3016 | 3013 | } |
| 3017 | 3014 | |
| 3018 | // This copy is guaranteed to succeed, since buf and path_buffer are the same size. | |
| 3019 | 3015 | 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); | |
| 3021 | 3017 | return; |
| 3022 | 3018 | } else if (builtin.os.tag == .windows) { |
| 3023 | 3019 | 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; |
| 22 | 22 | const ArenaAllocator = std.heap.ArenaAllocator; |
| 23 | 23 | |
| 24 | 24 | test "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 | |
| 27 | 26 | |
| 28 | 27 | // Get current working directory path |
| 29 | 28 | var old_cwd_buf: [fs.MAX_PATH_BYTES]u8 = undefined; |
| ... | ... | @@ -36,42 +35,16 @@ test "chdir smoke test" { |
| 36 | 35 | const new_cwd = try os.getcwd(new_cwd_buf[0..]); |
| 37 | 36 | try expect(mem.eql(u8, old_cwd, new_cwd)); |
| 38 | 37 | } |
| 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 | |
| 42 | 40 | const parent = fs.path.dirname(old_cwd) orelse unreachable; // old_cwd should be absolute |
| 43 | 41 | try os.chdir(parent); |
| 44 | ||
| 45 | 42 | // Restore cwd because process may have other tests that do not tolerate chdir. |
| 46 | 43 | defer os.chdir(old_cwd) catch unreachable; |
| 47 | ||
| 48 | 44 | var new_cwd_buf: [fs.MAX_PATH_BYTES]u8 = undefined; |
| 49 | 45 | const new_cwd = try os.getcwd(new_cwd_buf[0..]); |
| 50 | 46 | try expect(mem.eql(u8, parent, new_cwd)); |
| 51 | 47 | } |
| 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 | } | |
| 75 | 48 | } |
| 76 | 49 | |
| 77 | 50 | test "open smoke test" { |
lib/std/testing.zig+17-2| ... | ... | @@ -379,13 +379,28 @@ pub const TmpIterableDir = struct { |
| 379 | 379 | } |
| 380 | 380 | }; |
| 381 | 381 | |
| 382 | fn 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 | ||
| 382 | 397 | pub fn tmpDir(opts: std.fs.Dir.OpenDirOptions) TmpDir { |
| 383 | 398 | var random_bytes: [TmpDir.random_bytes_count]u8 = undefined; |
| 384 | 399 | std.crypto.random.bytes(&random_bytes); |
| 385 | 400 | var sub_path: [TmpDir.sub_path_len]u8 = undefined; |
| 386 | 401 | _ = std.fs.base64_encoder.encode(&sub_path, &random_bytes); |
| 387 | 402 | |
| 388 | var cwd = std.fs.cwd(); | |
| 403 | var cwd = getCwdOrWasiPreopen(); | |
| 389 | 404 | var cache_dir = cwd.makeOpenPath("zig-cache", .{}) catch |
| 390 | 405 | @panic("unable to make tmp dir for testing: unable to make and open zig-cache dir"); |
| 391 | 406 | defer cache_dir.close(); |
| ... | ... | @@ -407,7 +422,7 @@ pub fn tmpIterableDir(opts: std.fs.Dir.OpenDirOptions) TmpIterableDir { |
| 407 | 422 | var sub_path: [TmpIterableDir.sub_path_len]u8 = undefined; |
| 408 | 423 | _ = std.fs.base64_encoder.encode(&sub_path, &random_bytes); |
| 409 | 424 | |
| 410 | var cwd = std.fs.cwd(); | |
| 425 | var cwd = getCwdOrWasiPreopen(); | |
| 411 | 426 | var cache_dir = cwd.makeOpenPath("zig-cache", .{}) catch |
| 412 | 427 | @panic("unable to make tmp dir for testing: unable to make and open zig-cache dir"); |
| 413 | 428 | defer cache_dir.close(); |