diskplan_filesystem/
attributes.rs

1use std::{borrow::Cow, fmt::Debug};
2
3/// The default mode for directories (`0o755` or `rwxr-xr-x`)
4pub const DEFAULT_DIRECTORY_MODE: Mode = Mode(0o755);
5/// The default mode for files (`0o644` or `rw-r--r--`)
6pub const DEFAULT_FILE_MODE: Mode = Mode(0o644);
7
8/// Optional owner, group and UNIX permissions to be set
9#[derive(Debug, Default, Clone, PartialEq, Eq)]
10pub struct SetAttrs<'a> {
11    /// An optional owner to set given by name
12    pub owner: Option<&'a str>,
13    /// An optional group to set given by name
14    pub group: Option<&'a str>,
15    /// An optional [`Mode`] to set
16    pub mode: Option<Mode>,
17}
18
19/// Owner, group and UNIX permissions
20#[derive(Debug, Clone, PartialEq, Eq)]
21pub struct Attrs<'a> {
22    /// The owner of the file or directory
23    pub owner: Cow<'a, str>,
24    /// The group of the file or directory
25    pub group: Cow<'a, str>,
26    /// The UNIX permissions of the file or directory
27    pub mode: Mode,
28}
29
30/// UNIX permissions
31#[derive(Clone, Copy, Eq, PartialEq)]
32pub struct Mode(u16);
33
34impl Mode {
35    /// Returns the inner numeric value of the permissions
36    pub fn value(&self) -> u16 {
37        self.0
38    }
39}
40
41impl Debug for Mode {
42    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43        write!(f, "Mode(0o{:03o})", self.0)
44    }
45}
46
47impl From<u16> for Mode {
48    fn from(value: u16) -> Self {
49        Mode(value)
50    }
51}
52
53impl From<Mode> for u16 {
54    fn from(mode: Mode) -> Self {
55        mode.0
56    }
57}
58
59impl From<Mode> for u32 {
60    fn from(mode: Mode) -> Self {
61        mode.0 as u32
62    }
63}