diff --git a/lib/std/fs.zig b/lib/std/fs.zig index cdaa0091ec54a8a42d86ceba88ec32ddd502863e..33457c02557991a0b58514de45801c0c7d610a1f 100644 --- a/lib/std/fs.zig +++ b/lib/std/fs.zig @@ -602,7 +602,7 @@ pub const Dir = struct { rights |= wasi.FD_WRITE | wasi.FD_DATASYNC | wasi.FD_SEEK | wasi.FD_FDSTAT_SET_FLAGS | wasi.FD_SYNC | wasi.FD_ALLOCATE | wasi.FD_ADVISE | wasi.FD_FILESTAT_SET_TIMES | wasi.FD_FILESTAT_SET_SIZE; } - const fd = try os.openatWasi(self.fd, sub_path, 0x0, fdflags, rights); + const fd = try wasi.openat(self.fd, sub_path, 0x0, fdflags, rights); return File{ .handle = fd }; } @@ -713,7 +713,7 @@ pub const Dir = struct { oflags |= wasi.O_EXCL; } - const fd = try os.openatWasi(self.fd, sub_path, oflags, 0x0, rights); + const fd = try wasi.openat(self.fd, sub_path, oflags, 0x0, rights); return File{ .handle = fd }; } diff --git a/lib/std/os.zig b/lib/std/os.zig index 5335b2ca2457a2a9f1429af3b7b32fbf176a74a4..86130ed32c799fe2bb76d6594d24a49194ed10e7 100644 --- a/lib/std/os.zig +++ b/lib/std/os.zig @@ -923,16 +923,6 @@ pub fn openat(dir_fd: fd_t, file_path: []const u8, flags: u32, mode: mode_t) Ope return openatZ(dir_fd, &file_path_c, flags, mode); } -pub fn openatWasi(dir_fd: fd_t, file_path: []const u8, oflags: wasi.oflags_t, fdflags: wasi.fdflags_t, rights: wasi.rights_t) OpenError!fd_t { - var fd: fd_t = undefined; - switch (wasi.path_open(dir_fd, 0x0, file_path.ptr, file_path.len, oflags, rights, 0x0, fdflags, &fd)) { - 0 => {}, - // TODO map errors - else => |err| return unexpectedErrno(err), - } - return fd; -} - pub const openatC = @compileError("deprecated: renamed to openatZ"); /// Open and possibly create a file. Keeps trying if it gets interrupted. diff --git a/lib/std/os/wasi.zig b/lib/std/os/wasi.zig index 07f6786518e65a241bc9729dbfb0796c1118bef5..cb0ad5c3c6e0287df8f7fda7f7881f50f2da77b3 100644 --- a/lib/std/os/wasi.zig +++ b/lib/std/os/wasi.zig @@ -94,11 +94,32 @@ pub const PreopenList = struct { } } + pub fn find(self: *const Self, path: []const u8) ?*const Preopen { + for (self.buffer.items) |preopen| { + switch (preopen.@"type") { + PreopenType.Dir => |preopen_path| { + if (mem.eql(u8, path, preopen_path)) return &preopen; + }, + } + } + return null; + } + pub fn asSlice(self: *const Self) []const Preopen { return self.buffer.items; } }; +pub fn openat(dir_fd: fd_t, file_path: []const u8, oflags: oflags_t, fdflags: fdflags_t, rights: rights_t) std.os.OpenError!fd_t { + var fd: fd_t = undefined; + switch (path_open(dir_fd, 0x0, file_path.ptr, file_path.len, oflags, rights, 0x0, fdflags, &fd)) { + 0 => {}, + // TODO map errors + else => |err| return std.os.unexpectedErrno(err), + } + return fd; +} + pub extern "wasi_snapshot_preview1" fn args_get(argv: [*][*:0]u8, argv_buf: [*]u8) errno_t; pub extern "wasi_snapshot_preview1" fn args_sizes_get(argc: *usize, argv_buf_size: *usize) errno_t;