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
use partition_identity::PartitionID;
use std::char;
use std::ffi::OsString;
use std::fmt::{self, Display, Formatter};
use std::io::{self, Error, ErrorKind};
use std::os::unix::ffi::OsStringExt;
use std::path::PathBuf;
use std::str::FromStr;

/// A mount entry which contains information regarding how and where a source
/// is mounted.
#[derive(Debug, Default, Clone, Hash, Eq, PartialEq)]
pub struct MountInfo {
    /// The source which is mounted.
    pub source: PathBuf,
    /// Where the source is mounted.
    pub dest: PathBuf,
    /// The type of the mounted file system.
    pub fstype: String,
    /// Options specified for this file system.
    pub options: Vec<String>,
    /// Defines if the file system should be dumped.
    pub dump: i32,
    /// Defines if the file system should be checked, and in what order.
    pub pass: i32,
}

impl Display for MountInfo {
    fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
        write!(
            fmt,
            "{} {} {} {} {} {}",
            self.source.display(),
            self.dest.display(),
            self.fstype,
            if self.options.is_empty() {
                "defaults".into()
            } else {
                self.options.join(",")
            },
            self.dump,
            self.pass
        )
    }
}

impl FromStr for MountInfo {
    type Err = io::Error;

    fn from_str(line: &str) -> Result<Self, Self::Err> {
        let mut parts = line.split_whitespace();

        fn map_err(why: &'static str) -> io::Error {
            Error::new(ErrorKind::InvalidData, why)
        }

        let source = parts.next().ok_or_else(|| map_err("missing source"))?;
        let dest = parts.next().ok_or_else(|| map_err("missing dest"))?;
        let fstype = parts.next().ok_or_else(|| map_err("missing type"))?;
        let options = parts.next().ok_or_else(|| map_err("missing options"))?;

        let dump = parts.next().map_or(Ok(0), |value| {
            value.parse::<i32>().map_err(|_| map_err("dump value is not a number"))
        })?;

        let pass = parts.next().map_or(Ok(0), |value| {
            value.parse::<i32>().map_err(|_| map_err("pass value is not a number"))
        })?;

        let path = Self::parse_value(source)?;
        let path = path.to_str().ok_or_else(|| map_err("non-utf8 paths are unsupported"))?;

        let source = if path.starts_with("/dev/disk/by-") {
            Self::fetch_from_disk_by_path(path)?
        } else {
            PathBuf::from(path)
        };

        let path = Self::parse_value(dest)?;
        let path = path.to_str().ok_or_else(|| map_err("non-utf8 paths are unsupported"))?;

        let dest = PathBuf::from(path);

        Ok(MountInfo {
            source,
            dest,
            fstype: fstype.to_owned(),
            options: options.split(',').map(String::from).collect(),
            dump,
            pass,
        })
    }
}

impl MountInfo {
    /// Attempt to parse a `/proc/mounts`-like line.
    #[deprecated]
    pub fn parse_line(line: &str) -> io::Result<MountInfo> {
        line.parse::<Self>()
    }

    fn fetch_from_disk_by_path(path: &str) -> io::Result<PathBuf> {
        PartitionID::from_disk_by_path(path)
            .map_err(|why| Error::new(ErrorKind::InvalidData, format!("{}: {}", path, why)))?
            .get_device_path()
            .ok_or_else(|| {
                Error::new(ErrorKind::NotFound, format!("device path for {} was not found", path))
            })
    }

    fn parse_value(value: &str) -> io::Result<OsString> {
        let mut ret = Vec::new();

        let mut bytes = value.bytes();
        while let Some(b) = bytes.next() {
            match b {
                b'\\' => {
                    let mut code = 0;
                    for _i in 0..3 {
                        if let Some(b) = bytes.next() {
                            code *= 8;
                            code += u32::from_str_radix(&(b as char).to_string(), 8)
                                .map_err(|err| Error::new(ErrorKind::Other, err))?;
                        } else {
                            return Err(Error::new(ErrorKind::Other, "truncated octal code"));
                        }
                    }
                    ret.push(code as u8);
                }
                _ => {
                    ret.push(b);
                }
            }
        }

        Ok(OsString::from_vec(ret))
    }
}