Here's how they do it in WASI libc:
In the Zig standard library, what we currently do for WASI is this:
|
} 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.
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
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.FDCWDwhich 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:
This is a prerequisite for #10716.