20
20
* represent a zephyr file descriptor and hold useful informations.
21
21
* We also created a file descriptor table to keep track of all the file
22
22
* descriptors.
23
+ *
23
24
* To pass the file descriptor reference to the higher level abstraction, we
24
25
* pass the index of the fd table to an `os_file_handle` struct.
25
26
* Then in the WASI implementation layer we can retrieve the file descriptor
26
27
* reference.
28
+ *
27
29
* We also fake the stdin, stdout and stderr file descriptors.
28
30
* We redirect the write operation on stdin, stdout and stderr to `os_printf`.
29
31
* We do not handle write on stdin and read on stdin, stdout and stderr.
43
45
44
46
// We will take the maximum number of open files
45
47
// from the Zephyr POSIX configuration
46
- #define CONFIG_WASI_MAX_OPEN_FILES 16
48
+ #define CONFIG_WASI_MAX_OPEN_FILES CONFIG_POSIX_MAX_FDS
47
49
48
50
// Macro to retrieve a file system descriptor and check it's validity.
49
51
#define GET_FILE_SYSTEM_DESCRIPTOR (fd , ptr ) \
@@ -711,22 +713,24 @@ os_renameat(os_file_handle old_handle, const char *old_path,
711
713
char abs_old_path [MAX_FILE_NAME + 1 ];
712
714
char abs_new_path [MAX_FILE_NAME + 1 ];
713
715
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
+
714
723
snprintf (abs_old_path , MAX_FILE_NAME , "%s/%s" , prestat_dir , old_path );
715
724
snprintf (abs_new_path , MAX_FILE_NAME , "%s/%s" , prestat_dir , new_path );
716
725
717
726
int rc = fs_rename (abs_old_path , abs_new_path );
718
727
if (rc < 0 ) {
728
+ free (path );
719
729
return convert_errno (- rc );
720
730
}
721
731
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 ;
730
734
return __WASI_ESUCCESS ;
731
735
}
732
736
@@ -983,4 +987,22 @@ bool
983
987
os_compare_file_handle (os_file_handle handle1 , os_file_handle handle2 )
984
988
{
985
989
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 ;
986
1008
}
0 commit comments