Skip to content

Commit

Permalink
Provide a public interface to preopened directory lookups. (#10)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
sunfishcode authored Apr 5, 2019
1 parent 320054e commit 685d014
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions expected/wasm32-wasi/defined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ __unlist_locked_file
__uselocale
__utc
__wasilibc_fd_renumber
__wasilibc_find_relpath
__wasilibc_init_preopen
__wasilibc_register_preopened_fd
__wasilibc_rmdirat
Expand Down
1 change: 1 addition & 0 deletions expected/wasm32-wasi/include-all.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@
#include <utime.h>
#include <values.h>
#include <wasi/core.h>
#include <wasi/libc-find-relpath.h>
#include <wasi/libc.h>
#include <wchar.h>
#include <wctype.h>
1 change: 1 addition & 0 deletions expected/wasm32-wasi/predefined-macros.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions libc-bottom-half/headers/public/wasi/libc-find-relpath.h
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions libc-bottom-half/libpreopen/include/libpreopen.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <wasi/libc-find-relpath.h>
#endif

/**
* Create a @ref po_map of at least the specified capacity.
Expand Down
10 changes: 10 additions & 0 deletions libc-bottom-half/libpreopen/lib/po_libc_wrappers.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 685d014

Please sign in to comment.