authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-05-04 21:47:33+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2020-05-05 15:08:52+02:00
logd4c33129cf86441a59079e15fd887711fae8d924
tree339b93ebd6304d12bc5d6303ccfbd8a2e157f129
parentdd238352a4d3a9f02ff62d32cd109adabee30b66

Shuffle things around; add PreopenList.findByPath method

This commit removes `std.os.openatWasi` function, and renames it to `std.os.wasi.openat`. Additionally, the added `PreopenList.findByPath` method allows querying the list for a matching preopen by path.

3 files changed, 23 insertions(+), 12 deletions(-)

lib/std/fs.zig+2-2
...@@ -602,7 +602,7 @@ pub const Dir = struct {...@@ -602,7 +602,7 @@ pub const Dir = struct {
602 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;602 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;
603 }603 }
604604
605 const fd = try os.openatWasi(self.fd, sub_path, 0x0, fdflags, rights);605 const fd = try wasi.openat(self.fd, sub_path, 0x0, fdflags, rights);
606606
607 return File{ .handle = fd };607 return File{ .handle = fd };
608 }608 }
...@@ -713,7 +713,7 @@ pub const Dir = struct {...@@ -713,7 +713,7 @@ pub const Dir = struct {
713 oflags |= wasi.O_EXCL;713 oflags |= wasi.O_EXCL;
714 }714 }
715715
716 const fd = try os.openatWasi(self.fd, sub_path, oflags, 0x0, rights);716 const fd = try wasi.openat(self.fd, sub_path, oflags, 0x0, rights);
717717
718 return File{ .handle = fd };718 return File{ .handle = fd };
719 }719 }
lib/std/os.zig-10
...@@ -923,16 +923,6 @@ pub fn openat(dir_fd: fd_t, file_path: []const u8, flags: u32, mode: mode_t) Ope...@@ -923,16 +923,6 @@ pub fn openat(dir_fd: fd_t, file_path: []const u8, flags: u32, mode: mode_t) Ope
923 return openatZ(dir_fd, &file_path_c, flags, mode);923 return openatZ(dir_fd, &file_path_c, flags, mode);
924}924}
925925
926pub 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 {
927 var fd: fd_t = undefined;
928 switch (wasi.path_open(dir_fd, 0x0, file_path.ptr, file_path.len, oflags, rights, 0x0, fdflags, &fd)) {
929 0 => {},
930 // TODO map errors
931 else => |err| return unexpectedErrno(err),
932 }
933 return fd;
934}
935
936pub const openatC = @compileError("deprecated: renamed to openatZ");926pub const openatC = @compileError("deprecated: renamed to openatZ");
937927
938/// Open and possibly create a file. Keeps trying if it gets interrupted.928/// Open and possibly create a file. Keeps trying if it gets interrupted.
lib/std/os/wasi.zig+21
...@@ -94,11 +94,32 @@ pub const PreopenList = struct {...@@ -94,11 +94,32 @@ pub const PreopenList = struct {
94 }94 }
95 }95 }
9696
97 pub fn find(self: *const Self, path: []const u8) ?*const Preopen {
98 for (self.buffer.items) |preopen| {
99 switch (preopen.@"type") {
100 PreopenType.Dir => |preopen_path| {
101 if (mem.eql(u8, path, preopen_path)) return &preopen;
102 },
103 }
104 }
105 return null;
106 }
107
97 pub fn asSlice(self: *const Self) []const Preopen {108 pub fn asSlice(self: *const Self) []const Preopen {
98 return self.buffer.items;109 return self.buffer.items;
99 }110 }
100};111};
101112
113pub fn openat(dir_fd: fd_t, file_path: []const u8, oflags: oflags_t, fdflags: fdflags_t, rights: rights_t) std.os.OpenError!fd_t {
114 var fd: fd_t = undefined;
115 switch (path_open(dir_fd, 0x0, file_path.ptr, file_path.len, oflags, rights, 0x0, fdflags, &fd)) {
116 0 => {},
117 // TODO map errors
118 else => |err| return std.os.unexpectedErrno(err),
119 }
120 return fd;
121}
122
102pub extern "wasi_snapshot_preview1" fn args_get(argv: [*][*:0]u8, argv_buf: [*]u8) errno_t;123pub extern "wasi_snapshot_preview1" fn args_get(argv: [*][*:0]u8, argv_buf: [*]u8) errno_t;
103pub extern "wasi_snapshot_preview1" fn args_sizes_get(argc: *usize, argv_buf_size: *usize) errno_t;124pub extern "wasi_snapshot_preview1" fn args_sizes_get(argc: *usize, argv_buf_size: *usize) errno_t;
104125