Skip to content

Commit

Permalink
Add basic emulation of getcwd/chdir
Browse files Browse the repository at this point in the history
This commit adds basic emulation of a current working directory to
wasi-libc. The `getcwd` and `chdir` symbols are now implemented and
available for use. The `getcwd` implementation is pretty simple in that
it just copies out of a new global, `__wasilibc_cwd`, which defaults to
`"/"`. The `chdir` implementation is much more involved and has more
ramification, however.

A new function, `make_absolute`, was added to the preopens object. Paths
stored in the preopen table are now always stored as absolute paths
instead of relative paths, and initial relative paths are interpreted as
being relative to `/`. Looking up a path to preopen now always turns it
into an absolute path, relative to the current working directory, and an
appropriate path is then returned.

The signature of `__wasilibc_find_relpath` has changed as well. It now
returns two path components, one for the absolute part and one for the
relative part. Additionally the relative part is always dynamically
allocated since it may no longer be a substring of the original input
path.

This has been tested lightly against the Rust standard library so far,
but I'm not a regular C developer so there's likely a few things to
improve!
  • Loading branch information
alexcrichton committed Aug 4, 2020
1 parent 215adc8 commit 952305f
Show file tree
Hide file tree
Showing 8 changed files with 236 additions and 92 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ LIBC_TOP_HALF_MUSL_SOURCES = \
env/setenv.c \
env/unsetenv.c \
unistd/posix_close.c \
unistd/getcwd.c \
unistd/chdir.c \
) \
$(filter-out %/procfdname.c %/syscall.c %/syscall_ret.c %/vdso.c %/version.c, \
$(wildcard $(LIBC_TOP_HALF_MUSL_SRC_DIR)/internal/*.c)) \
Expand Down
3 changes: 3 additions & 0 deletions expected/wasm32-wasi/defined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ __uflow
__unlist_locked_file
__uselocale
__utc
__wasilibc_cwd
__wasilibc_ensure_environ
__wasilibc_environ
__wasilibc_environ
Expand Down Expand Up @@ -367,6 +368,7 @@ ceill
cexp
cexpf
cexpl
chdir
cimag
cimagf
cimagl
Expand Down Expand Up @@ -578,6 +580,7 @@ getc
getc_unlocked
getchar
getchar_unlocked
getcwd
getdate
getdate_err
getdelim
Expand Down
18 changes: 13 additions & 5 deletions libc-bottom-half/headers/public/wasi/libc-find-relpath.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,22 @@ extern "C" {
#endif

/**
* Look up the given path in the preopened directory map. If a suitable
* entry is found, return its directory file descriptor, and store the
* computed relative path in *relative_path.
* Look up the given `path`, relative to the cwd, in the preopened directory
* map. If a suitable entry is found, then the file descriptor for that entry
* is returned. Additionally the absolute path of the directory's file
* descriptor is returned in `abs_prefix` and the relative portion that needs
* to be opened is stored in `relative_path`.
*
* Returns -1 if no directories were suitable.
* Returns -1 if no directories were suitable or if an allocation error
* happens.
*
* On success the `relative_path` points to a malloc'd string, so you'll
* need to call `free` on it. The `abs_prefix` return does not need to be
* free'd.
*/
int __wasilibc_find_relpath(const char *path,
const char **__restrict__ relative_path);
const char **restrict abs_prefix,
char **__restrict__ relative_path);

#ifdef __cplusplus
}
Expand Down
Loading

0 comments on commit 952305f

Please sign in to comment.