commit 81d824bf80b71fceeb01238824c272d899a364fc (tree) parent 07a968b3444c1db036d605ed8a25bc128110b646 Author: Jakub Konka <kubkon@jakubkonka.com> Date: Tue, 5 May 2020 17:23:49 +0200 Clear PreopenList on every populate call Diffstat:
| M | lib/std/fs/wasi.zig | | | 14 | ++++++++++++++ |
1 file changed, 14 insertions(+), 0 deletions(-)
diff --git a/lib/std/fs/wasi.zig b/lib/std/fs/wasi.zig @@ -71,7 +71,16 @@ pub const PreopenList = struct { /// Populate the list with the preopens by issuing `std.os.wasi.fd_prestat_get` /// and `std.os.wasi.fd_prestat_dir_name` syscalls to the runtime. + /// + /// If called more than once, it will clear its contents every time before + /// issuing the syscalls. pub fn populate(self: *Self) Error!void { + // Clear contents if we're being called again + for (self.toOwnedSlice()) |preopen| { + switch (preopen.@"type") { + PreopenType.Dir => |path| self.buffer.allocator.free(path), + } + } errdefer self.deinit(); var fd: fd_t = 3; // start fd has to be beyond stdio fds @@ -123,6 +132,11 @@ pub const PreopenList = struct { pub fn asSlice(self: *const Self) []const Preopen { return self.buffer.items; } + + /// The caller owns the returned memory. ArrayList becomes empty. + pub fn toOwnedSlice(self: *Self) []Preopen { + return self.buffer.toOwnedSlice(); + } }; /// Convenience wrapper for `std.os.wasi.path_open` syscall.