| ... | @@ -71,7 +71,16 @@ pub const PreopenList = struct { | ... | @@ -71,7 +71,16 @@ pub const PreopenList = struct { |
| 71 | | 71 | |
| 72 | /// Populate the list with the preopens by issuing `std.os.wasi.fd_prestat_get` | 72 | /// Populate the list with the preopens by issuing `std.os.wasi.fd_prestat_get` |
| 73 | /// and `std.os.wasi.fd_prestat_dir_name` syscalls to the runtime. | 73 | /// and `std.os.wasi.fd_prestat_dir_name` syscalls to the runtime. |
| | 74 | /// |
| | 75 | /// If called more than once, it will clear its contents every time before |
| | 76 | /// issuing the syscalls. |
| 74 | pub fn populate(self: *Self) Error!void { | 77 | pub fn populate(self: *Self) Error!void { |
| | 78 | // Clear contents if we're being called again |
| | 79 | for (self.toOwnedSlice()) |preopen| { |
| | 80 | switch (preopen.@"type") { |
| | 81 | PreopenType.Dir => |path| self.buffer.allocator.free(path), |
| | 82 | } |
| | 83 | } |
| 75 | errdefer self.deinit(); | 84 | errdefer self.deinit(); |
| 76 | var fd: fd_t = 3; // start fd has to be beyond stdio fds | 85 | var fd: fd_t = 3; // start fd has to be beyond stdio fds |
| 77 | | 86 | |
| ... | @@ -123,6 +132,11 @@ pub const PreopenList = struct { | ... | @@ -123,6 +132,11 @@ pub const PreopenList = struct { |
| 123 | pub fn asSlice(self: *const Self) []const Preopen { | 132 | pub fn asSlice(self: *const Self) []const Preopen { |
| 124 | return self.buffer.items; | 133 | return self.buffer.items; |
| 125 | } | 134 | } |
| | 135 | |
| | 136 | /// The caller owns the returned memory. ArrayList becomes empty. |
| | 137 | pub fn toOwnedSlice(self: *Self) []Preopen { |
| | 138 | return self.buffer.toOwnedSlice(); |
| | 139 | } |
| 126 | }; | 140 | }; |
| 127 | | 141 | |
| 128 | /// Convenience wrapper for `std.os.wasi.path_open` syscall. | 142 | /// Convenience wrapper for `std.os.wasi.path_open` syscall. |