#![feature(non_exhaustive)]
#[macro_use]
extern crate bitflags;
#[macro_use]
extern crate proper;
#[repr(u8)]
#[derive(Clone, Copy, PartialEq, Prim)]
pub enum Advice {
Normal,
Sequential,
Random,
DontNeed,
NoReuse,
WillNeed,
}
#[repr(u8)]
#[derive(Clone, Copy, PartialEq, Prim)]
pub enum ClockId {
RealTime,
Monotonic,
ProcessCpuTime,
ThreadCpuTime,
}
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Prim)]
pub struct Device(u64);
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Prim)]
pub struct DirCookie(u64);
impl DirCookie {
pub fn start() -> Self {
DirCookie(0)
}
}
#[repr(C)]
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct DirEnt {
pub next: DirCookie,
pub inode: Inode,
pub name_len: u32,
pub file_type: FileType,
}
#[repr(u16)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Prim)]
#[prim(ty = "u16")]
#[non_exhaustive]
pub enum ErrNo {
Success,
TooBig,
Access,
AddrInUse,
AddrNotAvail,
AfNoSupport,
Again,
Already,
BadF,
BadMsg,
Busy,
Canceled,
Child,
ConnAborted,
ConnRefused,
ConnReset,
Deadlk,
DestAddrReq,
Domain,
DQuot,
Exist,
Fault,
FBig,
HostUnreach,
IdRm,
IlSeq,
InProgress,
Intr,
Inval,
Io,
IsConn,
IsDir,
Loop,
MFile,
MLink,
MsgSize,
Multihop,
NameTooLong,
NetDown,
NetReset,
NetUnreach,
NFile,
NoBufS,
NoDev,
NoEnt,
NoExec,
NoLock,
NoLink,
NoMem,
NoMsg,
NoProtoOpt,
NoSpace,
NoSys,
NotConn,
NotDir,
NotEmpty,
NotRecoverable,
NotSock,
NotSup,
NoTty,
NxIo,
Overflow,
OwnerDead,
Perm,
Pipe,
Proto,
ProtoNoSupport,
ProtoType,
Range,
RoFs,
SPipe,
Srch,
Stale,
TimedOut,
TxtBsy,
XDev,
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>, }
#[repr(u8)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Prim)]
pub enum EventType {
Clock,
FdRead,
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,
}
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Prim)]
pub struct Fd(u32);
bitflags! {
#[derive(Default)]
pub struct FdFlags: u16 {
const APPEND = 1 << 0;
const DSYNC = 1 << 1;
const NONBLOCK = 1 << 2;
const RSYNC = 1 << 3;
const SYNC = 1 << 4;
}
}
bitflags! {
#[derive(Default)]
pub struct OpenFlags: u16 {
const CREATE = 1 << 0;
const DIRECTORY = 1 << 1;
const EXCL = 1 << 2;
const TRUNC = 1 << 3;
}
}
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct FdStat {
pub file_type: FileType,
pub flags: FdFlags,
pub rights_base: Rights,
pub rights_inheriting: Rights,
}
pub type FileDelta = i64;
#[repr(u8)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Prim)]
pub enum FileType {
Unknown,
BlockDevice,
CharacterDevice,
Directory,
RegularFile,
SocketDgram,
SocketStream,
SymbolicLink,
}
pub type FileSize = u64;
#[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,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Prim)]
pub struct Inode(u64);
pub type Size = u32;
pub type Pointer = u32;
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct IoVec {
pub buf: Pointer,
pub len: Size,
}
pub type LinkCount = u32;
bitflags! {
#[derive(Default)]
pub struct LookupFlags: u32 {
const SYMLINK_FOLLOW = 1 << 0;
}
}
#[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;
}
}
#[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,
}
#[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;
#[repr(u8)]
#[derive(Clone, Copy, Debug, PartialEq, Prim)]
pub enum Whence {
Current,
End,
Start,
}