1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
//! Rusty WASI type definitions based on
//! [the spec](https://github.com/CraneStation/wasmtime/blob/master/docs/WASI-api.md)
#![feature(non_exhaustive)]

#[macro_use]
extern crate bitflags;
#[macro_use]
extern crate proper;

/// File or memory access pattern advisory information.
#[repr(u8)]
#[derive(Clone, Copy, PartialEq, Prim)]
pub enum Advice {
    /// The application has no advice to give on its behavior with respect to the specified data.
    Normal,

    /// The application expects to access the data sequentially from lower to higher offsets.
    Sequential,

    /// The application expects to access the specified data in a random order.
    Random,

    /// The application expects that it will not access the specified data in the near future.
    DontNeed,

    /// The application expects to access the specified data once and then not reuse it thereafter.
    NoReuse,

    /// The application expects to access the specified data in the near future.
    WillNeed,
}

/// Identifiers for clocks.
#[repr(u8)]
#[derive(Clone, Copy, PartialEq, Prim)]
pub enum ClockId {
    /// The clock measuring real time. Time value zero corresponds with 1970-01-01T00:00:00Z.
    RealTime,

    /// The store-wide monotonic clock, which is defined as a clock measuring real time, whose
    /// value cannot be adjusted and which cannot have negative clock jumps.
    ///
    /// The epoch of this clock is undefined. The absolute time value of this clock therefore
    /// has no meaning.
    Monotonic,

    /// The CPU-time clock associated with the current process.
    ProcessCpuTime,

    /// The CPU-time clock associated with the current thread.
    ThreadCpuTime,
}

/// Identifier for a device containing a file system. Can be used in combination with `Inode`
/// to uniquely identify a file or directory in the filesystem.
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Prim)]
pub struct Device(u64);

/// A reference to the offset of a directory entry.
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Prim)]
pub struct DirCookie(u64);

impl DirCookie {
    /// Creates a new `DirCookie` repreenting a permanent reference to the first directory entry
    /// within a directory.
    pub fn start() -> Self {
        DirCookie(0)
    }
}

/// A directory entry.
#[repr(C)]
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct DirEnt {
    /// The offset of the next directory entry stored in this directory.
    pub next: DirCookie,

    /// The serial number of the file referred to by this directory entry.
    pub inode: Inode,

    /// The length of the name of the directory entry.
    pub name_len: u32,

    /// The type of the file referred to by this directory entry.
    pub file_type: FileType,
}

/// Error codes returned by functions.
#[repr(u16)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Prim)]
#[prim(ty = "u16")]
#[non_exhaustive]
pub enum ErrNo {
    /// No error occurred. System call completed successfully.
    Success,

    /// Argument list too long.
    TooBig,

    /// Permission denied.
    Access,

    /// Address in use.
    AddrInUse,

    /// Address not available.
    AddrNotAvail,

    /// Address family not supported.
    AfNoSupport,

    /// Resource unavailable, or operation would block.
    Again,

    /// Connection already in progress.
    Already,

    /// Bad file descriptor.
    BadF,

    /// Bad message.
    BadMsg,

    /// Device or resource busy.
    Busy,

    /// Operation canceled.
    Canceled,

    /// No child processes.
    Child,

    /// Connection aborted.
    ConnAborted,

    /// Connection refused.
    ConnRefused,

    /// Connection reset.
    ConnReset,

    /// Resource deadlock would occur.
    Deadlk,

    /// Destination address required.
    DestAddrReq,

    /// Mathematics argument out of domain of function.
    Domain,

    /// Reserved. (Quota exceeded.)
    DQuot,

    /// File exists.
    Exist,

    /// Bad address.
    Fault,

    /// File too large.
    FBig,

    /// Host is unreachable.
    HostUnreach,

    /// Identifier removed.
    IdRm,

    /// Illegal byte sequence.
    IlSeq,

    /// Operation in progress.
    InProgress,

    /// Interrupted function.
    Intr,

    /// Invalid argument.
    Inval,

    /// I/O error.
    Io,

    /// Socket is connected.
    IsConn,

    /// Is a directory.
    IsDir,

    /// Too many levels of symbolic links.
    Loop,

    /// File descriptor value too large.
    MFile,

    /// Too many links.
    MLink,

    /// Message too large.
    MsgSize,

    /// Reserved. (Multihop attempted.)
    Multihop,

    /// Filename too long.
    NameTooLong,

    /// Network is down.
    NetDown,

    /// Connection aborted by network.
    NetReset,

    /// Network unreachable.
    NetUnreach,

    /// Too many files open in system.
    NFile,

    /// No buffer space available.
    NoBufS,

    /// No such device.
    NoDev,

    /// No such file or directory.
    NoEnt,

    /// Executable file format error.
    NoExec,

    /// No locks available.
    NoLock,

    /// Reserved. (Link has been severed.)
    NoLink,

    /// Not enough space.
    NoMem,

    /// No message of the desired type.
    NoMsg,

    /// Protocol not available.
    NoProtoOpt,

    /// No space left on device.
    NoSpace,

    /// Function not supported. (Always unsupported.)
    NoSys,

    /// The socket is not connected.
    NotConn,

    /// Not a directory or a symbolic link to a directory.
    NotDir,

    /// Directory not empty.
    NotEmpty,

    /// State not recoverable.
    NotRecoverable,

    /// Not a socket.
    NotSock,

    /// Not supported, or operation not supported on socket. (Transient unsupported.)
    NotSup,

    /// Inappropriate I/O control operation.
    NoTty,

    /// No such device or address.
    NxIo,

    /// Value too large to be stored in data type.
    Overflow,

    /// Previous owner died.
    OwnerDead,

    /// Operation not permitted.
    Perm,

    /// Broken pipe.
    Pipe,

    /// Protocol error.
    Proto,

    /// Protocol not supported.
    ProtoNoSupport,

    /// Protocol wrong type for socket.
    ProtoType,

    /// Result too large.
    Range,

    /// Read-only file system.
    RoFs,

    /// Invalid seek.
    SPipe,

    /// No such process.
    Srch,

    /// Reserved. (Stale file handle.)
    Stale,

    /// Connection timed out.
    TimedOut,

    /// Text file busy.
    TxtBsy,

    /// Cross-device link.
    XDev,

    /// Extension: Capabilities insufficient.
    NotCapable,
}

impl From<std::io::Error> for ErrNo {
    fn from(err: std::io::Error) -> Self {
        use std::io::ErrorKind;
        use ErrNo::*;
        match err.kind() {
            ErrorKind::NotFound => NoEnt,
            ErrorKind::PermissionDenied => Access,
            ErrorKind::ConnectionRefused => ConnRefused,
            ErrorKind::ConnectionReset => ConnReset,
            ErrorKind::ConnectionAborted => ConnAborted,
            ErrorKind::NotConnected => NotConn,
            ErrorKind::AddrInUse => AddrInUse,
            ErrorKind::AddrNotAvailable => AddrNotAvail,
            ErrorKind::BrokenPipe => Pipe,
            ErrorKind::AlreadyExists => Exist,
            ErrorKind::WouldBlock => Again,
            ErrorKind::InvalidInput | ErrorKind::InvalidData => Inval,
            ErrorKind::TimedOut => TimedOut,
            ErrorKind::Interrupted => Intr,
            ErrorKind::WriteZero | ErrorKind::Other | ErrorKind::UnexpectedEof | _ => Io,
            // _ => ,
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Event {
    pub user_data: UserData,
    pub error: ErrNo,
    pub ty: EventType,
    pub fd_state: Option<EventFdState>, // only valid when `ty \in {FdRead, FdWrite}`
}

#[repr(u8)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Prim)]
pub enum EventType {
    /// The time value of clock `SubscriptionType::clock.clock_id` has reached timestamp
    /// `Subscription::clock.timeout`.
    Clock,

    /// File descriptor `SubscriptionType::FdRw.fd` has data available for reading.
    /// This event always triggers for regular files.
    FdRead,

    /// File descriptor `SubscriptionType::FdRw.fd` has capacity available for writing.
    /// This event always triggers for regular files.
    FdWrite,
}

/// The state of the file descriptor subscribed to with `EventType::FdRead` or `EventType::FdWrite`.
#[repr(u16)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Prim)]
#[prim(ty = "u16")]
pub enum EventRwFlags {
    None,
    Hangup,
}

pub type ExitCode = u32;

#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct EventFdState {
    pub file_size: FileSize,
    pub flags: EventRwFlags,
}

/// A file descriptor number.
/// As in POSIX, 0, 1, and 2 are stdin, stdout, and stderr, respectively.
/// File descriptors are not guaranteed to be contiguous or allocated in ascending order.
/// Information about a file descriptor may be obtained through `fd_prestat_get`.
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Prim)]
pub struct Fd(u32);

bitflags! {
    #[derive(Default)]
    pub struct FdFlags: u16 {
        /// Append mode: Data written to the file is always appended to the file's end.
        const APPEND = 1 << 0;

        /// Write according to synchronized I/O data integrity completion.
        /// Only the data stored in the file is synchronized.
        const DSYNC = 1 << 1;

        /// Non-blocking mode.
        const NONBLOCK = 1 << 2;

        /// Synchronized read I/O operations.
        const RSYNC = 1 << 3;

        /// Write according to synchronized I/O file integrity completion. In addition to synchronizing
        /// the data stored in the file, the implementation may also synchronously update the file's
        /// metadata.
        const SYNC = 1 << 4;
    }
}

bitflags! {
    #[derive(Default)]
    pub struct OpenFlags: u16 {
        /// Create file if it does not exist.
        const CREATE = 1 << 0;

        /// Fail if not a directory.
        const DIRECTORY = 1 << 1;

        /// Fail if file already exists.
        const EXCL = 1 << 2;

        /// Truncate file to size 0.
        const TRUNC = 1 << 3;
    }
}

#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct FdStat {
    pub file_type: FileType,
    pub flags: FdFlags,

    /// Rights that apply to this file descriptor.
    pub rights_base: Rights,

    /// Maximum set of rights that may be installed on new file descriptors that are created
    /// through this file descriptor.
    pub rights_inheriting: Rights,
}

/// Relative offset within a file.
pub type FileDelta = i64;

/// The type of a file descriptor or file.
#[repr(u8)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Prim)]
pub enum FileType {
    Unknown,
    BlockDevice,
    CharacterDevice,
    Directory,
    RegularFile,
    SocketDgram,
    SocketStream,
    SymbolicLink,
}

pub type FileSize = u64;

/// File attributes.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(C)]
pub struct FileStat {
    pub device: Device,
    pub inode: Inode,
    pub file_type: FileType,
    pub num_links: LinkCount,
    pub file_size: FileSize,
    pub atime: Timestamp,
    pub mtime: Timestamp,
    pub ctime: Timestamp,
}

/// File serial number that is unique within its file system.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Prim)]
pub struct Inode(u64);

pub type Size = u32;
pub type Pointer = u32;

/// A region of memory for scatter/gather reads.
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct IoVec {
    pub buf: Pointer,
    pub len: Size,
}

/// Number of hard links to an inode.
pub type LinkCount = u32;

bitflags! {
    #[derive(Default)]
    pub struct LookupFlags: u32 {
        /// Follow symlinks.
        const SYMLINK_FOLLOW = 1 << 0;
    }
}

/// Information about a preopened resource.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Prestat {
    pub resource_type: PreopenType,
}

#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PreopenType {
    Dir { name_len: Size },
}

bitflags! {
    #[derive(Default)]
    pub struct Rights: u64 {
        const FD_DATASYNC             = 1 << 0;
        const FD_READ                 = 1 << 1;
        const FD_SEEK                 = 1 << 2;
        const FD_FDSTAT_SET_FLAGS     = 1 << 3;
        const FD_SYNC                 = 1 << 4;
        const FD_TELL                 = 1 << 5;
        const FD_WRITE                = 1 << 6;
        const FD_ADVISE               = 1 << 7;
        const FD_ALLOCATE             = 1 << 8;
        const PATH_CREATE_DIRECTORY   = 1 << 9;
        const PATH_CREATE_FILE        = 1 << 10;
        const PATH_LINK_SOURCE        = 1 << 11;
        const PATH_LINK_TARGET        = 1 << 12;
        const PATH_OPEN               = 1 << 13;
        const FD_READDIR              = 1 << 14;
        const PATH_READLINK           = 1 << 15;
        const PATH_RENAME_SOURCE      = 1 << 16;
        const PATH_RENAME_TARGET      = 1 << 17;
        const PATH_FILESTAT_GET       = 1 << 18;
        const PATH_FILESTAT_SET_SIZE  = 1 << 19;
        const PATH_FILESTAT_SET_TIMES = 1 << 20;
        const FD_FILESTAT_GET         = 1 << 21;
        const FD_FILESTAT_SET_SIZE    = 1 << 22;
        const FD_FILESTAT_SET_TIMES   = 1 << 23;
        const PATH_SYMLINK            = 1 << 24;
        const PATH_REMOVE_DIRECTORY   = 1 << 25;
        const PATH_UNLINK_FILE        = 1 << 26;
        const POLL_FD_READWRITE       = 1 << 27;
    }
}

/// Signal condition.
#[repr(u8)]
#[derive(Clone, Copy, PartialEq, Prim)]
pub enum Signal {
    Reserved,
    Abort,
    Alarm,
    Bus,
    Child,
    Cont,
    FP,
    Hup,
    Ill,
    Int,
    Kill,
    Pipe,
    Quit,
    Seg,
    Stop,
    Sys,
    Term,
    Trap,
    TStp,
    TTIn,
    TTOut,
    Urg,
    Usr1,
    Usr2,
    VTAlrm,
    XCpu,
    XFSz,
}

/// Timestamp in nanoseconds.
#[derive(Prim, Clone, Copy, Debug, PartialEq, Eq)]
pub struct Timestamp(u64);

impl Timestamp {
    pub fn from_nanos(nanos: u64) -> Self {
        Timestamp(nanos)
    }

    pub fn from_sec(sec: u64) -> Self {
        Self::from_nanos(sec * 1_000_000_000)
    }

    pub fn as_nanos(&self) -> u64 {
        self.0
    }
}

bitflags! {
    pub struct SetTimeFlags: u16 {
        const ATIME     = 1 << 0;
        const ATIME_NOW = 1 << 1;
        const MTIME     = 1 << 2;
        const MTIME_NOW = 1 << 3;
    }
}

pub type UserData = u64;

/// The position relative to which to set the offset of the file descriptor.
#[repr(u8)]
#[derive(Clone, Copy, Debug, PartialEq, Prim)]
pub enum Whence {
    Current,
    End,
    Start,
}