wasi/
lib_generated.rs

1// This file is automatically generated, DO NOT EDIT
2//
3// To regenerate this file run the `crates/witx-bindgen` command
4
5use core::fmt;
6use core::mem::MaybeUninit;
7pub type Size = usize;
8pub type Filesize = u64;
9pub type Timestamp = u64;
10#[repr(transparent)]
11#[derive(Copy, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
12pub struct Clockid(u32);
13/// The clock measuring real time. Time value zero corresponds with
14/// 1970-01-01T00:00:00Z.
15pub const CLOCKID_REALTIME: Clockid = 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.
20pub const CLOCKID_MONOTONIC: Clockid = Clockid(1);
21/// The CPU-time clock associated with the current process.
22pub const CLOCKID_PROCESS_CPUTIME_ID: Clockid = Clockid(2);
23/// The CPU-time clock associated with the current thread.
24pub const CLOCKID_THREAD_CPUTIME_ID: Clockid = Clockid(3);
25impl Clockid {
26    pub const fn raw(&self) -> u32 {
27        self.0
28    }
29
30    pub fn name(&self) -> &'static str {
31        match self.0 {
32            0 => "REALTIME",
33            1 => "MONOTONIC",
34            2 => "PROCESS_CPUTIME_ID",
35            3 => "THREAD_CPUTIME_ID",
36            _ => unsafe { core::hint::unreachable_unchecked() },
37        }
38    }
39    pub fn message(&self) -> &'static str {
40        match self.0 {
41            0 => {
42                "The clock measuring real time. Time value zero corresponds with
431970-01-01T00:00:00Z."
44            }
45            1 => {
46                "The store-wide monotonic clock, which is defined as a clock measuring
47real time, whose value cannot be adjusted and which cannot have negative
48clock jumps. The epoch of this clock is undefined. The absolute time
49value of this clock therefore has no meaning."
50            }
51            2 => "The CPU-time clock associated with the current process.",
52            3 => "The CPU-time clock associated with the current thread.",
53            _ => unsafe { core::hint::unreachable_unchecked() },
54        }
55    }
56}
57impl fmt::Debug for Clockid {
58    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59        f.debug_struct("Clockid")
60            .field("code", &self.0)
61            .field("name", &self.name())
62            .field("message", &self.message())
63            .finish()
64    }
65}
66
67#[repr(transparent)]
68#[derive(Copy, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
69pub struct Errno(u16);
70/// No error occurred. System call completed successfully.
71pub const ERRNO_SUCCESS: Errno = Errno(0);
72/// Argument list too long.
73pub const ERRNO_2BIG: Errno = Errno(1);
74/// Permission denied.
75pub const ERRNO_ACCES: Errno = Errno(2);
76/// Address in use.
77pub const ERRNO_ADDRINUSE: Errno = Errno(3);
78/// Address not available.
79pub const ERRNO_ADDRNOTAVAIL: Errno = Errno(4);
80/// Address family not supported.
81pub const ERRNO_AFNOSUPPORT: Errno = Errno(5);
82/// Resource unavailable, or operation would block.
83pub const ERRNO_AGAIN: Errno = Errno(6);
84/// Connection already in progress.
85pub const ERRNO_ALREADY: Errno = Errno(7);
86/// Bad file descriptor.
87pub const ERRNO_BADF: Errno = Errno(8);
88/// Bad message.
89pub const ERRNO_BADMSG: Errno = Errno(9);
90/// Device or resource busy.
91pub const ERRNO_BUSY: Errno = Errno(10);
92/// Operation canceled.
93pub const ERRNO_CANCELED: Errno = Errno(11);
94/// No child processes.
95pub const ERRNO_CHILD: Errno = Errno(12);
96/// Connection aborted.
97pub const ERRNO_CONNABORTED: Errno = Errno(13);
98/// Connection refused.
99pub const ERRNO_CONNREFUSED: Errno = Errno(14);
100/// Connection reset.
101pub const ERRNO_CONNRESET: Errno = Errno(15);
102/// Resource deadlock would occur.
103pub const ERRNO_DEADLK: Errno = Errno(16);
104/// Destination address required.
105pub const ERRNO_DESTADDRREQ: Errno = Errno(17);
106/// Mathematics argument out of domain of function.
107pub const ERRNO_DOM: Errno = Errno(18);
108/// Reserved.
109pub const ERRNO_DQUOT: Errno = Errno(19);
110/// File exists.
111pub const ERRNO_EXIST: Errno = Errno(20);
112/// Bad address.
113pub const ERRNO_FAULT: Errno = Errno(21);
114/// File too large.
115pub const ERRNO_FBIG: Errno = Errno(22);
116/// Host is unreachable.
117pub const ERRNO_HOSTUNREACH: Errno = Errno(23);
118/// Identifier removed.
119pub const ERRNO_IDRM: Errno = Errno(24);
120/// Illegal byte sequence.
121pub const ERRNO_ILSEQ: Errno = Errno(25);
122/// Operation in progress.
123pub const ERRNO_INPROGRESS: Errno = Errno(26);
124/// Interrupted function.
125pub const ERRNO_INTR: Errno = Errno(27);
126/// Invalid argument.
127pub const ERRNO_INVAL: Errno = Errno(28);
128/// I/O error.
129pub const ERRNO_IO: Errno = Errno(29);
130/// Socket is connected.
131pub const ERRNO_ISCONN: Errno = Errno(30);
132/// Is a directory.
133pub const ERRNO_ISDIR: Errno = Errno(31);
134/// Too many levels of symbolic links.
135pub const ERRNO_LOOP: Errno = Errno(32);
136/// File descriptor value too large.
137pub const ERRNO_MFILE: Errno = Errno(33);
138/// Too many links.
139pub const ERRNO_MLINK: Errno = Errno(34);
140/// Message too large.
141pub const ERRNO_MSGSIZE: Errno = Errno(35);
142/// Reserved.
143pub const ERRNO_MULTIHOP: Errno = Errno(36);
144/// Filename too long.
145pub const ERRNO_NAMETOOLONG: Errno = Errno(37);
146/// Network is down.
147pub const ERRNO_NETDOWN: Errno = Errno(38);
148/// Connection aborted by network.
149pub const ERRNO_NETRESET: Errno = Errno(39);
150/// Network unreachable.
151pub const ERRNO_NETUNREACH: Errno = Errno(40);
152/// Too many files open in system.
153pub const ERRNO_NFILE: Errno = Errno(41);
154/// No buffer space available.
155pub const ERRNO_NOBUFS: Errno = Errno(42);
156/// No such device.
157pub const ERRNO_NODEV: Errno = Errno(43);
158/// No such file or directory.
159pub const ERRNO_NOENT: Errno = Errno(44);
160/// Executable file format error.
161pub const ERRNO_NOEXEC: Errno = Errno(45);
162/// No locks available.
163pub const ERRNO_NOLCK: Errno = Errno(46);
164/// Reserved.
165pub const ERRNO_NOLINK: Errno = Errno(47);
166/// Not enough space.
167pub const ERRNO_NOMEM: Errno = Errno(48);
168/// No message of the desired type.
169pub const ERRNO_NOMSG: Errno = Errno(49);
170/// Protocol not available.
171pub const ERRNO_NOPROTOOPT: Errno = Errno(50);
172/// No space left on device.
173pub const ERRNO_NOSPC: Errno = Errno(51);
174/// Function not supported.
175pub const ERRNO_NOSYS: Errno = Errno(52);
176/// The socket is not connected.
177pub const ERRNO_NOTCONN: Errno = Errno(53);
178/// Not a directory or a symbolic link to a directory.
179pub const ERRNO_NOTDIR: Errno = Errno(54);
180/// Directory not empty.
181pub const ERRNO_NOTEMPTY: Errno = Errno(55);
182/// State not recoverable.
183pub const ERRNO_NOTRECOVERABLE: Errno = Errno(56);
184/// Not a socket.
185pub const ERRNO_NOTSOCK: Errno = Errno(57);
186/// Not supported, or operation not supported on socket.
187pub const ERRNO_NOTSUP: Errno = Errno(58);
188/// Inappropriate I/O control operation.
189pub const ERRNO_NOTTY: Errno = Errno(59);
190/// No such device or address.
191pub const ERRNO_NXIO: Errno = Errno(60);
192/// Value too large to be stored in data type.
193pub const ERRNO_OVERFLOW: Errno = Errno(61);
194/// Previous owner died.
195pub const ERRNO_OWNERDEAD: Errno = Errno(62);
196/// Operation not permitted.
197pub const ERRNO_PERM: Errno = Errno(63);
198/// Broken pipe.
199pub const ERRNO_PIPE: Errno = Errno(64);
200/// Protocol error.
201pub const ERRNO_PROTO: Errno = Errno(65);
202/// Protocol not supported.
203pub const ERRNO_PROTONOSUPPORT: Errno = Errno(66);
204/// Protocol wrong type for socket.
205pub const ERRNO_PROTOTYPE: Errno = Errno(67);
206/// Result too large.
207pub const ERRNO_RANGE: Errno = Errno(68);
208/// Read-only file system.
209pub const ERRNO_ROFS: Errno = Errno(69);
210/// Invalid seek.
211pub const ERRNO_SPIPE: Errno = Errno(70);
212/// No such process.
213pub const ERRNO_SRCH: Errno = Errno(71);
214/// Reserved.
215pub const ERRNO_STALE: Errno = Errno(72);
216/// Connection timed out.
217pub const ERRNO_TIMEDOUT: Errno = Errno(73);
218/// Text file busy.
219pub const ERRNO_TXTBSY: Errno = Errno(74);
220/// Cross-device link.
221pub const ERRNO_XDEV: Errno = Errno(75);
222/// Extension: Capabilities insufficient.
223pub const ERRNO_NOTCAPABLE: Errno = Errno(76);
224impl Errno {
225    pub const fn raw(&self) -> u16 {
226        self.0
227    }
228
229    pub fn name(&self) -> &'static str {
230        match self.0 {
231            0 => "SUCCESS",
232            1 => "2BIG",
233            2 => "ACCES",
234            3 => "ADDRINUSE",
235            4 => "ADDRNOTAVAIL",
236            5 => "AFNOSUPPORT",
237            6 => "AGAIN",
238            7 => "ALREADY",
239            8 => "BADF",
240            9 => "BADMSG",
241            10 => "BUSY",
242            11 => "CANCELED",
243            12 => "CHILD",
244            13 => "CONNABORTED",
245            14 => "CONNREFUSED",
246            15 => "CONNRESET",
247            16 => "DEADLK",
248            17 => "DESTADDRREQ",
249            18 => "DOM",
250            19 => "DQUOT",
251            20 => "EXIST",
252            21 => "FAULT",
253            22 => "FBIG",
254            23 => "HOSTUNREACH",
255            24 => "IDRM",
256            25 => "ILSEQ",
257            26 => "INPROGRESS",
258            27 => "INTR",
259            28 => "INVAL",
260            29 => "IO",
261            30 => "ISCONN",
262            31 => "ISDIR",
263            32 => "LOOP",
264            33 => "MFILE",
265            34 => "MLINK",
266            35 => "MSGSIZE",
267            36 => "MULTIHOP",
268            37 => "NAMETOOLONG",
269            38 => "NETDOWN",
270            39 => "NETRESET",
271            40 => "NETUNREACH",
272            41 => "NFILE",
273            42 => "NOBUFS",
274            43 => "NODEV",
275            44 => "NOENT",
276            45 => "NOEXEC",
277            46 => "NOLCK",
278            47 => "NOLINK",
279            48 => "NOMEM",
280            49 => "NOMSG",
281            50 => "NOPROTOOPT",
282            51 => "NOSPC",
283            52 => "NOSYS",
284            53 => "NOTCONN",
285            54 => "NOTDIR",
286            55 => "NOTEMPTY",
287            56 => "NOTRECOVERABLE",
288            57 => "NOTSOCK",
289            58 => "NOTSUP",
290            59 => "NOTTY",
291            60 => "NXIO",
292            61 => "OVERFLOW",
293            62 => "OWNERDEAD",
294            63 => "PERM",
295            64 => "PIPE",
296            65 => "PROTO",
297            66 => "PROTONOSUPPORT",
298            67 => "PROTOTYPE",
299            68 => "RANGE",
300            69 => "ROFS",
301            70 => "SPIPE",
302            71 => "SRCH",
303            72 => "STALE",
304            73 => "TIMEDOUT",
305            74 => "TXTBSY",
306            75 => "XDEV",
307            76 => "NOTCAPABLE",
308            _ => unsafe { core::hint::unreachable_unchecked() },
309        }
310    }
311    pub fn message(&self) -> &'static str {
312        match self.0 {
313            0 => "No error occurred. System call completed successfully.",
314            1 => "Argument list too long.",
315            2 => "Permission denied.",
316            3 => "Address in use.",
317            4 => "Address not available.",
318            5 => "Address family not supported.",
319            6 => "Resource unavailable, or operation would block.",
320            7 => "Connection already in progress.",
321            8 => "Bad file descriptor.",
322            9 => "Bad message.",
323            10 => "Device or resource busy.",
324            11 => "Operation canceled.",
325            12 => "No child processes.",
326            13 => "Connection aborted.",
327            14 => "Connection refused.",
328            15 => "Connection reset.",
329            16 => "Resource deadlock would occur.",
330            17 => "Destination address required.",
331            18 => "Mathematics argument out of domain of function.",
332            19 => "Reserved.",
333            20 => "File exists.",
334            21 => "Bad address.",
335            22 => "File too large.",
336            23 => "Host is unreachable.",
337            24 => "Identifier removed.",
338            25 => "Illegal byte sequence.",
339            26 => "Operation in progress.",
340            27 => "Interrupted function.",
341            28 => "Invalid argument.",
342            29 => "I/O error.",
343            30 => "Socket is connected.",
344            31 => "Is a directory.",
345            32 => "Too many levels of symbolic links.",
346            33 => "File descriptor value too large.",
347            34 => "Too many links.",
348            35 => "Message too large.",
349            36 => "Reserved.",
350            37 => "Filename too long.",
351            38 => "Network is down.",
352            39 => "Connection aborted by network.",
353            40 => "Network unreachable.",
354            41 => "Too many files open in system.",
355            42 => "No buffer space available.",
356            43 => "No such device.",
357            44 => "No such file or directory.",
358            45 => "Executable file format error.",
359            46 => "No locks available.",
360            47 => "Reserved.",
361            48 => "Not enough space.",
362            49 => "No message of the desired type.",
363            50 => "Protocol not available.",
364            51 => "No space left on device.",
365            52 => "Function not supported.",
366            53 => "The socket is not connected.",
367            54 => "Not a directory or a symbolic link to a directory.",
368            55 => "Directory not empty.",
369            56 => "State not recoverable.",
370            57 => "Not a socket.",
371            58 => "Not supported, or operation not supported on socket.",
372            59 => "Inappropriate I/O control operation.",
373            60 => "No such device or address.",
374            61 => "Value too large to be stored in data type.",
375            62 => "Previous owner died.",
376            63 => "Operation not permitted.",
377            64 => "Broken pipe.",
378            65 => "Protocol error.",
379            66 => "Protocol not supported.",
380            67 => "Protocol wrong type for socket.",
381            68 => "Result too large.",
382            69 => "Read-only file system.",
383            70 => "Invalid seek.",
384            71 => "No such process.",
385            72 => "Reserved.",
386            73 => "Connection timed out.",
387            74 => "Text file busy.",
388            75 => "Cross-device link.",
389            76 => "Extension: Capabilities insufficient.",
390            _ => unsafe { core::hint::unreachable_unchecked() },
391        }
392    }
393}
394impl fmt::Debug for Errno {
395    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
396        f.debug_struct("Errno")
397            .field("code", &self.0)
398            .field("name", &self.name())
399            .field("message", &self.message())
400            .finish()
401    }
402}
403impl fmt::Display for Errno {
404    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
405        write!(f, "{} (error {})", self.name(), self.0)
406    }
407}
408
409#[cfg(feature = "std")]
410extern crate std;
411#[cfg(feature = "std")]
412impl std::error::Error for Errno {}
413
414pub type Rights = u64;
415/// The right to invoke `fd_datasync`.
416/// If `path_open` is set, includes the right to invoke
417/// `path_open` with `fdflags::dsync`.
418pub const RIGHTS_FD_DATASYNC: Rights = 1 << 0;
419/// The right to invoke `fd_read` and `sock_recv`.
420/// If `rights::fd_seek` is set, includes the right to invoke `fd_pread`.
421pub const RIGHTS_FD_READ: Rights = 1 << 1;
422/// The right to invoke `fd_seek`. This flag implies `rights::fd_tell`.
423pub const RIGHTS_FD_SEEK: Rights = 1 << 2;
424/// The right to invoke `fd_fdstat_set_flags`.
425pub const RIGHTS_FD_FDSTAT_SET_FLAGS: Rights = 1 << 3;
426/// The right to invoke `fd_sync`.
427/// If `path_open` is set, includes the right to invoke
428/// `path_open` with `fdflags::rsync` and `fdflags::dsync`.
429pub const RIGHTS_FD_SYNC: Rights = 1 << 4;
430/// The right to invoke `fd_seek` in such a way that the file offset
431/// remains unaltered (i.e., `whence::cur` with offset zero), or to
432/// invoke `fd_tell`.
433pub const RIGHTS_FD_TELL: Rights = 1 << 5;
434/// The right to invoke `fd_write` and `sock_send`.
435/// If `rights::fd_seek` is set, includes the right to invoke `fd_pwrite`.
436pub const RIGHTS_FD_WRITE: Rights = 1 << 6;
437/// The right to invoke `fd_advise`.
438pub const RIGHTS_FD_ADVISE: Rights = 1 << 7;
439/// The right to invoke `fd_allocate`.
440pub const RIGHTS_FD_ALLOCATE: Rights = 1 << 8;
441/// The right to invoke `path_create_directory`.
442pub const RIGHTS_PATH_CREATE_DIRECTORY: Rights = 1 << 9;
443/// If `path_open` is set, the right to invoke `path_open` with `oflags::creat`.
444pub const RIGHTS_PATH_CREATE_FILE: Rights = 1 << 10;
445/// The right to invoke `path_link` with the file descriptor as the
446/// source directory.
447pub const RIGHTS_PATH_LINK_SOURCE: Rights = 1 << 11;
448/// The right to invoke `path_link` with the file descriptor as the
449/// target directory.
450pub const RIGHTS_PATH_LINK_TARGET: Rights = 1 << 12;
451/// The right to invoke `path_open`.
452pub const RIGHTS_PATH_OPEN: Rights = 1 << 13;
453/// The right to invoke `fd_readdir`.
454pub const RIGHTS_FD_READDIR: Rights = 1 << 14;
455/// The right to invoke `path_readlink`.
456pub const RIGHTS_PATH_READLINK: Rights = 1 << 15;
457/// The right to invoke `path_rename` with the file descriptor as the source directory.
458pub const RIGHTS_PATH_RENAME_SOURCE: Rights = 1 << 16;
459/// The right to invoke `path_rename` with the file descriptor as the target directory.
460pub const RIGHTS_PATH_RENAME_TARGET: Rights = 1 << 17;
461/// The right to invoke `path_filestat_get`.
462pub const RIGHTS_PATH_FILESTAT_GET: Rights = 1 << 18;
463/// The right to change a file's size (there is no `path_filestat_set_size`).
464/// If `path_open` is set, includes the right to invoke `path_open` with `oflags::trunc`.
465pub const RIGHTS_PATH_FILESTAT_SET_SIZE: Rights = 1 << 19;
466/// The right to invoke `path_filestat_set_times`.
467pub const RIGHTS_PATH_FILESTAT_SET_TIMES: Rights = 1 << 20;
468/// The right to invoke `fd_filestat_get`.
469pub const RIGHTS_FD_FILESTAT_GET: Rights = 1 << 21;
470/// The right to invoke `fd_filestat_set_size`.
471pub const RIGHTS_FD_FILESTAT_SET_SIZE: Rights = 1 << 22;
472/// The right to invoke `fd_filestat_set_times`.
473pub const RIGHTS_FD_FILESTAT_SET_TIMES: Rights = 1 << 23;
474/// The right to invoke `path_symlink`.
475pub const RIGHTS_PATH_SYMLINK: Rights = 1 << 24;
476/// The right to invoke `path_remove_directory`.
477pub const RIGHTS_PATH_REMOVE_DIRECTORY: Rights = 1 << 25;
478/// The right to invoke `path_unlink_file`.
479pub const RIGHTS_PATH_UNLINK_FILE: Rights = 1 << 26;
480/// If `rights::fd_read` is set, includes the right to invoke `poll_oneoff` to subscribe to `eventtype::fd_read`.
481/// If `rights::fd_write` is set, includes the right to invoke `poll_oneoff` to subscribe to `eventtype::fd_write`.
482pub const RIGHTS_POLL_FD_READWRITE: Rights = 1 << 27;
483/// The right to invoke `sock_shutdown`.
484pub const RIGHTS_SOCK_SHUTDOWN: Rights = 1 << 28;
485/// The right to invoke `sock_accept`.
486pub const RIGHTS_SOCK_ACCEPT: Rights = 1 << 29;
487
488pub type Fd = u32;
489#[repr(C)]
490#[derive(Copy, Clone, Debug)]
491pub struct Iovec {
492    /// The address of the buffer to be filled.
493    pub buf: *mut u8,
494    /// The length of the buffer to be filled.
495    pub buf_len: Size,
496}
497#[repr(C)]
498#[derive(Copy, Clone, Debug)]
499pub struct Ciovec {
500    /// The address of the buffer to be written.
501    pub buf: *const u8,
502    /// The length of the buffer to be written.
503    pub buf_len: Size,
504}
505pub type IovecArray<'a> = &'a [Iovec];
506pub type CiovecArray<'a> = &'a [Ciovec];
507pub type Filedelta = i64;
508#[repr(transparent)]
509#[derive(Copy, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
510pub struct Whence(u8);
511/// Seek relative to start-of-file.
512pub const WHENCE_SET: Whence = Whence(0);
513/// Seek relative to current position.
514pub const WHENCE_CUR: Whence = Whence(1);
515/// Seek relative to end-of-file.
516pub const WHENCE_END: Whence = Whence(2);
517impl Whence {
518    pub const fn raw(&self) -> u8 {
519        self.0
520    }
521
522    pub fn name(&self) -> &'static str {
523        match self.0 {
524            0 => "SET",
525            1 => "CUR",
526            2 => "END",
527            _ => unsafe { core::hint::unreachable_unchecked() },
528        }
529    }
530    pub fn message(&self) -> &'static str {
531        match self.0 {
532            0 => "Seek relative to start-of-file.",
533            1 => "Seek relative to current position.",
534            2 => "Seek relative to end-of-file.",
535            _ => unsafe { core::hint::unreachable_unchecked() },
536        }
537    }
538}
539impl fmt::Debug for Whence {
540    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
541        f.debug_struct("Whence")
542            .field("code", &self.0)
543            .field("name", &self.name())
544            .field("message", &self.message())
545            .finish()
546    }
547}
548
549pub type Dircookie = u64;
550pub type Dirnamlen = u32;
551pub type Inode = u64;
552#[repr(transparent)]
553#[derive(Copy, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
554pub struct Filetype(u8);
555/// The type of the file descriptor or file is unknown or is different from any of the other types specified.
556pub const FILETYPE_UNKNOWN: Filetype = Filetype(0);
557/// The file descriptor or file refers to a block device inode.
558pub const FILETYPE_BLOCK_DEVICE: Filetype = Filetype(1);
559/// The file descriptor or file refers to a character device inode.
560pub const FILETYPE_CHARACTER_DEVICE: Filetype = Filetype(2);
561/// The file descriptor or file refers to a directory inode.
562pub const FILETYPE_DIRECTORY: Filetype = Filetype(3);
563/// The file descriptor or file refers to a regular file inode.
564pub const FILETYPE_REGULAR_FILE: Filetype = Filetype(4);
565/// The file descriptor or file refers to a datagram socket.
566pub const FILETYPE_SOCKET_DGRAM: Filetype = Filetype(5);
567/// The file descriptor or file refers to a byte-stream socket.
568pub const FILETYPE_SOCKET_STREAM: Filetype = Filetype(6);
569/// The file refers to a symbolic link inode.
570pub const FILETYPE_SYMBOLIC_LINK: Filetype = Filetype(7);
571impl Filetype {
572    pub const fn raw(&self) -> u8 {
573        self.0
574    }
575
576    pub fn name(&self) -> &'static str {
577        match self.0 {
578            0 => "UNKNOWN",
579            1 => "BLOCK_DEVICE",
580            2 => "CHARACTER_DEVICE",
581            3 => "DIRECTORY",
582            4 => "REGULAR_FILE",
583            5 => "SOCKET_DGRAM",
584            6 => "SOCKET_STREAM",
585            7 => "SYMBOLIC_LINK",
586            _ => unsafe { core::hint::unreachable_unchecked() },
587        }
588    }
589    pub fn message(&self) -> &'static str {
590        match self.0 {0 => "The type of the file descriptor or file is unknown or is different from any of the other types specified.",1 => "The file descriptor or file refers to a block device inode.",2 => "The file descriptor or file refers to a character device inode.",3 => "The file descriptor or file refers to a directory inode.",4 => "The file descriptor or file refers to a regular file inode.",5 => "The file descriptor or file refers to a datagram socket.",6 => "The file descriptor or file refers to a byte-stream socket.",7 => "The file refers to a symbolic link inode.",_ => unsafe { core::hint::unreachable_unchecked() },}
591    }
592}
593impl fmt::Debug for Filetype {
594    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
595        f.debug_struct("Filetype")
596            .field("code", &self.0)
597            .field("name", &self.name())
598            .field("message", &self.message())
599            .finish()
600    }
601}
602
603#[repr(C)]
604#[derive(Copy, Clone, Debug)]
605pub struct Dirent {
606    /// The offset of the next directory entry stored in this directory.
607    pub d_next: Dircookie,
608    /// The serial number of the file referred to by this directory entry.
609    pub d_ino: Inode,
610    /// The length of the name of the directory entry.
611    pub d_namlen: Dirnamlen,
612    /// The type of the file referred to by this directory entry.
613    pub d_type: Filetype,
614}
615#[repr(transparent)]
616#[derive(Copy, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
617pub struct Advice(u8);
618/// The application has no advice to give on its behavior with respect to the specified data.
619pub const ADVICE_NORMAL: Advice = Advice(0);
620/// The application expects to access the specified data sequentially from lower offsets to higher offsets.
621pub const ADVICE_SEQUENTIAL: Advice = Advice(1);
622/// The application expects to access the specified data in a random order.
623pub const ADVICE_RANDOM: Advice = Advice(2);
624/// The application expects to access the specified data in the near future.
625pub const ADVICE_WILLNEED: Advice = Advice(3);
626/// The application expects that it will not access the specified data in the near future.
627pub const ADVICE_DONTNEED: Advice = Advice(4);
628/// The application expects to access the specified data once and then not reuse it thereafter.
629pub const ADVICE_NOREUSE: Advice = Advice(5);
630impl Advice {
631    pub const fn raw(&self) -> u8 {
632        self.0
633    }
634
635    pub fn name(&self) -> &'static str {
636        match self.0 {
637            0 => "NORMAL",
638            1 => "SEQUENTIAL",
639            2 => "RANDOM",
640            3 => "WILLNEED",
641            4 => "DONTNEED",
642            5 => "NOREUSE",
643            _ => unsafe { core::hint::unreachable_unchecked() },
644        }
645    }
646    pub fn message(&self) -> &'static str {
647        match self.0 {0 => "The application has no advice to give on its behavior with respect to the specified data.",1 => "The application expects to access the specified data sequentially from lower offsets to higher offsets.",2 => "The application expects to access the specified data in a random order.",3 => "The application expects to access the specified data in the near future.",4 => "The application expects that it will not access the specified data in the near future.",5 => "The application expects to access the specified data once and then not reuse it thereafter.",_ => unsafe { core::hint::unreachable_unchecked() },}
648    }
649}
650impl fmt::Debug for Advice {
651    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
652        f.debug_struct("Advice")
653            .field("code", &self.0)
654            .field("name", &self.name())
655            .field("message", &self.message())
656            .finish()
657    }
658}
659
660pub type Fdflags = u16;
661/// Append mode: Data written to the file is always appended to the file's end.
662pub const FDFLAGS_APPEND: Fdflags = 1 << 0;
663/// Write according to synchronized I/O data integrity completion. Only the data stored in the file is synchronized.
664pub const FDFLAGS_DSYNC: Fdflags = 1 << 1;
665/// Non-blocking mode.
666pub const FDFLAGS_NONBLOCK: Fdflags = 1 << 2;
667/// Synchronized read I/O operations.
668pub const FDFLAGS_RSYNC: Fdflags = 1 << 3;
669/// Write according to synchronized I/O file integrity completion. In
670/// addition to synchronizing the data stored in the file, the implementation
671/// may also synchronously update the file's metadata.
672pub const FDFLAGS_SYNC: Fdflags = 1 << 4;
673
674#[repr(C)]
675#[derive(Copy, Clone, Debug)]
676pub struct Fdstat {
677    /// File type.
678    pub fs_filetype: Filetype,
679    /// File descriptor flags.
680    pub fs_flags: Fdflags,
681    /// Rights that apply to this file descriptor.
682    pub fs_rights_base: Rights,
683    /// Maximum set of rights that may be installed on new file descriptors that
684    /// are created through this file descriptor, e.g., through `path_open`.
685    pub fs_rights_inheriting: Rights,
686}
687pub type Device = u64;
688pub type Fstflags = u16;
689/// Adjust the last data access timestamp to the value stored in `filestat::atim`.
690pub const FSTFLAGS_ATIM: Fstflags = 1 << 0;
691/// Adjust the last data access timestamp to the time of clock `clockid::realtime`.
692pub const FSTFLAGS_ATIM_NOW: Fstflags = 1 << 1;
693/// Adjust the last data modification timestamp to the value stored in `filestat::mtim`.
694pub const FSTFLAGS_MTIM: Fstflags = 1 << 2;
695/// Adjust the last data modification timestamp to the time of clock `clockid::realtime`.
696pub const FSTFLAGS_MTIM_NOW: Fstflags = 1 << 3;
697
698pub type Lookupflags = u32;
699/// As long as the resolved path corresponds to a symbolic link, it is expanded.
700pub const LOOKUPFLAGS_SYMLINK_FOLLOW: Lookupflags = 1 << 0;
701
702pub type Oflags = u16;
703/// Create file if it does not exist.
704pub const OFLAGS_CREAT: Oflags = 1 << 0;
705/// Fail if not a directory.
706pub const OFLAGS_DIRECTORY: Oflags = 1 << 1;
707/// Fail if file already exists.
708pub const OFLAGS_EXCL: Oflags = 1 << 2;
709/// Truncate file to size 0.
710pub const OFLAGS_TRUNC: Oflags = 1 << 3;
711
712pub type Linkcount = u64;
713#[repr(C)]
714#[derive(Copy, Clone, Debug)]
715pub struct Filestat {
716    /// Device ID of device containing the file.
717    pub dev: Device,
718    /// File serial number.
719    pub ino: Inode,
720    /// File type.
721    pub filetype: Filetype,
722    /// Number of hard links to the file.
723    pub nlink: Linkcount,
724    /// For regular files, the file size in bytes. For symbolic links, the length in bytes of the pathname contained in the symbolic link.
725    pub size: Filesize,
726    /// Last data access timestamp.
727    pub atim: Timestamp,
728    /// Last data modification timestamp.
729    pub mtim: Timestamp,
730    /// Last file status change timestamp.
731    pub ctim: Timestamp,
732}
733pub type Userdata = u64;
734#[repr(transparent)]
735#[derive(Copy, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
736pub struct Eventtype(u8);
737/// The time value of clock `subscription_clock::id` has
738/// reached timestamp `subscription_clock::timeout`.
739pub const EVENTTYPE_CLOCK: Eventtype = Eventtype(0);
740/// File descriptor `subscription_fd_readwrite::file_descriptor` has data
741/// available for reading. This event always triggers for regular files.
742pub const EVENTTYPE_FD_READ: Eventtype = Eventtype(1);
743/// File descriptor `subscription_fd_readwrite::file_descriptor` has capacity
744/// available for writing. This event always triggers for regular files.
745pub const EVENTTYPE_FD_WRITE: Eventtype = Eventtype(2);
746impl Eventtype {
747    pub const fn raw(&self) -> u8 {
748        self.0
749    }
750
751    pub fn name(&self) -> &'static str {
752        match self.0 {
753            0 => "CLOCK",
754            1 => "FD_READ",
755            2 => "FD_WRITE",
756            _ => unsafe { core::hint::unreachable_unchecked() },
757        }
758    }
759    pub fn message(&self) -> &'static str {
760        match self.0 {
761            0 => {
762                "The time value of clock `subscription_clock::id` has
763reached timestamp `subscription_clock::timeout`."
764            }
765            1 => {
766                "File descriptor `subscription_fd_readwrite::file_descriptor` has data
767available for reading. This event always triggers for regular files."
768            }
769            2 => {
770                "File descriptor `subscription_fd_readwrite::file_descriptor` has capacity
771available for writing. This event always triggers for regular files."
772            }
773            _ => unsafe { core::hint::unreachable_unchecked() },
774        }
775    }
776}
777impl fmt::Debug for Eventtype {
778    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
779        f.debug_struct("Eventtype")
780            .field("code", &self.0)
781            .field("name", &self.name())
782            .field("message", &self.message())
783            .finish()
784    }
785}
786
787pub type Eventrwflags = u16;
788/// The peer of this socket has closed or disconnected.
789pub const EVENTRWFLAGS_FD_READWRITE_HANGUP: Eventrwflags = 1 << 0;
790
791#[repr(C)]
792#[derive(Copy, Clone, Debug)]
793pub struct EventFdReadwrite {
794    /// The number of bytes available for reading or writing.
795    pub nbytes: Filesize,
796    /// The state of the file descriptor.
797    pub flags: Eventrwflags,
798}
799#[repr(C)]
800#[derive(Copy, Clone, Debug)]
801pub struct Event {
802    /// User-provided value that got attached to `subscription::userdata`.
803    pub userdata: Userdata,
804    /// If non-zero, an error that occurred while processing the subscription request.
805    pub error: Errno,
806    /// The type of event that occured
807    pub type_: Eventtype,
808    /// The contents of the event, if it is an `eventtype::fd_read` or
809    /// `eventtype::fd_write`. `eventtype::clock` events ignore this field.
810    pub fd_readwrite: EventFdReadwrite,
811}
812pub type Subclockflags = u16;
813/// If set, treat the timestamp provided in
814/// `subscription_clock::timeout` as an absolute timestamp of clock
815/// `subscription_clock::id`. If clear, treat the timestamp
816/// provided in `subscription_clock::timeout` relative to the
817/// current time value of clock `subscription_clock::id`.
818pub const SUBCLOCKFLAGS_SUBSCRIPTION_CLOCK_ABSTIME: Subclockflags = 1 << 0;
819
820#[repr(C)]
821#[derive(Copy, Clone, Debug)]
822pub struct SubscriptionClock {
823    /// The clock against which to compare the timestamp.
824    pub id: Clockid,
825    /// The absolute or relative timestamp.
826    pub timeout: Timestamp,
827    /// The amount of time that the implementation may wait additionally
828    /// to coalesce with other events.
829    pub precision: Timestamp,
830    /// Flags specifying whether the timeout is absolute or relative
831    pub flags: Subclockflags,
832}
833#[repr(C)]
834#[derive(Copy, Clone, Debug)]
835pub struct SubscriptionFdReadwrite {
836    /// The file descriptor on which to wait for it to become ready for reading or writing.
837    pub file_descriptor: Fd,
838}
839#[repr(C)]
840#[derive(Copy, Clone)]
841pub union SubscriptionUU {
842    pub clock: SubscriptionClock,
843    pub fd_read: SubscriptionFdReadwrite,
844    pub fd_write: SubscriptionFdReadwrite,
845}
846#[repr(C)]
847#[derive(Copy, Clone)]
848pub struct SubscriptionU {
849    pub tag: u8,
850    pub u: SubscriptionUU,
851}
852
853#[repr(C)]
854#[derive(Copy, Clone)]
855pub struct Subscription {
856    /// User-provided value that is attached to the subscription in the
857    /// implementation and returned through `event::userdata`.
858    pub userdata: Userdata,
859    /// The type of the event to which to subscribe, and its contents
860    pub u: SubscriptionU,
861}
862pub type Exitcode = u32;
863#[repr(transparent)]
864#[derive(Copy, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
865pub struct Signal(u8);
866/// No signal. Note that POSIX has special semantics for `kill(pid, 0)`,
867/// so this value is reserved.
868pub const SIGNAL_NONE: Signal = Signal(0);
869/// Hangup.
870/// Action: Terminates the process.
871pub const SIGNAL_HUP: Signal = Signal(1);
872/// Terminate interrupt signal.
873/// Action: Terminates the process.
874pub const SIGNAL_INT: Signal = Signal(2);
875/// Terminal quit signal.
876/// Action: Terminates the process.
877pub const SIGNAL_QUIT: Signal = Signal(3);
878/// Illegal instruction.
879/// Action: Terminates the process.
880pub const SIGNAL_ILL: Signal = Signal(4);
881/// Trace/breakpoint trap.
882/// Action: Terminates the process.
883pub const SIGNAL_TRAP: Signal = Signal(5);
884/// Process abort signal.
885/// Action: Terminates the process.
886pub const SIGNAL_ABRT: Signal = Signal(6);
887/// Access to an undefined portion of a memory object.
888/// Action: Terminates the process.
889pub const SIGNAL_BUS: Signal = Signal(7);
890/// Erroneous arithmetic operation.
891/// Action: Terminates the process.
892pub const SIGNAL_FPE: Signal = Signal(8);
893/// Kill.
894/// Action: Terminates the process.
895pub const SIGNAL_KILL: Signal = Signal(9);
896/// User-defined signal 1.
897/// Action: Terminates the process.
898pub const SIGNAL_USR1: Signal = Signal(10);
899/// Invalid memory reference.
900/// Action: Terminates the process.
901pub const SIGNAL_SEGV: Signal = Signal(11);
902/// User-defined signal 2.
903/// Action: Terminates the process.
904pub const SIGNAL_USR2: Signal = Signal(12);
905/// Write on a pipe with no one to read it.
906/// Action: Ignored.
907pub const SIGNAL_PIPE: Signal = Signal(13);
908/// Alarm clock.
909/// Action: Terminates the process.
910pub const SIGNAL_ALRM: Signal = Signal(14);
911/// Termination signal.
912/// Action: Terminates the process.
913pub const SIGNAL_TERM: Signal = Signal(15);
914/// Child process terminated, stopped, or continued.
915/// Action: Ignored.
916pub const SIGNAL_CHLD: Signal = Signal(16);
917/// Continue executing, if stopped.
918/// Action: Continues executing, if stopped.
919pub const SIGNAL_CONT: Signal = Signal(17);
920/// Stop executing.
921/// Action: Stops executing.
922pub const SIGNAL_STOP: Signal = Signal(18);
923/// Terminal stop signal.
924/// Action: Stops executing.
925pub const SIGNAL_TSTP: Signal = Signal(19);
926/// Background process attempting read.
927/// Action: Stops executing.
928pub const SIGNAL_TTIN: Signal = Signal(20);
929/// Background process attempting write.
930/// Action: Stops executing.
931pub const SIGNAL_TTOU: Signal = Signal(21);
932/// High bandwidth data is available at a socket.
933/// Action: Ignored.
934pub const SIGNAL_URG: Signal = Signal(22);
935/// CPU time limit exceeded.
936/// Action: Terminates the process.
937pub const SIGNAL_XCPU: Signal = Signal(23);
938/// File size limit exceeded.
939/// Action: Terminates the process.
940pub const SIGNAL_XFSZ: Signal = Signal(24);
941/// Virtual timer expired.
942/// Action: Terminates the process.
943pub const SIGNAL_VTALRM: Signal = Signal(25);
944/// Profiling timer expired.
945/// Action: Terminates the process.
946pub const SIGNAL_PROF: Signal = Signal(26);
947/// Window changed.
948/// Action: Ignored.
949pub const SIGNAL_WINCH: Signal = Signal(27);
950/// I/O possible.
951/// Action: Terminates the process.
952pub const SIGNAL_POLL: Signal = Signal(28);
953/// Power failure.
954/// Action: Terminates the process.
955pub const SIGNAL_PWR: Signal = Signal(29);
956/// Bad system call.
957/// Action: Terminates the process.
958pub const SIGNAL_SYS: Signal = Signal(30);
959impl Signal {
960    pub const fn raw(&self) -> u8 {
961        self.0
962    }
963
964    pub fn name(&self) -> &'static str {
965        match self.0 {
966            0 => "NONE",
967            1 => "HUP",
968            2 => "INT",
969            3 => "QUIT",
970            4 => "ILL",
971            5 => "TRAP",
972            6 => "ABRT",
973            7 => "BUS",
974            8 => "FPE",
975            9 => "KILL",
976            10 => "USR1",
977            11 => "SEGV",
978            12 => "USR2",
979            13 => "PIPE",
980            14 => "ALRM",
981            15 => "TERM",
982            16 => "CHLD",
983            17 => "CONT",
984            18 => "STOP",
985            19 => "TSTP",
986            20 => "TTIN",
987            21 => "TTOU",
988            22 => "URG",
989            23 => "XCPU",
990            24 => "XFSZ",
991            25 => "VTALRM",
992            26 => "PROF",
993            27 => "WINCH",
994            28 => "POLL",
995            29 => "PWR",
996            30 => "SYS",
997            _ => unsafe { core::hint::unreachable_unchecked() },
998        }
999    }
1000    pub fn message(&self) -> &'static str {
1001        match self.0 {
1002            0 => {
1003                "No signal. Note that POSIX has special semantics for `kill(pid, 0)`,
1004so this value is reserved."
1005            }
1006            1 => {
1007                "Hangup.
1008Action: Terminates the process."
1009            }
1010            2 => {
1011                "Terminate interrupt signal.
1012Action: Terminates the process."
1013            }
1014            3 => {
1015                "Terminal quit signal.
1016Action: Terminates the process."
1017            }
1018            4 => {
1019                "Illegal instruction.
1020Action: Terminates the process."
1021            }
1022            5 => {
1023                "Trace/breakpoint trap.
1024Action: Terminates the process."
1025            }
1026            6 => {
1027                "Process abort signal.
1028Action: Terminates the process."
1029            }
1030            7 => {
1031                "Access to an undefined portion of a memory object.
1032Action: Terminates the process."
1033            }
1034            8 => {
1035                "Erroneous arithmetic operation.
1036Action: Terminates the process."
1037            }
1038            9 => {
1039                "Kill.
1040Action: Terminates the process."
1041            }
1042            10 => {
1043                "User-defined signal 1.
1044Action: Terminates the process."
1045            }
1046            11 => {
1047                "Invalid memory reference.
1048Action: Terminates the process."
1049            }
1050            12 => {
1051                "User-defined signal 2.
1052Action: Terminates the process."
1053            }
1054            13 => {
1055                "Write on a pipe with no one to read it.
1056Action: Ignored."
1057            }
1058            14 => {
1059                "Alarm clock.
1060Action: Terminates the process."
1061            }
1062            15 => {
1063                "Termination signal.
1064Action: Terminates the process."
1065            }
1066            16 => {
1067                "Child process terminated, stopped, or continued.
1068Action: Ignored."
1069            }
1070            17 => {
1071                "Continue executing, if stopped.
1072Action: Continues executing, if stopped."
1073            }
1074            18 => {
1075                "Stop executing.
1076Action: Stops executing."
1077            }
1078            19 => {
1079                "Terminal stop signal.
1080Action: Stops executing."
1081            }
1082            20 => {
1083                "Background process attempting read.
1084Action: Stops executing."
1085            }
1086            21 => {
1087                "Background process attempting write.
1088Action: Stops executing."
1089            }
1090            22 => {
1091                "High bandwidth data is available at a socket.
1092Action: Ignored."
1093            }
1094            23 => {
1095                "CPU time limit exceeded.
1096Action: Terminates the process."
1097            }
1098            24 => {
1099                "File size limit exceeded.
1100Action: Terminates the process."
1101            }
1102            25 => {
1103                "Virtual timer expired.
1104Action: Terminates the process."
1105            }
1106            26 => {
1107                "Profiling timer expired.
1108Action: Terminates the process."
1109            }
1110            27 => {
1111                "Window changed.
1112Action: Ignored."
1113            }
1114            28 => {
1115                "I/O possible.
1116Action: Terminates the process."
1117            }
1118            29 => {
1119                "Power failure.
1120Action: Terminates the process."
1121            }
1122            30 => {
1123                "Bad system call.
1124Action: Terminates the process."
1125            }
1126            _ => unsafe { core::hint::unreachable_unchecked() },
1127        }
1128    }
1129}
1130impl fmt::Debug for Signal {
1131    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1132        f.debug_struct("Signal")
1133            .field("code", &self.0)
1134            .field("name", &self.name())
1135            .field("message", &self.message())
1136            .finish()
1137    }
1138}
1139
1140pub type Riflags = u16;
1141/// Returns the message without removing it from the socket's receive queue.
1142pub const RIFLAGS_RECV_PEEK: Riflags = 1 << 0;
1143/// On byte-stream sockets, block until the full amount of data can be returned.
1144pub const RIFLAGS_RECV_WAITALL: Riflags = 1 << 1;
1145
1146pub type Roflags = u16;
1147/// Returned by `sock_recv`: Message data has been truncated.
1148pub const ROFLAGS_RECV_DATA_TRUNCATED: Roflags = 1 << 0;
1149
1150pub type Siflags = u16;
1151pub type Sdflags = u8;
1152/// Disables further receive operations.
1153pub const SDFLAGS_RD: Sdflags = 1 << 0;
1154/// Disables further send operations.
1155pub const SDFLAGS_WR: Sdflags = 1 << 1;
1156
1157#[repr(transparent)]
1158#[derive(Copy, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
1159pub struct Preopentype(u8);
1160/// A pre-opened directory.
1161pub const PREOPENTYPE_DIR: Preopentype = Preopentype(0);
1162impl Preopentype {
1163    pub const fn raw(&self) -> u8 {
1164        self.0
1165    }
1166
1167    pub fn name(&self) -> &'static str {
1168        match self.0 {
1169            0 => "DIR",
1170            _ => unsafe { core::hint::unreachable_unchecked() },
1171        }
1172    }
1173    pub fn message(&self) -> &'static str {
1174        match self.0 {
1175            0 => "A pre-opened directory.",
1176            _ => unsafe { core::hint::unreachable_unchecked() },
1177        }
1178    }
1179}
1180impl fmt::Debug for Preopentype {
1181    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1182        f.debug_struct("Preopentype")
1183            .field("code", &self.0)
1184            .field("name", &self.name())
1185            .field("message", &self.message())
1186            .finish()
1187    }
1188}
1189
1190#[repr(C)]
1191#[derive(Copy, Clone, Debug)]
1192pub struct PrestatDir {
1193    /// The length of the directory name for use with `fd_prestat_dir_name`.
1194    pub pr_name_len: Size,
1195}
1196#[repr(C)]
1197#[derive(Copy, Clone)]
1198pub union PrestatU {
1199    pub dir: PrestatDir,
1200}
1201#[repr(C)]
1202#[derive(Copy, Clone)]
1203pub struct Prestat {
1204    pub tag: u8,
1205    pub u: PrestatU,
1206}
1207
1208/// Read command-line argument data.
1209/// The size of the array should match that returned by `args_sizes_get`.
1210/// Each argument is expected to be `\0` terminated.
1211pub unsafe fn args_get(argv: *mut *mut u8, argv_buf: *mut u8) -> Result<(), Errno> {
1212    let ret = wasi_snapshot_preview1::args_get(argv as i32, argv_buf as i32);
1213    match ret {
1214        0 => Ok(()),
1215        _ => Err(Errno(ret as u16)),
1216    }
1217}
1218
1219/// Return command-line argument data sizes.
1220///
1221/// ## Return
1222///
1223/// Returns the number of arguments and the size of the argument string
1224/// data, or an error.
1225pub unsafe fn args_sizes_get() -> Result<(Size, Size), Errno> {
1226    let mut rp0 = MaybeUninit::<Size>::uninit();
1227    let mut rp1 = MaybeUninit::<Size>::uninit();
1228    let ret =
1229        wasi_snapshot_preview1::args_sizes_get(rp0.as_mut_ptr() as i32, rp1.as_mut_ptr() as i32);
1230    match ret {
1231        0 => Ok((
1232            core::ptr::read(rp0.as_mut_ptr() as i32 as *const Size),
1233            core::ptr::read(rp1.as_mut_ptr() as i32 as *const Size),
1234        )),
1235        _ => Err(Errno(ret as u16)),
1236    }
1237}
1238
1239/// Read environment variable data.
1240/// The sizes of the buffers should match that returned by `environ_sizes_get`.
1241/// Key/value pairs are expected to be joined with `=`s, and terminated with `\0`s.
1242pub unsafe fn environ_get(environ: *mut *mut u8, environ_buf: *mut u8) -> Result<(), Errno> {
1243    let ret = wasi_snapshot_preview1::environ_get(environ as i32, environ_buf as i32);
1244    match ret {
1245        0 => Ok(()),
1246        _ => Err(Errno(ret as u16)),
1247    }
1248}
1249
1250/// Return environment variable data sizes.
1251///
1252/// ## Return
1253///
1254/// Returns the number of environment variable arguments and the size of the
1255/// environment variable data.
1256pub unsafe fn environ_sizes_get() -> Result<(Size, Size), Errno> {
1257    let mut rp0 = MaybeUninit::<Size>::uninit();
1258    let mut rp1 = MaybeUninit::<Size>::uninit();
1259    let ret =
1260        wasi_snapshot_preview1::environ_sizes_get(rp0.as_mut_ptr() as i32, rp1.as_mut_ptr() as i32);
1261    match ret {
1262        0 => Ok((
1263            core::ptr::read(rp0.as_mut_ptr() as i32 as *const Size),
1264            core::ptr::read(rp1.as_mut_ptr() as i32 as *const Size),
1265        )),
1266        _ => Err(Errno(ret as u16)),
1267    }
1268}
1269
1270/// Return the resolution of a clock.
1271/// Implementations are required to provide a non-zero value for supported clocks. For unsupported clocks,
1272/// return `errno::inval`.
1273/// Note: This is similar to `clock_getres` in POSIX.
1274///
1275/// ## Parameters
1276///
1277/// * `id` - The clock for which to return the resolution.
1278///
1279/// ## Return
1280///
1281/// The resolution of the clock, or an error if one happened.
1282pub unsafe fn clock_res_get(id: Clockid) -> Result<Timestamp, Errno> {
1283    let mut rp0 = MaybeUninit::<Timestamp>::uninit();
1284    let ret = wasi_snapshot_preview1::clock_res_get(id.0 as i32, rp0.as_mut_ptr() as i32);
1285    match ret {
1286        0 => Ok(core::ptr::read(rp0.as_mut_ptr() as i32 as *const Timestamp)),
1287        _ => Err(Errno(ret as u16)),
1288    }
1289}
1290
1291/// Return the time value of a clock.
1292/// Note: This is similar to `clock_gettime` in POSIX.
1293///
1294/// ## Parameters
1295///
1296/// * `id` - The clock for which to return the time.
1297/// * `precision` - The maximum lag (exclusive) that the returned time value may have, compared to its actual value.
1298///
1299/// ## Return
1300///
1301/// The time value of the clock.
1302pub unsafe fn clock_time_get(id: Clockid, precision: Timestamp) -> Result<Timestamp, Errno> {
1303    let mut rp0 = MaybeUninit::<Timestamp>::uninit();
1304    let ret = wasi_snapshot_preview1::clock_time_get(
1305        id.0 as i32,
1306        precision as i64,
1307        rp0.as_mut_ptr() as i32,
1308    );
1309    match ret {
1310        0 => Ok(core::ptr::read(rp0.as_mut_ptr() as i32 as *const Timestamp)),
1311        _ => Err(Errno(ret as u16)),
1312    }
1313}
1314
1315/// Provide file advisory information on a file descriptor.
1316/// Note: This is similar to `posix_fadvise` in POSIX.
1317///
1318/// ## Parameters
1319///
1320/// * `offset` - The offset within the file to which the advisory applies.
1321/// * `len` - The length of the region to which the advisory applies.
1322/// * `advice` - The advice.
1323pub unsafe fn fd_advise(
1324    fd: Fd,
1325    offset: Filesize,
1326    len: Filesize,
1327    advice: Advice,
1328) -> Result<(), Errno> {
1329    let ret =
1330        wasi_snapshot_preview1::fd_advise(fd as i32, offset as i64, len as i64, advice.0 as i32);
1331    match ret {
1332        0 => Ok(()),
1333        _ => Err(Errno(ret as u16)),
1334    }
1335}
1336
1337/// Force the allocation of space in a file.
1338/// Note: This is similar to `posix_fallocate` in POSIX.
1339///
1340/// ## Parameters
1341///
1342/// * `offset` - The offset at which to start the allocation.
1343/// * `len` - The length of the area that is allocated.
1344pub unsafe fn fd_allocate(fd: Fd, offset: Filesize, len: Filesize) -> Result<(), Errno> {
1345    let ret = wasi_snapshot_preview1::fd_allocate(fd as i32, offset as i64, len as i64);
1346    match ret {
1347        0 => Ok(()),
1348        _ => Err(Errno(ret as u16)),
1349    }
1350}
1351
1352/// Close a file descriptor.
1353/// Note: This is similar to `close` in POSIX.
1354pub unsafe fn fd_close(fd: Fd) -> Result<(), Errno> {
1355    let ret = wasi_snapshot_preview1::fd_close(fd as i32);
1356    match ret {
1357        0 => Ok(()),
1358        _ => Err(Errno(ret as u16)),
1359    }
1360}
1361
1362/// Synchronize the data of a file to disk.
1363/// Note: This is similar to `fdatasync` in POSIX.
1364pub unsafe fn fd_datasync(fd: Fd) -> Result<(), Errno> {
1365    let ret = wasi_snapshot_preview1::fd_datasync(fd as i32);
1366    match ret {
1367        0 => Ok(()),
1368        _ => Err(Errno(ret as u16)),
1369    }
1370}
1371
1372/// Get the attributes of a file descriptor.
1373/// Note: This returns similar flags to `fsync(fd, F_GETFL)` in POSIX, as well as additional fields.
1374///
1375/// ## Return
1376///
1377/// The buffer where the file descriptor's attributes are stored.
1378pub unsafe fn fd_fdstat_get(fd: Fd) -> Result<Fdstat, Errno> {
1379    let mut rp0 = MaybeUninit::<Fdstat>::uninit();
1380    let ret = wasi_snapshot_preview1::fd_fdstat_get(fd as i32, rp0.as_mut_ptr() as i32);
1381    match ret {
1382        0 => Ok(core::ptr::read(rp0.as_mut_ptr() as i32 as *const Fdstat)),
1383        _ => Err(Errno(ret as u16)),
1384    }
1385}
1386
1387/// Adjust the flags associated with a file descriptor.
1388/// Note: This is similar to `fcntl(fd, F_SETFL, flags)` in POSIX.
1389///
1390/// ## Parameters
1391///
1392/// * `flags` - The desired values of the file descriptor flags.
1393pub unsafe fn fd_fdstat_set_flags(fd: Fd, flags: Fdflags) -> Result<(), Errno> {
1394    let ret = wasi_snapshot_preview1::fd_fdstat_set_flags(fd as i32, flags as i32);
1395    match ret {
1396        0 => Ok(()),
1397        _ => Err(Errno(ret as u16)),
1398    }
1399}
1400
1401/// Adjust the rights associated with a file descriptor.
1402/// This can only be used to remove rights, and returns `errno::notcapable` if called in a way that would attempt to add rights
1403///
1404/// ## Parameters
1405///
1406/// * `fs_rights_base` - The desired rights of the file descriptor.
1407pub unsafe fn fd_fdstat_set_rights(
1408    fd: Fd,
1409    fs_rights_base: Rights,
1410    fs_rights_inheriting: Rights,
1411) -> Result<(), Errno> {
1412    let ret = wasi_snapshot_preview1::fd_fdstat_set_rights(
1413        fd as i32,
1414        fs_rights_base as i64,
1415        fs_rights_inheriting as i64,
1416    );
1417    match ret {
1418        0 => Ok(()),
1419        _ => Err(Errno(ret as u16)),
1420    }
1421}
1422
1423/// Return the attributes of an open file.
1424///
1425/// ## Return
1426///
1427/// The buffer where the file's attributes are stored.
1428pub unsafe fn fd_filestat_get(fd: Fd) -> Result<Filestat, Errno> {
1429    let mut rp0 = MaybeUninit::<Filestat>::uninit();
1430    let ret = wasi_snapshot_preview1::fd_filestat_get(fd as i32, rp0.as_mut_ptr() as i32);
1431    match ret {
1432        0 => Ok(core::ptr::read(rp0.as_mut_ptr() as i32 as *const Filestat)),
1433        _ => Err(Errno(ret as u16)),
1434    }
1435}
1436
1437/// Adjust the size of an open file. If this increases the file's size, the extra bytes are filled with zeros.
1438/// Note: This is similar to `ftruncate` in POSIX.
1439///
1440/// ## Parameters
1441///
1442/// * `size` - The desired file size.
1443pub unsafe fn fd_filestat_set_size(fd: Fd, size: Filesize) -> Result<(), Errno> {
1444    let ret = wasi_snapshot_preview1::fd_filestat_set_size(fd as i32, size as i64);
1445    match ret {
1446        0 => Ok(()),
1447        _ => Err(Errno(ret as u16)),
1448    }
1449}
1450
1451/// Adjust the timestamps of an open file or directory.
1452/// Note: This is similar to `futimens` in POSIX.
1453///
1454/// ## Parameters
1455///
1456/// * `atim` - The desired values of the data access timestamp.
1457/// * `mtim` - The desired values of the data modification timestamp.
1458/// * `fst_flags` - A bitmask indicating which timestamps to adjust.
1459pub unsafe fn fd_filestat_set_times(
1460    fd: Fd,
1461    atim: Timestamp,
1462    mtim: Timestamp,
1463    fst_flags: Fstflags,
1464) -> Result<(), Errno> {
1465    let ret = wasi_snapshot_preview1::fd_filestat_set_times(
1466        fd as i32,
1467        atim as i64,
1468        mtim as i64,
1469        fst_flags as i32,
1470    );
1471    match ret {
1472        0 => Ok(()),
1473        _ => Err(Errno(ret as u16)),
1474    }
1475}
1476
1477/// Read from a file descriptor, without using and updating the file descriptor's offset.
1478/// Note: This is similar to `preadv` in POSIX.
1479///
1480/// ## Parameters
1481///
1482/// * `iovs` - List of scatter/gather vectors in which to store data.
1483/// * `offset` - The offset within the file at which to read.
1484///
1485/// ## Return
1486///
1487/// The number of bytes read.
1488pub unsafe fn fd_pread(fd: Fd, iovs: IovecArray<'_>, offset: Filesize) -> Result<Size, Errno> {
1489    let mut rp0 = MaybeUninit::<Size>::uninit();
1490    let ret = wasi_snapshot_preview1::fd_pread(
1491        fd as i32,
1492        iovs.as_ptr() as i32,
1493        iovs.len() as i32,
1494        offset as i64,
1495        rp0.as_mut_ptr() as i32,
1496    );
1497    match ret {
1498        0 => Ok(core::ptr::read(rp0.as_mut_ptr() as i32 as *const Size)),
1499        _ => Err(Errno(ret as u16)),
1500    }
1501}
1502
1503/// Return a description of the given preopened file descriptor.
1504///
1505/// ## Return
1506///
1507/// The buffer where the description is stored.
1508pub unsafe fn fd_prestat_get(fd: Fd) -> Result<Prestat, Errno> {
1509    let mut rp0 = MaybeUninit::<Prestat>::uninit();
1510    let ret = wasi_snapshot_preview1::fd_prestat_get(fd as i32, rp0.as_mut_ptr() as i32);
1511    match ret {
1512        0 => Ok(core::ptr::read(rp0.as_mut_ptr() as i32 as *const Prestat)),
1513        _ => Err(Errno(ret as u16)),
1514    }
1515}
1516
1517/// Return a description of the given preopened file descriptor.
1518///
1519/// ## Parameters
1520///
1521/// * `path` - A buffer into which to write the preopened directory name.
1522pub unsafe fn fd_prestat_dir_name(fd: Fd, path: *mut u8, path_len: Size) -> Result<(), Errno> {
1523    let ret = wasi_snapshot_preview1::fd_prestat_dir_name(fd as i32, path as i32, path_len as i32);
1524    match ret {
1525        0 => Ok(()),
1526        _ => Err(Errno(ret as u16)),
1527    }
1528}
1529
1530/// Write to a file descriptor, without using and updating the file descriptor's offset.
1531/// Note: This is similar to `pwritev` in POSIX.
1532///
1533/// ## Parameters
1534///
1535/// * `iovs` - List of scatter/gather vectors from which to retrieve data.
1536/// * `offset` - The offset within the file at which to write.
1537///
1538/// ## Return
1539///
1540/// The number of bytes written.
1541pub unsafe fn fd_pwrite(fd: Fd, iovs: CiovecArray<'_>, offset: Filesize) -> Result<Size, Errno> {
1542    let mut rp0 = MaybeUninit::<Size>::uninit();
1543    let ret = wasi_snapshot_preview1::fd_pwrite(
1544        fd as i32,
1545        iovs.as_ptr() as i32,
1546        iovs.len() as i32,
1547        offset as i64,
1548        rp0.as_mut_ptr() as i32,
1549    );
1550    match ret {
1551        0 => Ok(core::ptr::read(rp0.as_mut_ptr() as i32 as *const Size)),
1552        _ => Err(Errno(ret as u16)),
1553    }
1554}
1555
1556/// Read from a file descriptor.
1557/// Note: This is similar to `readv` in POSIX.
1558///
1559/// ## Parameters
1560///
1561/// * `iovs` - List of scatter/gather vectors to which to store data.
1562///
1563/// ## Return
1564///
1565/// The number of bytes read.
1566pub unsafe fn fd_read(fd: Fd, iovs: IovecArray<'_>) -> Result<Size, Errno> {
1567    let mut rp0 = MaybeUninit::<Size>::uninit();
1568    let ret = wasi_snapshot_preview1::fd_read(
1569        fd as i32,
1570        iovs.as_ptr() as i32,
1571        iovs.len() as i32,
1572        rp0.as_mut_ptr() as i32,
1573    );
1574    match ret {
1575        0 => Ok(core::ptr::read(rp0.as_mut_ptr() as i32 as *const Size)),
1576        _ => Err(Errno(ret as u16)),
1577    }
1578}
1579
1580/// Read directory entries from a directory.
1581/// When successful, the contents of the output buffer consist of a sequence of
1582/// directory entries. Each directory entry consists of a `dirent` object,
1583/// followed by `dirent::d_namlen` bytes holding the name of the directory
1584/// entry.
1585/// This function fills the output buffer as much as possible, potentially
1586/// truncating the last directory entry. This allows the caller to grow its
1587/// read buffer size in case it's too small to fit a single large directory
1588/// entry, or skip the oversized directory entry.
1589///
1590/// ## Parameters
1591///
1592/// * `buf` - The buffer where directory entries are stored
1593/// * `cookie` - The location within the directory to start reading
1594///
1595/// ## Return
1596///
1597/// 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.
1598pub unsafe fn fd_readdir(
1599    fd: Fd,
1600    buf: *mut u8,
1601    buf_len: Size,
1602    cookie: Dircookie,
1603) -> Result<Size, Errno> {
1604    let mut rp0 = MaybeUninit::<Size>::uninit();
1605    let ret = wasi_snapshot_preview1::fd_readdir(
1606        fd as i32,
1607        buf as i32,
1608        buf_len as i32,
1609        cookie as i64,
1610        rp0.as_mut_ptr() as i32,
1611    );
1612    match ret {
1613        0 => Ok(core::ptr::read(rp0.as_mut_ptr() as i32 as *const Size)),
1614        _ => Err(Errno(ret as u16)),
1615    }
1616}
1617
1618/// Atomically replace a file descriptor by renumbering another file descriptor.
1619/// Due to the strong focus on thread safety, this environment does not provide
1620/// a mechanism to duplicate or renumber a file descriptor to an arbitrary
1621/// number, like `dup2()`. This would be prone to race conditions, as an actual
1622/// file descriptor with the same number could be allocated by a different
1623/// thread at the same time.
1624/// This function provides a way to atomically renumber file descriptors, which
1625/// would disappear if `dup2()` were to be removed entirely.
1626///
1627/// ## Parameters
1628///
1629/// * `to` - The file descriptor to overwrite.
1630pub unsafe fn fd_renumber(fd: Fd, to: Fd) -> Result<(), Errno> {
1631    let ret = wasi_snapshot_preview1::fd_renumber(fd as i32, to as i32);
1632    match ret {
1633        0 => Ok(()),
1634        _ => Err(Errno(ret as u16)),
1635    }
1636}
1637
1638/// Move the offset of a file descriptor.
1639/// Note: This is similar to `lseek` in POSIX.
1640///
1641/// ## Parameters
1642///
1643/// * `offset` - The number of bytes to move.
1644/// * `whence` - The base from which the offset is relative.
1645///
1646/// ## Return
1647///
1648/// The new offset of the file descriptor, relative to the start of the file.
1649pub unsafe fn fd_seek(fd: Fd, offset: Filedelta, whence: Whence) -> Result<Filesize, Errno> {
1650    let mut rp0 = MaybeUninit::<Filesize>::uninit();
1651    let ret = wasi_snapshot_preview1::fd_seek(
1652        fd as i32,
1653        offset,
1654        whence.0 as i32,
1655        rp0.as_mut_ptr() as i32,
1656    );
1657    match ret {
1658        0 => Ok(core::ptr::read(rp0.as_mut_ptr() as i32 as *const Filesize)),
1659        _ => Err(Errno(ret as u16)),
1660    }
1661}
1662
1663/// Synchronize the data and metadata of a file to disk.
1664/// Note: This is similar to `fsync` in POSIX.
1665pub unsafe fn fd_sync(fd: Fd) -> Result<(), Errno> {
1666    let ret = wasi_snapshot_preview1::fd_sync(fd as i32);
1667    match ret {
1668        0 => Ok(()),
1669        _ => Err(Errno(ret as u16)),
1670    }
1671}
1672
1673/// Return the current offset of a file descriptor.
1674/// Note: This is similar to `lseek(fd, 0, SEEK_CUR)` in POSIX.
1675///
1676/// ## Return
1677///
1678/// The current offset of the file descriptor, relative to the start of the file.
1679pub unsafe fn fd_tell(fd: Fd) -> Result<Filesize, Errno> {
1680    let mut rp0 = MaybeUninit::<Filesize>::uninit();
1681    let ret = wasi_snapshot_preview1::fd_tell(fd as i32, rp0.as_mut_ptr() as i32);
1682    match ret {
1683        0 => Ok(core::ptr::read(rp0.as_mut_ptr() as i32 as *const Filesize)),
1684        _ => Err(Errno(ret as u16)),
1685    }
1686}
1687
1688/// Write to a file descriptor.
1689/// Note: This is similar to `writev` in POSIX.
1690///
1691/// ## Parameters
1692///
1693/// * `iovs` - List of scatter/gather vectors from which to retrieve data.
1694pub unsafe fn fd_write(fd: Fd, iovs: CiovecArray<'_>) -> Result<Size, Errno> {
1695    let mut rp0 = MaybeUninit::<Size>::uninit();
1696    let ret = wasi_snapshot_preview1::fd_write(
1697        fd as i32,
1698        iovs.as_ptr() as i32,
1699        iovs.len() as i32,
1700        rp0.as_mut_ptr() as i32,
1701    );
1702    match ret {
1703        0 => Ok(core::ptr::read(rp0.as_mut_ptr() as i32 as *const Size)),
1704        _ => Err(Errno(ret as u16)),
1705    }
1706}
1707
1708/// Create a directory.
1709/// Note: This is similar to `mkdirat` in POSIX.
1710///
1711/// ## Parameters
1712///
1713/// * `path` - The path at which to create the directory.
1714pub unsafe fn path_create_directory(fd: Fd, path: &str) -> Result<(), Errno> {
1715    let ret = wasi_snapshot_preview1::path_create_directory(
1716        fd as i32,
1717        path.as_ptr() as i32,
1718        path.len() as i32,
1719    );
1720    match ret {
1721        0 => Ok(()),
1722        _ => Err(Errno(ret as u16)),
1723    }
1724}
1725
1726/// Return the attributes of a file or directory.
1727/// Note: This is similar to `stat` in POSIX.
1728///
1729/// ## Parameters
1730///
1731/// * `flags` - Flags determining the method of how the path is resolved.
1732/// * `path` - The path of the file or directory to inspect.
1733///
1734/// ## Return
1735///
1736/// The buffer where the file's attributes are stored.
1737pub unsafe fn path_filestat_get(fd: Fd, flags: Lookupflags, path: &str) -> Result<Filestat, Errno> {
1738    let mut rp0 = MaybeUninit::<Filestat>::uninit();
1739    let ret = wasi_snapshot_preview1::path_filestat_get(
1740        fd as i32,
1741        flags as i32,
1742        path.as_ptr() as i32,
1743        path.len() as i32,
1744        rp0.as_mut_ptr() as i32,
1745    );
1746    match ret {
1747        0 => Ok(core::ptr::read(rp0.as_mut_ptr() as i32 as *const Filestat)),
1748        _ => Err(Errno(ret as u16)),
1749    }
1750}
1751
1752/// Adjust the timestamps of a file or directory.
1753/// Note: This is similar to `utimensat` in POSIX.
1754///
1755/// ## Parameters
1756///
1757/// * `flags` - Flags determining the method of how the path is resolved.
1758/// * `path` - The path of the file or directory to operate on.
1759/// * `atim` - The desired values of the data access timestamp.
1760/// * `mtim` - The desired values of the data modification timestamp.
1761/// * `fst_flags` - A bitmask indicating which timestamps to adjust.
1762pub unsafe fn path_filestat_set_times(
1763    fd: Fd,
1764    flags: Lookupflags,
1765    path: &str,
1766    atim: Timestamp,
1767    mtim: Timestamp,
1768    fst_flags: Fstflags,
1769) -> Result<(), Errno> {
1770    let ret = wasi_snapshot_preview1::path_filestat_set_times(
1771        fd as i32,
1772        flags as i32,
1773        path.as_ptr() as i32,
1774        path.len() as i32,
1775        atim as i64,
1776        mtim as i64,
1777        fst_flags as i32,
1778    );
1779    match ret {
1780        0 => Ok(()),
1781        _ => Err(Errno(ret as u16)),
1782    }
1783}
1784
1785/// Create a hard link.
1786/// Note: This is similar to `linkat` in POSIX.
1787///
1788/// ## Parameters
1789///
1790/// * `old_flags` - Flags determining the method of how the path is resolved.
1791/// * `old_path` - The source path from which to link.
1792/// * `new_fd` - The working directory at which the resolution of the new path starts.
1793/// * `new_path` - The destination path at which to create the hard link.
1794pub unsafe fn path_link(
1795    old_fd: Fd,
1796    old_flags: Lookupflags,
1797    old_path: &str,
1798    new_fd: Fd,
1799    new_path: &str,
1800) -> Result<(), Errno> {
1801    let ret = wasi_snapshot_preview1::path_link(
1802        old_fd as i32,
1803        old_flags as i32,
1804        old_path.as_ptr() as i32,
1805        old_path.len() as i32,
1806        new_fd as i32,
1807        new_path.as_ptr() as i32,
1808        new_path.len() as i32,
1809    );
1810    match ret {
1811        0 => Ok(()),
1812        _ => Err(Errno(ret as u16)),
1813    }
1814}
1815
1816/// Open a file or directory.
1817/// The returned file descriptor is not guaranteed to be the lowest-numbered
1818/// file descriptor not currently open; it is randomized to prevent
1819/// applications from depending on making assumptions about indexes, since this
1820/// is error-prone in multi-threaded contexts. The returned file descriptor is
1821/// guaranteed to be less than 2**31.
1822/// Note: This is similar to `openat` in POSIX.
1823///
1824/// ## Parameters
1825///
1826/// * `dirflags` - Flags determining the method of how the path is resolved.
1827/// * `path` - The relative path of the file or directory to open, relative to the
1828///   `path_open::fd` directory.
1829/// * `oflags` - The method by which to open the file.
1830/// * `fs_rights_base` - The initial rights of the newly created file descriptor. The
1831///   implementation is allowed to return a file descriptor with fewer rights
1832///   than specified, if and only if those rights do not apply to the type of
1833///   file being opened.
1834///   The *base* rights are rights that will apply to operations using the file
1835///   descriptor itself, while the *inheriting* rights are rights that apply to
1836///   file descriptors derived from it.
1837///
1838/// ## Return
1839///
1840/// The file descriptor of the file that has been opened.
1841pub unsafe fn path_open(
1842    fd: Fd,
1843    dirflags: Lookupflags,
1844    path: &str,
1845    oflags: Oflags,
1846    fs_rights_base: Rights,
1847    fs_rights_inheriting: Rights,
1848    fdflags: Fdflags,
1849) -> Result<Fd, Errno> {
1850    let mut rp0 = MaybeUninit::<Fd>::uninit();
1851    let ret = wasi_snapshot_preview1::path_open(
1852        fd as i32,
1853        dirflags as i32,
1854        path.as_ptr() as i32,
1855        path.len() as i32,
1856        oflags as i32,
1857        fs_rights_base as i64,
1858        fs_rights_inheriting as i64,
1859        fdflags as i32,
1860        rp0.as_mut_ptr() as i32,
1861    );
1862    match ret {
1863        0 => Ok(core::ptr::read(rp0.as_mut_ptr() as i32 as *const Fd)),
1864        _ => Err(Errno(ret as u16)),
1865    }
1866}
1867
1868/// Read the contents of a symbolic link.
1869/// Note: This is similar to `readlinkat` in POSIX.
1870///
1871/// ## Parameters
1872///
1873/// * `path` - The path of the symbolic link from which to read.
1874/// * `buf` - The buffer to which to write the contents of the symbolic link.
1875///
1876/// ## Return
1877///
1878/// The number of bytes placed in the buffer.
1879pub unsafe fn path_readlink(
1880    fd: Fd,
1881    path: &str,
1882    buf: *mut u8,
1883    buf_len: Size,
1884) -> Result<Size, Errno> {
1885    let mut rp0 = MaybeUninit::<Size>::uninit();
1886    let ret = wasi_snapshot_preview1::path_readlink(
1887        fd as i32,
1888        path.as_ptr() as i32,
1889        path.len() as i32,
1890        buf as i32,
1891        buf_len as i32,
1892        rp0.as_mut_ptr() as i32,
1893    );
1894    match ret {
1895        0 => Ok(core::ptr::read(rp0.as_mut_ptr() as i32 as *const Size)),
1896        _ => Err(Errno(ret as u16)),
1897    }
1898}
1899
1900/// Remove a directory.
1901/// Return `errno::notempty` if the directory is not empty.
1902/// Note: This is similar to `unlinkat(fd, path, AT_REMOVEDIR)` in POSIX.
1903///
1904/// ## Parameters
1905///
1906/// * `path` - The path to a directory to remove.
1907pub unsafe fn path_remove_directory(fd: Fd, path: &str) -> Result<(), Errno> {
1908    let ret = wasi_snapshot_preview1::path_remove_directory(
1909        fd as i32,
1910        path.as_ptr() as i32,
1911        path.len() as i32,
1912    );
1913    match ret {
1914        0 => Ok(()),
1915        _ => Err(Errno(ret as u16)),
1916    }
1917}
1918
1919/// Rename a file or directory.
1920/// Note: This is similar to `renameat` in POSIX.
1921///
1922/// ## Parameters
1923///
1924/// * `old_path` - The source path of the file or directory to rename.
1925/// * `new_fd` - The working directory at which the resolution of the new path starts.
1926/// * `new_path` - The destination path to which to rename the file or directory.
1927pub unsafe fn path_rename(fd: Fd, old_path: &str, new_fd: Fd, new_path: &str) -> Result<(), Errno> {
1928    let ret = wasi_snapshot_preview1::path_rename(
1929        fd as i32,
1930        old_path.as_ptr() as i32,
1931        old_path.len() as i32,
1932        new_fd as i32,
1933        new_path.as_ptr() as i32,
1934        new_path.len() as i32,
1935    );
1936    match ret {
1937        0 => Ok(()),
1938        _ => Err(Errno(ret as u16)),
1939    }
1940}
1941
1942/// Create a symbolic link.
1943/// Note: This is similar to `symlinkat` in POSIX.
1944///
1945/// ## Parameters
1946///
1947/// * `old_path` - The contents of the symbolic link.
1948/// * `new_path` - The destination path at which to create the symbolic link.
1949pub unsafe fn path_symlink(old_path: &str, fd: Fd, new_path: &str) -> Result<(), Errno> {
1950    let ret = wasi_snapshot_preview1::path_symlink(
1951        old_path.as_ptr() as i32,
1952        old_path.len() as i32,
1953        fd as i32,
1954        new_path.as_ptr() as i32,
1955        new_path.len() as i32,
1956    );
1957    match ret {
1958        0 => Ok(()),
1959        _ => Err(Errno(ret as u16)),
1960    }
1961}
1962
1963/// Unlink a file.
1964/// Return `errno::isdir` if the path refers to a directory.
1965/// Note: This is similar to `unlinkat(fd, path, 0)` in POSIX.
1966///
1967/// ## Parameters
1968///
1969/// * `path` - The path to a file to unlink.
1970pub unsafe fn path_unlink_file(fd: Fd, path: &str) -> Result<(), Errno> {
1971    let ret = wasi_snapshot_preview1::path_unlink_file(
1972        fd as i32,
1973        path.as_ptr() as i32,
1974        path.len() as i32,
1975    );
1976    match ret {
1977        0 => Ok(()),
1978        _ => Err(Errno(ret as u16)),
1979    }
1980}
1981
1982/// Concurrently poll for the occurrence of a set of events.
1983///
1984/// ## Parameters
1985///
1986/// * `in_` - The events to which to subscribe.
1987/// * `out` - The events that have occurred.
1988/// * `nsubscriptions` - Both the number of subscriptions and events.
1989///
1990/// ## Return
1991///
1992/// The number of events stored.
1993pub unsafe fn poll_oneoff(
1994    in_: *const Subscription,
1995    out: *mut Event,
1996    nsubscriptions: Size,
1997) -> Result<Size, Errno> {
1998    let mut rp0 = MaybeUninit::<Size>::uninit();
1999    let ret = wasi_snapshot_preview1::poll_oneoff(
2000        in_ as i32,
2001        out as i32,
2002        nsubscriptions as i32,
2003        rp0.as_mut_ptr() as i32,
2004    );
2005    match ret {
2006        0 => Ok(core::ptr::read(rp0.as_mut_ptr() as i32 as *const Size)),
2007        _ => Err(Errno(ret as u16)),
2008    }
2009}
2010
2011/// Terminate the process normally. An exit code of 0 indicates successful
2012/// termination of the program. The meanings of other values is dependent on
2013/// the environment.
2014///
2015/// ## Parameters
2016///
2017/// * `rval` - The exit code returned by the process.
2018pub unsafe fn proc_exit(rval: Exitcode) {
2019    wasi_snapshot_preview1::proc_exit(rval as i32);
2020}
2021
2022/// Send a signal to the process of the calling thread.
2023/// Note: This is similar to `raise` in POSIX.
2024///
2025/// ## Parameters
2026///
2027/// * `sig` - The signal condition to trigger.
2028pub unsafe fn proc_raise(sig: Signal) -> Result<(), Errno> {
2029    let ret = wasi_snapshot_preview1::proc_raise(sig.0 as i32);
2030    match ret {
2031        0 => Ok(()),
2032        _ => Err(Errno(ret as u16)),
2033    }
2034}
2035
2036/// Temporarily yield execution of the calling thread.
2037/// Note: This is similar to `sched_yield` in POSIX.
2038pub unsafe fn sched_yield() -> Result<(), Errno> {
2039    let ret = wasi_snapshot_preview1::sched_yield();
2040    match ret {
2041        0 => Ok(()),
2042        _ => Err(Errno(ret as u16)),
2043    }
2044}
2045
2046/// Write high-quality random data into a buffer.
2047/// This function blocks when the implementation is unable to immediately
2048/// provide sufficient high-quality random data.
2049/// This function may execute slowly, so when large mounts of random data are
2050/// required, it's advisable to use this function to seed a pseudo-random
2051/// number generator, rather than to provide the random data directly.
2052///
2053/// ## Parameters
2054///
2055/// * `buf` - The buffer to fill with random data.
2056pub unsafe fn random_get(buf: *mut u8, buf_len: Size) -> Result<(), Errno> {
2057    let ret = wasi_snapshot_preview1::random_get(buf as i32, buf_len as i32);
2058    match ret {
2059        0 => Ok(()),
2060        _ => Err(Errno(ret as u16)),
2061    }
2062}
2063
2064/// Accept a new incoming connection.
2065/// Note: This is similar to `accept` in POSIX.
2066///
2067/// ## Parameters
2068///
2069/// * `fd` - The listening socket.
2070/// * `flags` - The desired values of the file descriptor flags.
2071///
2072/// ## Return
2073///
2074/// New socket connection
2075pub unsafe fn sock_accept(fd: Fd, flags: Fdflags) -> Result<Fd, Errno> {
2076    let mut rp0 = MaybeUninit::<Fd>::uninit();
2077    let ret = wasi_snapshot_preview1::sock_accept(fd as i32, flags as i32, rp0.as_mut_ptr() as i32);
2078    match ret {
2079        0 => Ok(core::ptr::read(rp0.as_mut_ptr() as i32 as *const Fd)),
2080        _ => Err(Errno(ret as u16)),
2081    }
2082}
2083
2084/// Receive a message from a socket.
2085/// Note: This is similar to `recv` in POSIX, though it also supports reading
2086/// the data into multiple buffers in the manner of `readv`.
2087///
2088/// ## Parameters
2089///
2090/// * `ri_data` - List of scatter/gather vectors to which to store data.
2091/// * `ri_flags` - Message flags.
2092///
2093/// ## Return
2094///
2095/// Number of bytes stored in ri_data and message flags.
2096pub unsafe fn sock_recv(
2097    fd: Fd,
2098    ri_data: IovecArray<'_>,
2099    ri_flags: Riflags,
2100) -> Result<(Size, Roflags), Errno> {
2101    let mut rp0 = MaybeUninit::<Size>::uninit();
2102    let mut rp1 = MaybeUninit::<Roflags>::uninit();
2103    let ret = wasi_snapshot_preview1::sock_recv(
2104        fd as i32,
2105        ri_data.as_ptr() as i32,
2106        ri_data.len() as i32,
2107        ri_flags as i32,
2108        rp0.as_mut_ptr() as i32,
2109        rp1.as_mut_ptr() as i32,
2110    );
2111    match ret {
2112        0 => Ok((
2113            core::ptr::read(rp0.as_mut_ptr() as i32 as *const Size),
2114            core::ptr::read(rp1.as_mut_ptr() as i32 as *const Roflags),
2115        )),
2116        _ => Err(Errno(ret as u16)),
2117    }
2118}
2119
2120/// Send a message on a socket.
2121/// Note: This is similar to `send` in POSIX, though it also supports writing
2122/// the data from multiple buffers in the manner of `writev`.
2123///
2124/// ## Parameters
2125///
2126/// * `si_data` - List of scatter/gather vectors to which to retrieve data
2127/// * `si_flags` - Message flags.
2128///
2129/// ## Return
2130///
2131/// Number of bytes transmitted.
2132pub unsafe fn sock_send(
2133    fd: Fd,
2134    si_data: CiovecArray<'_>,
2135    si_flags: Siflags,
2136) -> Result<Size, Errno> {
2137    let mut rp0 = MaybeUninit::<Size>::uninit();
2138    let ret = wasi_snapshot_preview1::sock_send(
2139        fd as i32,
2140        si_data.as_ptr() as i32,
2141        si_data.len() as i32,
2142        si_flags as i32,
2143        rp0.as_mut_ptr() as i32,
2144    );
2145    match ret {
2146        0 => Ok(core::ptr::read(rp0.as_mut_ptr() as i32 as *const Size)),
2147        _ => Err(Errno(ret as u16)),
2148    }
2149}
2150
2151/// Shut down socket send and receive channels.
2152/// Note: This is similar to `shutdown` in POSIX.
2153///
2154/// ## Parameters
2155///
2156/// * `how` - Which channels on the socket to shut down.
2157pub unsafe fn sock_shutdown(fd: Fd, how: Sdflags) -> Result<(), Errno> {
2158    let ret = wasi_snapshot_preview1::sock_shutdown(fd as i32, how as i32);
2159    match ret {
2160        0 => Ok(()),
2161        _ => Err(Errno(ret as u16)),
2162    }
2163}
2164
2165pub mod wasi_snapshot_preview1 {
2166    #[link(wasm_import_module = "wasi_snapshot_preview1")]
2167    extern "C" {
2168        /// Read command-line argument data.
2169        /// The size of the array should match that returned by `args_sizes_get`.
2170        /// Each argument is expected to be `\0` terminated.
2171        pub fn args_get(arg0: i32, arg1: i32) -> i32;
2172        /// Return command-line argument data sizes.
2173        pub fn args_sizes_get(arg0: i32, arg1: i32) -> i32;
2174        /// Read environment variable data.
2175        /// The sizes of the buffers should match that returned by `environ_sizes_get`.
2176        /// Key/value pairs are expected to be joined with `=`s, and terminated with `\0`s.
2177        pub fn environ_get(arg0: i32, arg1: i32) -> i32;
2178        /// Return environment variable data sizes.
2179        pub fn environ_sizes_get(arg0: i32, arg1: i32) -> i32;
2180        /// Return the resolution of a clock.
2181        /// Implementations are required to provide a non-zero value for supported clocks. For unsupported clocks,
2182        /// return `errno::inval`.
2183        /// Note: This is similar to `clock_getres` in POSIX.
2184        pub fn clock_res_get(arg0: i32, arg1: i32) -> i32;
2185        /// Return the time value of a clock.
2186        /// Note: This is similar to `clock_gettime` in POSIX.
2187        pub fn clock_time_get(arg0: i32, arg1: i64, arg2: i32) -> i32;
2188        /// Provide file advisory information on a file descriptor.
2189        /// Note: This is similar to `posix_fadvise` in POSIX.
2190        pub fn fd_advise(arg0: i32, arg1: i64, arg2: i64, arg3: i32) -> i32;
2191        /// Force the allocation of space in a file.
2192        /// Note: This is similar to `posix_fallocate` in POSIX.
2193        pub fn fd_allocate(arg0: i32, arg1: i64, arg2: i64) -> i32;
2194        /// Close a file descriptor.
2195        /// Note: This is similar to `close` in POSIX.
2196        pub fn fd_close(arg0: i32) -> i32;
2197        /// Synchronize the data of a file to disk.
2198        /// Note: This is similar to `fdatasync` in POSIX.
2199        pub fn fd_datasync(arg0: i32) -> i32;
2200        /// Get the attributes of a file descriptor.
2201        /// Note: This returns similar flags to `fsync(fd, F_GETFL)` in POSIX, as well as additional fields.
2202        pub fn fd_fdstat_get(arg0: i32, arg1: i32) -> i32;
2203        /// Adjust the flags associated with a file descriptor.
2204        /// Note: This is similar to `fcntl(fd, F_SETFL, flags)` in POSIX.
2205        pub fn fd_fdstat_set_flags(arg0: i32, arg1: i32) -> i32;
2206        /// Adjust the rights associated with a file descriptor.
2207        /// This can only be used to remove rights, and returns `errno::notcapable` if called in a way that would attempt to add rights
2208        pub fn fd_fdstat_set_rights(arg0: i32, arg1: i64, arg2: i64) -> i32;
2209        /// Return the attributes of an open file.
2210        pub fn fd_filestat_get(arg0: i32, arg1: i32) -> i32;
2211        /// Adjust the size of an open file. If this increases the file's size, the extra bytes are filled with zeros.
2212        /// Note: This is similar to `ftruncate` in POSIX.
2213        pub fn fd_filestat_set_size(arg0: i32, arg1: i64) -> i32;
2214        /// Adjust the timestamps of an open file or directory.
2215        /// Note: This is similar to `futimens` in POSIX.
2216        pub fn fd_filestat_set_times(arg0: i32, arg1: i64, arg2: i64, arg3: i32) -> i32;
2217        /// Read from a file descriptor, without using and updating the file descriptor's offset.
2218        /// Note: This is similar to `preadv` in POSIX.
2219        pub fn fd_pread(arg0: i32, arg1: i32, arg2: i32, arg3: i64, arg4: i32) -> i32;
2220        /// Return a description of the given preopened file descriptor.
2221        pub fn fd_prestat_get(arg0: i32, arg1: i32) -> i32;
2222        /// Return a description of the given preopened file descriptor.
2223        pub fn fd_prestat_dir_name(arg0: i32, arg1: i32, arg2: i32) -> i32;
2224        /// Write to a file descriptor, without using and updating the file descriptor's offset.
2225        /// Note: This is similar to `pwritev` in POSIX.
2226        pub fn fd_pwrite(arg0: i32, arg1: i32, arg2: i32, arg3: i64, arg4: i32) -> i32;
2227        /// Read from a file descriptor.
2228        /// Note: This is similar to `readv` in POSIX.
2229        pub fn fd_read(arg0: i32, arg1: i32, arg2: i32, arg3: i32) -> i32;
2230        /// Read directory entries from a directory.
2231        /// When successful, the contents of the output buffer consist of a sequence of
2232        /// directory entries. Each directory entry consists of a `dirent` object,
2233        /// followed by `dirent::d_namlen` bytes holding the name of the directory
2234        /// entry.
2235        /// This function fills the output buffer as much as possible, potentially
2236        /// truncating the last directory entry. This allows the caller to grow its
2237        /// read buffer size in case it's too small to fit a single large directory
2238        /// entry, or skip the oversized directory entry.
2239        pub fn fd_readdir(arg0: i32, arg1: i32, arg2: i32, arg3: i64, arg4: i32) -> i32;
2240        /// Atomically replace a file descriptor by renumbering another file descriptor.
2241        /// Due to the strong focus on thread safety, this environment does not provide
2242        /// a mechanism to duplicate or renumber a file descriptor to an arbitrary
2243        /// number, like `dup2()`. This would be prone to race conditions, as an actual
2244        /// file descriptor with the same number could be allocated by a different
2245        /// thread at the same time.
2246        /// This function provides a way to atomically renumber file descriptors, which
2247        /// would disappear if `dup2()` were to be removed entirely.
2248        pub fn fd_renumber(arg0: i32, arg1: i32) -> i32;
2249        /// Move the offset of a file descriptor.
2250        /// Note: This is similar to `lseek` in POSIX.
2251        pub fn fd_seek(arg0: i32, arg1: i64, arg2: i32, arg3: i32) -> i32;
2252        /// Synchronize the data and metadata of a file to disk.
2253        /// Note: This is similar to `fsync` in POSIX.
2254        pub fn fd_sync(arg0: i32) -> i32;
2255        /// Return the current offset of a file descriptor.
2256        /// Note: This is similar to `lseek(fd, 0, SEEK_CUR)` in POSIX.
2257        pub fn fd_tell(arg0: i32, arg1: i32) -> i32;
2258        /// Write to a file descriptor.
2259        /// Note: This is similar to `writev` in POSIX.
2260        pub fn fd_write(arg0: i32, arg1: i32, arg2: i32, arg3: i32) -> i32;
2261        /// Create a directory.
2262        /// Note: This is similar to `mkdirat` in POSIX.
2263        pub fn path_create_directory(arg0: i32, arg1: i32, arg2: i32) -> i32;
2264        /// Return the attributes of a file or directory.
2265        /// Note: This is similar to `stat` in POSIX.
2266        pub fn path_filestat_get(arg0: i32, arg1: i32, arg2: i32, arg3: i32, arg4: i32) -> i32;
2267        /// Adjust the timestamps of a file or directory.
2268        /// Note: This is similar to `utimensat` in POSIX.
2269        pub fn path_filestat_set_times(
2270            arg0: i32,
2271            arg1: i32,
2272            arg2: i32,
2273            arg3: i32,
2274            arg4: i64,
2275            arg5: i64,
2276            arg6: i32,
2277        ) -> i32;
2278        /// Create a hard link.
2279        /// Note: This is similar to `linkat` in POSIX.
2280        pub fn path_link(
2281            arg0: i32,
2282            arg1: i32,
2283            arg2: i32,
2284            arg3: i32,
2285            arg4: i32,
2286            arg5: i32,
2287            arg6: i32,
2288        ) -> i32;
2289        /// Open a file or directory.
2290        /// The returned file descriptor is not guaranteed to be the lowest-numbered
2291        /// file descriptor not currently open; it is randomized to prevent
2292        /// applications from depending on making assumptions about indexes, since this
2293        /// is error-prone in multi-threaded contexts. The returned file descriptor is
2294        /// guaranteed to be less than 2**31.
2295        /// Note: This is similar to `openat` in POSIX.
2296        pub fn path_open(
2297            arg0: i32,
2298            arg1: i32,
2299            arg2: i32,
2300            arg3: i32,
2301            arg4: i32,
2302            arg5: i64,
2303            arg6: i64,
2304            arg7: i32,
2305            arg8: i32,
2306        ) -> i32;
2307        /// Read the contents of a symbolic link.
2308        /// Note: This is similar to `readlinkat` in POSIX.
2309        pub fn path_readlink(
2310            arg0: i32,
2311            arg1: i32,
2312            arg2: i32,
2313            arg3: i32,
2314            arg4: i32,
2315            arg5: i32,
2316        ) -> i32;
2317        /// Remove a directory.
2318        /// Return `errno::notempty` if the directory is not empty.
2319        /// Note: This is similar to `unlinkat(fd, path, AT_REMOVEDIR)` in POSIX.
2320        pub fn path_remove_directory(arg0: i32, arg1: i32, arg2: i32) -> i32;
2321        /// Rename a file or directory.
2322        /// Note: This is similar to `renameat` in POSIX.
2323        pub fn path_rename(arg0: i32, arg1: i32, arg2: i32, arg3: i32, arg4: i32, arg5: i32)
2324            -> i32;
2325        /// Create a symbolic link.
2326        /// Note: This is similar to `symlinkat` in POSIX.
2327        pub fn path_symlink(arg0: i32, arg1: i32, arg2: i32, arg3: i32, arg4: i32) -> i32;
2328        /// Unlink a file.
2329        /// Return `errno::isdir` if the path refers to a directory.
2330        /// Note: This is similar to `unlinkat(fd, path, 0)` in POSIX.
2331        pub fn path_unlink_file(arg0: i32, arg1: i32, arg2: i32) -> i32;
2332        /// Concurrently poll for the occurrence of a set of events.
2333        pub fn poll_oneoff(arg0: i32, arg1: i32, arg2: i32, arg3: i32) -> i32;
2334        /// Terminate the process normally. An exit code of 0 indicates successful
2335        /// termination of the program. The meanings of other values is dependent on
2336        /// the environment.
2337        pub fn proc_exit(arg0: i32) -> !;
2338        /// Send a signal to the process of the calling thread.
2339        /// Note: This is similar to `raise` in POSIX.
2340        pub fn proc_raise(arg0: i32) -> i32;
2341        /// Temporarily yield execution of the calling thread.
2342        /// Note: This is similar to `sched_yield` in POSIX.
2343        pub fn sched_yield() -> i32;
2344        /// Write high-quality random data into a buffer.
2345        /// This function blocks when the implementation is unable to immediately
2346        /// provide sufficient high-quality random data.
2347        /// This function may execute slowly, so when large mounts of random data are
2348        /// required, it's advisable to use this function to seed a pseudo-random
2349        /// number generator, rather than to provide the random data directly.
2350        pub fn random_get(arg0: i32, arg1: i32) -> i32;
2351        /// Accept a new incoming connection.
2352        /// Note: This is similar to `accept` in POSIX.
2353        pub fn sock_accept(arg0: i32, arg1: i32, arg2: i32) -> i32;
2354        /// Receive a message from a socket.
2355        /// Note: This is similar to `recv` in POSIX, though it also supports reading
2356        /// the data into multiple buffers in the manner of `readv`.
2357        pub fn sock_recv(arg0: i32, arg1: i32, arg2: i32, arg3: i32, arg4: i32, arg5: i32) -> i32;
2358        /// Send a message on a socket.
2359        /// Note: This is similar to `send` in POSIX, though it also supports writing
2360        /// the data from multiple buffers in the manner of `writev`.
2361        pub fn sock_send(arg0: i32, arg1: i32, arg2: i32, arg3: i32, arg4: i32) -> i32;
2362        /// Shut down socket send and receive channels.
2363        /// Note: This is similar to `shutdown` in POSIX.
2364        pub fn sock_shutdown(arg0: i32, arg1: i32) -> i32;
2365    }
2366}