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
pub mod attrs;
pub mod basic;
pub mod error;
mod runner;

pub use crate::runner::Runner;

use crate::attrs::*;
use crate::error::{FSError, FSResult};

use std::ffi::{OsStr, OsString};
use std::io::BufRead;
use std::time::Duration;

use typed_builder::TypedBuilder;

#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq)]
pub struct INode(u64);

impl INode {
    pub const fn to_u64(self) -> u64 {
        self.0
    }

    const fn next_inode(self) -> INode {
        INode(self.0 + 1)
    }
}

impl From<u64> for INode {
    fn from(i: u64) -> INode {
        INode(i)
    }
}

#[derive(Debug, TypedBuilder)]
pub struct Lookup {
    attributes: FileAttributes,
    inode: INode,

    #[builder(default = None)]
    generation: Option<u64>,

    #[builder(default = Some(Duration::from_secs(1)))]
    attr_timeout: Option<Duration>,

    #[builder(default = Some(Duration::from_secs(1)))]
    entry_timeout: Option<Duration>,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Filehandle(u64);

impl Filehandle {
    pub const fn from_raw(old: u64) -> Self {
        Self(old)
    }

    pub const fn to_raw(self) -> u64 {
        self.0
    }
}

#[derive(Debug, TypedBuilder)]
pub struct OpenFile {
    handle: Filehandle,

    #[builder(default = true)]
    direct_io: bool,

    #[builder(default = false)]
    keep_cache: bool,

    #[builder(default = true)]
    seekable: bool,
}

#[derive(Debug, TypedBuilder)]
pub struct OpenDir {
    handle: Filehandle,

    #[builder(default = true)]
    direct_io: bool,

    #[builder(default = false)]
    keep_cache: bool,

    #[builder(default = true)]
    seekable: bool,

    #[builder(default = true)]
    cache_dir: bool,
}

#[derive(Debug, Copy, Clone)]
pub enum FileType {
    FIFO,
    Unknown,
    Regular,
    Directory,
    Socket,
    Char,
    Block,
    Link,
}

impl FileType {
    pub const fn to_libc_type(self) -> u8 {
        match self {
            Self::FIFO => libc::DT_FIFO,
            Self::Unknown => libc::DT_UNKNOWN,
            Self::Regular => libc::DT_REG,
            Self::Directory => libc::DT_DIR,
            Self::Socket => libc::DT_SOCK,
            Self::Char => libc::DT_CHR,
            Self::Block => libc::DT_BLK,
            Self::Link => libc::DT_LNK,
        }
    }
}

#[derive(Debug, TypedBuilder, Clone)]
pub struct DirEntry {
    name: OsString,
    inode: INode,
    typ: FileType,
    offset: u64,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum SetXAttrFlags {
    Create,
    Replace,
}

impl SetXAttrFlags {
    pub const fn to_libc_type(self) -> i32 {
        match self {
            Self::Create => libc::XATTR_CREATE,
            Self::Replace => libc::XATTR_REPLACE,
        }
    }

    pub const fn from_libc_type(from: i32) -> Option<Self> {
        let create = from & libc::XATTR_CREATE != 0;
        let replace = from & libc::XATTR_REPLACE != 0;

        if create && replace {
            // (false && false) and (true && true)
            None
        } else if create {
            Some(Self::Create)
        } else if replace {
            Some(Self::Replace)
        } else {
            // I don't think this case would ever actually be called
            None
        }
    }
}

/// Acts as a reference to an xattr, containing a slice for the requested data along with the
/// length of its data source.
#[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct XAttrRef<'a> {
    /// Represents the actual, full size of `data`. This is not the same as `data.len()`, but
    /// rather then length of the origianl source of the slice.
    full_len: usize,
    data: &'a [u8],
}

impl<'a> XAttrRef<'a> {
    pub fn new(data: &'a [u8], full_len: usize) -> XAttrRef<'a> {
        XAttrRef { full_len, data }
    }

    pub fn full_len(&self) -> usize {
        self.full_len
    }

    pub fn data(&self) -> &[u8] {
        self.data
    }
}

pub trait Filesystem {
    fn open(&mut self, _ino: INode, _flags: u32) -> FSResult<OpenFile> {
        Err(FSError::NotImplemented)
    }

    fn open_dir(&mut self, _ino: INode, _flags: u32) -> FSResult<OpenDir> {
        Err(FSError::NotImplemented)
    }

    fn lookup(&mut self, _parent: INode, _name: &OsStr) -> FSResult<Lookup> {
        Err(FSError::NotImplemented)
    }

    fn getattr(&mut self, _inode: INode) -> FSResult<FileAttributes> {
        Err(FSError::NotImplemented)
    }

    fn setattr(&mut self, _inode: INode, _attr: SetFileAttributes) -> FSResult<FileAttributes> {
        Err(FSError::NotImplemented)
    }

    fn setxattr(
        &mut self,
        _ino: INode,
        _attr_name: &OsStr,
        _attr_value: &[u8],
        _flags: SetXAttrFlags,
    ) -> FSResult<()> {
        Err(FSError::NotImplemented)
    }

    /// When `max_len == 0`, this is functionally requesting only the length of the requested
    /// attribute.
    fn getxattr(
        &mut self,
        _ino: INode,
        _attr_name: &OsStr,
        _max_len: u32,
    ) -> FSResult<XAttrRef<'_>> {
        Err(FSError::NotImplemented)
    }

    /// When `max_len` is 0, the return value should be an empty string and the length of all the
    /// attributes with an additional nul byte.
    ///
    /// When `max_len` is greater than 0, this function should return an `OsString` composed of all
    /// the xattr names seperated by a nul (\0) byte. If the length of that string is greater than
    /// `max_len`, however, the method should error and return `FSError::BufferWouldOverflow`.
    fn listxattrs(&mut self, _ino: INode, _max_len: u32) -> FSResult<(OsString, u32)> {
        Err(FSError::NotImplemented)
    }

    /// Reads a directory.
    ///
    /// # Warning
    /// This method **must** include the "." and ".." directories, as well as properly accounting
    /// for `offset`. If not, some operations may get stuck in an infinite loop while trying to
    /// read a directory.
    fn readdir(&mut self, _dir: INode, _offset: u64) -> FSResult<Vec<DirEntry>> {
        Err(FSError::NotImplemented)
    }

    fn read(&mut self, _ino: INode, _offset: u64, _size: u32) -> FSResult<&[u8]> {
        Err(FSError::NotImplemented)
    }

    /// Returns the amount of bytes written
    fn write<T: BufRead>(
        &mut self,
        _ino: INode,
        _offset: u64,
        _size: u32,
        _buf: T,
    ) -> FSResult<u32> {
        Err(FSError::NotImplemented)
    }
}