use std::path::Path;
use crate::error::{Error, Result};
use crate::util::parse;
#[derive(Debug)]
pub struct Mount {
pub spec: Box<str>,
pub file: Box<str>,
pub vfstype: Box<str>,
pub mntops: Box<str>,
pub freq: i32,
pub passno: i32,
}
pub fn mounts() -> Result<Vec<Mount>> {
let path = Path::new("/proc/mounts");
let bytes = parse::read_file(path)?;
let mut entries = Vec::new();
for line in bytes.split(|&b| b == b'\n').filter(|l| !l.is_empty()) {
let fields = parse::SplitFields::<6>::new(line);
if fields.len() < 6 {
return Err(Error::Parse {
path: path.to_path_buf(),
line: 0,
msg: "not enough fields",
});
}
entries.push(Mount {
spec: decode_escaped_path(fields[0]),
file: decode_escaped_path(fields[1]),
vfstype: bytes_to_box_str(fields[2]),
mntops: bytes_to_box_str(fields[3]),
freq: parse::parse_dec_i64(fields[4]).unwrap_or(0) as i32,
passno: parse::parse_dec_i64(fields[5]).unwrap_or(0) as i32,
});
}
Ok(entries)
}
fn decode_escaped_path(s: &[u8]) -> Box<str> {
let mut result = Vec::with_capacity(s.len());
let mut i = 0;
while i < s.len() {
if s[i] == b'\\' && i + 3 < s.len() {
let oct = std::str::from_utf8(&s[i + 1..i + 4]).unwrap_or("000");
if let Ok(val) = u8::from_str_radix(oct, 8) {
result.push(val);
i += 4;
continue;
}
}
result.push(s[i]);
i += 1;
}
std::str::from_utf8(&result).unwrap_or("").into()
}
fn bytes_to_box_str(b: &[u8]) -> Box<str> {
std::str::from_utf8(b).unwrap_or("").into()
}