Skip to content

Commit 5036ce7

Browse files
committed
move posix stat converter to own file
We will need these functions in the zephyr_file implementation.
1 parent 3bf08a0 commit 5036ce7

3 files changed

Lines changed: 88 additions & 80 deletions

File tree

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#include "platform_api_extension.h"
2+
3+
// Converts a POSIX timespec to a WASI timestamp.
4+
__wasi_timestamp_t
5+
static posix_convert_timespec(const struct timespec *ts)
6+
{
7+
if (ts->tv_sec < 0)
8+
return 0;
9+
if ((__wasi_timestamp_t)ts->tv_sec >= UINT64_MAX / 1000000000)
10+
return UINT64_MAX;
11+
return (__wasi_timestamp_t)ts->tv_sec * 1000000000
12+
+ (__wasi_timestamp_t)ts->tv_nsec;
13+
}
14+
15+
// Converts a POSIX stat structure to a WASI filestat structure
16+
void
17+
posix_convert_stat(os_file_handle handle, const struct stat *in,
18+
__wasi_filestat_t *out)
19+
{
20+
out->st_dev = in->st_dev;
21+
out->st_ino = in->st_ino;
22+
out->st_nlink = (__wasi_linkcount_t)in->st_nlink;
23+
out->st_size = (__wasi_filesize_t)in->st_size;
24+
#ifdef __APPLE__
25+
out->st_atim = posix_convert_timespec(&in->st_atimespec);
26+
out->st_mtim = posix_convert_timespec(&in->st_mtimespec);
27+
out->st_ctim = posix_convert_timespec(&in->st_ctimespec);
28+
#else
29+
out->st_atim = posix_convert_timespec(&in->st_atim);
30+
out->st_mtim = posix_convert_timespec(&in->st_mtim);
31+
out->st_ctim = posix_convert_timespec(&in->st_ctim);
32+
#endif
33+
34+
// Convert the file type. In the case of sockets there is no way we
35+
// can easily determine the exact socket type.
36+
if (S_ISBLK(in->st_mode)) {
37+
out->st_filetype = __WASI_FILETYPE_BLOCK_DEVICE;
38+
}
39+
else if (S_ISCHR(in->st_mode)) {
40+
out->st_filetype = __WASI_FILETYPE_CHARACTER_DEVICE;
41+
}
42+
else if (S_ISDIR(in->st_mode)) {
43+
out->st_filetype = __WASI_FILETYPE_DIRECTORY;
44+
}
45+
else if (S_ISFIFO(in->st_mode)) {
46+
out->st_filetype = __WASI_FILETYPE_SOCKET_STREAM;
47+
}
48+
else if (S_ISLNK(in->st_mode)) {
49+
out->st_filetype = __WASI_FILETYPE_SYMBOLIC_LINK;
50+
}
51+
else if (S_ISREG(in->st_mode)) {
52+
out->st_filetype = __WASI_FILETYPE_REGULAR_FILE;
53+
}
54+
else if (S_ISSOCK(in->st_mode)) {
55+
int socktype;
56+
socklen_t socktypelen = sizeof(socktype);
57+
58+
if (getsockopt(handle, SOL_SOCKET, SO_TYPE, &socktype, &socktypelen)
59+
< 0) {
60+
out->st_filetype = __WASI_FILETYPE_UNKNOWN;
61+
return;
62+
}
63+
64+
switch (socktype) {
65+
case SOCK_DGRAM:
66+
out->st_filetype = __WASI_FILETYPE_SOCKET_DGRAM;
67+
break;
68+
case SOCK_STREAM:
69+
out->st_filetype = __WASI_FILETYPE_SOCKET_STREAM;
70+
break;
71+
default:
72+
out->st_filetype = __WASI_FILETYPE_UNKNOWN;
73+
return;
74+
}
75+
}
76+
else {
77+
out->st_filetype = __WASI_FILETYPE_UNKNOWN;
78+
}
79+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include "platform_api_extension.h"
2+
3+
// Converts a POSIX stat structure to a WASI filestat structure
4+
void
5+
posix_convert_stat(os_file_handle handle, const struct stat *in,
6+
__wasi_filestat_t *out);

core/shared/platform/common/posix/posix_file.c

Lines changed: 3 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
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
@@ -66,84 +67,6 @@
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-
14770
static void
14871
convert_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

Comments
 (0)