diff --git a/expected/wasm32-wasi/defined-symbols.txt b/expected/wasm32-wasi/defined-symbols.txt index 6c0dc4e24..716b119a8 100644 --- a/expected/wasm32-wasi/defined-symbols.txt +++ b/expected/wasm32-wasi/defined-symbols.txt @@ -242,6 +242,7 @@ __unlist_locked_file __uselocale __utc __wasilibc_fd_renumber +__wasilibc_find_relpath __wasilibc_init_preopen __wasilibc_register_preopened_fd __wasilibc_rmdirat diff --git a/expected/wasm32-wasi/include-all.c b/expected/wasm32-wasi/include-all.c index 8295222d1..62760dd2d 100644 --- a/expected/wasm32-wasi/include-all.c +++ b/expected/wasm32-wasi/include-all.c @@ -183,6 +183,7 @@ #include #include #include +#include #include #include #include diff --git a/expected/wasm32-wasi/predefined-macros.txt b/expected/wasm32-wasi/predefined-macros.txt index 7c89429ec..9b636b8d1 100644 --- a/expected/wasm32-wasi/predefined-macros.txt +++ b/expected/wasm32-wasi/predefined-macros.txt @@ -3622,6 +3622,7 @@ #define __va_copy(d,s) __builtin_va_copy(d,s) #define __wasi__ 1 #define __wasi_core_h +#define __wasi_libc_find_relpath_h #define __wasi_libc_h #define __wasilibc___errno_values_h #define __wasilibc___fd_set_h diff --git a/libc-bottom-half/headers/public/wasi/libc-find-relpath.h b/libc-bottom-half/headers/public/wasi/libc-find-relpath.h new file mode 100644 index 000000000..9f5fab3d8 --- /dev/null +++ b/libc-bottom-half/headers/public/wasi/libc-find-relpath.h @@ -0,0 +1,25 @@ +#ifndef __wasi_libc_find_relpath_h +#define __wasi_libc_find_relpath_h + +#ifdef __cplusplus +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. Ignore preopened directories + * which don't provide the specified rights. + * + * Returns -1 if no directories were suitable. + */ +int __wasilibc_find_relpath(const char *path, + __wasi_rights_t rights_base, + __wasi_rights_t rights_inheriting, + const char **relative_path); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/libc-bottom-half/libpreopen/include/libpreopen.h b/libc-bottom-half/libpreopen/include/libpreopen.h index 8185dcfa0..9c86b9fe1 100644 --- a/libc-bottom-half/libpreopen/include/libpreopen.h +++ b/libc-bottom-half/libpreopen/include/libpreopen.h @@ -82,6 +82,12 @@ struct po_relpath { /** The path, relative to the directory represented by @ref dirfd */ const char *relative_path; }; +#ifdef __wasilibc_unmodified_upstream +#else +// This functionality is exposed in a libc header, so include that header +// and use its declarations. +#include +#endif /** * Create a @ref po_map of at least the specified capacity. diff --git a/libc-bottom-half/libpreopen/lib/po_libc_wrappers.c b/libc-bottom-half/libpreopen/lib/po_libc_wrappers.c index a6d444531..5aead7931 100644 --- a/libc-bottom-half/libpreopen/lib/po_libc_wrappers.c +++ b/libc-bottom-half/libpreopen/lib/po_libc_wrappers.c @@ -682,4 +682,14 @@ __wasilibc_register_preopened_fd(int fd, const char *path) return 0; } + +int __wasilibc_find_relpath(const char *path, + __wasi_rights_t rights_base, + __wasi_rights_t rights_inheriting, + const char **relpath) +{ + struct po_relpath rel = find_relative(path, rights_base, rights_inheriting); + *relpath = rel.relative_path; + return rel.dirfd; +} #endif