Skip to content

Commit

Permalink
Always take CWD into account when looking up relative paths
Browse files Browse the repository at this point in the history
  • Loading branch information
Arshia001 committed Dec 9, 2024
1 parent 04df9f4 commit d9e7fe2
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 12 deletions.
6 changes: 5 additions & 1 deletion libc-bottom-half/headers/public/wasi/libc-find-relpath.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ int __wasilibc_find_abspath(const char *abspath,
* `relative` as a malloc'd buffer that will be `realloc`'d to the appropriate
* size to contain the relative path.
*
* This used to be a weak function for WASI, but since WASIX has a host-side
* CWD that can be set, we always need this. The rest of this comment block is
* the documentation for the original weak WASI version of it.
*
* Note that this is a weak symbol and if it's not defined you can use
* `__wasilibc_find_relpath`. The weak-nature of this symbols means that if it's
* not otherwise included in the compilation then `chdir` wasn't used an there's
Expand All @@ -70,7 +74,7 @@ int __wasilibc_find_relpath_alloc(
char **relative,
size_t *relative_len,
int can_realloc
) __attribute__((__weak__));
);

#ifdef __cplusplus
}
Expand Down
5 changes: 4 additions & 1 deletion libc-bottom-half/sources/chdir.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,10 @@ int chdir(const char *path)
{
// we run the legacy chdir function that emulates real
// current directory functionality for backwards compatibility reasons
chdir_legacy(path);
if (chdir_legacy(path) != 0) {
// chdir_legacy already sets errno
return -1;
}

// otherwise we let the operating system know we are changing the current path
__wasi_errno_t error = __wasi_chdir(path);
Expand Down
5 changes: 1 addition & 4 deletions libc-bottom-half/sources/posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@ static int find_relpath2(
char **relative,
size_t *relative_len
) {
// See comments in `preopens.c` for what this trick is doing.
const char *abs;
if (__wasilibc_find_relpath_alloc)
return __wasilibc_find_relpath_alloc(path, &abs, relative, relative_len, 1);
return __wasilibc_find_relpath(path, &abs, relative, *relative_len);
return __wasilibc_find_relpath_alloc(path, &abs, relative, relative_len, 1);
}

// Helper to call `__wasilibc_find_relpath` and return an already-managed
Expand Down
7 changes: 1 addition & 6 deletions libc-bottom-half/sources/preopens.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,7 @@ int __wasilibc_find_relpath(const char *path,
const char **abs_prefix,
char **relative_path,
size_t relative_path_len) {
// If `chdir` is linked, whose object file defines this symbol, then we
// call that. Otherwise if the program can't `chdir` then `path` is
// absolute (or relative to the root dir), so we delegate to `find_abspath`
if (__wasilibc_find_relpath_alloc)
return __wasilibc_find_relpath_alloc(path, abs_prefix, relative_path, &relative_path_len, 0);
return __wasilibc_find_abspath(path, abs_prefix, (const char**) relative_path);
return __wasilibc_find_relpath_alloc(path, abs_prefix, relative_path, &relative_path_len, 0);
}

// See the documentation in libc-find-relpath.h.
Expand Down

0 comments on commit d9e7fe2

Please sign in to comment.