Skip to content

Commit f709b21

Browse files
committed
Port __wasilibc_fd_renumber
1 parent 78edd0d commit f709b21

File tree

3 files changed

+23
-21
lines changed

3 files changed

+23
-21
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ DIR *fdopendir(int fd) {
5656
stream_info.directory_file_handle = file_handle;
5757
new_entry.directory_stream_info = stream_info;
5858
int new_fd = -1;
59-
descriptor_table_insert(new_entry, &dirp->fd);
59+
descriptor_table_update(dirp->fd, new_entry);
6060

6161
dirp->cookie = __WASI_DIRCOOKIE_START;
6262
dirp->buffer_processed = 0;

libc-bottom-half/sources/__wasilibc_fd_renumber.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
#ifdef __wasilibc_use_wasip2
2+
#include <wasi/descriptor_table.h>
3+
#else
14
#include <wasi/api.h>
5+
#endif
26
#include <wasi/libc.h>
37
#include <errno.h>
48
#include <unistd.h>
@@ -7,16 +11,27 @@ int __wasilibc_fd_renumber(int fd, int newfd) {
711
// Scan the preopen fds before making any changes.
812
__wasilibc_populate_preopens();
913

14+
#ifdef __wasilibc_use_wasip2
15+
descriptor_table_entry_t* entry;
16+
if (!descriptor_table_get_ref(fd, &entry)) {
17+
errno = EBADF;
18+
return -1;
19+
}
20+
if (!descriptor_table_update(newfd, *entry)) {
21+
errno = EBADF;
22+
return -1;
23+
}
24+
#else
1025
__wasi_errno_t error = __wasi_fd_renumber(fd, newfd);
1126
if (error != 0) {
1227
errno = error;
1328
return -1;
1429
}
30+
#endif
1531
return 0;
1632
}
1733

1834
#ifdef __wasilibc_use_wasip2
19-
#include <wasi/descriptor_table.h>
2035

2136
void drop_tcp_socket(tcp_socket_t socket) {
2237
switch (socket.state.tag) {

libc-bottom-half/sources/descriptor_table.c

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,8 @@ static bool remove(int fd, descriptor_table_entry_t *entry,
207207

208208
bool descriptor_table_insert(descriptor_table_entry_t entry, int *fd)
209209
{
210-
*fd = ++next_fd;
211-
if (insert(entry, *fd, &global_table, false)) {
212-
return true;
213-
} else {
214-
*fd = -1;
215-
return false;
216-
}
210+
*fd = ++next_fd;
211+
return insert(entry, *fd, &global_table, false);
217212
}
218213

219214
bool descriptor_table_get_ref(int fd, descriptor_table_entry_t **entry)
@@ -222,18 +217,10 @@ bool descriptor_table_get_ref(int fd, descriptor_table_entry_t **entry)
222217
}
223218

224219
bool descriptor_table_update(int fd, descriptor_table_entry_t entry) {
225-
if (!global_table.entries)
226-
return false;
227-
228-
size_t hash = keyhash(fd);
229-
descriptor_table_item_t *e = lookup(fd, hash, &global_table);
230-
if (e->occupied) {
231-
insert(entry, fd, &global_table, true);
232-
return true;
233-
}
234-
else {
235-
return false;
236-
}
220+
if (!global_table.entries)
221+
return false;
222+
223+
return insert(entry, fd, &global_table, true);
237224
}
238225

239226
bool descriptor_table_remove(int fd, descriptor_table_entry_t *entry)

0 commit comments

Comments
 (0)