[libstd]: Enable CacheHash in WASI #5636
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR fixes #5437.
Summary of changes to achieve this:
adds
std.os.realpathat
which is similar tostd.os.realpath
, however, it accepts a tuple(fd_t, []const u8)
of args and thus works out the realpath of a relative path wrt to some opened file descriptor.If the input pathname argument turns out to be an absolute path, this function reverts to calling
realpath
on that pathname completely ignoring the input file descriptor. This behaviour is standard in POSIX and IMHO a good rule of thumb to follow.If the input file descriptor was obtained using
std.fs.cwd()
call, this function reverts tostd.os.getcwd()
to obtain the file descriptor's path.adds
std.fs.path.resolveat
functions which is similar tostd.fs.path.resolve
, however, differs in the sense that, in addition to a slice of paths, it also accepts aDir
handle with respect to which the paths should be resolved. It also issues one syscall in order to workout the path to theDir
handle.With
std.fs.path.resolveat
, it is now possible to storework_dir
handle directly inCacheHash
struct and workout paths to cached files wrt to it rather than wrtcwd
.adds
std.fs.path.resolveRelative
function which is likestd.fs.path.resolve
however it resolves relative paths only, and they are always resolved to wrt ".". If the final path is resolved to "." or beyond,null
is returned instead to signify this. Also, if an absolute path is passed as an argument,ResolveRelativeError.AbsolutePath
is thrown. This function is particularly useful for Capsicum-like targets such as WASI.This commit also tweaks
std.cache_hash.CacheHash
to use eitherstd.fs.path.resolveat
orstd.fs.path.resolveRelative
(the latter used when targeting WASI). This way, we essentially got rid of any mention of CWD, and hence, it is now possible to useCacheHash
struct in WASI. As a result, all tests in the said module have been enabled for WASI.cc @andrewrk, as per usual, looking forward to your thoughts on this!