Function nextWasi [src]

Implementation of next that can return platform-dependent errors depending on the host platform. When the host platform is Linux, error.DirNotFound can be returned if the directory being iterated was deleted during iteration.

Prototype

pub fn nextWasi(self: *Self) ErrorWasi!?Entry

Parameters

self: *Self

Possible Errors

AccessDenied IteratorError
DirNotFound
InvalidUtf8 IteratorError

WASI-only. The path of an entry could not be encoded as valid UTF-8. WASI is unable to handle paths that cannot be encoded as well-formed UTF-8. https://github.com/WebAssembly/wasi-filesystem/issues/17#issuecomment-1430639353

SystemResources IteratorError
Unexpected UnexpectedError

The Operating System returned an undocumented error code.

This error is in theory not possible, but it would be better to handle this error than to invoke undefined behavior.

When this error code is observed, it usually means the Zig Standard Library needs a small patch to add the error code to the error set for the respective function.

Source

pub fn nextWasi(self: *Self) ErrorWasi!?Entry { // We intentinally use fd_readdir even when linked with libc, // since its implementation is exactly the same as below, // and we avoid the code complexity here. const w = std.os.wasi; start_over: while (true) { // According to the WASI spec, the last entry might be truncated, // so we need to check if the left buffer contains the whole dirent. if (self.end_index - self.index < @sizeOf(w.dirent_t)) { var bufused: usize = undefined; switch (w.fd_readdir(self.dir.fd, &self.buf, self.buf.len, self.cookie, &bufused)) { .SUCCESS => {}, .BADF => unreachable, // Dir is invalid or was opened without iteration ability .FAULT => unreachable, .NOTDIR => unreachable, .INVAL => unreachable, .NOENT => return error.DirNotFound, // The directory being iterated was deleted during iteration. .NOTCAPABLE => return error.AccessDenied, .ILSEQ => return error.InvalidUtf8, // An entry's name cannot be encoded as UTF-8. else => |err| return posix.unexpectedErrno(err), } if (bufused == 0) return null; self.index = 0; self.end_index = bufused; } const entry = @as(*align(1) w.dirent_t, @ptrCast(&self.buf[self.index])); const entry_size = @sizeOf(w.dirent_t); const name_index = self.index + entry_size; if (name_index + entry.namlen > self.end_index) { // This case, the name is truncated, so we need to call readdir to store the entire name. self.end_index = self.index; // Force fd_readdir in the next loop. continue :start_over; } const name = self.buf[name_index .. name_index + entry.namlen]; const next_index = name_index + entry.namlen; self.index = next_index; self.cookie = entry.next; // skip . and .. entries if (mem.eql(u8, name, ".") or mem.eql(u8, name, "..")) { continue :start_over; } const entry_kind: Entry.Kind = switch (entry.type) { .BLOCK_DEVICE => .block_device, .CHARACTER_DEVICE => .character_device, .DIRECTORY => .directory, .SYMBOLIC_LINK => .sym_link, .REGULAR_FILE => .file, .SOCKET_STREAM, .SOCKET_DGRAM => .unix_domain_socket, else => .unknown, }; return Entry{ .name = name, .kind = entry_kind, }; } }