Skip to content

add the concept of cwd to standard library file system abstraction for WASI #10749

@andrewrk

Description

@andrewrk

Here's how they do it in WASI libc:

In the Zig standard library, what we currently do for WASI is this:

zig/lib/std/fs.zig

Lines 2242 to 2245 in abbcf40

} else if (builtin.os.tag == .wasi and !builtin.link_libc) {
@compileError("WASI doesn't have a concept of cwd(); use std.fs.wasi.PreopenList to get available Dir handles instead");
} else {
return Dir{ .fd = os.AT.FDCWD };

Notice that for WASI libc however they have an "emulated FD_ATCWD". It looks like it just treats / as the cwd.

Instead of erroring out, let's follow this convention by returning fd = std.os.wasi.AT.FDCWD which matches the WASI libc convention of -2.

Then we have to check for FD_ATCWD at every std.os function (see similar code in the WASI libc PR linked above). For example:

int fstatat(int dirfd, const char *__restrict pathname, struct stat *__restrict statbuf, int flags) {
    if (dirfd == AT_FDCWD || pathname[0] == '/') {
        return __wasilibc_stat(pathname, statbuf, flags);
    }

    return __wasilibc_nocwd_fstatat(dirfd, pathname, statbuf, flags);
}

This is a prerequisite for #10716.

Metadata

Metadata

Assignees

No one assigned

    Labels

    contributor friendlyThis issue is limited in scope and/or knowledge of Zig internals.enhancementSolving this issue will likely involve adding new logic or components to the codebase.os-wasistandard libraryThis issue involves writing Zig code for the standard library.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions