Repository navigation
Expand file tree
/
Copy pathfs.wave
More file actions
301 lines (275 loc) · 10.8 KB
/
Copy pathfs.wave
File metadata and controls
301 lines (275 loc) · 10.8 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// This file is part of the Wave language project.
// Copyright (c) 2024-2026 Wave Foundation
// Copyright (c) 2024-2026 LunaStev and contributors
// SPDX-License-Identifier: Apache-2.0
// Capability-oriented WASI Preview 1 filesystem provider. Relative paths are
// resolved under preopen descriptor 3, which hosts normally map explicitly.
import("std::sys::errors")::{SYS_ERR_INVAL, SYS_ERR_OVERFLOW};
import("std::sys::wasm::io")::{wasi_read, wasi_write};
extern(c, "fd_close") fun wasi_fd_close(fd: u32) -> u16;
extern(c, "fd_sync") fun wasi_fd_sync(fd: u32) -> u16;
extern(c, "fd_seek") fun wasi_fd_seek(
fd: u32, offset: i64, whence: u8, position: ptr<u64>
) -> u16;
extern(c, "fd_filestat_get") fun wasi_fd_filestat_get(
fd: u32, result: ptr<WasiFileStat>
) -> u16;
extern(c, "path_open") fun wasi_path_open(
dir_fd: u32, dir_flags: u32, path: ptr<u8>, path_len: u32,
open_flags: u16, rights: u64, inheriting_rights: u64,
fd_flags: u16, result: ptr<u32>
) -> u16;
extern(c, "path_filestat_get") fun wasi_path_filestat_get(
dir_fd: u32, flags: u32, path: ptr<u8>, path_len: u32,
result: ptr<WasiFileStat>
) -> u16;
extern(c, "path_create_directory") fun wasi_path_create_directory(
dir_fd: u32, path: ptr<u8>, path_len: u32
) -> u16;
extern(c, "path_unlink_file") fun wasi_path_unlink_file(
dir_fd: u32, path: ptr<u8>, path_len: u32
) -> u16;
extern(c, "path_remove_directory") fun wasi_path_remove_directory(
dir_fd: u32, path: ptr<u8>, path_len: u32
) -> u16;
extern(c, "path_rename") fun wasi_path_rename(
old_dir_fd: u32, old_path: ptr<u8>, old_len: u32,
new_dir_fd: u32, new_path: ptr<u8>, new_len: u32
) -> u16;
pub const FS_O_RDONLY: i32 = 0;
pub const FS_O_WRONLY: i32 = 1;
pub const FS_O_RDWR: i32 = 2;
pub const FS_O_CREAT: i32 = 64;
pub const FS_O_EXCL: i32 = 128;
pub const FS_O_TRUNC: i32 = 512;
pub const FS_O_APPEND: i32 = 1024;
pub const FS_O_NONBLOCK: i32 = 2048;
pub const FS_F_OK: i32 = 0;
pub const FS_X_OK: i32 = 1;
pub const FS_W_OK: i32 = 2;
pub const FS_R_OK: i32 = 4;
pub const FS_SEEK_SET: i32 = 0;
pub const FS_SEEK_CUR: i32 = 1;
pub const FS_SEEK_END: i32 = 2;
pub const FS_F_GETFL: i32 = 3;
pub const FS_F_SETFL: i32 = 4;
const WASI_PREOPEN_ROOT: u32 = 3;
const WASI_LOOKUP_SYMLINK_FOLLOW: u32 = 1;
const WASI_RIGHT_READ: u64 = 2;
const WASI_RIGHT_SEEK: u64 = 4;
const WASI_RIGHT_SYNC: u64 = 16;
const WASI_RIGHT_TELL: u64 = 32;
const WASI_RIGHT_WRITE: u64 = 64;
const WASI_RIGHT_FILESTAT_GET: u64 = 2097152;
const WASI_RIGHT_FILESTAT_SET_SIZE: u64 = 4194304;
const WASI_O_CREAT: u16 = 1;
const WASI_O_EXCL: u16 = 4;
const WASI_O_TRUNC: u16 = 8;
const WASI_FD_APPEND: u16 = 1;
const WASI_FD_NONBLOCK: u16 = 4;
const WASI_FILETYPE_DIRECTORY: u8 = 3;
const WASI_FILETYPE_REGULAR: u8 = 4;
// WASI Preview 1 NOSYS; do not substitute the POSIX numeric value.
const SYS_ERR_NOT_IMPLEMENTED: i64 = -52;
struct WasiFileStat {
dev: u64;
ino: u64;
file_type: u8;
nlink: u64;
size: u64;
atime_ns: u64;
mtime_ns: u64;
ctime_ns: u64;
}
pub struct Stat {
mode: i16;
size: i64;
mtime: i64;
}
fun path_len_u32(path: str) -> i64 {
if ((path as ptr<u8>) == null) { return SYS_ERR_INVAL; }
var length: i64 = 0;
// Bound the scan to the WASI ABI width. An unbounded strlen idiom can
// otherwise introduce an unavailable libc symbol during optimization.
while (length <= 4294967295) {
if (path[length] == 0) { return length; }
length += 1;
}
return SYS_ERR_OVERFLOW;
}
fun stat_from_wasi(native: WasiFileStat, result: ptr<Stat>) -> i64 {
if (result == null) { return SYS_ERR_INVAL; }
if (native.size > 9223372036854775807) { return SYS_ERR_OVERFLOW; }
deref result.size = native.size as i64;
deref result.mtime = (native.mtime_ns / 1000000000) as i64;
if (native.file_type == WASI_FILETYPE_DIRECTORY) {
deref result.mode = 16384 as i16;
} else if (native.file_type == WASI_FILETYPE_REGULAR) {
deref result.mode = 32768 as i16;
} else {
deref result.mode = 0;
}
return 0;
}
pub fun open(path: str, flags: i32, mode: i32) -> i64 {
var length: i64 = path_len_u32(path);
if (length < 0) { return length; }
var rights: u64 = WASI_RIGHT_SEEK | WASI_RIGHT_TELL | WASI_RIGHT_FILESTAT_GET;
if ((flags & FS_O_RDWR) != 0) {
rights = rights | WASI_RIGHT_READ | WASI_RIGHT_WRITE | WASI_RIGHT_SYNC | WASI_RIGHT_FILESTAT_SET_SIZE;
} else if ((flags & FS_O_WRONLY) != 0) {
rights = rights | WASI_RIGHT_WRITE | WASI_RIGHT_SYNC | WASI_RIGHT_FILESTAT_SET_SIZE;
} else {
rights = rights | WASI_RIGHT_READ;
}
var open_flags: u16 = 0;
if ((flags & FS_O_CREAT) != 0) { open_flags = open_flags | WASI_O_CREAT; }
if ((flags & FS_O_EXCL) != 0) { open_flags = open_flags | WASI_O_EXCL; }
if ((flags & FS_O_TRUNC) != 0) { open_flags = open_flags | WASI_O_TRUNC; }
var fd_flags: u16 = 0;
if ((flags & FS_O_APPEND) != 0) { fd_flags = fd_flags | WASI_FD_APPEND; }
if ((flags & FS_O_NONBLOCK) != 0) { fd_flags = fd_flags | WASI_FD_NONBLOCK; }
var fd: u32 = 0;
var error: u16 = wasi_path_open(
WASI_PREOPEN_ROOT, 0, path as ptr<u8>, length as u32,
open_flags, rights, 0, fd_flags, &fd
);
if (error != 0) { return -(error as i64); }
return fd as i64;
}
pub fun close(fd: i64) -> i64 {
if (fd < 0 || fd > 4294967295) { return SYS_ERR_INVAL; }
var error: u16 = wasi_fd_close(fd as u32);
if (error != 0) { return -(error as i64); }
return 0;
}
pub fun read(fd: i64, buf: ptr<u8>, len: i64) -> i64 { return wasi_read(fd, buf, len); }
pub fun write(fd: i64, buf: ptr<u8>, len: i64) -> i64 { return wasi_write(fd, buf, len); }
pub fun fsync(fd: i64) -> i64 {
if (fd < 0 || fd > 4294967295) { return SYS_ERR_INVAL; }
var error: u16 = wasi_fd_sync(fd as u32);
if (error != 0) { return -(error as i64); }
return 0;
}
pub fun lseek(fd: i64, offset: i64, whence: i32) -> i64 {
if (fd < 0 || fd > 4294967295 || whence < 0 || whence > 2) { return SYS_ERR_INVAL; }
var position: u64 = 0;
var error: u16 = wasi_fd_seek(fd as u32, offset, whence as u8, &position);
if (error != 0) { return -(error as i64); }
if (position > 9223372036854775807) { return SYS_ERR_OVERFLOW; }
return position as i64;
}
pub fun access(path: str, mode: i32) -> i64 {
// WASI errno values: invalid mode and unsupported POSIX permission checks.
if ((mode & ~(FS_R_OK | FS_W_OK | FS_X_OK)) != 0) { return -28; }
if ((mode & FS_X_OK) != 0) { return -58; }
var native: WasiFileStat;
var length: i64 = path_len_u32(path);
if (length < 0) { return length; }
var error: u16 = wasi_path_filestat_get(
WASI_PREOPEN_ROOT, WASI_LOOKUP_SYMLINK_FOLLOW, path as ptr<u8>, length as u32, &native
);
if (error != 0) { return -(error as i64); }
if (mode == FS_F_OK) { return 0; }
if (native.file_type != WASI_FILETYPE_REGULAR) { return -58; }
var rights: u64 = 0;
if ((mode & FS_R_OK) != 0) { rights = rights | WASI_RIGHT_READ; }
if ((mode & FS_W_OK) != 0) { rights = rights | WASI_RIGHT_WRITE; }
var fd: u32 = 0;
error = wasi_path_open(
WASI_PREOPEN_ROOT, WASI_LOOKUP_SYMLINK_FOLLOW, path as ptr<u8>, length as u32,
0, rights, 0, 0, &fd
);
if (error != 0) { return -(error as i64); }
error = wasi_fd_close(fd);
if (error != 0) { return -(error as i64); }
return 0;
}
pub fun stat(path: str, result: ptr<Stat>) -> i64 {
if (result == null) { return SYS_ERR_INVAL; }
var native: WasiFileStat;
var length: i64 = path_len_u32(path);
if (length < 0) { return length; }
var error: u16 = wasi_path_filestat_get(
WASI_PREOPEN_ROOT, WASI_LOOKUP_SYMLINK_FOLLOW, path as ptr<u8>, length as u32, &native
);
if (error != 0) { return -(error as i64); }
return stat_from_wasi(native, result);
}
pub fun fstat(fd: i64, result: ptr<Stat>) -> i64 {
if (fd < 0 || fd > 4294967295 || result == null) { return SYS_ERR_INVAL; }
var native: WasiFileStat;
var error: u16 = wasi_fd_filestat_get(fd as u32, &native);
if (error != 0) { return -(error as i64); }
return stat_from_wasi(native, result);
}
pub fun mkdir(path: str, mode: i32) -> i64 {
var length: i64 = path_len_u32(path);
if (length < 0) { return length; }
var error: u16 = wasi_path_create_directory(
WASI_PREOPEN_ROOT, path as ptr<u8>, length as u32
);
if (error != 0) { return -(error as i64); }
return 0;
}
pub fun unlink(path: str) -> i64 {
var length: i64 = path_len_u32(path);
if (length < 0) { return length; }
var error: u16 = wasi_path_unlink_file(WASI_PREOPEN_ROOT, path as ptr<u8>, length as u32);
if (error != 0) { return -(error as i64); }
return 0;
}
pub fun rmdir(path: str) -> i64 {
var length: i64 = path_len_u32(path);
if (length < 0) { return length; }
var error: u16 = wasi_path_remove_directory(WASI_PREOPEN_ROOT, path as ptr<u8>, length as u32);
if (error != 0) { return -(error as i64); }
return 0;
}
pub fun rename_path(old_path: str, new_path: str) -> i64 {
var old_len: i64 = path_len_u32(old_path);
var new_len: i64 = path_len_u32(new_path);
if (old_len < 0) { return old_len; }
if (new_len < 0) { return new_len; }
var error: u16 = wasi_path_rename(
WASI_PREOPEN_ROOT, old_path as ptr<u8>, old_len as u32,
WASI_PREOPEN_ROOT, new_path as ptr<u8>, new_len as u32
);
if (error != 0) { return -(error as i64); }
return 0;
}
pub fun getcwd(buf: ptr<u8>, size: i64) -> i64 {
if (buf == null || size < 2) { return SYS_ERR_INVAL; }
deref buf[0] = 46;
deref buf[1] = 0;
return 1;
}
pub fun chdir(path: str) -> i64 { return SYS_ERR_NOT_IMPLEMENTED; }
pub fun dup(fd: i64) -> i64 { return SYS_ERR_NOT_IMPLEMENTED; }
pub fun dup2(oldfd: i64, newfd: i64) -> i64 { return SYS_ERR_NOT_IMPLEMENTED; }
pub fun pipe(fds: ptr<i32>) -> i64 { return SYS_ERR_NOT_IMPLEMENTED; }
pub fun fcntl(fd: i64, cmd: i32, arg: i64) -> i64 { return SYS_ERR_NOT_IMPLEMENTED; }
extern(c, "fd_filestat_set_size") fun wasi_fd_set_size(fd: u32, size: u64) -> u16;
pub fun ftruncate(fd: i64, length: i64) -> i64 {
if (fd < 0 || fd > 4294967295 || length < 0) {
return SYS_ERR_INVAL;
}
var error: u16 = wasi_fd_set_size(fd as u32, length as u64);
return -(error as i64);
}
pub fun same_file(first_fd: i64, second_fd: i64) -> i64 {
if (first_fd < 0 || first_fd > 4294967295 || second_fd < 0 || second_fd > 4294967295) {
return SYS_ERR_INVAL;
}
var first: WasiFileStat;
var second: WasiFileStat;
var error: u16 = wasi_fd_filestat_get(first_fd as u32, &first);
if (error != 0) {
return -(error as i64);
}
error = wasi_fd_filestat_get(second_fd as u32, &second);
if (error != 0) {
return -(error as i64);
}
return (first.dev == second.dev && first.ino == second.ino) as i64;
}