]>
Commit | Line | Data |
---|---|---|
60c5eb7d XL |
1 | // This file is automatically generated, DO NOT EDIT |
2 | // | |
3 | // To regenerate this file run the `crates/generate-raw` command | |
4 | ||
5 | use core::mem::MaybeUninit; | |
6 | ||
7 | pub use crate::error::Error; | |
8 | pub type Result<T, E = Error> = core::result::Result<T, E>; | |
9 | pub type Size = usize; | |
10 | pub type Filesize = u64; | |
11 | pub type Timestamp = u64; | |
12 | pub type Clockid = u32; | |
13 | /// The clock measuring real time. Time value zero corresponds with | |
14 | /// 1970-01-01T00:00:00Z. | |
15 | pub const CLOCKID_REALTIME: Clockid = 0; | |
16 | /// The store-wide monotonic clock, which is defined as a clock measuring | |
17 | /// real time, whose value cannot be adjusted and which cannot have negative | |
18 | /// clock jumps. The epoch of this clock is undefined. The absolute time | |
19 | /// value of this clock therefore has no meaning. | |
20 | pub const CLOCKID_MONOTONIC: Clockid = 1; | |
21 | /// The CPU-time clock associated with the current process. | |
22 | pub const CLOCKID_PROCESS_CPUTIME_ID: Clockid = 2; | |
23 | /// The CPU-time clock associated with the current thread. | |
24 | pub const CLOCKID_THREAD_CPUTIME_ID: Clockid = 3; | |
25 | pub type Errno = u16; | |
26 | /// No error occurred. System call completed successfully. | |
27 | pub const ERRNO_SUCCESS: Errno = 0; | |
28 | /// Argument list too long. | |
29 | pub const ERRNO_2BIG: Errno = 1; | |
30 | /// Permission denied. | |
31 | pub const ERRNO_ACCES: Errno = 2; | |
32 | /// Address in use. | |
33 | pub const ERRNO_ADDRINUSE: Errno = 3; | |
34 | /// Address not available. | |
35 | pub const ERRNO_ADDRNOTAVAIL: Errno = 4; | |
36 | /// Address family not supported. | |
37 | pub const ERRNO_AFNOSUPPORT: Errno = 5; | |
38 | /// Resource unavailable, or operation would block. | |
39 | pub const ERRNO_AGAIN: Errno = 6; | |
40 | /// Connection already in progress. | |
41 | pub const ERRNO_ALREADY: Errno = 7; | |
42 | /// Bad file descriptor. | |
43 | pub const ERRNO_BADF: Errno = 8; | |
44 | /// Bad message. | |
45 | pub const ERRNO_BADMSG: Errno = 9; | |
46 | /// Device or resource busy. | |
47 | pub const ERRNO_BUSY: Errno = 10; | |
48 | /// Operation canceled. | |
49 | pub const ERRNO_CANCELED: Errno = 11; | |
50 | /// No child processes. | |
51 | pub const ERRNO_CHILD: Errno = 12; | |
52 | /// Connection aborted. | |
53 | pub const ERRNO_CONNABORTED: Errno = 13; | |
54 | /// Connection refused. | |
55 | pub const ERRNO_CONNREFUSED: Errno = 14; | |
56 | /// Connection reset. | |
57 | pub const ERRNO_CONNRESET: Errno = 15; | |
58 | /// Resource deadlock would occur. | |
59 | pub const ERRNO_DEADLK: Errno = 16; | |
60 | /// Destination address required. | |
61 | pub const ERRNO_DESTADDRREQ: Errno = 17; | |
62 | /// Mathematics argument out of domain of function. | |
63 | pub const ERRNO_DOM: Errno = 18; | |
64 | /// Reserved. | |
65 | pub const ERRNO_DQUOT: Errno = 19; | |
66 | /// File exists. | |
67 | pub const ERRNO_EXIST: Errno = 20; | |
68 | /// Bad address. | |
69 | pub const ERRNO_FAULT: Errno = 21; | |
70 | /// File too large. | |
71 | pub const ERRNO_FBIG: Errno = 22; | |
72 | /// Host is unreachable. | |
73 | pub const ERRNO_HOSTUNREACH: Errno = 23; | |
74 | /// Identifier removed. | |
75 | pub const ERRNO_IDRM: Errno = 24; | |
76 | /// Illegal byte sequence. | |
77 | pub const ERRNO_ILSEQ: Errno = 25; | |
78 | /// Operation in progress. | |
79 | pub const ERRNO_INPROGRESS: Errno = 26; | |
80 | /// Interrupted function. | |
81 | pub const ERRNO_INTR: Errno = 27; | |
82 | /// Invalid argument. | |
83 | pub const ERRNO_INVAL: Errno = 28; | |
84 | /// I/O error. | |
85 | pub const ERRNO_IO: Errno = 29; | |
86 | /// Socket is connected. | |
87 | pub const ERRNO_ISCONN: Errno = 30; | |
88 | /// Is a directory. | |
89 | pub const ERRNO_ISDIR: Errno = 31; | |
90 | /// Too many levels of symbolic links. | |
91 | pub const ERRNO_LOOP: Errno = 32; | |
92 | /// File descriptor value too large. | |
93 | pub const ERRNO_MFILE: Errno = 33; | |
94 | /// Too many links. | |
95 | pub const ERRNO_MLINK: Errno = 34; | |
96 | /// Message too large. | |
97 | pub const ERRNO_MSGSIZE: Errno = 35; | |
98 | /// Reserved. | |
99 | pub const ERRNO_MULTIHOP: Errno = 36; | |
100 | /// Filename too long. | |
101 | pub const ERRNO_NAMETOOLONG: Errno = 37; | |
102 | /// Network is down. | |
103 | pub const ERRNO_NETDOWN: Errno = 38; | |
104 | /// Connection aborted by network. | |
105 | pub const ERRNO_NETRESET: Errno = 39; | |
106 | /// Network unreachable. | |
107 | pub const ERRNO_NETUNREACH: Errno = 40; | |
108 | /// Too many files open in system. | |
109 | pub const ERRNO_NFILE: Errno = 41; | |
110 | /// No buffer space available. | |
111 | pub const ERRNO_NOBUFS: Errno = 42; | |
112 | /// No such device. | |
113 | pub const ERRNO_NODEV: Errno = 43; | |
114 | /// No such file or directory. | |
115 | pub const ERRNO_NOENT: Errno = 44; | |
116 | /// Executable file format error. | |
117 | pub const ERRNO_NOEXEC: Errno = 45; | |
118 | /// No locks available. | |
119 | pub const ERRNO_NOLCK: Errno = 46; | |
120 | /// Reserved. | |
121 | pub const ERRNO_NOLINK: Errno = 47; | |
122 | /// Not enough space. | |
123 | pub const ERRNO_NOMEM: Errno = 48; | |
124 | /// No message of the desired type. | |
125 | pub const ERRNO_NOMSG: Errno = 49; | |
126 | /// Protocol not available. | |
127 | pub const ERRNO_NOPROTOOPT: Errno = 50; | |
128 | /// No space left on device. | |
129 | pub const ERRNO_NOSPC: Errno = 51; | |
130 | /// Function not supported. | |
131 | pub const ERRNO_NOSYS: Errno = 52; | |
132 | /// The socket is not connected. | |
133 | pub const ERRNO_NOTCONN: Errno = 53; | |
134 | /// Not a directory or a symbolic link to a directory. | |
135 | pub const ERRNO_NOTDIR: Errno = 54; | |
136 | /// Directory not empty. | |
137 | pub const ERRNO_NOTEMPTY: Errno = 55; | |
138 | /// State not recoverable. | |
139 | pub const ERRNO_NOTRECOVERABLE: Errno = 56; | |
140 | /// Not a socket. | |
141 | pub const ERRNO_NOTSOCK: Errno = 57; | |
142 | /// Not supported, or operation not supported on socket. | |
143 | pub const ERRNO_NOTSUP: Errno = 58; | |
144 | /// Inappropriate I/O control operation. | |
145 | pub const ERRNO_NOTTY: Errno = 59; | |
146 | /// No such device or address. | |
147 | pub const ERRNO_NXIO: Errno = 60; | |
148 | /// Value too large to be stored in data type. | |
149 | pub const ERRNO_OVERFLOW: Errno = 61; | |
150 | /// Previous owner died. | |
151 | pub const ERRNO_OWNERDEAD: Errno = 62; | |
152 | /// Operation not permitted. | |
153 | pub const ERRNO_PERM: Errno = 63; | |
154 | /// Broken pipe. | |
155 | pub const ERRNO_PIPE: Errno = 64; | |
156 | /// Protocol error. | |
157 | pub const ERRNO_PROTO: Errno = 65; | |
158 | /// Protocol not supported. | |
159 | pub const ERRNO_PROTONOSUPPORT: Errno = 66; | |
160 | /// Protocol wrong type for socket. | |
161 | pub const ERRNO_PROTOTYPE: Errno = 67; | |
162 | /// Result too large. | |
163 | pub const ERRNO_RANGE: Errno = 68; | |
164 | /// Read-only file system. | |
165 | pub const ERRNO_ROFS: Errno = 69; | |
166 | /// Invalid seek. | |
167 | pub const ERRNO_SPIPE: Errno = 70; | |
168 | /// No such process. | |
169 | pub const ERRNO_SRCH: Errno = 71; | |
170 | /// Reserved. | |
171 | pub const ERRNO_STALE: Errno = 72; | |
172 | /// Connection timed out. | |
173 | pub const ERRNO_TIMEDOUT: Errno = 73; | |
174 | /// Text file busy. | |
175 | pub const ERRNO_TXTBSY: Errno = 74; | |
176 | /// Cross-device link. | |
177 | pub const ERRNO_XDEV: Errno = 75; | |
178 | /// Extension: Capabilities insufficient. | |
179 | pub const ERRNO_NOTCAPABLE: Errno = 76; | |
180 | pub(crate) fn strerror(code: u16) -> &'static str { | |
181 | match code { | |
182 | ERRNO_SUCCESS => "No error occurred. System call completed successfully.", | |
183 | ERRNO_2BIG => "Argument list too long.", | |
184 | ERRNO_ACCES => "Permission denied.", | |
185 | ERRNO_ADDRINUSE => "Address in use.", | |
186 | ERRNO_ADDRNOTAVAIL => "Address not available.", | |
187 | ERRNO_AFNOSUPPORT => "Address family not supported.", | |
188 | ERRNO_AGAIN => "Resource unavailable, or operation would block.", | |
189 | ERRNO_ALREADY => "Connection already in progress.", | |
190 | ERRNO_BADF => "Bad file descriptor.", | |
191 | ERRNO_BADMSG => "Bad message.", | |
192 | ERRNO_BUSY => "Device or resource busy.", | |
193 | ERRNO_CANCELED => "Operation canceled.", | |
194 | ERRNO_CHILD => "No child processes.", | |
195 | ERRNO_CONNABORTED => "Connection aborted.", | |
196 | ERRNO_CONNREFUSED => "Connection refused.", | |
197 | ERRNO_CONNRESET => "Connection reset.", | |
198 | ERRNO_DEADLK => "Resource deadlock would occur.", | |
199 | ERRNO_DESTADDRREQ => "Destination address required.", | |
200 | ERRNO_DOM => "Mathematics argument out of domain of function.", | |
201 | ERRNO_DQUOT => "Reserved.", | |
202 | ERRNO_EXIST => "File exists.", | |
203 | ERRNO_FAULT => "Bad address.", | |
204 | ERRNO_FBIG => "File too large.", | |
205 | ERRNO_HOSTUNREACH => "Host is unreachable.", | |
206 | ERRNO_IDRM => "Identifier removed.", | |
207 | ERRNO_ILSEQ => "Illegal byte sequence.", | |
208 | ERRNO_INPROGRESS => "Operation in progress.", | |
209 | ERRNO_INTR => "Interrupted function.", | |
210 | ERRNO_INVAL => "Invalid argument.", | |
211 | ERRNO_IO => "I/O error.", | |
212 | ERRNO_ISCONN => "Socket is connected.", | |
213 | ERRNO_ISDIR => "Is a directory.", | |
214 | ERRNO_LOOP => "Too many levels of symbolic links.", | |
215 | ERRNO_MFILE => "File descriptor value too large.", | |
216 | ERRNO_MLINK => "Too many links.", | |
217 | ERRNO_MSGSIZE => "Message too large.", | |
218 | ERRNO_MULTIHOP => "Reserved.", | |
219 | ERRNO_NAMETOOLONG => "Filename too long.", | |
220 | ERRNO_NETDOWN => "Network is down.", | |
221 | ERRNO_NETRESET => "Connection aborted by network.", | |
222 | ERRNO_NETUNREACH => "Network unreachable.", | |
223 | ERRNO_NFILE => "Too many files open in system.", | |
224 | ERRNO_NOBUFS => "No buffer space available.", | |
225 | ERRNO_NODEV => "No such device.", | |
226 | ERRNO_NOENT => "No such file or directory.", | |
227 | ERRNO_NOEXEC => "Executable file format error.", | |
228 | ERRNO_NOLCK => "No locks available.", | |
229 | ERRNO_NOLINK => "Reserved.", | |
230 | ERRNO_NOMEM => "Not enough space.", | |
231 | ERRNO_NOMSG => "No message of the desired type.", | |
232 | ERRNO_NOPROTOOPT => "Protocol not available.", | |
233 | ERRNO_NOSPC => "No space left on device.", | |
234 | ERRNO_NOSYS => "Function not supported.", | |
235 | ERRNO_NOTCONN => "The socket is not connected.", | |
236 | ERRNO_NOTDIR => "Not a directory or a symbolic link to a directory.", | |
237 | ERRNO_NOTEMPTY => "Directory not empty.", | |
238 | ERRNO_NOTRECOVERABLE => "State not recoverable.", | |
239 | ERRNO_NOTSOCK => "Not a socket.", | |
240 | ERRNO_NOTSUP => "Not supported, or operation not supported on socket.", | |
241 | ERRNO_NOTTY => "Inappropriate I/O control operation.", | |
242 | ERRNO_NXIO => "No such device or address.", | |
243 | ERRNO_OVERFLOW => "Value too large to be stored in data type.", | |
244 | ERRNO_OWNERDEAD => "Previous owner died.", | |
245 | ERRNO_PERM => "Operation not permitted.", | |
246 | ERRNO_PIPE => "Broken pipe.", | |
247 | ERRNO_PROTO => "Protocol error.", | |
248 | ERRNO_PROTONOSUPPORT => "Protocol not supported.", | |
249 | ERRNO_PROTOTYPE => "Protocol wrong type for socket.", | |
250 | ERRNO_RANGE => "Result too large.", | |
251 | ERRNO_ROFS => "Read-only file system.", | |
252 | ERRNO_SPIPE => "Invalid seek.", | |
253 | ERRNO_SRCH => "No such process.", | |
254 | ERRNO_STALE => "Reserved.", | |
255 | ERRNO_TIMEDOUT => "Connection timed out.", | |
256 | ERRNO_TXTBSY => "Text file busy.", | |
257 | ERRNO_XDEV => "Cross-device link.", | |
258 | ERRNO_NOTCAPABLE => "Extension: Capabilities insufficient.", | |
259 | _ => "Unknown error.", | |
260 | } | |
261 | } | |
262 | pub type Rights = u64; | |
263 | /// The right to invoke `fd_datasync`. | |
264 | /// If `path_open` is set, includes the right to invoke | |
6a06907d | 265 | /// `path_open` with `fdflag::dsync`. |
60c5eb7d XL |
266 | pub const RIGHTS_FD_DATASYNC: Rights = 0x1; |
267 | /// The right to invoke `fd_read` and `sock_recv`. | |
268 | /// If `rights::fd_seek` is set, includes the right to invoke `fd_pread`. | |
269 | pub const RIGHTS_FD_READ: Rights = 0x2; | |
270 | /// The right to invoke `fd_seek`. This flag implies `rights::fd_tell`. | |
271 | pub const RIGHTS_FD_SEEK: Rights = 0x4; | |
272 | /// The right to invoke `fd_fdstat_set_flags`. | |
273 | pub const RIGHTS_FD_FDSTAT_SET_FLAGS: Rights = 0x8; | |
274 | /// The right to invoke `fd_sync`. | |
275 | /// If `path_open` is set, includes the right to invoke | |
6a06907d | 276 | /// `path_open` with `fdflag::rsync` and `fdflag::dsync`. |
60c5eb7d XL |
277 | pub const RIGHTS_FD_SYNC: Rights = 0x10; |
278 | /// The right to invoke `fd_seek` in such a way that the file offset | |
6a06907d | 279 | /// remains unaltered (i.e., `WHENCE_CUR` with offset zero), or to |
60c5eb7d XL |
280 | /// invoke `fd_tell`. |
281 | pub const RIGHTS_FD_TELL: Rights = 0x20; | |
282 | /// The right to invoke `fd_write` and `sock_send`. | |
283 | /// If `rights::fd_seek` is set, includes the right to invoke `fd_pwrite`. | |
284 | pub const RIGHTS_FD_WRITE: Rights = 0x40; | |
285 | /// The right to invoke `fd_advise`. | |
286 | pub const RIGHTS_FD_ADVISE: Rights = 0x80; | |
287 | /// The right to invoke `fd_allocate`. | |
288 | pub const RIGHTS_FD_ALLOCATE: Rights = 0x100; | |
289 | /// The right to invoke `path_create_directory`. | |
290 | pub const RIGHTS_PATH_CREATE_DIRECTORY: Rights = 0x200; | |
291 | /// If `path_open` is set, the right to invoke `path_open` with `oflags::creat`. | |
292 | pub const RIGHTS_PATH_CREATE_FILE: Rights = 0x400; | |
293 | /// The right to invoke `path_link` with the file descriptor as the | |
294 | /// source directory. | |
295 | pub const RIGHTS_PATH_LINK_SOURCE: Rights = 0x800; | |
296 | /// The right to invoke `path_link` with the file descriptor as the | |
297 | /// target directory. | |
298 | pub const RIGHTS_PATH_LINK_TARGET: Rights = 0x1000; | |
299 | /// The right to invoke `path_open`. | |
300 | pub const RIGHTS_PATH_OPEN: Rights = 0x2000; | |
301 | /// The right to invoke `fd_readdir`. | |
302 | pub const RIGHTS_FD_READDIR: Rights = 0x4000; | |
303 | /// The right to invoke `path_readlink`. | |
304 | pub const RIGHTS_PATH_READLINK: Rights = 0x8000; | |
305 | /// The right to invoke `path_rename` with the file descriptor as the source directory. | |
306 | pub const RIGHTS_PATH_RENAME_SOURCE: Rights = 0x10000; | |
307 | /// The right to invoke `path_rename` with the file descriptor as the target directory. | |
308 | pub const RIGHTS_PATH_RENAME_TARGET: Rights = 0x20000; | |
309 | /// The right to invoke `path_filestat_get`. | |
310 | pub const RIGHTS_PATH_FILESTAT_GET: Rights = 0x40000; | |
311 | /// The right to change a file's size (there is no `path_filestat_set_size`). | |
312 | /// If `path_open` is set, includes the right to invoke `path_open` with `oflags::trunc`. | |
313 | pub const RIGHTS_PATH_FILESTAT_SET_SIZE: Rights = 0x80000; | |
314 | /// The right to invoke `path_filestat_set_times`. | |
315 | pub const RIGHTS_PATH_FILESTAT_SET_TIMES: Rights = 0x100000; | |
316 | /// The right to invoke `fd_filestat_get`. | |
317 | pub const RIGHTS_FD_FILESTAT_GET: Rights = 0x200000; | |
318 | /// The right to invoke `fd_filestat_set_size`. | |
319 | pub const RIGHTS_FD_FILESTAT_SET_SIZE: Rights = 0x400000; | |
320 | /// The right to invoke `fd_filestat_set_times`. | |
321 | pub const RIGHTS_FD_FILESTAT_SET_TIMES: Rights = 0x800000; | |
322 | /// The right to invoke `path_symlink`. | |
323 | pub const RIGHTS_PATH_SYMLINK: Rights = 0x1000000; | |
324 | /// The right to invoke `path_remove_directory`. | |
325 | pub const RIGHTS_PATH_REMOVE_DIRECTORY: Rights = 0x2000000; | |
326 | /// The right to invoke `path_unlink_file`. | |
327 | pub const RIGHTS_PATH_UNLINK_FILE: Rights = 0x4000000; | |
328 | /// If `rights::fd_read` is set, includes the right to invoke `poll_oneoff` to subscribe to `eventtype::fd_read`. | |
329 | /// If `rights::fd_write` is set, includes the right to invoke `poll_oneoff` to subscribe to `eventtype::fd_write`. | |
330 | pub const RIGHTS_POLL_FD_READWRITE: Rights = 0x8000000; | |
331 | /// The right to invoke `sock_shutdown`. | |
332 | pub const RIGHTS_SOCK_SHUTDOWN: Rights = 0x10000000; | |
333 | pub type Fd = u32; | |
334 | #[repr(C)] | |
6a06907d | 335 | #[derive(Copy, Clone)] |
60c5eb7d XL |
336 | pub struct Iovec { |
337 | /// The address of the buffer to be filled. | |
338 | pub buf: *mut u8, | |
339 | /// The length of the buffer to be filled. | |
340 | pub buf_len: Size, | |
341 | } | |
342 | #[repr(C)] | |
6a06907d | 343 | #[derive(Copy, Clone)] |
60c5eb7d XL |
344 | pub struct Ciovec { |
345 | /// The address of the buffer to be written. | |
346 | pub buf: *const u8, | |
347 | /// The length of the buffer to be written. | |
348 | pub buf_len: Size, | |
349 | } | |
350 | pub type IovecArray<'a> = &'a [Iovec]; | |
351 | pub type CiovecArray<'a> = &'a [Ciovec]; | |
352 | pub type Filedelta = i64; | |
353 | pub type Whence = u8; | |
354 | /// Seek relative to start-of-file. | |
355 | pub const WHENCE_SET: Whence = 0; | |
356 | /// Seek relative to current position. | |
357 | pub const WHENCE_CUR: Whence = 1; | |
358 | /// Seek relative to end-of-file. | |
359 | pub const WHENCE_END: Whence = 2; | |
360 | pub type Dircookie = u64; | |
361 | pub type Dirnamlen = u32; | |
362 | pub type Inode = u64; | |
363 | pub type Filetype = u8; | |
364 | /// The type of the file descriptor or file is unknown or is different from any of the other types specified. | |
365 | pub const FILETYPE_UNKNOWN: Filetype = 0; | |
366 | /// The file descriptor or file refers to a block device inode. | |
367 | pub const FILETYPE_BLOCK_DEVICE: Filetype = 1; | |
368 | /// The file descriptor or file refers to a character device inode. | |
369 | pub const FILETYPE_CHARACTER_DEVICE: Filetype = 2; | |
370 | /// The file descriptor or file refers to a directory inode. | |
371 | pub const FILETYPE_DIRECTORY: Filetype = 3; | |
372 | /// The file descriptor or file refers to a regular file inode. | |
373 | pub const FILETYPE_REGULAR_FILE: Filetype = 4; | |
374 | /// The file descriptor or file refers to a datagram socket. | |
375 | pub const FILETYPE_SOCKET_DGRAM: Filetype = 5; | |
376 | /// The file descriptor or file refers to a byte-stream socket. | |
377 | pub const FILETYPE_SOCKET_STREAM: Filetype = 6; | |
378 | /// The file refers to a symbolic link inode. | |
379 | pub const FILETYPE_SYMBOLIC_LINK: Filetype = 7; | |
380 | #[repr(C)] | |
6a06907d | 381 | #[derive(Copy, Clone)] |
60c5eb7d XL |
382 | pub struct Dirent { |
383 | /// The offset of the next directory entry stored in this directory. | |
384 | pub d_next: Dircookie, | |
385 | /// The serial number of the file referred to by this directory entry. | |
386 | pub d_ino: Inode, | |
387 | /// The length of the name of the directory entry. | |
388 | pub d_namlen: Dirnamlen, | |
389 | /// The type of the file referred to by this directory entry. | |
390 | pub d_type: Filetype, | |
391 | } | |
392 | pub type Advice = u8; | |
393 | /// The application has no advice to give on its behavior with respect to the specified data. | |
394 | pub const ADVICE_NORMAL: Advice = 0; | |
395 | /// The application expects to access the specified data sequentially from lower offsets to higher offsets. | |
396 | pub const ADVICE_SEQUENTIAL: Advice = 1; | |
397 | /// The application expects to access the specified data in a random order. | |
398 | pub const ADVICE_RANDOM: Advice = 2; | |
399 | /// The application expects to access the specified data in the near future. | |
400 | pub const ADVICE_WILLNEED: Advice = 3; | |
401 | /// The application expects that it will not access the specified data in the near future. | |
402 | pub const ADVICE_DONTNEED: Advice = 4; | |
403 | /// The application expects to access the specified data once and then not reuse it thereafter. | |
404 | pub const ADVICE_NOREUSE: Advice = 5; | |
405 | pub type Fdflags = u16; | |
406 | /// Append mode: Data written to the file is always appended to the file's end. | |
407 | pub const FDFLAGS_APPEND: Fdflags = 0x1; | |
408 | /// Write according to synchronized I/O data integrity completion. Only the data stored in the file is synchronized. | |
409 | pub const FDFLAGS_DSYNC: Fdflags = 0x2; | |
410 | /// Non-blocking mode. | |
411 | pub const FDFLAGS_NONBLOCK: Fdflags = 0x4; | |
412 | /// Synchronized read I/O operations. | |
413 | pub const FDFLAGS_RSYNC: Fdflags = 0x8; | |
414 | /// Write according to synchronized I/O file integrity completion. In | |
415 | /// addition to synchronizing the data stored in the file, the implementation | |
416 | /// may also synchronously update the file's metadata. | |
417 | pub const FDFLAGS_SYNC: Fdflags = 0x10; | |
418 | #[repr(C)] | |
6a06907d | 419 | #[derive(Copy, Clone)] |
60c5eb7d XL |
420 | pub struct Fdstat { |
421 | /// File type. | |
422 | pub fs_filetype: Filetype, | |
423 | /// File descriptor flags. | |
424 | pub fs_flags: Fdflags, | |
425 | /// Rights that apply to this file descriptor. | |
426 | pub fs_rights_base: Rights, | |
427 | /// Maximum set of rights that may be installed on new file descriptors that | |
428 | /// are created through this file descriptor, e.g., through `path_open`. | |
429 | pub fs_rights_inheriting: Rights, | |
430 | } | |
431 | pub type Device = u64; | |
432 | pub type Fstflags = u16; | |
6a06907d | 433 | /// Adjust the last data access timestamp to the value stored in `filestat::st_atim`. |
60c5eb7d | 434 | pub const FSTFLAGS_ATIM: Fstflags = 0x1; |
6a06907d | 435 | /// Adjust the last data access timestamp to the time of clock `clock::realtime`. |
60c5eb7d | 436 | pub const FSTFLAGS_ATIM_NOW: Fstflags = 0x2; |
6a06907d | 437 | /// Adjust the last data modification timestamp to the value stored in `filestat::st_mtim`. |
60c5eb7d | 438 | pub const FSTFLAGS_MTIM: Fstflags = 0x4; |
6a06907d | 439 | /// Adjust the last data modification timestamp to the time of clock `clock::realtime`. |
60c5eb7d XL |
440 | pub const FSTFLAGS_MTIM_NOW: Fstflags = 0x8; |
441 | pub type Lookupflags = u32; | |
442 | /// As long as the resolved path corresponds to a symbolic link, it is expanded. | |
443 | pub const LOOKUPFLAGS_SYMLINK_FOLLOW: Lookupflags = 0x1; | |
444 | pub type Oflags = u16; | |
445 | /// Create file if it does not exist. | |
446 | pub const OFLAGS_CREAT: Oflags = 0x1; | |
447 | /// Fail if not a directory. | |
448 | pub const OFLAGS_DIRECTORY: Oflags = 0x2; | |
449 | /// Fail if file already exists. | |
450 | pub const OFLAGS_EXCL: Oflags = 0x4; | |
451 | /// Truncate file to size 0. | |
452 | pub const OFLAGS_TRUNC: Oflags = 0x8; | |
453 | pub type Linkcount = u64; | |
454 | #[repr(C)] | |
6a06907d | 455 | #[derive(Copy, Clone)] |
60c5eb7d XL |
456 | pub struct Filestat { |
457 | /// Device ID of device containing the file. | |
458 | pub dev: Device, | |
459 | /// File serial number. | |
460 | pub ino: Inode, | |
461 | /// File type. | |
462 | pub filetype: Filetype, | |
463 | /// Number of hard links to the file. | |
464 | pub nlink: Linkcount, | |
465 | /// For regular files, the file size in bytes. For symbolic links, the length in bytes of the pathname contained in the symbolic link. | |
466 | pub size: Filesize, | |
467 | /// Last data access timestamp. | |
468 | pub atim: Timestamp, | |
469 | /// Last data modification timestamp. | |
470 | pub mtim: Timestamp, | |
471 | /// Last file status change timestamp. | |
472 | pub ctim: Timestamp, | |
473 | } | |
474 | pub type Userdata = u64; | |
475 | pub type Eventtype = u8; | |
6a06907d XL |
476 | /// The time value of clock `subscription::u.clock.clock_id` has |
477 | /// reached timestamp `subscription::u.clock.timeout`. | |
60c5eb7d | 478 | pub const EVENTTYPE_CLOCK: Eventtype = 0; |
6a06907d | 479 | /// File descriptor `subscription::u.fd_readwrite.fd` has data |
60c5eb7d XL |
480 | /// available for reading. This event always triggers for regular files. |
481 | pub const EVENTTYPE_FD_READ: Eventtype = 1; | |
6a06907d | 482 | /// File descriptor `subscription::u.fd_readwrite.fd` has capacity |
60c5eb7d XL |
483 | /// available for writing. This event always triggers for regular files. |
484 | pub const EVENTTYPE_FD_WRITE: Eventtype = 2; | |
485 | pub type Eventrwflags = u16; | |
486 | /// The peer of this socket has closed or disconnected. | |
487 | pub const EVENTRWFLAGS_FD_READWRITE_HANGUP: Eventrwflags = 0x1; | |
488 | #[repr(C)] | |
6a06907d | 489 | #[derive(Copy, Clone)] |
60c5eb7d XL |
490 | pub struct EventFdReadwrite { |
491 | /// The number of bytes available for reading or writing. | |
492 | pub nbytes: Filesize, | |
493 | /// The state of the file descriptor. | |
494 | pub flags: Eventrwflags, | |
495 | } | |
496 | #[repr(C)] | |
6a06907d XL |
497 | #[derive(Copy, Clone)] |
498 | pub union EventU { | |
499 | /// When type is `eventtype::fd_read` or `eventtype::fd_write`: | |
500 | pub fd_readwrite: EventFdReadwrite, | |
501 | } | |
502 | #[repr(C)] | |
503 | #[derive(Copy, Clone)] | |
60c5eb7d XL |
504 | pub struct Event { |
505 | /// User-provided value that got attached to `subscription::userdata`. | |
506 | pub userdata: Userdata, | |
507 | /// If non-zero, an error that occurred while processing the subscription request. | |
508 | pub error: Errno, | |
6a06907d | 509 | /// The type of the event that occurred. |
60c5eb7d | 510 | pub r#type: Eventtype, |
6a06907d XL |
511 | /// The contents of the event. |
512 | pub u: EventU, | |
60c5eb7d XL |
513 | } |
514 | pub type Subclockflags = u16; | |
515 | /// If set, treat the timestamp provided in | |
6a06907d XL |
516 | /// `subscription::u.clock.timeout` as an absolute timestamp of clock |
517 | /// `subscription::u.clock.clock_id.` If clear, treat the timestamp | |
518 | /// provided in `subscription::u.clock.timeout` relative to the | |
519 | /// current time value of clock `subscription::u.clock.clock_id.` | |
60c5eb7d XL |
520 | pub const SUBCLOCKFLAGS_SUBSCRIPTION_CLOCK_ABSTIME: Subclockflags = 0x1; |
521 | #[repr(C)] | |
6a06907d | 522 | #[derive(Copy, Clone)] |
60c5eb7d XL |
523 | pub struct SubscriptionClock { |
524 | /// The clock against which to compare the timestamp. | |
525 | pub id: Clockid, | |
526 | /// The absolute or relative timestamp. | |
527 | pub timeout: Timestamp, | |
528 | /// The amount of time that the implementation may wait additionally | |
529 | /// to coalesce with other events. | |
530 | pub precision: Timestamp, | |
531 | /// Flags specifying whether the timeout is absolute or relative | |
532 | pub flags: Subclockflags, | |
533 | } | |
534 | #[repr(C)] | |
6a06907d | 535 | #[derive(Copy, Clone)] |
60c5eb7d XL |
536 | pub struct SubscriptionFdReadwrite { |
537 | /// The file descriptor on which to wait for it to become ready for reading or writing. | |
538 | pub file_descriptor: Fd, | |
539 | } | |
540 | #[repr(C)] | |
541 | #[derive(Copy, Clone)] | |
6a06907d XL |
542 | pub union SubscriptionU { |
543 | /// When type is `eventtype::clock`: | |
60c5eb7d | 544 | pub clock: SubscriptionClock, |
6a06907d XL |
545 | /// When type is `eventtype::fd_read` or `eventtype::fd_write`: |
546 | pub fd_readwrite: SubscriptionFdReadwrite, | |
60c5eb7d XL |
547 | } |
548 | #[repr(C)] | |
549 | #[derive(Copy, Clone)] | |
550 | pub struct Subscription { | |
551 | /// User-provided value that is attached to the subscription in the | |
552 | /// implementation and returned through `event::userdata`. | |
553 | pub userdata: Userdata, | |
6a06907d XL |
554 | /// The type of the event to which to subscribe. |
555 | pub r#type: Eventtype, | |
556 | /// The contents of the subscription. | |
60c5eb7d XL |
557 | pub u: SubscriptionU, |
558 | } | |
559 | pub type Exitcode = u32; | |
560 | pub type Signal = u8; | |
561 | /// No signal. Note that POSIX has special semantics for `kill(pid, 0)`, | |
562 | /// so this value is reserved. | |
563 | pub const SIGNAL_NONE: Signal = 0; | |
564 | /// Hangup. | |
565 | /// Action: Terminates the process. | |
566 | pub const SIGNAL_HUP: Signal = 1; | |
567 | /// Terminate interrupt signal. | |
568 | /// Action: Terminates the process. | |
569 | pub const SIGNAL_INT: Signal = 2; | |
570 | /// Terminal quit signal. | |
571 | /// Action: Terminates the process. | |
572 | pub const SIGNAL_QUIT: Signal = 3; | |
573 | /// Illegal instruction. | |
574 | /// Action: Terminates the process. | |
575 | pub const SIGNAL_ILL: Signal = 4; | |
576 | /// Trace/breakpoint trap. | |
577 | /// Action: Terminates the process. | |
578 | pub const SIGNAL_TRAP: Signal = 5; | |
579 | /// Process abort signal. | |
580 | /// Action: Terminates the process. | |
581 | pub const SIGNAL_ABRT: Signal = 6; | |
582 | /// Access to an undefined portion of a memory object. | |
583 | /// Action: Terminates the process. | |
584 | pub const SIGNAL_BUS: Signal = 7; | |
585 | /// Erroneous arithmetic operation. | |
586 | /// Action: Terminates the process. | |
587 | pub const SIGNAL_FPE: Signal = 8; | |
588 | /// Kill. | |
589 | /// Action: Terminates the process. | |
590 | pub const SIGNAL_KILL: Signal = 9; | |
591 | /// User-defined signal 1. | |
592 | /// Action: Terminates the process. | |
593 | pub const SIGNAL_USR1: Signal = 10; | |
594 | /// Invalid memory reference. | |
595 | /// Action: Terminates the process. | |
596 | pub const SIGNAL_SEGV: Signal = 11; | |
597 | /// User-defined signal 2. | |
598 | /// Action: Terminates the process. | |
599 | pub const SIGNAL_USR2: Signal = 12; | |
600 | /// Write on a pipe with no one to read it. | |
601 | /// Action: Ignored. | |
602 | pub const SIGNAL_PIPE: Signal = 13; | |
603 | /// Alarm clock. | |
604 | /// Action: Terminates the process. | |
605 | pub const SIGNAL_ALRM: Signal = 14; | |
606 | /// Termination signal. | |
607 | /// Action: Terminates the process. | |
608 | pub const SIGNAL_TERM: Signal = 15; | |
609 | /// Child process terminated, stopped, or continued. | |
610 | /// Action: Ignored. | |
611 | pub const SIGNAL_CHLD: Signal = 16; | |
612 | /// Continue executing, if stopped. | |
613 | /// Action: Continues executing, if stopped. | |
614 | pub const SIGNAL_CONT: Signal = 17; | |
615 | /// Stop executing. | |
616 | /// Action: Stops executing. | |
617 | pub const SIGNAL_STOP: Signal = 18; | |
618 | /// Terminal stop signal. | |
619 | /// Action: Stops executing. | |
620 | pub const SIGNAL_TSTP: Signal = 19; | |
621 | /// Background process attempting read. | |
622 | /// Action: Stops executing. | |
623 | pub const SIGNAL_TTIN: Signal = 20; | |
624 | /// Background process attempting write. | |
625 | /// Action: Stops executing. | |
626 | pub const SIGNAL_TTOU: Signal = 21; | |
627 | /// High bandwidth data is available at a socket. | |
628 | /// Action: Ignored. | |
629 | pub const SIGNAL_URG: Signal = 22; | |
630 | /// CPU time limit exceeded. | |
631 | /// Action: Terminates the process. | |
632 | pub const SIGNAL_XCPU: Signal = 23; | |
633 | /// File size limit exceeded. | |
634 | /// Action: Terminates the process. | |
635 | pub const SIGNAL_XFSZ: Signal = 24; | |
636 | /// Virtual timer expired. | |
637 | /// Action: Terminates the process. | |
638 | pub const SIGNAL_VTALRM: Signal = 25; | |
639 | /// Profiling timer expired. | |
640 | /// Action: Terminates the process. | |
641 | pub const SIGNAL_PROF: Signal = 26; | |
642 | /// Window changed. | |
643 | /// Action: Ignored. | |
644 | pub const SIGNAL_WINCH: Signal = 27; | |
645 | /// I/O possible. | |
646 | /// Action: Terminates the process. | |
647 | pub const SIGNAL_POLL: Signal = 28; | |
648 | /// Power failure. | |
649 | /// Action: Terminates the process. | |
650 | pub const SIGNAL_PWR: Signal = 29; | |
651 | /// Bad system call. | |
652 | /// Action: Terminates the process. | |
653 | pub const SIGNAL_SYS: Signal = 30; | |
654 | pub type Riflags = u16; | |
655 | /// Returns the message without removing it from the socket's receive queue. | |
656 | pub const RIFLAGS_RECV_PEEK: Riflags = 0x1; | |
657 | /// On byte-stream sockets, block until the full amount of data can be returned. | |
658 | pub const RIFLAGS_RECV_WAITALL: Riflags = 0x2; | |
659 | pub type Roflags = u16; | |
660 | /// Returned by `sock_recv`: Message data has been truncated. | |
661 | pub const ROFLAGS_RECV_DATA_TRUNCATED: Roflags = 0x1; | |
662 | pub type Siflags = u16; | |
663 | pub type Sdflags = u8; | |
664 | /// Disables further receive operations. | |
665 | pub const SDFLAGS_RD: Sdflags = 0x1; | |
666 | /// Disables further send operations. | |
667 | pub const SDFLAGS_WR: Sdflags = 0x2; | |
668 | pub type Preopentype = u8; | |
669 | /// A pre-opened directory. | |
670 | pub const PREOPENTYPE_DIR: Preopentype = 0; | |
671 | #[repr(C)] | |
6a06907d | 672 | #[derive(Copy, Clone)] |
60c5eb7d XL |
673 | pub struct PrestatDir { |
674 | /// The length of the directory name for use with `fd_prestat_dir_name`. | |
675 | pub pr_name_len: Size, | |
676 | } | |
677 | #[repr(C)] | |
678 | #[derive(Copy, Clone)] | |
679 | pub union PrestatU { | |
6a06907d | 680 | /// When type is `preopentype::dir`: |
60c5eb7d XL |
681 | pub dir: PrestatDir, |
682 | } | |
683 | #[repr(C)] | |
684 | #[derive(Copy, Clone)] | |
685 | pub struct Prestat { | |
6a06907d XL |
686 | /// The type of the pre-opened capability. |
687 | pub pr_type: Preopentype, | |
688 | /// The contents of the information. | |
60c5eb7d XL |
689 | pub u: PrestatU, |
690 | } | |
691 | /// Read command-line argument data. | |
6a06907d | 692 | /// The size of the array should match that returned by `wasi_args_sizes_get()` |
60c5eb7d XL |
693 | pub unsafe fn args_get(argv: *mut *mut u8, argv_buf: *mut u8) -> Result<()> { |
694 | let rc = wasi_snapshot_preview1::args_get(argv, argv_buf); | |
695 | if let Some(err) = Error::from_raw_error(rc) { | |
696 | Err(err) | |
697 | } else { | |
698 | Ok(()) | |
699 | } | |
700 | } | |
701 | ||
702 | /// Return command-line argument data sizes. | |
703 | /// | |
704 | /// ## Return | |
705 | /// | |
706 | /// * `argc` - The number of arguments. | |
707 | /// * `argv_buf_size` - The size of the argument string data. | |
708 | pub unsafe fn args_sizes_get() -> Result<(Size, Size)> { | |
709 | let mut argc = MaybeUninit::uninit(); | |
710 | let mut argv_buf_size = MaybeUninit::uninit(); | |
711 | let rc = wasi_snapshot_preview1::args_sizes_get(argc.as_mut_ptr(), argv_buf_size.as_mut_ptr()); | |
712 | if let Some(err) = Error::from_raw_error(rc) { | |
713 | Err(err) | |
714 | } else { | |
715 | Ok((argc.assume_init(), argv_buf_size.assume_init())) | |
716 | } | |
717 | } | |
718 | ||
719 | /// Read environment variable data. | |
6a06907d | 720 | /// The sizes of the buffers should match that returned by `environ.sizes_get()`. |
60c5eb7d XL |
721 | pub unsafe fn environ_get(environ: *mut *mut u8, environ_buf: *mut u8) -> Result<()> { |
722 | let rc = wasi_snapshot_preview1::environ_get(environ, environ_buf); | |
723 | if let Some(err) = Error::from_raw_error(rc) { | |
724 | Err(err) | |
725 | } else { | |
726 | Ok(()) | |
727 | } | |
728 | } | |
729 | ||
6a06907d | 730 | /// Return command-line argument data sizes. |
60c5eb7d XL |
731 | /// |
732 | /// ## Return | |
733 | /// | |
6a06907d XL |
734 | /// * `argc` - The number of arguments. |
735 | /// * `argv_buf_size` - The size of the argument string data. | |
60c5eb7d | 736 | pub unsafe fn environ_sizes_get() -> Result<(Size, Size)> { |
6a06907d XL |
737 | let mut argc = MaybeUninit::uninit(); |
738 | let mut argv_buf_size = MaybeUninit::uninit(); | |
739 | let rc = | |
740 | wasi_snapshot_preview1::environ_sizes_get(argc.as_mut_ptr(), argv_buf_size.as_mut_ptr()); | |
60c5eb7d XL |
741 | if let Some(err) = Error::from_raw_error(rc) { |
742 | Err(err) | |
743 | } else { | |
6a06907d | 744 | Ok((argc.assume_init(), argv_buf_size.assume_init())) |
60c5eb7d XL |
745 | } |
746 | } | |
747 | ||
748 | /// Return the resolution of a clock. | |
6a06907d | 749 | /// Implementations are required to provide a non-zero value for supported clocks. For unsupported clocks, return `WASI_EINVAL` |
60c5eb7d XL |
750 | /// Note: This is similar to `clock_getres` in POSIX. |
751 | /// | |
752 | /// ## Parameters | |
753 | /// | |
754 | /// * `id` - The clock for which to return the resolution. | |
755 | /// | |
756 | /// ## Return | |
757 | /// | |
758 | /// * `resolution` - The resolution of the clock. | |
759 | pub unsafe fn clock_res_get(id: Clockid) -> Result<Timestamp> { | |
760 | let mut resolution = MaybeUninit::uninit(); | |
761 | let rc = wasi_snapshot_preview1::clock_res_get(id, resolution.as_mut_ptr()); | |
762 | if let Some(err) = Error::from_raw_error(rc) { | |
763 | Err(err) | |
764 | } else { | |
765 | Ok(resolution.assume_init()) | |
766 | } | |
767 | } | |
768 | ||
769 | /// Return the time value of a clock. | |
770 | /// Note: This is similar to `clock_gettime` in POSIX. | |
771 | /// | |
772 | /// ## Parameters | |
773 | /// | |
774 | /// * `id` - The clock for which to return the time. | |
775 | /// * `precision` - The maximum lag (exclusive) that the returned time value may have, compared to its actual value. | |
776 | /// | |
777 | /// ## Return | |
778 | /// | |
779 | /// * `time` - The time value of the clock. | |
780 | pub unsafe fn clock_time_get(id: Clockid, precision: Timestamp) -> Result<Timestamp> { | |
781 | let mut time = MaybeUninit::uninit(); | |
782 | let rc = wasi_snapshot_preview1::clock_time_get(id, precision, time.as_mut_ptr()); | |
783 | if let Some(err) = Error::from_raw_error(rc) { | |
784 | Err(err) | |
785 | } else { | |
786 | Ok(time.assume_init()) | |
787 | } | |
788 | } | |
789 | ||
790 | /// Provide file advisory information on a file descriptor. | |
791 | /// Note: This is similar to `posix_fadvise` in POSIX. | |
792 | /// | |
793 | /// ## Parameters | |
794 | /// | |
795 | /// * `offset` - The offset within the file to which the advisory applies. | |
796 | /// * `len` - The length of the region to which the advisory applies. | |
797 | /// * `advice` - The advice. | |
798 | pub unsafe fn fd_advise(fd: Fd, offset: Filesize, len: Filesize, advice: Advice) -> Result<()> { | |
799 | let rc = wasi_snapshot_preview1::fd_advise(fd, offset, len, advice); | |
800 | if let Some(err) = Error::from_raw_error(rc) { | |
801 | Err(err) | |
802 | } else { | |
803 | Ok(()) | |
804 | } | |
805 | } | |
806 | ||
807 | /// Force the allocation of space in a file. | |
808 | /// Note: This is similar to `posix_fallocate` in POSIX. | |
809 | /// | |
810 | /// ## Parameters | |
811 | /// | |
812 | /// * `offset` - The offset at which to start the allocation. | |
813 | /// * `len` - The length of the area that is allocated. | |
814 | pub unsafe fn fd_allocate(fd: Fd, offset: Filesize, len: Filesize) -> Result<()> { | |
815 | let rc = wasi_snapshot_preview1::fd_allocate(fd, offset, len); | |
816 | if let Some(err) = Error::from_raw_error(rc) { | |
817 | Err(err) | |
818 | } else { | |
819 | Ok(()) | |
820 | } | |
821 | } | |
822 | ||
823 | /// Close a file descriptor. | |
824 | /// Note: This is similar to `close` in POSIX. | |
825 | pub unsafe fn fd_close(fd: Fd) -> Result<()> { | |
826 | let rc = wasi_snapshot_preview1::fd_close(fd); | |
827 | if let Some(err) = Error::from_raw_error(rc) { | |
828 | Err(err) | |
829 | } else { | |
830 | Ok(()) | |
831 | } | |
832 | } | |
833 | ||
834 | /// Synchronize the data of a file to disk. | |
835 | /// Note: This is similar to `fdatasync` in POSIX. | |
836 | pub unsafe fn fd_datasync(fd: Fd) -> Result<()> { | |
837 | let rc = wasi_snapshot_preview1::fd_datasync(fd); | |
838 | if let Some(err) = Error::from_raw_error(rc) { | |
839 | Err(err) | |
840 | } else { | |
841 | Ok(()) | |
842 | } | |
843 | } | |
844 | ||
845 | /// Get the attributes of a file descriptor. | |
846 | /// Note: This returns similar flags to `fsync(fd, F_GETFL)` in POSIX, as well as additional fields. | |
847 | /// | |
848 | /// ## Return | |
849 | /// | |
850 | /// * `stat` - The buffer where the file descriptor's attributes are stored. | |
851 | pub unsafe fn fd_fdstat_get(fd: Fd) -> Result<Fdstat> { | |
852 | let mut stat = MaybeUninit::uninit(); | |
853 | let rc = wasi_snapshot_preview1::fd_fdstat_get(fd, stat.as_mut_ptr()); | |
854 | if let Some(err) = Error::from_raw_error(rc) { | |
855 | Err(err) | |
856 | } else { | |
857 | Ok(stat.assume_init()) | |
858 | } | |
859 | } | |
860 | ||
861 | /// Adjust the flags associated with a file descriptor. | |
862 | /// Note: This is similar to `fcntl(fd, F_SETFL, flags)` in POSIX. | |
863 | /// | |
864 | /// ## Parameters | |
865 | /// | |
866 | /// * `flags` - The desired values of the file descriptor flags. | |
867 | pub unsafe fn fd_fdstat_set_flags(fd: Fd, flags: Fdflags) -> Result<()> { | |
868 | let rc = wasi_snapshot_preview1::fd_fdstat_set_flags(fd, flags); | |
869 | if let Some(err) = Error::from_raw_error(rc) { | |
870 | Err(err) | |
871 | } else { | |
872 | Ok(()) | |
873 | } | |
874 | } | |
875 | ||
876 | /// Adjust the rights associated with a file descriptor. | |
6a06907d | 877 | /// This can only be used to remove rights, and returns `ENOTCAPABLE` if called in a way that would attempt to add rights |
60c5eb7d XL |
878 | /// |
879 | /// ## Parameters | |
880 | /// | |
881 | /// * `fs_rights_base` - The desired rights of the file descriptor. | |
882 | pub unsafe fn fd_fdstat_set_rights( | |
883 | fd: Fd, | |
884 | fs_rights_base: Rights, | |
885 | fs_rights_inheriting: Rights, | |
886 | ) -> Result<()> { | |
887 | let rc = wasi_snapshot_preview1::fd_fdstat_set_rights(fd, fs_rights_base, fs_rights_inheriting); | |
888 | if let Some(err) = Error::from_raw_error(rc) { | |
889 | Err(err) | |
890 | } else { | |
891 | Ok(()) | |
892 | } | |
893 | } | |
894 | ||
895 | /// Return the attributes of an open file. | |
896 | /// | |
897 | /// ## Return | |
898 | /// | |
899 | /// * `buf` - The buffer where the file's attributes are stored. | |
900 | pub unsafe fn fd_filestat_get(fd: Fd) -> Result<Filestat> { | |
901 | let mut buf = MaybeUninit::uninit(); | |
902 | let rc = wasi_snapshot_preview1::fd_filestat_get(fd, buf.as_mut_ptr()); | |
903 | if let Some(err) = Error::from_raw_error(rc) { | |
904 | Err(err) | |
905 | } else { | |
906 | Ok(buf.assume_init()) | |
907 | } | |
908 | } | |
909 | ||
910 | /// Adjust the size of an open file. If this increases the file's size, the extra bytes are filled with zeros. | |
911 | /// Note: This is similar to `ftruncate` in POSIX. | |
912 | /// | |
913 | /// ## Parameters | |
914 | /// | |
915 | /// * `size` - The desired file size. | |
916 | pub unsafe fn fd_filestat_set_size(fd: Fd, size: Filesize) -> Result<()> { | |
917 | let rc = wasi_snapshot_preview1::fd_filestat_set_size(fd, size); | |
918 | if let Some(err) = Error::from_raw_error(rc) { | |
919 | Err(err) | |
920 | } else { | |
921 | Ok(()) | |
922 | } | |
923 | } | |
924 | ||
925 | /// Adjust the timestamps of an open file or directory. | |
926 | /// Note: This is similar to `futimens` in POSIX. | |
927 | /// | |
928 | /// ## Parameters | |
929 | /// | |
930 | /// * `atim` - The desired values of the data access timestamp. | |
931 | /// * `mtim` - The desired values of the data modification timestamp. | |
932 | /// * `fst_flags` - A bitmask indicating which timestamps to adjust. | |
933 | pub unsafe fn fd_filestat_set_times( | |
934 | fd: Fd, | |
935 | atim: Timestamp, | |
936 | mtim: Timestamp, | |
937 | fst_flags: Fstflags, | |
938 | ) -> Result<()> { | |
939 | let rc = wasi_snapshot_preview1::fd_filestat_set_times(fd, atim, mtim, fst_flags); | |
940 | if let Some(err) = Error::from_raw_error(rc) { | |
941 | Err(err) | |
942 | } else { | |
943 | Ok(()) | |
944 | } | |
945 | } | |
946 | ||
947 | /// Read from a file descriptor, without using and updating the file descriptor's offset. | |
948 | /// Note: This is similar to `preadv` in POSIX. | |
949 | /// | |
950 | /// ## Parameters | |
951 | /// | |
952 | /// * `iovs` - List of scatter/gather vectors in which to store data. | |
953 | /// * `offset` - The offset within the file at which to read. | |
954 | /// | |
955 | /// ## Return | |
956 | /// | |
957 | /// * `nread` - The number of bytes read. | |
958 | pub unsafe fn fd_pread(fd: Fd, iovs: IovecArray, offset: Filesize) -> Result<Size> { | |
959 | let mut nread = MaybeUninit::uninit(); | |
960 | let rc = | |
961 | wasi_snapshot_preview1::fd_pread(fd, iovs.as_ptr(), iovs.len(), offset, nread.as_mut_ptr()); | |
962 | if let Some(err) = Error::from_raw_error(rc) { | |
963 | Err(err) | |
964 | } else { | |
965 | Ok(nread.assume_init()) | |
966 | } | |
967 | } | |
968 | ||
969 | /// Return a description of the given preopened file descriptor. | |
970 | /// | |
971 | /// ## Return | |
972 | /// | |
973 | /// * `buf` - The buffer where the description is stored. | |
974 | pub unsafe fn fd_prestat_get(fd: Fd) -> Result<Prestat> { | |
975 | let mut buf = MaybeUninit::uninit(); | |
976 | let rc = wasi_snapshot_preview1::fd_prestat_get(fd, buf.as_mut_ptr()); | |
977 | if let Some(err) = Error::from_raw_error(rc) { | |
978 | Err(err) | |
979 | } else { | |
980 | Ok(buf.assume_init()) | |
981 | } | |
982 | } | |
983 | ||
984 | /// Return a description of the given preopened file descriptor. | |
985 | /// | |
986 | /// ## Parameters | |
987 | /// | |
988 | /// * `path` - A buffer into which to write the preopened directory name. | |
989 | pub unsafe fn fd_prestat_dir_name(fd: Fd, path: *mut u8, path_len: Size) -> Result<()> { | |
990 | let rc = wasi_snapshot_preview1::fd_prestat_dir_name(fd, path, path_len); | |
991 | if let Some(err) = Error::from_raw_error(rc) { | |
992 | Err(err) | |
993 | } else { | |
994 | Ok(()) | |
995 | } | |
996 | } | |
997 | ||
998 | /// Write to a file descriptor, without using and updating the file descriptor's offset. | |
999 | /// Note: This is similar to `pwritev` in POSIX. | |
1000 | /// | |
1001 | /// ## Parameters | |
1002 | /// | |
1003 | /// * `iovs` - List of scatter/gather vectors from which to retrieve data. | |
1004 | /// * `offset` - The offset within the file at which to write. | |
1005 | /// | |
1006 | /// ## Return | |
1007 | /// | |
1008 | /// * `nwritten` - The number of bytes written. | |
1009 | pub unsafe fn fd_pwrite(fd: Fd, iovs: CiovecArray, offset: Filesize) -> Result<Size> { | |
1010 | let mut nwritten = MaybeUninit::uninit(); | |
1011 | let rc = wasi_snapshot_preview1::fd_pwrite( | |
1012 | fd, | |
1013 | iovs.as_ptr(), | |
1014 | iovs.len(), | |
1015 | offset, | |
1016 | nwritten.as_mut_ptr(), | |
1017 | ); | |
1018 | if let Some(err) = Error::from_raw_error(rc) { | |
1019 | Err(err) | |
1020 | } else { | |
1021 | Ok(nwritten.assume_init()) | |
1022 | } | |
1023 | } | |
1024 | ||
1025 | /// Read from a file descriptor. | |
1026 | /// Note: This is similar to `readv` in POSIX. | |
1027 | /// | |
1028 | /// ## Parameters | |
1029 | /// | |
1030 | /// * `iovs` - List of scatter/gather vectors to which to store data. | |
1031 | /// | |
1032 | /// ## Return | |
1033 | /// | |
1034 | /// * `nread` - The number of bytes read. | |
1035 | pub unsafe fn fd_read(fd: Fd, iovs: IovecArray) -> Result<Size> { | |
1036 | let mut nread = MaybeUninit::uninit(); | |
1037 | let rc = wasi_snapshot_preview1::fd_read(fd, iovs.as_ptr(), iovs.len(), nread.as_mut_ptr()); | |
1038 | if let Some(err) = Error::from_raw_error(rc) { | |
1039 | Err(err) | |
1040 | } else { | |
1041 | Ok(nread.assume_init()) | |
1042 | } | |
1043 | } | |
1044 | ||
1045 | /// Read directory entries from a directory. | |
1046 | /// When successful, the contents of the output buffer consist of a sequence of | |
1047 | /// directory entries. Each directory entry consists of a dirent_t object, | |
1048 | /// followed by dirent_t::d_namlen bytes holding the name of the directory | |
1049 | /// entry. | |
1050 | /// This function fills the output buffer as much as possible, potentially | |
1051 | /// truncating the last directory entry. This allows the caller to grow its | |
1052 | /// read buffer size in case it's too small to fit a single large directory | |
1053 | /// entry, or skip the oversized directory entry. | |
1054 | /// | |
1055 | /// ## Parameters | |
1056 | /// | |
1057 | /// * `buf` - The buffer where directory entries are stored | |
1058 | /// * `cookie` - The location within the directory to start reading | |
1059 | /// | |
1060 | /// ## Return | |
1061 | /// | |
1062 | /// * `bufused` - The number of bytes stored in the read buffer. If less than the size of the read buffer, the end of the directory has been reached. | |
1063 | pub unsafe fn fd_readdir(fd: Fd, buf: *mut u8, buf_len: Size, cookie: Dircookie) -> Result<Size> { | |
1064 | let mut bufused = MaybeUninit::uninit(); | |
1065 | let rc = wasi_snapshot_preview1::fd_readdir(fd, buf, buf_len, cookie, bufused.as_mut_ptr()); | |
1066 | if let Some(err) = Error::from_raw_error(rc) { | |
1067 | Err(err) | |
1068 | } else { | |
1069 | Ok(bufused.assume_init()) | |
1070 | } | |
1071 | } | |
1072 | ||
1073 | /// Atomically replace a file descriptor by renumbering another file descriptor. | |
1074 | /// Due to the strong focus on thread safety, this environment does not provide | |
1075 | /// a mechanism to duplicate or renumber a file descriptor to an arbitrary | |
1076 | /// number, like `dup2()`. This would be prone to race conditions, as an actual | |
1077 | /// file descriptor with the same number could be allocated by a different | |
1078 | /// thread at the same time. | |
1079 | /// This function provides a way to atomically renumber file descriptors, which | |
1080 | /// would disappear if `dup2()` were to be removed entirely. | |
1081 | /// | |
1082 | /// ## Parameters | |
1083 | /// | |
1084 | /// * `to` - The file descriptor to overwrite. | |
1085 | pub unsafe fn fd_renumber(fd: Fd, to: Fd) -> Result<()> { | |
1086 | let rc = wasi_snapshot_preview1::fd_renumber(fd, to); | |
1087 | if let Some(err) = Error::from_raw_error(rc) { | |
1088 | Err(err) | |
1089 | } else { | |
1090 | Ok(()) | |
1091 | } | |
1092 | } | |
1093 | ||
1094 | /// Move the offset of a file descriptor. | |
1095 | /// Note: This is similar to `lseek` in POSIX. | |
1096 | /// | |
1097 | /// ## Parameters | |
1098 | /// | |
1099 | /// * `offset` - The number of bytes to move. | |
1100 | /// * `whence` - The base from which the offset is relative. | |
1101 | /// | |
1102 | /// ## Return | |
1103 | /// | |
1104 | /// * `newoffset` - The new offset of the file descriptor, relative to the start of the file. | |
1105 | pub unsafe fn fd_seek(fd: Fd, offset: Filedelta, whence: Whence) -> Result<Filesize> { | |
1106 | let mut newoffset = MaybeUninit::uninit(); | |
1107 | let rc = wasi_snapshot_preview1::fd_seek(fd, offset, whence, newoffset.as_mut_ptr()); | |
1108 | if let Some(err) = Error::from_raw_error(rc) { | |
1109 | Err(err) | |
1110 | } else { | |
1111 | Ok(newoffset.assume_init()) | |
1112 | } | |
1113 | } | |
1114 | ||
1115 | /// Synchronize the data and metadata of a file to disk. | |
1116 | /// Note: This is similar to `fsync` in POSIX. | |
1117 | pub unsafe fn fd_sync(fd: Fd) -> Result<()> { | |
1118 | let rc = wasi_snapshot_preview1::fd_sync(fd); | |
1119 | if let Some(err) = Error::from_raw_error(rc) { | |
1120 | Err(err) | |
1121 | } else { | |
1122 | Ok(()) | |
1123 | } | |
1124 | } | |
1125 | ||
1126 | /// Return the current offset of a file descriptor. | |
1127 | /// Note: This is similar to `lseek(fd, 0, SEEK_CUR)` in POSIX. | |
1128 | /// | |
1129 | /// ## Return | |
1130 | /// | |
1131 | /// * `offset` - The current offset of the file descriptor, relative to the start of the file. | |
1132 | pub unsafe fn fd_tell(fd: Fd) -> Result<Filesize> { | |
1133 | let mut offset = MaybeUninit::uninit(); | |
1134 | let rc = wasi_snapshot_preview1::fd_tell(fd, offset.as_mut_ptr()); | |
1135 | if let Some(err) = Error::from_raw_error(rc) { | |
1136 | Err(err) | |
1137 | } else { | |
1138 | Ok(offset.assume_init()) | |
1139 | } | |
1140 | } | |
1141 | ||
1142 | /// Write to a file descriptor. | |
1143 | /// Note: This is similar to `writev` in POSIX. | |
1144 | /// | |
1145 | /// ## Parameters | |
1146 | /// | |
1147 | /// * `iovs` - List of scatter/gather vectors from which to retrieve data. | |
1148 | /// | |
1149 | /// ## Return | |
1150 | /// | |
1151 | /// * `nwritten` - The number of bytes written. | |
1152 | pub unsafe fn fd_write(fd: Fd, iovs: CiovecArray) -> Result<Size> { | |
1153 | let mut nwritten = MaybeUninit::uninit(); | |
1154 | let rc = wasi_snapshot_preview1::fd_write(fd, iovs.as_ptr(), iovs.len(), nwritten.as_mut_ptr()); | |
1155 | if let Some(err) = Error::from_raw_error(rc) { | |
1156 | Err(err) | |
1157 | } else { | |
1158 | Ok(nwritten.assume_init()) | |
1159 | } | |
1160 | } | |
1161 | ||
1162 | /// Create a directory. | |
1163 | /// Note: This is similar to `mkdirat` in POSIX. | |
1164 | /// | |
1165 | /// ## Parameters | |
1166 | /// | |
1167 | /// * `path` - The path at which to create the directory. | |
1168 | pub unsafe fn path_create_directory(fd: Fd, path: &str) -> Result<()> { | |
1169 | let rc = wasi_snapshot_preview1::path_create_directory(fd, path.as_ptr(), path.len()); | |
1170 | if let Some(err) = Error::from_raw_error(rc) { | |
1171 | Err(err) | |
1172 | } else { | |
1173 | Ok(()) | |
1174 | } | |
1175 | } | |
1176 | ||
1177 | /// Return the attributes of a file or directory. | |
1178 | /// Note: This is similar to `stat` in POSIX. | |
1179 | /// | |
1180 | /// ## Parameters | |
1181 | /// | |
1182 | /// * `flags` - Flags determining the method of how the path is resolved. | |
1183 | /// * `path` - The path of the file or directory to inspect. | |
1184 | /// | |
1185 | /// ## Return | |
1186 | /// | |
1187 | /// * `buf` - The buffer where the file's attributes are stored. | |
1188 | pub unsafe fn path_filestat_get(fd: Fd, flags: Lookupflags, path: &str) -> Result<Filestat> { | |
1189 | let mut buf = MaybeUninit::uninit(); | |
1190 | let rc = wasi_snapshot_preview1::path_filestat_get( | |
1191 | fd, | |
1192 | flags, | |
1193 | path.as_ptr(), | |
1194 | path.len(), | |
1195 | buf.as_mut_ptr(), | |
1196 | ); | |
1197 | if let Some(err) = Error::from_raw_error(rc) { | |
1198 | Err(err) | |
1199 | } else { | |
1200 | Ok(buf.assume_init()) | |
1201 | } | |
1202 | } | |
1203 | ||
1204 | /// Adjust the timestamps of a file or directory. | |
1205 | /// Note: This is similar to `utimensat` in POSIX. | |
1206 | /// | |
1207 | /// ## Parameters | |
1208 | /// | |
1209 | /// * `flags` - Flags determining the method of how the path is resolved. | |
1210 | /// * `path` - The path of the file or directory to operate on. | |
1211 | /// * `atim` - The desired values of the data access timestamp. | |
1212 | /// * `mtim` - The desired values of the data modification timestamp. | |
1213 | /// * `fst_flags` - A bitmask indicating which timestamps to adjust. | |
1214 | pub unsafe fn path_filestat_set_times( | |
1215 | fd: Fd, | |
1216 | flags: Lookupflags, | |
1217 | path: &str, | |
1218 | atim: Timestamp, | |
1219 | mtim: Timestamp, | |
1220 | fst_flags: Fstflags, | |
1221 | ) -> Result<()> { | |
1222 | let rc = wasi_snapshot_preview1::path_filestat_set_times( | |
1223 | fd, | |
1224 | flags, | |
1225 | path.as_ptr(), | |
1226 | path.len(), | |
1227 | atim, | |
1228 | mtim, | |
1229 | fst_flags, | |
1230 | ); | |
1231 | if let Some(err) = Error::from_raw_error(rc) { | |
1232 | Err(err) | |
1233 | } else { | |
1234 | Ok(()) | |
1235 | } | |
1236 | } | |
1237 | ||
1238 | /// Create a hard link. | |
1239 | /// Note: This is similar to `linkat` in POSIX. | |
1240 | /// | |
1241 | /// ## Parameters | |
1242 | /// | |
1243 | /// * `old_flags` - Flags determining the method of how the path is resolved. | |
1244 | /// * `old_path` - The source path from which to link. | |
1245 | /// * `new_fd` - The working directory at which the resolution of the new path starts. | |
1246 | /// * `new_path` - The destination path at which to create the hard link. | |
1247 | pub unsafe fn path_link( | |
1248 | old_fd: Fd, | |
1249 | old_flags: Lookupflags, | |
1250 | old_path: &str, | |
1251 | new_fd: Fd, | |
1252 | new_path: &str, | |
1253 | ) -> Result<()> { | |
1254 | let rc = wasi_snapshot_preview1::path_link( | |
1255 | old_fd, | |
1256 | old_flags, | |
1257 | old_path.as_ptr(), | |
1258 | old_path.len(), | |
1259 | new_fd, | |
1260 | new_path.as_ptr(), | |
1261 | new_path.len(), | |
1262 | ); | |
1263 | if let Some(err) = Error::from_raw_error(rc) { | |
1264 | Err(err) | |
1265 | } else { | |
1266 | Ok(()) | |
1267 | } | |
1268 | } | |
1269 | ||
1270 | /// Open a file or directory. | |
1271 | /// The returned file descriptor is not guaranteed to be the lowest-numbered | |
1272 | /// file descriptor not currently open; it is randomized to prevent | |
1273 | /// applications from depending on making assumptions about indexes, since this | |
1274 | /// is error-prone in multi-threaded contexts. The returned file descriptor is | |
1275 | /// guaranteed to be less than 2**31. | |
1276 | /// Note: This is similar to `openat` in POSIX. | |
1277 | /// | |
1278 | /// ## Parameters | |
1279 | /// | |
1280 | /// * `dirflags` - Flags determining the method of how the path is resolved. | |
1281 | /// * `path` - The relative path of the file or directory to open, relative to the | |
6a06907d | 1282 | /// `dirfd` directory. |
60c5eb7d XL |
1283 | /// * `oflags` - The method by which to open the file. |
1284 | /// * `fs_rights_base` - The initial rights of the newly created file descriptor. The | |
1285 | /// implementation is allowed to return a file descriptor with fewer rights | |
1286 | /// than specified, if and only if those rights do not apply to the type of | |
1287 | /// file being opened. | |
1288 | /// The *base* rights are rights that will apply to operations using the file | |
1289 | /// descriptor itself, while the *inheriting* rights are rights that apply to | |
1290 | /// file descriptors derived from it. | |
1291 | /// | |
1292 | /// ## Return | |
1293 | /// | |
1294 | /// * `opened_fd` - The file descriptor of the file that has been opened. | |
1295 | pub unsafe fn path_open( | |
1296 | fd: Fd, | |
1297 | dirflags: Lookupflags, | |
1298 | path: &str, | |
1299 | oflags: Oflags, | |
1300 | fs_rights_base: Rights, | |
1301 | fs_rights_inherting: Rights, | |
1302 | fdflags: Fdflags, | |
1303 | ) -> Result<Fd> { | |
1304 | let mut opened_fd = MaybeUninit::uninit(); | |
1305 | let rc = wasi_snapshot_preview1::path_open( | |
1306 | fd, | |
1307 | dirflags, | |
1308 | path.as_ptr(), | |
1309 | path.len(), | |
1310 | oflags, | |
1311 | fs_rights_base, | |
1312 | fs_rights_inherting, | |
1313 | fdflags, | |
1314 | opened_fd.as_mut_ptr(), | |
1315 | ); | |
1316 | if let Some(err) = Error::from_raw_error(rc) { | |
1317 | Err(err) | |
1318 | } else { | |
1319 | Ok(opened_fd.assume_init()) | |
1320 | } | |
1321 | } | |
1322 | ||
1323 | /// Read the contents of a symbolic link. | |
1324 | /// Note: This is similar to `readlinkat` in POSIX. | |
1325 | /// | |
1326 | /// ## Parameters | |
1327 | /// | |
1328 | /// * `path` - The path of the symbolic link from which to read. | |
1329 | /// * `buf` - The buffer to which to write the contents of the symbolic link. | |
1330 | /// | |
1331 | /// ## Return | |
1332 | /// | |
1333 | /// * `bufused` - The number of bytes placed in the buffer. | |
1334 | pub unsafe fn path_readlink(fd: Fd, path: &str, buf: *mut u8, buf_len: Size) -> Result<Size> { | |
1335 | let mut bufused = MaybeUninit::uninit(); | |
1336 | let rc = wasi_snapshot_preview1::path_readlink( | |
1337 | fd, | |
1338 | path.as_ptr(), | |
1339 | path.len(), | |
1340 | buf, | |
1341 | buf_len, | |
1342 | bufused.as_mut_ptr(), | |
1343 | ); | |
1344 | if let Some(err) = Error::from_raw_error(rc) { | |
1345 | Err(err) | |
1346 | } else { | |
1347 | Ok(bufused.assume_init()) | |
1348 | } | |
1349 | } | |
1350 | ||
1351 | /// Remove a directory. | |
6a06907d | 1352 | /// Return `ENOTEMPTY` if the directory is not empty. |
60c5eb7d XL |
1353 | /// Note: This is similar to `unlinkat(fd, path, AT_REMOVEDIR)` in POSIX. |
1354 | /// | |
1355 | /// ## Parameters | |
1356 | /// | |
1357 | /// * `path` - The path to a directory to remove. | |
1358 | pub unsafe fn path_remove_directory(fd: Fd, path: &str) -> Result<()> { | |
1359 | let rc = wasi_snapshot_preview1::path_remove_directory(fd, path.as_ptr(), path.len()); | |
1360 | if let Some(err) = Error::from_raw_error(rc) { | |
1361 | Err(err) | |
1362 | } else { | |
1363 | Ok(()) | |
1364 | } | |
1365 | } | |
1366 | ||
1367 | /// Rename a file or directory. | |
1368 | /// Note: This is similar to `renameat` in POSIX. | |
1369 | /// | |
1370 | /// ## Parameters | |
1371 | /// | |
1372 | /// * `old_path` - The source path of the file or directory to rename. | |
1373 | /// * `new_fd` - The working directory at which the resolution of the new path starts. | |
1374 | /// * `new_path` - The destination path to which to rename the file or directory. | |
1375 | pub unsafe fn path_rename(fd: Fd, old_path: &str, new_fd: Fd, new_path: &str) -> Result<()> { | |
1376 | let rc = wasi_snapshot_preview1::path_rename( | |
1377 | fd, | |
1378 | old_path.as_ptr(), | |
1379 | old_path.len(), | |
1380 | new_fd, | |
1381 | new_path.as_ptr(), | |
1382 | new_path.len(), | |
1383 | ); | |
1384 | if let Some(err) = Error::from_raw_error(rc) { | |
1385 | Err(err) | |
1386 | } else { | |
1387 | Ok(()) | |
1388 | } | |
1389 | } | |
1390 | ||
1391 | /// Create a symbolic link. | |
1392 | /// Note: This is similar to `symlinkat` in POSIX. | |
1393 | /// | |
1394 | /// ## Parameters | |
1395 | /// | |
1396 | /// * `old_path` - The contents of the symbolic link. | |
1397 | /// * `new_path` - The destination path at which to create the symbolic link. | |
1398 | pub unsafe fn path_symlink(old_path: &str, fd: Fd, new_path: &str) -> Result<()> { | |
1399 | let rc = wasi_snapshot_preview1::path_symlink( | |
1400 | old_path.as_ptr(), | |
1401 | old_path.len(), | |
1402 | fd, | |
1403 | new_path.as_ptr(), | |
1404 | new_path.len(), | |
1405 | ); | |
1406 | if let Some(err) = Error::from_raw_error(rc) { | |
1407 | Err(err) | |
1408 | } else { | |
1409 | Ok(()) | |
1410 | } | |
1411 | } | |
1412 | ||
1413 | /// Unlink a file. | |
6a06907d | 1414 | /// Return `EISDIR` if the path refers to a directory. |
60c5eb7d XL |
1415 | /// Note: This is similar to `unlinkat(fd, path, 0)` in POSIX. |
1416 | /// | |
1417 | /// ## Parameters | |
1418 | /// | |
1419 | /// * `path` - The path to a file to unlink. | |
1420 | pub unsafe fn path_unlink_file(fd: Fd, path: &str) -> Result<()> { | |
1421 | let rc = wasi_snapshot_preview1::path_unlink_file(fd, path.as_ptr(), path.len()); | |
1422 | if let Some(err) = Error::from_raw_error(rc) { | |
1423 | Err(err) | |
1424 | } else { | |
1425 | Ok(()) | |
1426 | } | |
1427 | } | |
1428 | ||
1429 | /// Concurrently poll for the occurrence of a set of events. | |
1430 | /// | |
1431 | /// ## Parameters | |
1432 | /// | |
1433 | /// * `r#in` - The events to which to subscribe. | |
1434 | /// * `out` - The events that have occurred. | |
1435 | /// * `nsubscriptions` - Both the number of subscriptions and events. | |
1436 | /// | |
1437 | /// ## Return | |
1438 | /// | |
1439 | /// * `nevents` - The number of events stored. | |
1440 | pub unsafe fn poll_oneoff( | |
1441 | r#in: *const Subscription, | |
1442 | out: *mut Event, | |
1443 | nsubscriptions: Size, | |
1444 | ) -> Result<Size> { | |
1445 | let mut nevents = MaybeUninit::uninit(); | |
1446 | let rc = wasi_snapshot_preview1::poll_oneoff(r#in, out, nsubscriptions, nevents.as_mut_ptr()); | |
1447 | if let Some(err) = Error::from_raw_error(rc) { | |
1448 | Err(err) | |
1449 | } else { | |
1450 | Ok(nevents.assume_init()) | |
1451 | } | |
1452 | } | |
1453 | ||
1454 | /// Terminate the process normally. An exit code of 0 indicates successful | |
1455 | /// termination of the program. The meanings of other values is dependent on | |
1456 | /// the environment. | |
1457 | /// | |
1458 | /// ## Parameters | |
1459 | /// | |
1460 | /// * `rval` - The exit code returned by the process. | |
1461 | pub unsafe fn proc_exit(rval: Exitcode) { | |
1462 | wasi_snapshot_preview1::proc_exit(rval); | |
1463 | } | |
1464 | ||
1465 | /// Send a signal to the process of the calling thread. | |
1466 | /// Note: This is similar to `raise` in POSIX. | |
1467 | /// | |
1468 | /// ## Parameters | |
1469 | /// | |
1470 | /// * `sig` - The signal condition to trigger. | |
1471 | pub unsafe fn proc_raise(sig: Signal) -> Result<()> { | |
1472 | let rc = wasi_snapshot_preview1::proc_raise(sig); | |
1473 | if let Some(err) = Error::from_raw_error(rc) { | |
1474 | Err(err) | |
1475 | } else { | |
1476 | Ok(()) | |
1477 | } | |
1478 | } | |
1479 | ||
1480 | /// Temporarily yield execution of the calling thread. | |
1481 | /// Note: This is similar to `sched_yield` in POSIX. | |
1482 | pub unsafe fn sched_yield() -> Result<()> { | |
1483 | let rc = wasi_snapshot_preview1::sched_yield(); | |
1484 | if let Some(err) = Error::from_raw_error(rc) { | |
1485 | Err(err) | |
1486 | } else { | |
1487 | Ok(()) | |
1488 | } | |
1489 | } | |
1490 | ||
1491 | /// Write high-quality random data into a buffer. | |
1492 | /// This function blocks when the implementation is unable to immediately | |
1493 | /// provide sufficient high-quality random data. | |
1494 | /// This function may execute slowly, so when large mounts of random data are | |
1495 | /// required, it's advisable to use this function to seed a pseudo-random | |
1496 | /// number generator, rather than to provide the random data directly. | |
1497 | /// | |
1498 | /// ## Parameters | |
1499 | /// | |
1500 | /// * `buf` - The buffer to fill with random data. | |
1501 | pub unsafe fn random_get(buf: *mut u8, buf_len: Size) -> Result<()> { | |
1502 | let rc = wasi_snapshot_preview1::random_get(buf, buf_len); | |
1503 | if let Some(err) = Error::from_raw_error(rc) { | |
1504 | Err(err) | |
1505 | } else { | |
1506 | Ok(()) | |
1507 | } | |
1508 | } | |
1509 | ||
1510 | /// Receive a message from a socket. | |
1511 | /// Note: This is similar to `recv` in POSIX, though it also supports reading | |
1512 | /// the data into multiple buffers in the manner of `readv`. | |
1513 | /// | |
1514 | /// ## Parameters | |
1515 | /// | |
1516 | /// * `ri_data` - List of scatter/gather vectors to which to store data. | |
1517 | /// * `ri_flags` - Message flags. | |
1518 | /// | |
1519 | /// ## Return | |
1520 | /// | |
1521 | /// * `ro_datalen` - Number of bytes stored in ri_data. | |
1522 | /// * `ro_flags` - Message flags. | |
1523 | pub unsafe fn sock_recv(fd: Fd, ri_data: IovecArray, ri_flags: Riflags) -> Result<(Size, Roflags)> { | |
1524 | let mut ro_datalen = MaybeUninit::uninit(); | |
1525 | let mut ro_flags = MaybeUninit::uninit(); | |
1526 | let rc = wasi_snapshot_preview1::sock_recv( | |
1527 | fd, | |
1528 | ri_data.as_ptr(), | |
1529 | ri_data.len(), | |
1530 | ri_flags, | |
1531 | ro_datalen.as_mut_ptr(), | |
1532 | ro_flags.as_mut_ptr(), | |
1533 | ); | |
1534 | if let Some(err) = Error::from_raw_error(rc) { | |
1535 | Err(err) | |
1536 | } else { | |
1537 | Ok((ro_datalen.assume_init(), ro_flags.assume_init())) | |
1538 | } | |
1539 | } | |
1540 | ||
1541 | /// Send a message on a socket. | |
1542 | /// Note: This is similar to `send` in POSIX, though it also supports writing | |
1543 | /// the data from multiple buffers in the manner of `writev`. | |
1544 | /// | |
1545 | /// ## Parameters | |
1546 | /// | |
1547 | /// * `si_data` - List of scatter/gather vectors to which to retrieve data | |
1548 | /// * `si_flags` - Message flags. | |
1549 | /// | |
1550 | /// ## Return | |
1551 | /// | |
1552 | /// * `so_datalen` - Number of bytes transmitted. | |
1553 | pub unsafe fn sock_send(fd: Fd, si_data: CiovecArray, si_flags: Siflags) -> Result<Size> { | |
1554 | let mut so_datalen = MaybeUninit::uninit(); | |
1555 | let rc = wasi_snapshot_preview1::sock_send( | |
1556 | fd, | |
1557 | si_data.as_ptr(), | |
1558 | si_data.len(), | |
1559 | si_flags, | |
1560 | so_datalen.as_mut_ptr(), | |
1561 | ); | |
1562 | if let Some(err) = Error::from_raw_error(rc) { | |
1563 | Err(err) | |
1564 | } else { | |
1565 | Ok(so_datalen.assume_init()) | |
1566 | } | |
1567 | } | |
1568 | ||
1569 | /// Shut down socket send and receive channels. | |
1570 | /// Note: This is similar to `shutdown` in POSIX. | |
1571 | /// | |
1572 | /// ## Parameters | |
1573 | /// | |
1574 | /// * `how` - Which channels on the socket to shut down. | |
1575 | pub unsafe fn sock_shutdown(fd: Fd, how: Sdflags) -> Result<()> { | |
1576 | let rc = wasi_snapshot_preview1::sock_shutdown(fd, how); | |
1577 | if let Some(err) = Error::from_raw_error(rc) { | |
1578 | Err(err) | |
1579 | } else { | |
1580 | Ok(()) | |
1581 | } | |
1582 | } | |
1583 | ||
1584 | pub mod wasi_snapshot_preview1 { | |
1585 | use super::*; | |
1586 | #[link(wasm_import_module = "wasi_snapshot_preview1")] | |
1587 | extern "C" { | |
1588 | /// Read command-line argument data. | |
6a06907d | 1589 | /// The size of the array should match that returned by `wasi_args_sizes_get()` |
60c5eb7d XL |
1590 | pub fn args_get(argv: *mut *mut u8, argv_buf: *mut u8) -> Errno; |
1591 | /// Return command-line argument data sizes. | |
1592 | pub fn args_sizes_get(argc: *mut Size, argv_buf_size: *mut Size) -> Errno; | |
1593 | /// Read environment variable data. | |
6a06907d | 1594 | /// The sizes of the buffers should match that returned by `environ.sizes_get()`. |
60c5eb7d | 1595 | pub fn environ_get(environ: *mut *mut u8, environ_buf: *mut u8) -> Errno; |
6a06907d XL |
1596 | /// Return command-line argument data sizes. |
1597 | pub fn environ_sizes_get(argc: *mut Size, argv_buf_size: *mut Size) -> Errno; | |
60c5eb7d | 1598 | /// Return the resolution of a clock. |
6a06907d | 1599 | /// Implementations are required to provide a non-zero value for supported clocks. For unsupported clocks, return `WASI_EINVAL` |
60c5eb7d XL |
1600 | /// Note: This is similar to `clock_getres` in POSIX. |
1601 | pub fn clock_res_get(id: Clockid, resolution: *mut Timestamp) -> Errno; | |
1602 | /// Return the time value of a clock. | |
1603 | /// Note: This is similar to `clock_gettime` in POSIX. | |
1604 | pub fn clock_time_get(id: Clockid, precision: Timestamp, time: *mut Timestamp) -> Errno; | |
1605 | /// Provide file advisory information on a file descriptor. | |
1606 | /// Note: This is similar to `posix_fadvise` in POSIX. | |
1607 | pub fn fd_advise(fd: Fd, offset: Filesize, len: Filesize, advice: Advice) -> Errno; | |
1608 | /// Force the allocation of space in a file. | |
1609 | /// Note: This is similar to `posix_fallocate` in POSIX. | |
1610 | pub fn fd_allocate(fd: Fd, offset: Filesize, len: Filesize) -> Errno; | |
1611 | /// Close a file descriptor. | |
1612 | /// Note: This is similar to `close` in POSIX. | |
1613 | pub fn fd_close(fd: Fd) -> Errno; | |
1614 | /// Synchronize the data of a file to disk. | |
1615 | /// Note: This is similar to `fdatasync` in POSIX. | |
1616 | pub fn fd_datasync(fd: Fd) -> Errno; | |
1617 | /// Get the attributes of a file descriptor. | |
1618 | /// Note: This returns similar flags to `fsync(fd, F_GETFL)` in POSIX, as well as additional fields. | |
1619 | pub fn fd_fdstat_get(fd: Fd, stat: *mut Fdstat) -> Errno; | |
1620 | /// Adjust the flags associated with a file descriptor. | |
1621 | /// Note: This is similar to `fcntl(fd, F_SETFL, flags)` in POSIX. | |
1622 | pub fn fd_fdstat_set_flags(fd: Fd, flags: Fdflags) -> Errno; | |
1623 | /// Adjust the rights associated with a file descriptor. | |
6a06907d | 1624 | /// This can only be used to remove rights, and returns `ENOTCAPABLE` if called in a way that would attempt to add rights |
60c5eb7d XL |
1625 | pub fn fd_fdstat_set_rights( |
1626 | fd: Fd, | |
1627 | fs_rights_base: Rights, | |
1628 | fs_rights_inheriting: Rights, | |
1629 | ) -> Errno; | |
1630 | /// Return the attributes of an open file. | |
1631 | pub fn fd_filestat_get(fd: Fd, buf: *mut Filestat) -> Errno; | |
1632 | /// Adjust the size of an open file. If this increases the file's size, the extra bytes are filled with zeros. | |
1633 | /// Note: This is similar to `ftruncate` in POSIX. | |
1634 | pub fn fd_filestat_set_size(fd: Fd, size: Filesize) -> Errno; | |
1635 | /// Adjust the timestamps of an open file or directory. | |
1636 | /// Note: This is similar to `futimens` in POSIX. | |
1637 | pub fn fd_filestat_set_times( | |
1638 | fd: Fd, | |
1639 | atim: Timestamp, | |
1640 | mtim: Timestamp, | |
1641 | fst_flags: Fstflags, | |
1642 | ) -> Errno; | |
1643 | /// Read from a file descriptor, without using and updating the file descriptor's offset. | |
1644 | /// Note: This is similar to `preadv` in POSIX. | |
1645 | pub fn fd_pread( | |
1646 | fd: Fd, | |
1647 | iovs_ptr: *const Iovec, | |
1648 | iovs_len: usize, | |
1649 | offset: Filesize, | |
1650 | nread: *mut Size, | |
1651 | ) -> Errno; | |
1652 | /// Return a description of the given preopened file descriptor. | |
1653 | pub fn fd_prestat_get(fd: Fd, buf: *mut Prestat) -> Errno; | |
1654 | /// Return a description of the given preopened file descriptor. | |
1655 | pub fn fd_prestat_dir_name(fd: Fd, path: *mut u8, path_len: Size) -> Errno; | |
1656 | /// Write to a file descriptor, without using and updating the file descriptor's offset. | |
1657 | /// Note: This is similar to `pwritev` in POSIX. | |
1658 | pub fn fd_pwrite( | |
1659 | fd: Fd, | |
1660 | iovs_ptr: *const Ciovec, | |
1661 | iovs_len: usize, | |
1662 | offset: Filesize, | |
1663 | nwritten: *mut Size, | |
1664 | ) -> Errno; | |
1665 | /// Read from a file descriptor. | |
1666 | /// Note: This is similar to `readv` in POSIX. | |
1667 | pub fn fd_read(fd: Fd, iovs_ptr: *const Iovec, iovs_len: usize, nread: *mut Size) -> Errno; | |
1668 | /// Read directory entries from a directory. | |
1669 | /// When successful, the contents of the output buffer consist of a sequence of | |
1670 | /// directory entries. Each directory entry consists of a dirent_t object, | |
1671 | /// followed by dirent_t::d_namlen bytes holding the name of the directory | |
1672 | /// entry. | |
1673 | /// This function fills the output buffer as much as possible, potentially | |
1674 | /// truncating the last directory entry. This allows the caller to grow its | |
1675 | /// read buffer size in case it's too small to fit a single large directory | |
1676 | /// entry, or skip the oversized directory entry. | |
1677 | pub fn fd_readdir( | |
1678 | fd: Fd, | |
1679 | buf: *mut u8, | |
1680 | buf_len: Size, | |
1681 | cookie: Dircookie, | |
1682 | bufused: *mut Size, | |
1683 | ) -> Errno; | |
1684 | /// Atomically replace a file descriptor by renumbering another file descriptor. | |
1685 | /// Due to the strong focus on thread safety, this environment does not provide | |
1686 | /// a mechanism to duplicate or renumber a file descriptor to an arbitrary | |
1687 | /// number, like `dup2()`. This would be prone to race conditions, as an actual | |
1688 | /// file descriptor with the same number could be allocated by a different | |
1689 | /// thread at the same time. | |
1690 | /// This function provides a way to atomically renumber file descriptors, which | |
1691 | /// would disappear if `dup2()` were to be removed entirely. | |
1692 | pub fn fd_renumber(fd: Fd, to: Fd) -> Errno; | |
1693 | /// Move the offset of a file descriptor. | |
1694 | /// Note: This is similar to `lseek` in POSIX. | |
1695 | pub fn fd_seek( | |
1696 | fd: Fd, | |
1697 | offset: Filedelta, | |
1698 | whence: Whence, | |
1699 | newoffset: *mut Filesize, | |
1700 | ) -> Errno; | |
1701 | /// Synchronize the data and metadata of a file to disk. | |
1702 | /// Note: This is similar to `fsync` in POSIX. | |
1703 | pub fn fd_sync(fd: Fd) -> Errno; | |
1704 | /// Return the current offset of a file descriptor. | |
1705 | /// Note: This is similar to `lseek(fd, 0, SEEK_CUR)` in POSIX. | |
1706 | pub fn fd_tell(fd: Fd, offset: *mut Filesize) -> Errno; | |
1707 | /// Write to a file descriptor. | |
1708 | /// Note: This is similar to `writev` in POSIX. | |
1709 | pub fn fd_write( | |
1710 | fd: Fd, | |
1711 | iovs_ptr: *const Ciovec, | |
1712 | iovs_len: usize, | |
1713 | nwritten: *mut Size, | |
1714 | ) -> Errno; | |
1715 | /// Create a directory. | |
1716 | /// Note: This is similar to `mkdirat` in POSIX. | |
1717 | pub fn path_create_directory(fd: Fd, path_ptr: *const u8, path_len: usize) -> Errno; | |
1718 | /// Return the attributes of a file or directory. | |
1719 | /// Note: This is similar to `stat` in POSIX. | |
1720 | pub fn path_filestat_get( | |
1721 | fd: Fd, | |
1722 | flags: Lookupflags, | |
1723 | path_ptr: *const u8, | |
1724 | path_len: usize, | |
1725 | buf: *mut Filestat, | |
1726 | ) -> Errno; | |
1727 | /// Adjust the timestamps of a file or directory. | |
1728 | /// Note: This is similar to `utimensat` in POSIX. | |
1729 | pub fn path_filestat_set_times( | |
1730 | fd: Fd, | |
1731 | flags: Lookupflags, | |
1732 | path_ptr: *const u8, | |
1733 | path_len: usize, | |
1734 | atim: Timestamp, | |
1735 | mtim: Timestamp, | |
1736 | fst_flags: Fstflags, | |
1737 | ) -> Errno; | |
1738 | /// Create a hard link. | |
1739 | /// Note: This is similar to `linkat` in POSIX. | |
1740 | pub fn path_link( | |
1741 | old_fd: Fd, | |
1742 | old_flags: Lookupflags, | |
1743 | old_path_ptr: *const u8, | |
1744 | old_path_len: usize, | |
1745 | new_fd: Fd, | |
1746 | new_path_ptr: *const u8, | |
1747 | new_path_len: usize, | |
1748 | ) -> Errno; | |
1749 | /// Open a file or directory. | |
1750 | /// The returned file descriptor is not guaranteed to be the lowest-numbered | |
1751 | /// file descriptor not currently open; it is randomized to prevent | |
1752 | /// applications from depending on making assumptions about indexes, since this | |
1753 | /// is error-prone in multi-threaded contexts. The returned file descriptor is | |
1754 | /// guaranteed to be less than 2**31. | |
1755 | /// Note: This is similar to `openat` in POSIX. | |
1756 | pub fn path_open( | |
1757 | fd: Fd, | |
1758 | dirflags: Lookupflags, | |
1759 | path_ptr: *const u8, | |
1760 | path_len: usize, | |
1761 | oflags: Oflags, | |
1762 | fs_rights_base: Rights, | |
1763 | fs_rights_inherting: Rights, | |
1764 | fdflags: Fdflags, | |
1765 | opened_fd: *mut Fd, | |
1766 | ) -> Errno; | |
1767 | /// Read the contents of a symbolic link. | |
1768 | /// Note: This is similar to `readlinkat` in POSIX. | |
1769 | pub fn path_readlink( | |
1770 | fd: Fd, | |
1771 | path_ptr: *const u8, | |
1772 | path_len: usize, | |
1773 | buf: *mut u8, | |
1774 | buf_len: Size, | |
1775 | bufused: *mut Size, | |
1776 | ) -> Errno; | |
1777 | /// Remove a directory. | |
6a06907d | 1778 | /// Return `ENOTEMPTY` if the directory is not empty. |
60c5eb7d XL |
1779 | /// Note: This is similar to `unlinkat(fd, path, AT_REMOVEDIR)` in POSIX. |
1780 | pub fn path_remove_directory(fd: Fd, path_ptr: *const u8, path_len: usize) -> Errno; | |
1781 | /// Rename a file or directory. | |
1782 | /// Note: This is similar to `renameat` in POSIX. | |
1783 | pub fn path_rename( | |
1784 | fd: Fd, | |
1785 | old_path_ptr: *const u8, | |
1786 | old_path_len: usize, | |
1787 | new_fd: Fd, | |
1788 | new_path_ptr: *const u8, | |
1789 | new_path_len: usize, | |
1790 | ) -> Errno; | |
1791 | /// Create a symbolic link. | |
1792 | /// Note: This is similar to `symlinkat` in POSIX. | |
1793 | pub fn path_symlink( | |
1794 | old_path_ptr: *const u8, | |
1795 | old_path_len: usize, | |
1796 | fd: Fd, | |
1797 | new_path_ptr: *const u8, | |
1798 | new_path_len: usize, | |
1799 | ) -> Errno; | |
1800 | /// Unlink a file. | |
6a06907d | 1801 | /// Return `EISDIR` if the path refers to a directory. |
60c5eb7d XL |
1802 | /// Note: This is similar to `unlinkat(fd, path, 0)` in POSIX. |
1803 | pub fn path_unlink_file(fd: Fd, path_ptr: *const u8, path_len: usize) -> Errno; | |
1804 | /// Concurrently poll for the occurrence of a set of events. | |
1805 | pub fn poll_oneoff( | |
1806 | r#in: *const Subscription, | |
1807 | out: *mut Event, | |
1808 | nsubscriptions: Size, | |
1809 | nevents: *mut Size, | |
1810 | ) -> Errno; | |
1811 | /// Terminate the process normally. An exit code of 0 indicates successful | |
1812 | /// termination of the program. The meanings of other values is dependent on | |
1813 | /// the environment. | |
1814 | pub fn proc_exit(rval: Exitcode) -> !; | |
1815 | /// Send a signal to the process of the calling thread. | |
1816 | /// Note: This is similar to `raise` in POSIX. | |
1817 | pub fn proc_raise(sig: Signal) -> Errno; | |
1818 | /// Temporarily yield execution of the calling thread. | |
1819 | /// Note: This is similar to `sched_yield` in POSIX. | |
1820 | pub fn sched_yield() -> Errno; | |
1821 | /// Write high-quality random data into a buffer. | |
1822 | /// This function blocks when the implementation is unable to immediately | |
1823 | /// provide sufficient high-quality random data. | |
1824 | /// This function may execute slowly, so when large mounts of random data are | |
1825 | /// required, it's advisable to use this function to seed a pseudo-random | |
1826 | /// number generator, rather than to provide the random data directly. | |
1827 | pub fn random_get(buf: *mut u8, buf_len: Size) -> Errno; | |
1828 | /// Receive a message from a socket. | |
1829 | /// Note: This is similar to `recv` in POSIX, though it also supports reading | |
1830 | /// the data into multiple buffers in the manner of `readv`. | |
1831 | pub fn sock_recv( | |
1832 | fd: Fd, | |
1833 | ri_data_ptr: *const Iovec, | |
1834 | ri_data_len: usize, | |
1835 | ri_flags: Riflags, | |
1836 | ro_datalen: *mut Size, | |
1837 | ro_flags: *mut Roflags, | |
1838 | ) -> Errno; | |
1839 | /// Send a message on a socket. | |
1840 | /// Note: This is similar to `send` in POSIX, though it also supports writing | |
1841 | /// the data from multiple buffers in the manner of `writev`. | |
1842 | pub fn sock_send( | |
1843 | fd: Fd, | |
1844 | si_data_ptr: *const Ciovec, | |
1845 | si_data_len: usize, | |
1846 | si_flags: Siflags, | |
1847 | so_datalen: *mut Size, | |
1848 | ) -> Errno; | |
1849 | /// Shut down socket send and receive channels. | |
1850 | /// Note: This is similar to `shutdown` in POSIX. | |
1851 | pub fn sock_shutdown(fd: Fd, how: Sdflags) -> Errno; | |
1852 | } | |
1853 | } |