Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions lib/std/fs/wasi.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const std = @import("std");
const os = std.os;
const mem = std.mem;
const math = std.math;
const Allocator = mem.Allocator;

usingnamespace std.os.wasi;
Expand Down Expand Up @@ -72,7 +73,7 @@ pub const PreopenList = struct {

const Self = @This();

pub const Error = os.UnexpectedError || Allocator.Error;
pub const Error = error{ OutOfMemory, Overflow } || os.UnexpectedError;

/// Deinitialize with `deinit`.
pub fn init(allocator: *Allocator) Self {
Expand All @@ -94,6 +95,12 @@ pub const PreopenList = struct {
///
/// If called more than once, it will clear its contents every time before
/// issuing the syscalls.
///
/// In the unlinkely event of overflowing the number of available file descriptors,
/// returns `error.Overflow`. In this case, even though an error condition was reached
/// the preopen list still contains all valid preopened file descriptors that are valid
/// for use. Therefore, it is fine to call `find`, `asSlice`, or `toOwnedSlice`. Finally,
/// `deinit` still must be called!
pub fn populate(self: *Self) Error!void {
// Clear contents if we're being called again
for (self.toOwnedSlice()) |preopen| {
Expand All @@ -110,6 +117,7 @@ pub const PreopenList = struct {
ESUCCESS => {},
ENOTSUP => {
// not a preopen, so keep going
fd = try math.add(fd_t, fd, 1);
continue;
},
EBADF => {
Expand All @@ -127,7 +135,7 @@ pub const PreopenList = struct {
}
const preopen = Preopen.new(fd, PreopenType{ .Dir = path_buf });
try self.buffer.append(preopen);
fd += 1;
fd = try math.add(fd_t, fd, 1);
}
}

Expand Down