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
use std::fmt;

/// Mode represents a file's mode and permission bits.
/// The bits have the same definition on all systems,
/// but not all bits apply to all systems.
///
/// It is modelled after Go's `os.FileMode`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Mode(pub u32);

impl Mode {
    /// d: is a directory
    pub const DIR: Self = Self(1 << 31);
    /// a: append-only
    pub const APPEND: Self = Self(1 << 30);
    /// l: exclusive use
    pub const EXCLUSIVE: Self = Self(1 << 29);
    /// T: temporary file; Plan 9 only
    pub const TEMPORARY: Self = Self(1 << 28);
    /// L: symbolic link
    pub const SYMLINK: Self = Self(1 << 27);
    /// D: device file
    pub const DEVICE: Self = Self(1 << 26);
    /// p: named pipe (FIFO)
    pub const NAMED_PIPE: Self = Self(1 << 25);
    /// S: Unix domain socket
    pub const SOCKET: Self = Self(1 << 24);
    /// u: setuid
    pub const SETUID: Self = Self(1 << 23);
    /// g: setgid
    pub const SETGID: Self = Self(1 << 22);
    /// c: Unix character device, when DEVICE is set
    pub const CHAR_DEVICE: Self = Self(1 << 21);
    /// t: sticky
    pub const STICKY: Self = Self(1 << 20);
    /// ?: non-regular file; nothing else is known
    pub const IRREGULAR: Self = Self(1 << 19);
}

impl fmt::Display for Mode {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut w = 0;
        if self.has(Self::DIR) {
            write!(f, "d")?;
            w += 1;
        }
        if self.has(Self::APPEND) {
            write!(f, "a")?;
            w += 1;
        }
        if self.has(Self::EXCLUSIVE) {
            write!(f, "l")?;
            w += 1;
        }
        if self.has(Self::TEMPORARY) {
            write!(f, "T")?;
            w += 1;
        }
        if self.has(Self::SYMLINK) {
            write!(f, "L")?;
            w += 1;
        }
        if self.has(Self::DEVICE) {
            write!(f, "D")?;
            w += 1;
        }
        if self.has(Self::NAMED_PIPE) {
            write!(f, "p")?;
            w += 1;
        }
        if self.has(Self::SOCKET) {
            write!(f, "S")?;
            w += 1;
        }
        if self.has(Self::SETUID) {
            write!(f, "u")?;
            w += 1;
        }
        if self.has(Self::SETGID) {
            write!(f, "g")?;
            w += 1;
        }
        if self.has(Self::CHAR_DEVICE) {
            write!(f, "c")?;
            w += 1;
        }
        if self.has(Self::STICKY) {
            write!(f, "t")?;
            w += 1;
        }
        if self.has(Self::IRREGULAR) {
            write!(f, "?")?;
            w += 1;
        }
        if w == 0 {
            write!(f, "-")?;
        }

        let rwx = "rwxrwxrwx";
        for (i, c) in rwx.char_indices() {
            if self.has(Mode(1 << (9 - 1 - i))) {
                write!(f, "{}", c)?;
            } else {
                write!(f, "-")?;
            }
        }

        Ok(())
    }
}

impl From<UnixMode> for Mode {
    fn from(m: UnixMode) -> Self {
        let mut mode = Mode(m.0 & 0o777);

        match m & UnixMode::IFMT {
            UnixMode::IFBLK => mode |= Mode::DEVICE,
            UnixMode::IFCHR => mode |= Mode::DEVICE & Mode::CHAR_DEVICE,
            UnixMode::IFDIR => mode |= Mode::DIR,
            UnixMode::IFIFO => mode |= Mode::NAMED_PIPE,
            UnixMode::IFLNK => mode |= Mode::SYMLINK,
            UnixMode::IFREG => { /* nothing to do */ }
            UnixMode::IFSOCK => mode |= Mode::SOCKET,
            _ => {}
        }

        if m.has(UnixMode::ISGID) {
            mode |= Mode::SETGID
        }
        if m.has(UnixMode::ISUID) {
            mode |= Mode::SETUID
        }
        if m.has(UnixMode::ISVTX) {
            mode |= Mode::STICKY
        }

        mode
    }
}

impl From<MsdosMode> for Mode {
    fn from(m: MsdosMode) -> Self {
        let mut mode = if m.has(MsdosMode::DIR) {
            Mode::DIR | Mode(0o777)
        } else {
            Mode(0o666)
        };
        if m.has(MsdosMode::READ_ONLY) {
            mode &= Mode(0o222);
        }

        mode
    }
}

impl From<u32> for Mode {
    fn from(u: u32) -> Self {
        Mode(u)
    }
}

/// UnixMode represents the file mode and permission bits for Unix systems.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct UnixMode(pub u32);

impl UnixMode {
    /// bit mask for the file type bit fields
    pub const IFMT: Self = Self(0xf000);

    /// the file is a socket
    pub const IFSOCK: Self = Self(0xc000);

    /// the file is a symbolic link
    pub const IFLNK: Self = Self(0xa000);

    /// the file is a regular file
    pub const IFREG: Self = Self(0x8000);

    /// the file is a block device
    pub const IFBLK: Self = Self(0x6000);

    /// the file is a directory
    pub const IFDIR: Self = Self(0x4000);

    /// the file is a character device
    pub const IFCHR: Self = Self(0x2000);

    /// the file is a FIFO
    pub const IFIFO: Self = Self(0x1000);

    /// the file is set-user-ID
    pub const ISUID: Self = Self(0x800);

    /// the file is set-group-ID
    pub const ISGID: Self = Self(0x400);

    /// the file is sticky
    pub const ISVTX: Self = Self(0x200);
}

impl From<u32> for UnixMode {
    fn from(u: u32) -> Self {
        UnixMode(u)
    }
}

/// MsdosMode represents the file mode and permission bits for MS-DOS
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct MsdosMode(pub u32);

impl MsdosMode {
    /// the file is a directory
    pub const DIR: Self = Self(0x10);

    /// the file is read-only
    pub const READ_ONLY: Self = Self(0x01);
}

impl From<u32> for MsdosMode {
    fn from(u: u32) -> Self {
        MsdosMode(u)
    }
}

macro_rules! derive_bitops {
    ($T: ty) => {
        impl std::ops::BitOr for $T {
            type Output = Self;

            fn bitor(self, rhs: Self) -> Self {
                Self(self.0 | rhs.0)
            }
        }

        impl std::ops::BitOrAssign for $T {
            fn bitor_assign(&mut self, rhs: Self) {
                self.0 |= rhs.0;
            }
        }

        impl std::ops::BitAnd for $T {
            type Output = Self;

            fn bitand(self, rhs: Self) -> Self {
                Self(self.0 & rhs.0)
            }
        }

        impl std::ops::BitAndAssign for $T {
            fn bitand_assign(&mut self, rhs: Self) {
                self.0 &= rhs.0;
            }
        }

        impl $T {
            /// Check if the mode has the given bits set.
            pub fn has(&self, rhs: Self) -> bool {
                self.0 & rhs.0 != 0
            }
        }
    };
}

derive_bitops!(Mode);
derive_bitops!(UnixMode);
derive_bitops!(MsdosMode);