Skip to content

Commit 13491a5

Browse files
authored
wasip3: Fill out more missing filesystem functionality (WebAssembly#758)
This is a further extension of WebAssembly#757 to implement much more functionality in wasi-libc related to filesystems for the `wasm32-wasip3` target. This largely reuses the `wasm32-wasip2` implementations of things by generated bindings in non-async mode. The end result of this is a whole slew of tests are now passing on the p3 target.
1 parent 58fa18a commit 13491a5

24 files changed

Lines changed: 610 additions & 268 deletions

cmake/bindings.cmake

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,23 @@ add_custom_target(
113113
"--async=-wasi:filesystem/types@${wasip3-version}#[method]descriptor.metadata-hash"
114114
"--async=-wasi:filesystem/types@${wasip3-version}#[method]descriptor.metadata-hash-at"
115115
"--async=-wasi:filesystem/types@${wasip3-version}#[method]descriptor.stat"
116+
"--async=-wasi:filesystem/types@${wasip3-version}#[method]descriptor.stat-at"
116117
"--async=-wasi:filesystem/types@${wasip3-version}#[method]descriptor.get-flags"
117118
"--async=-wasi:filesystem/types@${wasip3-version}#[method]descriptor.open-at"
118119
"--async=-wasi:filesystem/types@${wasip3-version}#[method]descriptor.read-directory"
120+
"--async=-wasi:filesystem/types@${wasip3-version}#[method]descriptor.create-directory-at"
121+
"--async=-wasi:filesystem/types@${wasip3-version}#[method]descriptor.remove-directory-at"
122+
"--async=-wasi:filesystem/types@${wasip3-version}#[method]descriptor.unlink-file-at"
123+
"--async=-wasi:filesystem/types@${wasip3-version}#[method]descriptor.advise"
124+
"--async=-wasi:filesystem/types@${wasip3-version}#[method]descriptor.sync-data"
125+
"--async=-wasi:filesystem/types@${wasip3-version}#[method]descriptor.sync"
126+
"--async=-wasi:filesystem/types@${wasip3-version}#[method]descriptor.set-size"
127+
"--async=-wasi:filesystem/types@${wasip3-version}#[method]descriptor.symlink-at"
128+
"--async=-wasi:filesystem/types@${wasip3-version}#[method]descriptor.link-at"
129+
"--async=-wasi:filesystem/types@${wasip3-version}#[method]descriptor.readlink-at"
130+
"--async=-wasi:filesystem/types@${wasip3-version}#[method]descriptor.rename-at"
131+
"--async=-wasi:filesystem/types@${wasip3-version}#[method]descriptor.set-times-at"
132+
"--async=-wasi:filesystem/types@${wasip3-version}#[method]descriptor.set-times"
119133
"--async=-wasi:clocks/monotonic-clock@${wasip3-version}#wait-until"
120134
"--async=-wasi:clocks/monotonic-clock@${wasip3-version}#wait-for"
121135
${CMAKE_SOURCE_DIR}/wasi/p3/wit

libc-bottom-half/cloudlibc/src/libc/dirent/dirent_impl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ struct _DIR {
2929
size_t offset;
3030
#elif defined(__wasip3__)
3131
filesystem_tuple2_stream_directory_entry_future_result_void_error_code_t stream;
32+
bool stream_done;
3233
size_t skip;
3334
size_t offset;
3435
#else
@@ -53,6 +54,7 @@ static inline void dirent_close_streams(DIR *dirp) {
5354
if (dirp->stream.f0 != 0) {
5455
filesystem_stream_directory_entry_drop_readable(dirp->stream.f0);
5556
dirp->stream.f0 = 0;
57+
dirp->stream_done = false;
5658
}
5759
if (dirp->stream.f1 != 0) {
5860
filesystem_future_result_void_error_code_drop_readable(dirp->stream.f1);

libc-bottom-half/cloudlibc/src/libc/dirent/fdopendir.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ DIR *fdopendir(int fd) {
7373
dirp->stream = result;
7474
#elif defined(__wasip3__)
7575
filesystem_method_descriptor_read_directory(file_handle, &dirp->stream);
76+
dirp->stream_done = false;
7677
#endif
7778
dirp->fd = fd;
7879
dirp->skip = 0;

libc-bottom-half/cloudlibc/src/libc/dirent/readdir.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -234,21 +234,24 @@ static struct dirent *readdir_next(DIR *dirp) {
234234
#elif defined(__wasip3__)
235235
filesystem_directory_entry_t dir_entry;
236236

237+
// Don't try to keep reading once the stream is closed.
238+
if (dirp->stream_done)
239+
return NULL;
240+
237241
// Loop until at least one stream entry is read, or until the stream is closed.
238-
bool closed = false;
239-
while (1) {
242+
while (!dirp->stream_done) {
240243
size_t amount =
241244
__wasilibc_stream_block_on(
242245
filesystem_stream_directory_entry_read(dirp->stream.f0, &dir_entry, 1),
243246
dirp->stream.f0,
244-
&closed);
247+
&dirp->stream_done);
245248

246249
// If something was read, then break out and process that below.
247250
if (amount > 0)
248251
break;
249252

250253
// If nothing was read and the stream isn't finished yet, try again.
251-
if (!closed)
254+
if (!dirp->stream_done)
252255
continue;
253256

254257
// If the stream's result future hasn't been read yet, do so here.
@@ -263,8 +266,8 @@ static struct dirent *readdir_next(DIR *dirp) {
263266
translate_error(result.val.err);
264267
}
265268

266-
// The stream is closed, so return NULL. This'll set `errno` based on the
267-
// result of the future above.
269+
// The stream is closed, so return NULL. If `errno` needs to be set it'll
270+
// have been done above with `f1`.
268271
return NULL;
269272
}
270273
#else

libc-bottom-half/cloudlibc/src/libc/fcntl/posix_fadvise.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ int posix_fadvise(int fd, off_t offset, off_t len, int advice) {
3030

3131
#if defined(__wasip1__)
3232
return __wasi_fd_advise(fd, offset, len, advice);
33-
#elif defined(__wasip2__)
33+
#elif defined(__wasip2__) || defined(__wasip3__)
3434
filesystem_borrow_descriptor_t file_handle;
3535
if (fd_to_file_handle(fd, &file_handle) < 0)
3636
return EBADF;
@@ -71,12 +71,6 @@ int posix_fadvise(int fd, off_t offset, off_t len, int advice) {
7171
return errno;
7272
}
7373
return 0;
74-
#elif defined(__wasip3__)
75-
(void) fd;
76-
(void) advice;
77-
// TODO(wasip3)
78-
errno = ENOTSUP;
79-
return ENOTSUP;
8074
#else
8175
# error "Unsupported WASI version"
8276
#endif

libc-bottom-half/cloudlibc/src/libc/stdio/renameat.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ int __wasilibc_nocwd_renameat(int oldfd, const char *old, int newfd, const char
1919
errno = error;
2020
return -1;
2121
}
22-
#elif defined(__wasip2__)
22+
#elif defined(__wasip2__) || defined(__wasip3__)
2323
// Translate the file descriptors to internal handles
2424
filesystem_borrow_descriptor_t old_file_handle;
2525
if (fd_to_file_handle(oldfd, &old_file_handle) < 0)
@@ -30,7 +30,7 @@ int __wasilibc_nocwd_renameat(int oldfd, const char *old, int newfd, const char
3030
return -1;
3131

3232
// Convert the strings into WASI strings
33-
wasip2_string_t old_path, new_path;
33+
wasi_string_t old_path, new_path;
3434
if (wasi_string_from_c(old, &old_path) < 0)
3535
return -1;
3636
if (wasi_string_from_c(new, &new_path) < 0)
@@ -47,14 +47,6 @@ int __wasilibc_nocwd_renameat(int oldfd, const char *old, int newfd, const char
4747
translate_error(error_code);
4848
return -1;
4949
}
50-
#elif defined(__wasip3__)
51-
(void) oldfd;
52-
(void) old;
53-
(void) newfd;
54-
(void) new;
55-
// TODO(wasip3)
56-
errno = ENOTSUP;
57-
return -1;
5850
#else
5951
# error "Unknown WASI version"
6052
#endif

libc-bottom-half/cloudlibc/src/libc/sys/stat/fstat.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,12 @@ int fstat(int fildes, struct stat *buf) {
1818
}
1919
to_public_stat(&internal_stat, buf);
2020
return 0;
21-
#elif defined(__wasip2__)
21+
#elif defined(__wasip2__) || defined(__wasip3__)
2222
// Translate the file descriptor to an internal handle
2323
descriptor_table_entry_t *entry = descriptor_table_get_ref(fildes);
2424
if (!entry)
2525
return -1;
2626
return entry->vtable->fstat(entry->data, buf);
27-
#elif defined(__wasip3__)
28-
(void) fildes;
29-
(void) buf;
30-
// TODO(wasip3)
31-
errno = ENOTSUP;
32-
return -1;
3327
#else
3428
# error "Unsupported WASI version"
3529
#endif

libc-bottom-half/cloudlibc/src/libc/sys/stat/fstatat.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ int __wasilibc_nocwd_fstatat(int fd, const char *restrict path, struct stat *res
3535
to_public_stat(&internal_stat, buf);
3636

3737
return 0;
38-
#elif defined(__wasip2__)
38+
#elif defined(__wasip2__) || defined(__wasip3__)
3939
// Translate the file descriptor to an internal handle
4040
filesystem_borrow_descriptor_t file_handle;
4141
if (fd_to_file_handle(fd, &file_handle) < 0)
4242
return -1;
4343

4444
// Convert the string into a Wasm string
45-
wasip2_string_t path_wasm_string;
45+
wasi_string_t path_wasm_string;
4646
if (wasi_string_from_c(path, &path_wasm_string) < 0)
4747
return -1;
4848

@@ -80,14 +80,6 @@ int __wasilibc_nocwd_fstatat(int fd, const char *restrict path, struct stat *res
8080
// Convert the internal data to an external struct
8181
to_public_stat(&metadata, &internal_stat, buf);
8282
return 0;
83-
#elif defined(__wasip3__)
84-
(void) fd;
85-
(void) path;
86-
(void) buf;
87-
(void) flag;
88-
// TODO(wasip3)
89-
errno = ENOTSUP;
90-
return -1;
9183
#else
9284
# error "Unsupported WASI version"
9385
#endif

libc-bottom-half/cloudlibc/src/libc/sys/stat/futimens.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ int futimens(int fd, const struct timespec *times) {
2929
errno = error;
3030
return -1;
3131
}
32-
#elif defined(__wasip2__)
32+
#elif defined(__wasip2__) || defined(__wasip3__)
3333
// Translate the file descriptor to an internal handle
3434
filesystem_borrow_descriptor_t file_handle;
3535
if (fd_to_file_handle(fd, &file_handle) < 0)
@@ -52,12 +52,6 @@ int futimens(int fd, const struct timespec *times) {
5252
translate_error(error);
5353
return -1;
5454
}
55-
#elif defined(__wasip3__)
56-
(void) fd;
57-
(void) times;
58-
// TODO(wasip3)
59-
errno = ENOTSUP;
60-
return -1;
6155
#else
6256
# error "Unsupported WASI version"
6357
#endif

libc-bottom-half/cloudlibc/src/libc/sys/stat/mkdirat.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ int __wasilibc_nocwd_mkdirat_nomode(int fd, const char *path) {
2121
return -1;
2222
}
2323
return 0;
24-
#elif defined(__wasip2__)
24+
#elif defined(__wasip2__) || defined(__wasip3__)
2525
// Translate the file descriptor to an internal handle
2626
filesystem_borrow_descriptor_t file_handle;
2727
if (fd_to_file_handle(fd, &file_handle) < 0)
2828
return -1;
2929

3030
// Create the directory
3131
filesystem_error_code_t error;
32-
wasip2_string_t path2;
32+
wasi_string_t path2;
3333
if (wasi_string_from_c(path, &path2) < 0)
3434
return -1;
3535
bool ok = filesystem_method_descriptor_create_directory_at(file_handle,
@@ -40,12 +40,6 @@ int __wasilibc_nocwd_mkdirat_nomode(int fd, const char *path) {
4040
return -1;
4141
}
4242
return 0;
43-
#elif defined(__wasip3__)
44-
(void) fd;
45-
(void) path;
46-
// TODO(wasip3)
47-
errno = ENOTSUP;
48-
return -1;
4943
#else
5044
# error "Unsupported WASI version"
5145
#endif

0 commit comments

Comments
 (0)