| ... | ... | @@ -4,6 +4,7 @@ |
| 4 | 4 | const std = @import("std"); |
| 5 | 5 | const mem = std.mem; |
| 6 | 6 | const assert = std.debug.assert; |
| 7 | const Allocator = mem.Allocator; |
| 7 | 8 | |
| 8 | 9 | pub usingnamespace @import("bits.zig"); |
| 9 | 10 | |
| ... | ... | @@ -21,51 +22,6 @@ comptime { |
| 21 | 22 | pub const iovec_t = iovec; |
| 22 | 23 | pub const ciovec_t = iovec_const; |
| 23 | 24 | |
| 24 | | fn is_prefix(preopen: []const u8, path: []const u8) bool { |
| 25 | | if (preopen.len > path.len) { |
| 26 | | return false; |
| 27 | | } |
| 28 | | var i: usize = 0; |
| 29 | | while (i < preopen.len) { |
| 30 | | std.debug.warn("{} == {}", .{ preopen[i], path[i] }); |
| 31 | | if (preopen[i] != path[i]) { |
| 32 | | return false; |
| 33 | | } |
| 34 | | i += 1; |
| 35 | | } |
| 36 | | return true; |
| 37 | | } |
| 38 | | |
| 39 | | pub fn resolve_preopen(path: []const u8, dirfd: *fd_t, prefix: *usize) errno_t { |
| 40 | | var fd: fd_t = 3; // start fd has to be beyond stdio fds |
| 41 | | var preopen_path = [_]u8{0} ** PATH_MAX; |
| 42 | | |
| 43 | | while (true) { |
| 44 | | var buf: prestat_t = undefined; |
| 45 | | switch (fd_prestat_get(fd, &buf)) { |
| 46 | | ESUCCESS => {}, |
| 47 | | EBADF => { |
| 48 | | break; |
| 49 | | }, |
| 50 | | else => |err| return err, |
| 51 | | } |
| 52 | | const preopen_len = buf.u.dir.pr_name_len; |
| 53 | | switch (fd_prestat_dir_name(fd, &preopen_path, preopen_len)) { |
| 54 | | ESUCCESS => {}, |
| 55 | | else => |err| return err, |
| 56 | | } |
| 57 | | if (is_prefix(preopen_path[0..preopen_len], path)) { |
| 58 | | dirfd.* = fd; |
| 59 | | prefix.* = preopen_len; |
| 60 | | return ESUCCESS; |
| 61 | | } |
| 62 | | std.mem.set(u8, &preopen_path, 0); |
| 63 | | fd += 1; |
| 64 | | } |
| 65 | | |
| 66 | | return ENOTCAPABLE; |
| 67 | | } |
| 68 | | |
| 69 | 25 | pub const PreopenType = enum { |
| 70 | 26 | Dir, |
| 71 | 27 | }; |
| ... | ... | @@ -86,50 +42,62 @@ pub const Preopen = struct { |
| 86 | 42 | } |
| 87 | 43 | }; |
| 88 | 44 | |
| 89 | | pub const GetPreopenError = std.os.UnexpectedError || mem.Allocator.Error; |
| 90 | | |
| 91 | | pub fn getPreopens(allocator: *mem.Allocator) GetPreopenError![]Preopen { |
| 92 | | var preopens = std.ArrayList(Preopen).init(allocator); |
| 93 | | errdefer freePreopens(allocator, preopens); |
| 94 | | var fd: fd_t = 3; // start fd has to be beyond stdio fds |
| 95 | | |
| 96 | | while (true) { |
| 97 | | var buf: prestat_t = undefined; |
| 98 | | switch (fd_prestat_get(fd, &buf)) { |
| 99 | | ESUCCESS => {}, |
| 100 | | ENOTSUP => { |
| 101 | | // not a preopen, so keep going |
| 102 | | continue; |
| 103 | | }, |
| 104 | | EBADF => { |
| 105 | | // OK, no more fds available |
| 106 | | break; |
| 107 | | }, |
| 108 | | else => |err| return std.os.unexpectedErrno(err), |
| 109 | | } |
| 110 | | const preopen_len = buf.u.dir.pr_name_len; |
| 111 | | const path_buf = try allocator.alloc(u8, preopen_len); |
| 112 | | mem.set(u8, path_buf, 0); |
| 113 | | switch (fd_prestat_dir_name(fd, path_buf.ptr, preopen_len)) { |
| 114 | | ESUCCESS => {}, |
| 115 | | else => |err| return std.os.unexpectedErrno(err), |
| 116 | | } |
| 117 | | const preopen = Preopen.newDir(fd, path_buf); |
| 118 | | try preopens.append(preopen); |
| 119 | | fd += 1; |
| 45 | pub const PreopenList = struct { |
| 46 | const InnerList = std.ArrayList(Preopen); |
| 47 | |
| 48 | buffer: InnerList, |
| 49 | |
| 50 | const Self = @This(); |
| 51 | pub const Error = std.os.UnexpectedError || Allocator.Error; |
| 52 | |
| 53 | pub fn init(allocator: *Allocator) Self { |
| 54 | return Self{ .buffer = InnerList.init(allocator) }; |
| 120 | 55 | } |
| 121 | 56 | |
| 122 | | return preopens.toOwnedSlice(); |
| 123 | | } |
| 57 | pub fn deinit(pm: Self) void { |
| 58 | for (pm.buffer.items) |preopen| { |
| 59 | switch (preopen.@"type") { |
| 60 | PreopenType.Dir => |path| pm.buffer.allocator.free(path), |
| 61 | } |
| 62 | } |
| 63 | pm.buffer.deinit(); |
| 64 | } |
| 124 | 65 | |
| 125 | | pub fn freePreopens(allocator: *mem.Allocator, preopens: std.ArrayList(Preopen)) void { |
| 126 | | for (preopens.items) |preopen| { |
| 127 | | switch (preopen.@"type") { |
| 128 | | PreopenType.Dir => |path| allocator.free(path), |
| 66 | pub fn populate(self: *Self) Error!void { |
| 67 | errdefer self.deinit(); |
| 68 | var fd: fd_t = 3; // start fd has to be beyond stdio fds |
| 69 | |
| 70 | while (true) { |
| 71 | var buf: prestat_t = undefined; |
| 72 | switch (fd_prestat_get(fd, &buf)) { |
| 73 | ESUCCESS => {}, |
| 74 | ENOTSUP => { |
| 75 | // not a preopen, so keep going |
| 76 | continue; |
| 77 | }, |
| 78 | EBADF => { |
| 79 | // OK, no more fds available |
| 80 | break; |
| 81 | }, |
| 82 | else => |err| return std.os.unexpectedErrno(err), |
| 83 | } |
| 84 | const preopen_len = buf.u.dir.pr_name_len; |
| 85 | const path_buf = try self.buffer.allocator.alloc(u8, preopen_len); |
| 86 | mem.set(u8, path_buf, 0); |
| 87 | switch (fd_prestat_dir_name(fd, path_buf.ptr, preopen_len)) { |
| 88 | ESUCCESS => {}, |
| 89 | else => |err| return std.os.unexpectedErrno(err), |
| 90 | } |
| 91 | const preopen = Preopen.newDir(fd, path_buf); |
| 92 | try self.buffer.append(preopen); |
| 93 | fd += 1; |
| 129 | 94 | } |
| 130 | 95 | } |
| 131 | | preopens.deinit(); |
| 132 | | } |
| 96 | |
| 97 | pub fn asSlice(self: *const Self) []const Preopen { |
| 98 | return self.buffer.items; |
| 99 | } |
| 100 | }; |
| 133 | 101 | |
| 134 | 102 | pub extern "wasi_snapshot_preview1" fn args_get(argv: [*][*:0]u8, argv_buf: [*]u8) errno_t; |
| 135 | 103 | pub extern "wasi_snapshot_preview1" fn args_sizes_get(argc: *usize, argv_buf_size: *usize) errno_t; |