Skip to content

Commit 685d014

Browse files
authored
Provide a public interface to preopened directory lookups. (#10)
* Provide a public interface to preopened directory lookups. For users of especially non-C compilers, provide an API for looking up preopened directories. This is used internally in WASI libc to translate from eg. `open` to `openat`, but it can now also be used by user code.
1 parent 320054e commit 685d014

File tree

6 files changed

+44
-0
lines changed

6 files changed

+44
-0
lines changed

expected/wasm32-wasi/defined-symbols.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ __unlist_locked_file
242242
__uselocale
243243
__utc
244244
__wasilibc_fd_renumber
245+
__wasilibc_find_relpath
245246
__wasilibc_init_preopen
246247
__wasilibc_register_preopened_fd
247248
__wasilibc_rmdirat

expected/wasm32-wasi/include-all.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@
183183
#include <utime.h>
184184
#include <values.h>
185185
#include <wasi/core.h>
186+
#include <wasi/libc-find-relpath.h>
186187
#include <wasi/libc.h>
187188
#include <wchar.h>
188189
#include <wctype.h>

expected/wasm32-wasi/predefined-macros.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3622,6 +3622,7 @@
36223622
#define __va_copy(d,s) __builtin_va_copy(d,s)
36233623
#define __wasi__ 1
36243624
#define __wasi_core_h
3625+
#define __wasi_libc_find_relpath_h
36253626
#define __wasi_libc_h
36263627
#define __wasilibc___errno_values_h
36273628
#define __wasilibc___fd_set_h
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#ifndef __wasi_libc_find_relpath_h
2+
#define __wasi_libc_find_relpath_h
3+
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
8+
/**
9+
* Look up the given path in the preopened directory map. If a suitable
10+
* entry is found, return its directory file descriptor, and store the
11+
* computed relative path in *relative_path. Ignore preopened directories
12+
* which don't provide the specified rights.
13+
*
14+
* Returns -1 if no directories were suitable.
15+
*/
16+
int __wasilibc_find_relpath(const char *path,
17+
__wasi_rights_t rights_base,
18+
__wasi_rights_t rights_inheriting,
19+
const char **relative_path);
20+
21+
#ifdef __cplusplus
22+
}
23+
#endif
24+
25+
#endif

libc-bottom-half/libpreopen/include/libpreopen.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ struct po_relpath {
8282
/** The path, relative to the directory represented by @ref dirfd */
8383
const char *relative_path;
8484
};
85+
#ifdef __wasilibc_unmodified_upstream
86+
#else
87+
// This functionality is exposed in a libc header, so include that header
88+
// and use its declarations.
89+
#include <wasi/libc-find-relpath.h>
90+
#endif
8591

8692
/**
8793
* Create a @ref po_map of at least the specified capacity.

libc-bottom-half/libpreopen/lib/po_libc_wrappers.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,4 +682,14 @@ __wasilibc_register_preopened_fd(int fd, const char *path)
682682

683683
return 0;
684684
}
685+
686+
int __wasilibc_find_relpath(const char *path,
687+
__wasi_rights_t rights_base,
688+
__wasi_rights_t rights_inheriting,
689+
const char **relpath)
690+
{
691+
struct po_relpath rel = find_relative(path, rights_base, rights_inheriting);
692+
*relpath = rel.relative_path;
693+
return rel.dirfd;
694+
}
685695
#endif

0 commit comments

Comments
 (0)