Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
andrewrk opened this issue Jan 31, 2022 · 0 comments · Fixed by #11021
Closed

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

andrewrk opened this issue Jan 31, 2022 · 0 comments · Fixed by #11021
Labels
contributor friendly This issue is limited in scope and/or knowledge of Zig internals. enhancement Solving this issue will likely involve adding new logic or components to the codebase. os-wasi standard library This issue involves writing Zig code for the standard library.
Milestone

Comments

@andrewrk
Copy link
Member

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.

@andrewrk andrewrk added enhancement Solving this issue will likely involve adding new logic or components to the codebase. contributor friendly This issue is limited in scope and/or knowledge of Zig internals. standard library This issue involves writing Zig code for the standard library. os-wasi labels Jan 31, 2022
@andrewrk andrewrk added this to the 0.10.0 milestone Jan 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor friendly This issue is limited in scope and/or knowledge of Zig internals. enhancement Solving this issue will likely involve adding new logic or components to the codebase. os-wasi standard library This issue involves writing Zig code for the standard library.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant