Skip to content

Commit e3b0244

Browse files
authored
Fixes to support WASI on Zephyr (#3948)
Fixed issues in os_renameat and added os_is_* methods for stdin/stdout/stderr. ps. #3311. Signed-off-by: Stephen Berard <[email protected]>
1 parent 87ffe3f commit e3b0244

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

core/shared/platform/zephyr/zephyr_file.c

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@
2020
* represent a zephyr file descriptor and hold useful informations.
2121
* We also created a file descriptor table to keep track of all the file
2222
* descriptors.
23+
*
2324
* To pass the file descriptor reference to the higher level abstraction, we
2425
* pass the index of the fd table to an `os_file_handle` struct.
2526
* Then in the WASI implementation layer we can retrieve the file descriptor
2627
* reference.
28+
*
2729
* We also fake the stdin, stdout and stderr file descriptors.
2830
* We redirect the write operation on stdin, stdout and stderr to `os_printf`.
2931
* We do not handle write on stdin and read on stdin, stdout and stderr.
@@ -43,7 +45,7 @@
4345

4446
// We will take the maximum number of open files
4547
// from the Zephyr POSIX configuration
46-
#define CONFIG_WASI_MAX_OPEN_FILES 16
48+
#define CONFIG_WASI_MAX_OPEN_FILES CONFIG_POSIX_MAX_FDS
4749

4850
// Macro to retrieve a file system descriptor and check it's validity.
4951
#define GET_FILE_SYSTEM_DESCRIPTOR(fd, ptr) \
@@ -711,22 +713,24 @@ os_renameat(os_file_handle old_handle, const char *old_path,
711713
char abs_old_path[MAX_FILE_NAME + 1];
712714
char abs_new_path[MAX_FILE_NAME + 1];
713715

716+
GET_FILE_SYSTEM_DESCRIPTOR(old_handle->fd, ptr);
717+
718+
char *path = strdup(new_path);
719+
if (path == NULL) {
720+
return __WASI_ENOMEM;
721+
}
722+
714723
snprintf(abs_old_path, MAX_FILE_NAME, "%s/%s", prestat_dir, old_path);
715724
snprintf(abs_new_path, MAX_FILE_NAME, "%s/%s", prestat_dir, new_path);
716725

717726
int rc = fs_rename(abs_old_path, abs_new_path);
718727
if (rc < 0) {
728+
free(path);
719729
return convert_errno(-rc);
720730
}
721731

722-
GET_FILE_SYSTEM_DESCRIPTOR(old_handle->fd, ptr);
723-
724-
ptr->path = strdup(new_path);
725-
if (ptr->path == NULL) {
726-
ptr->path = old_path;
727-
return __WASI_ENOMEM;
728-
}
729-
732+
free(ptr->path);
733+
ptr->path = path;
730734
return __WASI_ESUCCESS;
731735
}
732736

@@ -983,4 +987,22 @@ bool
983987
os_compare_file_handle(os_file_handle handle1, os_file_handle handle2)
984988
{
985989
return handle1->fd == handle2->fd && handle1->is_sock == handle2->is_sock;
990+
}
991+
992+
bool
993+
os_is_stdin_handle(os_file_handle fd)
994+
{
995+
return fd == stdin;
996+
}
997+
998+
bool
999+
os_is_stdout_handle(os_file_handle fd)
1000+
{
1001+
return fd == stdout;
1002+
}
1003+
1004+
bool
1005+
os_is_stderr_handle(os_file_handle fd)
1006+
{
1007+
return fd == stderr;
9861008
}

samples/socket-api/file.wasm

-34 KB
Binary file not shown.

0 commit comments

Comments
 (0)