lfs_core/
mount.rs

1use super::*;
2
3/// A mount point
4#[derive(Debug, Clone)]
5pub struct Mount {
6    pub info: MountInfo,
7    pub fs_label: Option<String>,
8    pub disk: Option<Disk>,
9    pub stats: Result<Stats, StatsError>,
10    pub uuid: Option<String>,
11    pub part_uuid: Option<String>,
12}
13
14impl Mount {
15    /// Return inodes information, when available and consistent
16    pub fn inodes(&self) -> Option<&Inodes> {
17        self.stats
18            .as_ref()
19            .ok()
20            .and_then(|stats| stats.inodes.as_ref())
21    }
22    /// Return the stats, if they could be fetched and
23    /// make sense.
24    ///
25    /// Most often, you don't care *why* there are no stats,
26    /// because the error cases are mostly non storage volumes,
27    /// so it's a best practice to no try to analyze the error
28    /// but just use this option returning method.
29    ///
30    /// The most interesting case is when a network volume is
31    /// unreachable, which you can test with is_unreachable().
32    pub fn stats(&self) -> Option<&Stats> {
33        self.stats.as_ref().ok()
34    }
35    /// Tell whether the reason we have no stats is because the
36    /// filesystem is unreachable
37    pub fn is_unreachable(&self) -> bool {
38        matches!(self.stats, Err(StatsError::Unreachable))
39    }
40}
41
42#[derive(Debug, Clone)]
43pub struct ReadOptions {
44    remote_stats: bool,
45}
46impl Default for ReadOptions {
47    fn default() -> Self {
48        Self {
49            remote_stats: true,
50        }
51    }
52}
53impl ReadOptions {
54    pub fn remote_stats(&mut self, v: bool) {
55        self.remote_stats = v;
56    }
57}
58
59/// Read all the mount points and load basic information on them
60pub fn read_mounts(options: &ReadOptions) -> Result<Vec<Mount>, Error> {
61    let by_label = read_by("label").ok();
62    let by_uuid = read_by("uuid").ok();
63    let by_partuuid = read_by("partuuid").ok();
64
65    // we'll find the disk for a filesystem by taking the longest
66    // disk whose name starts the one of our partition
67    // hence the sorting.
68    let bd_list = BlockDeviceList::read()?;
69    read_mountinfo()?
70        .drain(..)
71        .map(|info| {
72            let top_bd = bd_list.find_top(
73                info.dev,
74                info.dm_name(),
75                info.fs_name(),
76            );
77            let fs_label = get_label(&info.fs, by_label.as_deref());
78            let uuid = get_label(&info.fs, by_uuid.as_deref());
79            let part_uuid = get_label(&info.fs, by_partuuid.as_deref());
80            let disk = top_bd.map(|bd| Disk::new(bd.name.clone()));
81            let stats = if !options.remote_stats && info.is_remote() {
82                Err(StatsError::Excluded)
83            } else {
84                Stats::from(&info.mount_point)
85            };
86            Ok(Mount {
87                info,
88                fs_label,
89                disk,
90                stats,
91                uuid,
92                part_uuid,
93            })
94        })
95        .collect()
96}