1use std::{
4 fs::File,
5 io::{Read, Seek},
6};
7
8use thiserror::Error;
9
10pub const PROC_MOUNTS_PATH: &str = "/proc/mounts";
11
12#[derive(Debug, PartialEq, Eq, Hash, Clone)]
16pub struct LinuxMount {
17 pub spec: String,
18 pub mount_point: String,
19 pub fs_type: String,
20 pub mount_options: Vec<String>,
21 pub dump_fs_freq: u32,
22 pub fsck_fs_passno: u32,
23}
24
25#[derive(Debug, Error)]
27#[error("invalid mount line: {input}")]
28pub struct ParseError {
29 pub(crate) input: String,
30}
31
32#[derive(Debug, Error)]
34pub enum ReadError {
35 #[error("failed to parse {PROC_MOUNTS_PATH}")]
36 Parse(#[from] ParseError),
37 #[error("failed to read {PROC_MOUNTS_PATH}")]
38 Io(#[from] std::io::Error),
39}
40
41impl LinuxMount {
42 pub(crate) fn parse(line: &str) -> Option<Self> {
45 let mut fields = line.split_ascii_whitespace().into_iter();
46 let spec = fields.next()?.to_string();
47 let mount_point = fields.next()?.to_string();
48 let fs_type = fields.next()?.to_string();
49 let mount_options = fields.next()?.split(',').map(ToOwned::to_owned).collect();
50 let dump_fs_freq = fields.next()?.parse().ok()?;
51 let fsck_fs_passno = fields.next()?.parse().ok()?;
52 Some(Self {
53 spec,
54 mount_point,
55 fs_type,
56 mount_options,
57 dump_fs_freq,
58 fsck_fs_passno,
59 })
60 }
61}
62
63pub(crate) fn read_proc_mounts(file: &mut File) -> Result<Vec<LinuxMount>, ReadError> {
65 let mut content = String::with_capacity(4096);
66 file.rewind()?;
67 file.read_to_string(&mut content).map_err(ReadError::from)?;
68 let mut mounts = Vec::with_capacity(64);
69 parse_proc_mounts(&content, &mut mounts).map_err(ReadError::from)?;
70 Ok(mounts)
71}
72
73pub(crate) fn parse_proc_mounts(
75 content: &str,
76 buf: &mut Vec<LinuxMount>,
77) -> Result<(), ParseError> {
78 for line in content.lines() {
79 let line = line.trim_start_matches(|c: char| c.is_ascii_whitespace());
80 if !line.is_empty() && !line.starts_with('#') {
81 let m = LinuxMount::parse(line).ok_or_else(|| ParseError {
82 input: line.to_owned(),
83 })?;
84 buf.push(m);
85 }
86 }
87 Ok(())
88}
89
90#[cfg(test)]
91mod tests {
92 use pretty_assertions::assert_eq;
93
94 use super::{parse_proc_mounts, LinuxMount};
95
96 fn vec_str(values: &[&str]) -> Vec<String> {
97 values.into_iter().map(|s| s.to_string()).collect()
98 }
99
100 #[test]
101 fn parsing() {
102 let content = "
103sysfs /sys sysfs rw,nosuid,nodev,noexec,relatime 0 0
104tmpfs /run tmpfs rw,nosuid,nodev,noexec,relatime,size=1599352k,mode=755,inode64 1 2
105cgroup2 /sys/fs/cgroup cgroup2 rw,nosuid,nodev,noexec,relatime,nsdelegate,memory_recursiveprot 0 0
106/dev/nvme0n1p1 /boot/efi vfat rw,relatime,errors=remount-ro 0 0";
107 let mut mounts = Vec::new();
108 parse_proc_mounts(&content, &mut mounts).unwrap();
109
110 let expected = vec![
111 LinuxMount {
112 spec: String::from("sysfs"),
113 mount_point: String::from("/sys"),
114 fs_type: String::from("sysfs"),
115 mount_options: vec_str(&["rw", "nosuid", "nodev", "noexec", "relatime"]),
116 dump_fs_freq: 0,
117 fsck_fs_passno: 0,
118 },
119 LinuxMount {
120 spec: String::from("tmpfs"),
121 mount_point: String::from("/run"),
122 fs_type: String::from("tmpfs"),
123 mount_options: vec_str(&[
124 "rw",
125 "nosuid",
126 "nodev",
127 "noexec",
128 "relatime",
129 "size=1599352k",
130 "mode=755",
131 "inode64",
132 ]),
133 dump_fs_freq: 1,
134 fsck_fs_passno: 2,
135 },
136 LinuxMount {
137 spec: String::from("cgroup2"),
138 mount_point: String::from("/sys/fs/cgroup"),
139 fs_type: String::from("cgroup2"),
140 mount_options: vec_str(&[
141 "rw",
142 "nosuid",
143 "nodev",
144 "noexec",
145 "relatime",
146 "nsdelegate",
147 "memory_recursiveprot",
148 ]),
149 dump_fs_freq: 0,
150 fsck_fs_passno: 0,
151 },
152 LinuxMount {
153 spec: String::from("/dev/nvme0n1p1"),
154 mount_point: String::from("/boot/efi"),
155 fs_type: String::from("vfat"),
156 mount_options: vec_str(&["rw", "relatime", "errors=remount-ro"]),
157 dump_fs_freq: 0,
158 fsck_fs_passno: 0,
159 },
160 ];
161 assert_eq!(expected, mounts);
162 }
163
164 #[test]
165 fn parsing_error() {
166 let mut mounts = Vec::new();
167 parse_proc_mounts("badbad", &mut mounts).unwrap_err();
168 parse_proc_mounts("croup2 /sys/fs/cgroup", &mut mounts).unwrap_err();
169 }
170
171 #[test]
172 fn parsing_comments() {
173 let mut mounts = Vec::new();
174 parse_proc_mounts("\n# badbad\n", &mut mounts).unwrap();
175 }
176}