stable_fs/runtime/
types.rs

1#![allow(dead_code)]
2
3use bitflags::bitflags;
4use serde::{Deserialize, Serialize};
5
6// file descriptor
7pub type Fd = u32;
8
9pub type Rights = u64;
10/// The right to invoke `fd_datasync`.
11/// If `path_open` is set, includes the right to invoke
12/// `path_open` with `fdflags::dsync`.
13pub const RIGHTS_FD_DATASYNC: Rights = 1 << 0;
14/// The right to invoke `fd_read` and `sock_recv`.
15/// If `rights::fd_seek` is set, includes the right to invoke `fd_pread`.
16pub const RIGHTS_FD_READ: Rights = 1 << 1;
17/// The right to invoke `fd_seek`. This flag implies `rights::fd_tell`.
18pub const RIGHTS_FD_SEEK: Rights = 1 << 2;
19/// The right to invoke `fd_fdstat_set_flags`.
20pub const RIGHTS_FD_FDSTAT_SET_FLAGS: Rights = 1 << 3;
21/// The right to invoke `fd_sync`.
22/// If `path_open` is set, includes the right to invoke
23/// `path_open` with `fdflags::rsync` and `fdflags::dsync`.
24pub const RIGHTS_FD_SYNC: Rights = 1 << 4;
25/// The right to invoke `fd_seek` in such a way that the file offset
26/// remains unaltered (i.e., `whence::cur` with offset zero), or to
27/// invoke `fd_tell`.
28pub const RIGHTS_FD_TELL: Rights = 1 << 5;
29/// The right to invoke `fd_write` and `sock_send`.
30/// If `rights::fd_seek` is set, includes the right to invoke `fd_pwrite`.
31pub const RIGHTS_FD_WRITE: Rights = 1 << 6;
32/// The right to invoke `fd_advise`.
33pub const RIGHTS_FD_ADVISE: Rights = 1 << 7;
34/// The right to invoke `fd_allocate`.
35pub const RIGHTS_FD_ALLOCATE: Rights = 1 << 8;
36/// The right to invoke `path_create_directory`.
37pub const RIGHTS_PATH_CREATE_DIRECTORY: Rights = 1 << 9;
38/// If `path_open` is set, the right to invoke `path_open` with `oflags::creat`.
39pub const RIGHTS_PATH_CREATE_FILE: Rights = 1 << 10;
40/// The right to invoke `path_link` with the file descriptor as the
41/// source directory.
42pub const RIGHTS_PATH_LINK_SOURCE: Rights = 1 << 11;
43/// The right to invoke `path_link` with the file descriptor as the
44/// target directory.
45pub const RIGHTS_PATH_LINK_TARGET: Rights = 1 << 12;
46/// The right to invoke `path_open`.
47pub const RIGHTS_PATH_OPEN: Rights = 1 << 13;
48/// The right to invoke `fd_readdir`.
49pub const RIGHTS_FD_READDIR: Rights = 1 << 14;
50/// The right to invoke `path_readlink`.
51pub const RIGHTS_PATH_READLINK: Rights = 1 << 15;
52/// The right to invoke `path_rename` with the file descriptor as the source directory.
53pub const RIGHTS_PATH_RENAME_SOURCE: Rights = 1 << 16;
54/// The right to invoke `path_rename` with the file descriptor as the target directory.
55pub const RIGHTS_PATH_RENAME_TARGET: Rights = 1 << 17;
56/// The right to invoke `path_filestat_get`.
57pub const RIGHTS_PATH_FILESTAT_GET: Rights = 1 << 18;
58/// The right to change a file's size (there is no `path_filestat_set_size`).
59/// If `path_open` is set, includes the right to invoke `path_open` with `oflags::trunc`.
60pub const RIGHTS_PATH_FILESTAT_SET_SIZE: Rights = 1 << 19;
61/// The right to invoke `path_filestat_set_times`.
62pub const RIGHTS_PATH_FILESTAT_SET_TIMES: Rights = 1 << 20;
63/// The right to invoke `fd_filestat_get`.
64pub const RIGHTS_FD_FILESTAT_GET: Rights = 1 << 21;
65/// The right to invoke `fd_filestat_set_size`.
66pub const RIGHTS_FD_FILESTAT_SET_SIZE: Rights = 1 << 22;
67/// The right to invoke `fd_filestat_set_times`.
68pub const RIGHTS_FD_FILESTAT_SET_TIMES: Rights = 1 << 23;
69/// The right to invoke `path_symlink`.
70pub const RIGHTS_PATH_SYMLINK: Rights = 1 << 24;
71/// The right to invoke `path_remove_directory`.
72pub const RIGHTS_PATH_REMOVE_DIRECTORY: Rights = 1 << 25;
73/// The right to invoke `path_unlink_file`.
74pub const RIGHTS_PATH_UNLINK_FILE: Rights = 1 << 26;
75/// If `rights::fd_read` is set, includes the right to invoke `poll_oneoff` to subscribe to `eventtype::fd_read`.
76/// If `rights::fd_write` is set, includes the right to invoke `poll_oneoff` to subscribe to `eventtype::fd_write`.
77pub const RIGHTS_POLL_FD_READWRITE: Rights = 1 << 27;
78
79#[derive(Copy, Clone, Debug)]
80pub struct FdStat {
81    pub flags: FdFlags,
82    pub rights_base: Rights,
83    pub rights_inheriting: Rights,
84}
85
86impl Default for FdStat {
87    fn default() -> Self {
88        Self {
89            flags: FdFlags::empty(),
90            rights_base: (1 << 28) - 1, // allow anything by default appart from sockets
91            rights_inheriting: (1 << 28) - 1, // allow anything appart from sockets
92        }
93    }
94}
95
96#[derive(Clone, Copy, Debug)]
97pub enum Whence {
98    SET,
99    CUR,
100    END,
101}
102
103#[derive(Clone, Copy, Debug)]
104pub enum ChunkSize {
105    CHUNK4K = 4096,
106    CHUNK8K = 8192,
107    CHUNK16K = 16384,
108    CHUNK32K = 32768,
109    CHUNK64K = 65536,
110}
111
112impl ChunkSize {
113    pub const VALUES: [Self; 5] = [
114        Self::CHUNK4K,
115        Self::CHUNK8K,
116        Self::CHUNK16K,
117        Self::CHUNK32K,
118        Self::CHUNK64K,
119    ];
120}
121
122#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
123pub enum ChunkType {
124    V1 = 1,
125    V2 = 2,
126}
127
128#[derive(Clone, Copy, Debug)]
129pub enum Advice {
130    Normal = 0,
131    Sequential = 1,
132    Random = 2,
133    WillNeed = 3,
134    DontNeed = 4,
135    NoReuse = 5,
136}
137
138impl TryFrom<u8> for Advice {
139    type Error = crate::error::Error;
140
141    fn try_from(value: u8) -> Result<Self, crate::error::Error> {
142        match value {
143            0 => Ok(Advice::Normal),
144            1 => Ok(Advice::Sequential),
145            2 => Ok(Advice::Random),
146            3 => Ok(Advice::WillNeed),
147            4 => Ok(Advice::DontNeed),
148            5 => Ok(Advice::NoReuse),
149            _ => Err(crate::error::Error::InvalidArgument),
150        }
151    }
152}
153
154impl From<Advice> for u8 {
155    fn from(val: Advice) -> Self {
156        match val {
157            Advice::Normal => 0,
158            Advice::Sequential => 1,
159            Advice::Random => 2,
160            Advice::WillNeed => 3,
161            Advice::DontNeed => 4,
162            Advice::NoReuse => 5,
163        }
164    }
165}
166
167bitflags! {
168    #[derive(Copy, Clone, Debug, PartialEq)]
169    pub struct FdFlags: u16 {
170        const APPEND = 1;
171        const DSYNC = 2;
172        const NONBLOCK = 4;
173        const RSYNC = 8;
174        const SYNC = 16;
175    }
176}
177
178bitflags! {
179    pub struct OpenFlags: u16 {
180        /// Create file if it does not exist.
181        const CREATE = 1;
182        /// Fail if not a directory.
183        const DIRECTORY = 2;
184        /// Fail if file already exists.
185        const EXCLUSIVE = 4;
186        /// Truncate file to size 0.
187        const TRUNCATE = 8;
188    }
189}
190
191#[repr(C)]
192#[derive(Copy, Clone, Debug)]
193pub struct DstBuf {
194    pub buf: *mut u8,
195    pub len: usize,
196}
197#[repr(C)]
198#[derive(Copy, Clone, Debug)]
199pub struct SrcBuf {
200    pub buf: *const u8,
201    pub len: usize,
202}
203pub type SrcIoVec<'a> = &'a [SrcBuf];
204pub type DstIoVec<'a> = &'a [DstBuf];