Skip to content

Commit 5da3ca4

Browse files
authored
Improve wasmfs API doc comments and return types (#16267)
Update the return types of `wasmfs_create_file` and `wasmfs_create_directory` to match the return types of `open` and `mkdir`, respectively, and document that they have similar behavior to those functions.
1 parent 89c9312 commit 5da3ca4

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

system/include/emscripten/wasmfs.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,15 @@ backend_t wasmfs_get_backend_by_path(char* path);
2222
// Obtains the backend_t of a specified fd.
2323
backend_t wasmfs_get_backend_by_fd(int fd);
2424

25-
// Creates a new file in the new file system under a specific backend.
26-
uint32_t wasmfs_create_file(char* pathname, mode_t mode, backend_t backend);
25+
// Creates and opens a new file in the new file system under a specific backend.
26+
// Returns the file descriptor for the new file like `open`. Returns a negative
27+
// value on error. TODO: It might be worth returning a more specialized type
28+
// like __wasi_fd_t here.
29+
int wasmfs_create_file(char* pathname, mode_t mode, backend_t backend);
2730

2831
// Creates a new directory in the new file system under a specific backend.
29-
long wasmfs_create_directory(char* path, long mode, backend_t backend);
32+
// Returns 0 on success like `mkdir`, or a negative value on error.
33+
int wasmfs_create_directory(char* path, long mode, backend_t backend);
3034

3135
// Backend creation
3236

system/lib/wasmfs/syscalls.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,9 @@ static __wasi_fd_t doOpen(char* pathname,
429429

430430
// This function is exposed to users and allows users to create a file in a
431431
// specific backend. An fd to an open file is returned.
432-
__wasi_fd_t wasmfs_create_file(char* pathname, mode_t mode, backend_t backend) {
432+
int wasmfs_create_file(char* pathname, mode_t mode, backend_t backend) {
433+
static_assert(std::is_same_v<decltype(doOpen(0, 0, 0, 0)), unsigned int>,
434+
"unexpected conversion from result of doOpen to int");
433435
return doOpen(pathname, O_CREAT, mode, backend);
434436
}
435437

@@ -491,7 +493,9 @@ static long doMkdir(char* path, long mode, backend_t backend = NullBackend) {
491493

492494
// This function is exposed to users and allows users to specify a particular
493495
// backend that a directory should be created within.
494-
long wasmfs_create_directory(char* path, long mode, backend_t backend) {
496+
int wasmfs_create_directory(char* path, long mode, backend_t backend) {
497+
static_assert(std::is_same_v<decltype(doMkdir(0, 0, 0)), long>,
498+
"unexpected conversion from result of doMkdir to int");
495499
return doMkdir(path, mode, backend);
496500
}
497501

0 commit comments

Comments
 (0)