diff options
| author | Jakub Konka <kubkon@jakubkonka.com> | 2020-05-05 17:23:49 +0200 |
|---|---|---|
| committer | Jakub Konka <kubkon@jakubkonka.com> | 2020-05-05 17:23:49 +0200 |
| commit | 81d824bf80b71fceeb01238824c272d899a364fc (patch) | |
| tree | c8a5709c7df696a0790c6f2dd277aaea93a9eda6 /lib/std | |
| parent | 07a968b3444c1db036d605ed8a25bc128110b646 (diff) | |
| download | zig-81d824bf80b71fceeb01238824c272d899a364fc.tar.gz zig-81d824bf80b71fceeb01238824c272d899a364fc.zip | |
Clear PreopenList on every populate call
Diffstat (limited to 'lib/std')
| -rw-r--r-- | lib/std/fs/wasi.zig | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/lib/std/fs/wasi.zig b/lib/std/fs/wasi.zig index 21107c2094..1cf56e6c7b 100644 --- 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. |
