66#include "platform_api_extension.h"
77#include "libc_errno.h"
88#include <unistd.h>
9+ #include "posix_util_convert.h"
910
1011#if !defined(__APPLE__ ) && !defined(ESP_PLATFORM )
1112#define CONFIG_HAS_PWRITEV 1
6667#define STDERR_FILENO 2
6768#endif
6869
69- // Converts a POSIX timespec to a WASI timestamp.
70- static __wasi_timestamp_t
71- convert_timespec (const struct timespec * ts )
72- {
73- if (ts -> tv_sec < 0 )
74- return 0 ;
75- if ((__wasi_timestamp_t )ts -> tv_sec >= UINT64_MAX / 1000000000 )
76- return UINT64_MAX ;
77- return (__wasi_timestamp_t )ts -> tv_sec * 1000000000
78- + (__wasi_timestamp_t )ts -> tv_nsec ;
79- }
80-
81- // Converts a POSIX stat structure to a WASI filestat structure
82- static void
83- convert_stat (os_file_handle handle , const struct stat * in ,
84- __wasi_filestat_t * out )
85- {
86- out -> st_dev = in -> st_dev ;
87- out -> st_ino = in -> st_ino ;
88- out -> st_nlink = (__wasi_linkcount_t )in -> st_nlink ;
89- out -> st_size = (__wasi_filesize_t )in -> st_size ;
90- #ifdef __APPLE__
91- out -> st_atim = convert_timespec (& in -> st_atimespec );
92- out -> st_mtim = convert_timespec (& in -> st_mtimespec );
93- out -> st_ctim = convert_timespec (& in -> st_ctimespec );
94- #else
95- out -> st_atim = convert_timespec (& in -> st_atim );
96- out -> st_mtim = convert_timespec (& in -> st_mtim );
97- out -> st_ctim = convert_timespec (& in -> st_ctim );
98- #endif
99-
100- // Convert the file type. In the case of sockets there is no way we
101- // can easily determine the exact socket type.
102- if (S_ISBLK (in -> st_mode )) {
103- out -> st_filetype = __WASI_FILETYPE_BLOCK_DEVICE ;
104- }
105- else if (S_ISCHR (in -> st_mode )) {
106- out -> st_filetype = __WASI_FILETYPE_CHARACTER_DEVICE ;
107- }
108- else if (S_ISDIR (in -> st_mode )) {
109- out -> st_filetype = __WASI_FILETYPE_DIRECTORY ;
110- }
111- else if (S_ISFIFO (in -> st_mode )) {
112- out -> st_filetype = __WASI_FILETYPE_SOCKET_STREAM ;
113- }
114- else if (S_ISLNK (in -> st_mode )) {
115- out -> st_filetype = __WASI_FILETYPE_SYMBOLIC_LINK ;
116- }
117- else if (S_ISREG (in -> st_mode )) {
118- out -> st_filetype = __WASI_FILETYPE_REGULAR_FILE ;
119- }
120- else if (S_ISSOCK (in -> st_mode )) {
121- int socktype ;
122- socklen_t socktypelen = sizeof (socktype );
123-
124- if (getsockopt (handle , SOL_SOCKET , SO_TYPE , & socktype , & socktypelen )
125- < 0 ) {
126- out -> st_filetype = __WASI_FILETYPE_UNKNOWN ;
127- return ;
128- }
129-
130- switch (socktype ) {
131- case SOCK_DGRAM :
132- out -> st_filetype = __WASI_FILETYPE_SOCKET_DGRAM ;
133- break ;
134- case SOCK_STREAM :
135- out -> st_filetype = __WASI_FILETYPE_SOCKET_STREAM ;
136- break ;
137- default :
138- out -> st_filetype = __WASI_FILETYPE_UNKNOWN ;
139- return ;
140- }
141- }
142- else {
143- out -> st_filetype = __WASI_FILETYPE_UNKNOWN ;
144- }
145- }
146-
14770static void
14871convert_timestamp (__wasi_timestamp_t in , struct timespec * out )
14972{
@@ -196,7 +119,7 @@ os_fstat(os_file_handle handle, struct __wasi_filestat_t *buf)
196119 if (ret < 0 )
197120 return convert_errno (errno );
198121
199- convert_stat (handle , & stat_buf , buf );
122+ posix_convert_stat (handle , & stat_buf , buf );
200123
201124 return __WASI_ESUCCESS ;
202125}
@@ -214,7 +137,7 @@ os_fstatat(os_file_handle handle, const char *path,
214137 if (ret < 0 )
215138 return convert_errno (errno );
216139
217- convert_stat (handle , & stat_buf , buf );
140+ posix_convert_stat (handle , & stat_buf , buf );
218141
219142 return __WASI_ESUCCESS ;
220143}
0 commit comments