efs 0.5.0

An OS and architecture independent implementation of some Unix filesystems in Rust.
Documentation
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
//! General interface for Unix files.
//!
//! See [this Wikipedia page](https://en.wikipedia.org/wiki/Unix_file_types) and [the POSIX header of `<sys/stat.h>`](https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/sys_stat.h.html) for more information.

use alloc::vec::Vec;

use deku::no_std_io::{Read, Seek, Write};

use crate::error::Error;
use crate::fs::permissions::Permissions;
use crate::fs::types::{Blkcnt, Blksize, Dev, Gid, Ino, Mode, Nlink, Off, Timespec, Uid};
use crate::path::{PARENT_DIR, UnixStr};

/// Minimal stat structure.
///
/// More information on [the POSIX definition](https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/sys_stat.h.html).
#[derive(Debug, Clone)]
pub struct Stat {
    /// Device ID of device containing file.
    pub dev: Dev,

    /// File serial number.
    pub ino: Ino,

    /// Mode of file.
    pub mode: Mode,

    /// Number of hard links to the file.
    pub nlink: Nlink,

    /// User ID of file.
    pub uid: Uid,

    /// Group ID of file.
    pub gid: Gid,

    /// Device ID (if file is character or block special).
    pub rdev: Dev,

    /// For regular files, the file size in bytes.
    ///
    /// For symbolic links, the length in bytes of the pathname contained in the symbolic link.
    pub size: Off,

    /// Last data access time stamp.
    pub atim: Timespec,

    /// Last data modification time stamp.
    pub mtim: Timespec,

    /// Last file status change time stamp.
    pub ctim: Timespec,

    /// A file system-specific preferred I/O block size for this object. In some file system types, this may vary from
    /// file to file.
    pub blksize: Blksize,

    /// Number of blocks allocated for this object.
    pub blkcnt: Blkcnt,
}

/// Base trait to ensure a common filesystem error type.
pub trait Base {
    /// Error type corresponding to the [`FileSystem`](crate::fs::Filesystem) implemented.
    type FsError: core::error::Error;
}

/// A readable UNIX file.
///
/// This type can be used alone for read-only filesystems.
pub trait FileRead: Base {
    /// Retrieves information about this file.
    fn stat(&self) -> Stat;

    /// Retrieves the [`Type`] of this file.
    fn get_type(&self) -> Type;

    /// Returns the [`Permissions`] of this file.
    fn permissions(&self) -> Permissions {
        self.stat().mode.into()
    }
}

/// Main trait for all UNIX files.
///
/// Defined in [this POSIX definition](https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap03.html#tag_03_164).
pub trait File: FileRead {
    /// Sets the [`Mode`] of this file.
    ///
    /// The [`Permissions`] structure can be use as it's more convenient.
    ///
    /// # Errors
    ///
    /// Returns an [`Error::IO`] if the device on which the directory is located could not
    /// be read.
    fn set_mode(&mut self, mode: Mode) -> Result<(), Error<Self::FsError>>;

    /// Sets the [`Uid`] (identifier of the user owner) of this file.
    ///
    /// # Errors
    ///
    /// Returns an [`Error::IO`] if the device on which the directory is located could not
    /// be read.
    fn set_uid(&mut self, uid: Uid) -> Result<(), Error<Self::FsError>>;

    /// Sets the [`Gid`] (identifier of the group) of this file.
    ///
    /// # Errors
    ///
    /// Returns an [`Error::IO`] if the device on which the directory is located could not
    /// be read.
    fn set_gid(&mut self, gid: Gid) -> Result<(), Error<Self::FsError>>;

    /// Sets the `atim` (last data access time) of this file.
    ///
    /// # Errors
    ///
    /// Returns an [`Error::IO`] if the device on which the directory is located could not
    /// be read.
    fn set_atim(&mut self, atim: Timespec) -> Result<(), Error<Self::FsError>>;

    /// Sets the `mtim` (last data modification time) of this file.
    ///
    /// # Errors
    ///
    /// Returns an [`Error::IO`] if the device on which the directory is located could not
    /// be read.
    fn set_mtim(&mut self, mtim: Timespec) -> Result<(), Error<Self::FsError>>;

    /// Sets the `ctim` (last file status change time) of this file.
    ///
    /// # Errors
    ///
    /// Returns an [`Error::IO`] if the device on which the directory is located could not
    /// be read.
    fn set_ctim(&mut self, ctim: Timespec) -> Result<(), Error<Self::FsError>>;
}

/// A readable [`Regular`] file.
///
/// This type can be used alone for read-only filesystems.
pub trait RegularRead: FileRead + Read + Seek {}

/// A file that is a randomly accessible sequence of bytes, with no further structure imposed by the system.
///
/// Defined in [this POSIX definition](https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap03.html#tag_03_323).
pub trait Regular: File + RegularRead + Write {
    /// Trunctates the file size to the given `size` (in bytes).
    ///
    /// If the given `size` is greater than the previous file size, this function does nothing.
    ///
    /// # Errors
    ///
    /// Returns an [`Error::IO`] if the device on which the directory is located could not
    /// be read.
    fn truncate(&mut self, size: u64) -> Result<(), Error<<Self as Base>::FsError>>;
}

/// An object that associates a filename with a file. Several directory entries can associate names with the same file.
///
/// Defined in [this POSIX definition](https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap03.html#tag_03_130).
pub struct DirectoryEntry<'path, Dir: DirectoryRead> {
    /// Name of the file pointed by this directory entry.
    ///
    /// See more information on valid filenames in [this POSIX definition](https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap03.html#tag_03_170).
    pub filename: UnixStr<'path>,

    /// File pointed by this directory entry.
    pub file: TypeWithFile<Dir>,
}

// A readable [`Directory`] file.
//
/// This type can be used alone for read-only filesystems.
pub trait DirectoryRead: Sized + Base {
    /// Type of the regular files in the [`Filesystem`](crate::fs::Filesystem) this directory belongs to.
    type Regular: RegularRead<FsError = <Self as Base>::FsError>;

    /// Type of the symbolic links in the [`Filesystem`](crate::fs::Filesystem) this directory belongs to.
    type SymbolicLink: SymbolicLinkRead<FsError = <Self as Base>::FsError>;

    /// Type of the fifo in the [`Filesystem`](crate::fs::Filesystem) this directory belongs to.
    type Fifo: FifoRead<FsError = <Self as Base>::FsError>;

    /// Type of the character device in the [`Filesystem`](crate::fs::Filesystem) this directory belongs to.
    type CharacterDevice: CharacterDeviceRead<FsError = <Self as Base>::FsError>;

    /// Type of the character device in the [`Filesystem`](crate::fs::Filesystem) this directory belongs to.
    type BlockDevice: BlockDeviceRead<FsError = <Self as Base>::FsError>;

    /// Type of the UNIX socket in the [`Filesystem`](crate::fs::Filesystem) this directory belongs to.
    type Socket: SocketRead<FsError = <Self as Base>::FsError>;

    /// Returns the directory entries contained.
    ///
    /// No two [`DirectoryEntry`] returned can have the same `filename`.
    ///
    /// The result must contain at least the entries `.` (the current directory) and `..` (the parent directory).
    ///
    /// # Errors
    ///
    /// Returns an [`Error::IO`] if the device on which the directory is located could not
    /// be read.
    fn entries(&self) -> Result<Vec<DirectoryEntry<'_, Self>>, Error<Self::FsError>>;

    /// Returns the entry with the given name.
    ///
    /// # Errors
    ///
    /// Returns an [`Error::IO`] if the device on which the directory is located could not
    /// be read.
    fn entry(&self, name: UnixStr) -> Result<Option<TypeWithFile<Self>>, Error<Self::FsError>> {
        let children = self.entries()?;
        Ok(children.into_iter().find(|entry| entry.filename == name).map(|entry| entry.file))
    }

    /// Returns the parent directory.
    ///
    /// If `self` if the root directory, it must return itself.
    ///
    /// # Errors
    ///
    /// Returns an [`Error::IO`] if the device on which the directory is located could not
    /// be read.
    fn parent(&self) -> Result<Self, Error<Self::FsError>> {
        let Some(TypeWithFile::Directory(parent_entry)) = self.entry(PARENT_DIR.clone())? else {
            unreachable!("`entries` must return `..` that corresponds to the parent directory.")
        };
        Ok(parent_entry)
    }
}

/// A file that contains directory entries. No two directory entries in the same directory have the same name.
///
/// Defined in [this POSIX definition](https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap03.html#tag_03_129).
pub trait Directory: File + DirectoryRead
where
    <Self as DirectoryRead>::Regular: Regular<FsError = <Self as Base>::FsError>,
    <Self as DirectoryRead>::SymbolicLink: SymbolicLink<FsError = <Self as Base>::FsError>,
    <Self as DirectoryRead>::Fifo: Fifo<FsError = <Self as Base>::FsError>,
    <Self as DirectoryRead>::CharacterDevice: CharacterDevice<FsError = <Self as Base>::FsError>,
    <Self as DirectoryRead>::BlockDevice: BlockDevice<FsError = <Self as Base>::FsError>,
    <Self as DirectoryRead>::Socket: Socket<FsError = <Self as Base>::FsError>,
{
    /// Adds a new empty entry to the directory, meaning that a new file will be created.
    ///
    /// # Errors
    ///
    /// Returns an [`EntryAlreadyExist`](crate::fs::error::FsError::EntryAlreadyExist) error if the entry already exist.
    ///
    /// Returns an [`Error::IO`] if the device on which the directory is located could not
    /// be written.
    fn add_entry(
        &mut self,
        name: UnixStr<'_>,
        file_type: Type,
        permissions: Permissions,
        user_id: Uid,
        group_id: Gid,
    ) -> Result<TypeWithFile<Self>, Error<Self::FsError>>;

    /// Removes an entry from the directory.
    ///
    /// # Errors
    ///
    /// Returns an [`NotFound`](crate::fs::error::FsError::NotFound) error if there is no entry with the given name in
    /// this directory.
    ///
    /// Returns an [`Error::IO`] if the device on which the directory is located could not
    /// be written.
    fn remove_entry(&mut self, name: UnixStr) -> Result<(), Error<Self::FsError>>;
}

/// A readable [`SymbolicLink`] file.
///
/// This type can be used alone for read-only filesystems.
pub trait SymbolicLinkRead: FileRead {
    /// Returns the string stored in this symbolic link.
    ///
    /// # Errors
    ///
    /// Returns an [`Error::IO`] if the device on which the directory is located could not
    /// be read.
    fn get_pointed_file(&self) -> Result<&str, Error<Self::FsError>>;
}

/// A type of file with the property that when the file is encountered during pathname resolution, a string stored by
/// the file is used to modify the pathname resolution.
///
/// Defined in [this POSIX definition](https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap03.html#tag_03_381).
pub trait SymbolicLink: File + SymbolicLinkRead {
    /// Sets the pointed file in this symbolic link.
    ///
    /// # Errors
    ///
    /// Returns an [`Error::IO`] if the device on which the directory is located could not
    /// be written.
    fn set_pointed_file(&mut self, pointed_file: &str) -> Result<(), Error<Self::FsError>>;
}

/// A readable [`Fifo`] file.
///
/// This type can be used alone for read-only filesystems.
pub trait FifoRead: FileRead {}

/// A type of file with the property that data written to such a file is read on a first-in-first-out basis.
///
/// Defined in [this POSIX defintion](https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap03.html#tag_03_163)
pub trait Fifo: File + FifoRead {}

/// A readable [`CharacterDevice`] file.
///
/// This type can be used alone for read-only filesystems.
pub trait CharacterDeviceRead: FileRead {}

/// A file that refers to a character device (such as a terminal device file) or that has special properties (such as
/// /dev/null).
///
/// Defined in [this POSIX definition](https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap03.html#tag_03_91)
pub trait CharacterDevice: File + CharacterDeviceRead {}

/// A readable [`BlockDevice`] file.
///
/// This type can be used alone for read-only filesystems.
pub trait BlockDeviceRead: FileRead {}

/// A file that refers to a block device.
///
/// A block special file is normally distinguished from a character special file by
/// providing access to the device in a manner such that the hardware characteristics of the device are not visible.
///
/// Defined in [this POSIX definition](https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap03.html#tag_03_79)
pub trait BlockDevice: File + BlockDeviceRead {}

/// A readable [`Socket`] file.
///
/// This type can be used alone for read-only filesystems.
pub trait SocketRead: FileRead {}

/// A file of a particular type that is used as a communications endpoint for process-to-process communication as
/// described in the System Interfaces volume of POSIX.1-2017.
///
/// Defined in [this POSIX definition](https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap03.html#tag_03_356)
pub trait Socket: File + SocketRead {}

/// Enumeration of possible file types in a standard UNIX-like filesystem.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Type {
    /// Storage unit of a filesystem.
    Regular,

    /// Node containing other nodes.
    Directory,

    /// Node pointing towards an other node in the filesystem.
    SymbolicLink,

    /// Named pipe.
    Fifo,

    /// An inode that refers to a device communicating by sending chars (bytes) of data.
    CharacterDevice,

    /// An inode that refers to a device communicating by sending blocks of data.
    BlockDevice,

    /// Communication flow between two processes.
    Socket,
}

/// Enumeration of possible file types in a standard UNIX-like filesystem with an attached file object.
#[allow(clippy::module_name_repetitions)]
#[derive(Debug, Clone)]
pub enum TypeWithFile<Dir: DirectoryRead> {
    /// Storage unit of a filesystem.
    Regular(Dir::Regular),

    /// Node containing other nodes.
    Directory(Dir),

    /// Node pointing towards an other node in the filesystem.
    SymbolicLink(Dir::SymbolicLink),

    /// Special node containing a [`Fifo`].
    Fifo(Dir::Fifo),

    /// Special node containing a [`CharacterDevice`].
    CharacterDevice(Dir::CharacterDevice),

    /// Special node containing a [`BlockDevice`].
    BlockDevice(Dir::BlockDevice),

    /// Special node containing a [`Socket`].
    Socket(Dir::Socket),
}

impl<Dir: DirectoryRead> TypeWithFile<Dir> {
    /// Whether this file is a regular file or not.
    pub const fn is_regular(&self) -> bool {
        match self {
            Self::Regular(_) => true,
            Self::Directory(_)
            | Self::SymbolicLink(_)
            | Self::Fifo(_)
            | Self::CharacterDevice(_)
            | Self::BlockDevice(_)
            | Self::Socket(_) => false,
        }
    }

    /// Whether this file is a directory or not.
    pub const fn is_directory(&self) -> bool {
        match self {
            Self::Directory(_) => true,
            Self::Regular(_)
            | Self::SymbolicLink(_)
            | Self::Fifo(_)
            | Self::CharacterDevice(_)
            | Self::BlockDevice(_)
            | Self::Socket(_) => false,
        }
    }

    /// Whether this file is a symbolic link or not.
    pub const fn is_symlink(&self) -> bool {
        match self {
            Self::SymbolicLink(_) => true,
            Self::Regular(_)
            | Self::Directory(_)
            | Self::Fifo(_)
            | Self::CharacterDevice(_)
            | Self::BlockDevice(_)
            | Self::Socket(_) => false,
        }
    }

    /// Whether this file is a fifo or not.
    pub const fn is_fifo(&self) -> bool {
        match self {
            Self::Fifo(_) => true,
            Self::Regular(_)
            | Self::Directory(_)
            | Self::SymbolicLink(_)
            | Self::CharacterDevice(_)
            | Self::BlockDevice(_)
            | Self::Socket(_) => false,
        }
    }

    /// Whether this file is a character device or not.
    pub const fn is_character_device(&self) -> bool {
        match self {
            Self::CharacterDevice(_) => true,
            Self::Regular(_)
            | Self::Directory(_)
            | Self::SymbolicLink(_)
            | Self::Fifo(_)
            | Self::BlockDevice(_)
            | Self::Socket(_) => false,
        }
    }

    /// Whether this file is a block device or not.
    pub const fn is_block_device(&self) -> bool {
        match self {
            Self::BlockDevice(_) => true,
            Self::Regular(_)
            | Self::Directory(_)
            | Self::SymbolicLink(_)
            | Self::Fifo(_)
            | Self::CharacterDevice(_)
            | Self::Socket(_) => false,
        }
    }

    /// Whether this file is a UNIX socket or not.
    pub const fn is_socket(&self) -> bool {
        match self {
            Self::Socket(_) => true,
            Self::Regular(_)
            | Self::Directory(_)
            | Self::SymbolicLink(_)
            | Self::Fifo(_)
            | Self::CharacterDevice(_)
            | Self::BlockDevice(_) => false,
        }
    }
}

impl<Dir: DirectoryRead> From<&TypeWithFile<Dir>> for Type {
    fn from(value: &TypeWithFile<Dir>) -> Self {
        match value {
            TypeWithFile::Regular(_) => Self::Regular,
            TypeWithFile::Directory(_) => Self::Directory,
            TypeWithFile::SymbolicLink(_) => Self::SymbolicLink,
            TypeWithFile::Fifo(_) => Self::Fifo,
            TypeWithFile::CharacterDevice(_) => Self::CharacterDevice,
            TypeWithFile::BlockDevice(_) => Self::BlockDevice,
            TypeWithFile::Socket(_) => Self::Socket,
        }
    }
}

impl<Dir: DirectoryRead> From<TypeWithFile<Dir>> for Type {
    fn from(value: TypeWithFile<Dir>) -> Self {
        match value {
            TypeWithFile::Regular(_) => Self::Regular,
            TypeWithFile::Directory(_) => Self::Directory,
            TypeWithFile::SymbolicLink(_) => Self::SymbolicLink,
            TypeWithFile::Fifo(_) => Self::Fifo,
            TypeWithFile::CharacterDevice(_) => Self::CharacterDevice,
            TypeWithFile::BlockDevice(_) => Self::BlockDevice,
            TypeWithFile::Socket(_) => Self::Socket,
        }
    }
}