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
// Copyright 2018-2022 System76 <info@system76.com>
// SPDX-License-Identifier: MIT OR Apache-2.0

use std::{
    fs::File,
    io::{self, BufRead, BufReader},
};

/// Data structure for validating if a filesystem argument is valid, and used within
/// automatic file system mounting.
#[derive(Clone, Debug)]
#[allow(clippy::module_name_repetitions)]
pub struct SupportedFilesystems {
    nodev: Vec<bool>,
    fs: Vec<String>,
}

impl SupportedFilesystems {
    /// Detects available supported file systems on the system
    ///
    /// # Errors
    ///
    /// - On failure to open `/proc/filesystems`
    pub fn new() -> io::Result<Self> {
        let mut fss = Vec::with_capacity(64);
        let mut nodevs = Vec::with_capacity(64);

        for line in BufReader::new(File::open("/proc/filesystems")?).lines() {
            let line = line?;
            let mut line = line.split_whitespace();
            let (nodev, fs) = match (line.next(), line.next()) {
                (Some(fs), None) => (false, fs),
                (Some("nodev"), Some(fs)) => (true, fs),
                _ => continue,
            };

            nodevs.push(nodev);
            fss.push(fs.to_owned());
        }

        Ok(SupportedFilesystems {
            nodev: nodevs,
            fs: fss,
        })
    }

    /// Check if a provided file system is valid on this system.
    ///
    /// ```rust
    /// extern crate sys_mount;
    ///
    /// use sys_mount::SupportedFilesystems;
    ///
    /// fn main() {
    ///     let supports = SupportedFilesystems::new().unwrap();
    ///     println!("btrfs is {}", if supports.is_supported("btrfs") {
    ///         "supported"
    ///     } else {
    ///         "not supported"
    ///     });
    /// }
    /// ```
    #[must_use]
    pub fn is_supported(&self, fs: &str) -> bool {
        self.fs.iter().any(|s| s.as_str() == fs)
    }

    /// Iterate through file systems which are not associated with physical devices.
    #[must_use]
    pub fn nodev_file_systems<'a>(&'a self) -> Box<dyn Iterator<Item = &str> + 'a> {
        // TODO: When we can, switch to `impl Iterator`.
        let iter = self.nodev.iter().enumerate().filter_map(move |(id, &x)| {
            if x {
                Some(self.fs[id].as_str())
            } else {
                None
            }
        });

        Box::new(iter)
    }

    /// Iterate through file systems which are associated with physical devices.
    #[must_use]
    pub fn dev_file_systems<'a>(&'a self) -> Box<dyn Iterator<Item = &str> + 'a> {
        // TODO: When we can, switch to `impl Iterator`.
        let iter = self.nodev.iter().enumerate().filter_map(move |(id, &x)| {
            if x {
                None
            } else {
                Some(self.fs[id].as_str())
            }
        });

        Box::new(iter)
    }
}