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
use std::io::{self, BufRead};
use std::os::unix::ffi::OsStrExt;
use std::path::Path;
use std::str::FromStr;
use super::{MountInfo, MountIter};

/// A list of parsed mount entries from `/proc/mounts`.
#[derive(Debug, Default, Clone, Hash, Eq, PartialEq)]
pub struct MountList(pub Vec<MountInfo>);

impl MountList {
    /// Parse mounts given from an iterator of mount entry lines.
    pub fn parse_from<'a, I: Iterator<Item = &'a str>>(lines: I) -> io::Result<MountList> {
        lines.map(MountInfo::from_str).collect::<io::Result<Vec<MountInfo>>>().map(MountList)
    }

    /// Read a new list of mounts into memory from `/proc/mounts`.
    pub fn new() -> io::Result<MountList> {
        Ok(MountList(MountIter::new()?.collect::<io::Result<Vec<MountInfo>>>()?))
    }

    /// Read a new list of mounts into memory from any mount-tab-like file.
    pub fn new_from_file<P: AsRef<Path>>(path: P) -> io::Result<MountList> {
        Ok(MountList(MountIter::new_from_file(path)?.collect::<io::Result<Vec<MountInfo>>>()?))
    }

    /// Read a new list of mounts into memory from any mount-tab-like file.
    pub fn new_from_reader<R: BufRead>(reader: R) -> io::Result<MountList> {
        Ok(MountList(MountIter::new_from_reader(reader).collect::<io::Result<Vec<MountInfo>>>()?))
    }

    // Returns true if the `source` is mounted at the given `dest`.
    pub fn source_mounted_at<D: AsRef<Path>, P: AsRef<Path>>(&self, source: D, path: P) -> bool {
        self.get_mount_by_source(source)
            .map_or(false, |mount| mount.dest.as_path() == path.as_ref())
    }

    /// Find the first mount which which has the `path` destination.
    pub fn get_mount_by_dest<P: AsRef<Path>>(&self, path: P) -> Option<&MountInfo> {
        self.0.iter().find(|mount| mount.dest == path.as_ref())
    }

    /// Find the first mount hich has the source `path`.
    pub fn get_mount_by_source<P: AsRef<Path>>(&self, path: P) -> Option<&MountInfo> {
        self.0.iter().find(|mount| mount.source == path.as_ref())
    }

    /// Iterate through each source that starts with the given `path`.
    pub fn source_starts_with<'a>(
        &'a self,
        path: &'a Path,
    ) -> Box<Iterator<Item = &MountInfo> + 'a> {
        self.starts_with(path.as_os_str().as_bytes(), |m| &m.source)
    }

    /// Iterate through each destination that starts with the given `path`.
    pub fn destination_starts_with<'a>(
        &'a self,
        path: &'a Path,
    ) -> Box<Iterator<Item = &MountInfo> + 'a> {
        self.starts_with(path.as_os_str().as_bytes(), |m| &m.dest)
    }

    fn starts_with<'a, F: Fn(&'a MountInfo) -> &'a Path + 'a>(
        &'a self,
        path: &'a [u8],
        func: F,
    ) -> Box<Iterator<Item = &MountInfo> + 'a> {
        let iterator = self.0.iter().filter(move |mount| {
            let input = func(mount).as_os_str().as_bytes();
            input.len() >= path.len() && &input[..path.len()] == path
        });

        Box::new(iterator)
    }
}