filesystem_table/
lib.rs

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
use std::{path::PathBuf, str::FromStr};

use thiserror::Error;

#[derive(Error, Debug)]
pub enum FsTableError {
    #[error("Invalid fstab entry: {0}")]
    InvalidEntry(String),

    #[error("Invalid number conversion: {0}")]
    InvalidNumberConversion(String),

    #[error("Invalid fsck order: {0}")]
    InvalidFsckOrder(u8),

    #[error("IO error: {0}")]
    IoError(#[from] std::io::Error),

    #[error("lsblk error: {0}")]
    LsblkError(#[from] lsblk::LsblkError),
}

type Result<T> = std::result::Result<T, FsTableError>;

/// The order in which the filesystems should be checked.
#[derive(Debug, Clone, Default)]
#[repr(u8)]
pub enum FsckOrder {
    /// Never check the filesystem automatically.
    #[default]
    NoCheck = 0,
    /// Check the filesystem while booting.
    Boot = 1,
    /// Check the filesystem after the boot process has finished.
    PostBoot = 2,
}

impl TryFrom<&u8> for FsckOrder {
    type Error = FsTableError;

    fn try_from(value: &u8) -> Result<Self> {
        match value {
            0 => Ok(Self::NoCheck),
            1 => Ok(Self::Boot),
            2 => Ok(Self::PostBoot),
            _ => Err(FsTableError::InvalidFsckOrder(*value)),
        }
    }
}

impl TryFrom<u8> for FsckOrder {
    type Error = FsTableError;

    fn try_from(value: u8) -> Result<Self> {
        // use the TryFrom<&u8> implementation
        Self::try_from(&value)
    }
}

impl TryFrom<&str> for FsckOrder {
    type Error = FsTableError;

    fn try_from(value: &str) -> Result<Self> {
        let n = value
            .parse::<u8>()
            .map_err(|e| FsTableError::InvalidNumberConversion(e.to_string()))?;
        Self::try_from(n)
    }
}

#[derive(Debug, Clone, Default)]
pub struct FsEntry {
    /// The device spec for mounting the filesystem.
    ///
    /// Can be a device path, or some kind of filter to get the
    /// device, i.e `LABEL=ROOT` or `UUID=1234-5678`
    ///
    /// Examples:
    ///
    /// - `/dev/sda1`
    /// - `LABEL=ROOT`
    /// - `UUID=1234-5678`
    /// - `PARTUUID=1234-5678`
    /// - `PARTLABEL=ROOT`
    /// - `PARTUUID=1234-5678`
    /// - `PARTLABEL=ROOT`
    pub device_spec: String,
    /// The mountpoint for the filesystem.
    /// Specifies where the filesystem should be mounted.
    ///
    /// Doesn't actually need to be a real mountpoint, but
    /// most of the time it will be.
    ///
    /// Is an optional field, a [`None`] value will serialize into `none`.
    ///
    /// Examples:
    ///
    /// - `/`
    /// - `/boot`
    /// - `none` (for no mountpoint, used for swap or similar filesystems)
    /// - `/home`
    pub mountpoint: Option<String>,

    /// The filesystem type for the filesystem.
    ///
    /// Examples:
    ///
    /// - `ext4`
    /// - `btrfs`
    /// - `vfat`
    /// - ...
    pub fs_type: String,

    /// Mount options for the filesystem. Is a comma-separated list of options.
    ///
    /// This type returns a vector of strings, as there can be multiple options.
    /// They will be serialized into a comma-separated list.
    pub options: Vec<String>,

    /// The dump frequency for the filesystem.
    ///
    /// This is a number that specifies how often the filesystem should be backed up.
    ///
    pub dump_freq: u8,

    /// The pass number for the filesystem.
    ///
    /// Determines when the filesystem health should be checked using `fsck`.
    pub pass: FsckOrder,
}

impl FsEntry {
    /// Parse a FsEntry from a line in the fstab file.
    pub fn from_line_str(line: &str) -> std::result::Result<Self, FsTableError> {
        // split by whitespace
        let parts: Vec<&str> = line.split_whitespace().collect();

        if parts.len() < 6 {
            return Err(FsTableError::InvalidEntry(line.to_string()));
        }

        let device_spec = parts[0].to_string();

        let mountpoint = if parts[1] == "none" {
            None
        } else {
            Some(parts[1].to_string())
        };

        let fs_type = parts[2].to_string();

        let options = parts[3].split(',').map(|s| s.to_string()).collect();

        let dump_freq = parts[4]
            .parse::<u8>()
            .map_err(|_| FsTableError::InvalidEntry(line.to_string()))?;
        let pass = FsckOrder::try_from(parts[5])?;

        Ok(Self {
            device_spec,
            mountpoint,
            fs_type,
            options,
            dump_freq,
            pass,
        })
    }

    /// Serialize the FsEntry into a string that can be written to the fstab file.
    pub fn to_line_str(&self) -> String {
        let mountpoint = self.mountpoint.as_deref().unwrap_or("none");
        let options = if self.options.is_empty() {
            "defaults".to_string()
        } else {
            self.options.join(",")
        };
        let pass = self.pass.clone() as u8;

        format!(
            "{device_spec}\t{mountpoint}\t{fs_type}\t{options}\t{dump_freq}\t{pass}",
            device_spec = self.device_spec,
            mountpoint = mountpoint,
            fs_type = self.fs_type,
            options = options,
            pass = pass,
            dump_freq = self.dump_freq,
        )
    }
}

impl TryFrom<&str> for FsEntry {
    type Error = FsTableError;

    fn try_from(value: &str) -> Result<Self> {
        Self::from_line_str(value)
    }
}

impl std::fmt::Display for FsEntry {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.to_line_str())
    }
}

#[derive(Debug)]
pub struct FsTable {
    pub entries: Vec<FsEntry>,
}

impl FromStr for FsTable {
    type Err = FsTableError;

    fn from_str(table: &str) -> Result<Self> {
        let entries = table
            .lines()
            .map(FsEntry::from_line_str)
            .collect::<Result<Vec<FsEntry>>>()?;

        Ok(Self { entries })
    }
}

impl std::fmt::Display for FsTable {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.entries
            .iter()
            .map(|entry| entry.to_line_str())
            .collect::<Vec<_>>()
            .join("\n")
            .as_str()
            .fmt(f)
    }
}

// impl ToString for FsTable {
//     fn to_string(&self) -> String {
//         self.to_string()
//     }
// }

impl TryFrom<&str> for FsTable {
    type Error = FsTableError;

    fn try_from(value: &str) -> Result<Self> {
        Self::from_str(value)
    }
}

pub fn read_mtab() -> Result<FsTable> {
    let mtab = std::fs::read_to_string("/etc/mtab")
        .map_err(|e| FsTableError::InvalidEntry(e.to_string()))?;
    FsTable::from_str(&mtab)
}

pub fn read_fstab() -> Result<FsTable> {
    let fstab = std::fs::read_to_string("/etc/fstab")
        .map_err(|e| FsTableError::InvalidEntry(e.to_string()))?;
    FsTable::from_str(&fstab)
}

/// Generate a new fstab from mtab, using a chroot prefix to generate the new fstab.
pub fn generate_fstab(prefix: &str) -> Result<FsTable> {
    let mtab = read_mtab()?;

    let block_list = lsblk::BlockDevice::list()?;

    // filter by prefix 
    let entries = (mtab.entries.into_iter())
        .filter(|entry| (entry.mountpoint.as_ref()).is_some_and(|mp| mp.starts_with(prefix)))
        .map(|mut entry| -> Result<FsEntry> {
            entry.mountpoint = Some(
                match entry.mountpoint.unwrap().strip_prefix(prefix).unwrap() {
                    "" => "/",
                    path => path,
                }
                .to_string(),
            );

            let device_spec_og = entry.device_spec.clone();

            let uuid = block_list
                .iter()
                .find(|dev| dev.fullname == PathBuf::from(&device_spec_og))
                .and_then(|dev| dev.uuid.as_ref())
                .ok_or_else(|| {
                    FsTableError::InvalidEntry(format!(
                        "Could not find UUID for device: {}",
                        device_spec_og
                    ))
                })?;
            entry.device_spec = format!("UUID={uuid}");
            Ok(entry)
        })
        .collect::<Result<Vec<_>>>()?;

    Ok(FsTable { entries })
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_fstab_parse() {
        let line = "/dev/sda1\t/\text4\trw,relatime\t0\t1";
        let entry = FsEntry::from_line_str(line).unwrap();

        assert_eq!(entry.device_spec, "/dev/sda1");
        assert_eq!(entry.mountpoint, Some("/".to_string()));
        assert_eq!(entry.fs_type, "ext4");
        assert_eq!(entry.options, vec!["rw", "relatime"]);
        assert_eq!(entry.dump_freq, 0);
        assert_eq!(entry.pass as u8, 1);
    }

    #[test]
    fn test_fstab_serialize() {
        let entry = FsEntry {
            device_spec: "/dev/sda1".to_string(),
            mountpoint: Some("/".to_string()),
            fs_type: "ext4".to_string(),
            options: vec!["rw".to_string(), "relatime".to_string()],
            dump_freq: 0,
            pass: FsckOrder::Boot,
        };

        assert_eq!(entry.to_line_str(), "/dev/sda1\t/\text4\trw,relatime\t0\t1");
    }

    #[test]
    fn test_fsck_order() {
        assert_eq!(FsckOrder::try_from(&0u8).unwrap() as u8, 0);
        assert_eq!(FsckOrder::try_from(&1u8).unwrap() as u8, 1);
        assert_eq!(FsckOrder::try_from(&2u8).unwrap() as u8, 2);
        assert!(FsckOrder::try_from(&3u8).is_err());
    }

    #[test]
    fn test_fstab_table() {
        let table = "/dev/sda1\t/\text4\trw,relatime\t0\t1\n/dev/sda2\tnone\tswap\tsw\t0\t0";
        let fstab = FsTable::from_str(table).unwrap();

        assert_eq!(fstab.entries.len(), 2);
        assert_eq!(fstab.entries[0].device_spec, "/dev/sda1");
        assert_eq!(fstab.entries[1].device_spec, "/dev/sda2");

        let serialized = fstab.to_string();
        assert_eq!(serialized, table);
    }

    #[test]
    fn test_mtab_parse() {
        let mtab = std::fs::read_to_string("/etc/mtab").unwrap();

        let table = FsTable::from_str(&mtab).unwrap();

        println!("{:#?}", table.to_string());
    }
    
    #[test]
    fn test_generate_fstab() {
        let fstab = generate_fstab("/mnt/custom").unwrap();

        println!("{}", fstab.to_string());
        
        // check if theres newlines
        assert!(fstab.to_string().contains('\n'));
    }
}