forked from nodejs/uvwasi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.c
200 lines (168 loc) · 5.73 KB
/
app.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "uv.h"
#include "uvwasi.h"
#ifdef __APPLE__
#include <crt_externs.h>
#define environ (*_NSGetEnviron())
#elif !defined(_MSC_VER)
extern char** environ;
#endif
int main(void) {
uvwasi_options_t init_options;
uvwasi_t uvwasi;
uvwasi_t* uvw;
uvwasi_fdstat_t fdstat_buf;
uvwasi_errno_t r;
size_t argc;
size_t argv_buf_size;
size_t envc;
size_t env_buf_size;
size_t i;
assert(UVWASI_VERSION_HEX == 3);
assert(strcmp(UVWASI_VERSION_STRING, "0.0.3") == 0);
assert(strcmp(UVWASI_VERSION_WASI, "snapshot_1") == 0);
uvw = &uvwasi;
init_options.fd_table_size = 3;
init_options.argc = 3;
init_options.argv = calloc(3, sizeof(char*));
init_options.argv[0] = "--foo=bar";
init_options.argv[1] = "-baz";
init_options.argv[2] = "100";
init_options.envp = (char**) environ;
init_options.preopenc = 1;
init_options.preopens = calloc(1, sizeof(uvwasi_preopen_t));
init_options.preopens[0].mapped_path = "/var";
init_options.preopens[0].real_path = ".";
init_options.allocator = NULL;
r = uvwasi_init(uvw, &init_options);
assert(r == 0);
r = uvwasi_args_sizes_get(uvw, &argc, &argv_buf_size);
assert(r == 0);
assert(argc == init_options.argc);
assert(argv_buf_size > 0);
r = uvwasi_environ_sizes_get(uvw, &envc, &env_buf_size);
assert(r == 0);
assert(envc > 0);
assert(env_buf_size > 0);
char* buf = malloc(env_buf_size > argv_buf_size ? env_buf_size : argv_buf_size);
char** args_get_argv = calloc(argc, sizeof(char*));
assert(args_get_argv != NULL);
r = uvwasi_args_get(uvw, args_get_argv, buf);
assert(r == 0);
assert(strcmp(args_get_argv[0], init_options.argv[0]) == 0);
assert(strcmp(args_get_argv[1], init_options.argv[1]) == 0);
assert(strcmp(args_get_argv[2], init_options.argv[2]) == 0);
char** env_get_env = calloc(envc, sizeof(char*));
assert(env_get_env != NULL);
r = uvwasi_environ_get(uvw, env_get_env, buf);
assert(r == 0);
for (i = 0; i < envc; ++i)
assert(strlen(env_get_env[i]) > 0);
free(buf);
uvwasi_fd_t dirfd = 3;
uvwasi_lookupflags_t dirflags = 1;
const char* path = "./foo.txt";
uvwasi_oflags_t o_flags = UVWASI_O_CREAT;
uvwasi_rights_t fs_rights_base = UVWASI_RIGHT_FD_DATASYNC |
UVWASI_RIGHT_FD_ALLOCATE |
UVWASI_RIGHT_FD_FDSTAT_SET_FLAGS |
UVWASI_RIGHT_FD_FILESTAT_GET |
UVWASI_RIGHT_FD_FILESTAT_SET_SIZE |
UVWASI_RIGHT_FD_ADVISE |
UVWASI_RIGHT_FD_READ |
UVWASI_RIGHT_FD_SYNC |
UVWASI_RIGHT_PATH_READLINK |
UVWASI_RIGHT_PATH_UNLINK_FILE;
uvwasi_rights_t fs_rights_inheriting = 1;
uvwasi_fdflags_t fs_flags = 1;
uvwasi_fd_t fd;
r = uvwasi_path_open(uvw,
dirfd,
dirflags,
path,
strlen(path) + 1,
o_flags,
fs_rights_base,
fs_rights_inheriting,
fs_flags,
&fd);
assert(r == 0);
r = uvwasi_fd_sync(uvw, fd);
assert(r == 0);
r = uvwasi_fd_filestat_set_size(uvw, fd, 106);
assert(r == 0);
r = uvwasi_fd_datasync(uvw, fd);
assert(r == 0);
r = uvwasi_fd_advise(uvw, fd, 0, 0, UVWASI_ADVICE_DONTNEED);
assert(r == 0);
uvwasi_filestat_t stats;
r = uvwasi_fd_filestat_get(uvw, fd, &stats);
assert(r == 0);
assert(stats.st_dev > 0);
assert(stats.st_ino > 0);
assert(stats.st_nlink == 1);
assert(stats.st_size == 106);
assert(stats.st_filetype == UVWASI_FILETYPE_REGULAR_FILE);
assert(stats.st_atim > 0);
assert(stats.st_mtim > 0);
assert(stats.st_ctim > 0);
r = uvwasi_fd_allocate(uvw, fd, 9, 141);
assert(r == 0);
r = uvwasi_fd_filestat_get(uvw, fd, &stats);
assert(r == 0);
assert(stats.st_size == 150);
r = uvwasi_fd_fdstat_get(uvw, fd, &fdstat_buf);
assert(r == 0);
assert(fdstat_buf.fs_filetype == UVWASI_FILETYPE_REGULAR_FILE);
assert(fdstat_buf.fs_rights_inheriting == 0);
uvwasi_rights_t old_rights_base = fdstat_buf.fs_rights_base;
uvwasi_fdflags_t old_flags = fdstat_buf.fs_flags;
r = uvwasi_fd_fdstat_set_flags(uvw, fd, UVWASI_FDFLAG_NONBLOCK);
assert(r == 0);
r = uvwasi_fd_fdstat_get(uvw, fd, &fdstat_buf);
assert(r == 0);
assert(fdstat_buf.fs_filetype == UVWASI_FILETYPE_REGULAR_FILE);
assert(fdstat_buf.fs_rights_inheriting == 0);
assert(fdstat_buf.fs_rights_base == old_rights_base);
#ifdef _WIN32
assert(fdstat_buf.fs_flags == 0);
#else
assert(fdstat_buf.fs_flags != old_flags);
#endif /* _WIN32 */
uvwasi_iovec_t* iovs;
size_t iovs_len;
size_t nread;
nread = 0;
iovs_len = 2;
iovs = calloc(iovs_len, sizeof(*iovs));
for (i = 0; i < iovs_len; ++i) {
iovs[i].buf_len = 10;
iovs[i].buf = malloc(1024);
}
r = uvwasi_fd_read(uvw, fd, iovs, iovs_len, &nread);
assert(r == 0);
assert(nread == 20);
free(iovs);
r = uvwasi_fd_fdstat_set_rights(uvw, fd, UVWASI_RIGHT_FD_FILESTAT_GET, 0);
assert(r == 0);
r = uvwasi_fd_close(uvw, fd);
assert(r == 0);
r = uvwasi_path_unlink_file(uvw, dirfd, path, strlen(path) + 1);
assert(r == 0);
char readdir_buf[2048];
size_t bufused;
uvwasi_dircookie_t cookie;
cookie = UVWASI_DIRCOOKIE_START;
r = uvwasi_fd_readdir(uvw,
dirfd,
readdir_buf,
sizeof(readdir_buf),
cookie,
&bufused);
assert(r == 0);
uvwasi_destroy(uvw); /* Clean up memory. */
uvwasi_proc_exit(uvw, 75); /* proc_exit() actually still works. */
return 0;
}