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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use {
crate::{device_id::DeviceId, error::*, sys},
std::{
ffi::CString,
mem,
os::unix::ffi::OsStrExt,
path::PathBuf,
str::{FromStr, SplitWhitespace},
},
};
pub type MountId = u32;
#[derive(Debug)]
pub struct Stats {
pub bsize: u64,
pub blocks: u64,
pub bavail: u64,
pub bfree: u64,
}
#[derive(Debug)]
pub struct Mount {
pub id: MountId,
pub parent: MountId,
pub dev: DeviceId,
pub root: PathBuf,
pub mount_point: PathBuf,
pub fs: String,
pub fs_type: String,
pub rotational: Option<bool>,
pub stats: Option<Stats>,
}
impl Mount {
pub fn size(&self) -> u64 {
self.stats.as_ref().map_or(0, |s| s.bsize * s.blocks)
}
pub fn available(&self) -> u64 {
self.stats.as_ref().map_or(0, |s| s.bsize * s.bavail)
}
pub fn used(&self) -> u64 {
self.size() - self.available()
}
pub fn use_share(&self) -> f64 {
if self.size() == 0 {
0.0
} else {
self.used() as f64 / (self.size() as f64)
}
}
pub fn partition_name(&self) -> Option<String> {
self.fs.strip_prefix("/dev/").map(String::from)
}
pub fn disk_name(&self) -> Option<String> {
self.partition_name()
.map(|pn| {
pn.chars()
.take_while(char::is_ascii_alphabetic)
.collect::<String>()
})
.filter(|s| s.len() > 2)
}
pub fn disk_type(&self) -> &'static str {
match self.rotational {
Some(true) => "HDD",
Some(false) => "SSD",
None => "",
}
}
}
fn next<'a, 'b>(split: &'b mut SplitWhitespace<'a>) -> Result<&'a str> {
split.next().ok_or(Error::UnexpectedFormat)
}
fn skip_until<'a, 'b>(split: &'b mut SplitWhitespace<'a>, sep: &'static str) -> Result<()> {
Ok(loop {
if next(split)? == sep {
break;
}
})
}
impl FromStr for Mount {
type Err = Error;
fn from_str(line: &str) -> Result<Self> {
let mut tokens = line.split_whitespace();
let tokens = &mut tokens;
let id = next(tokens)?.parse()?;
let parent = next(tokens)?.parse()?;
let dev = next(tokens)?.parse()?;
let root = next(tokens)?.into();
let mount_point = PathBuf::from(next(tokens)?);
skip_until(tokens, "-")?;
let fs_type = next(tokens)?.to_string();
let fs = next(tokens)?.to_string();
let c_mount_point = CString::new(mount_point.as_os_str().as_bytes()).unwrap();
let stats = unsafe {
let mut statvfs = mem::MaybeUninit::<libc::statvfs>::uninit();
let code = libc::statvfs(c_mount_point.as_ptr(), statvfs.as_mut_ptr());
match code {
0 => {
let statvfs = statvfs.assume_init();
Some(Stats {
bsize: statvfs.f_bsize,
blocks: statvfs.f_blocks,
bavail: statvfs.f_bavail,
bfree: statvfs.f_bfree,
})
}
-1 => {
None
}
_ => {
return Err(Error::UnexpectedStavfsReturn {
code,
path: mount_point,
});
}
}
};
let mut mount = Mount {
id,
parent,
dev,
root,
mount_point,
fs,
fs_type,
rotational: None,
stats,
};
mount.rotational = mount
.disk_name()
.and_then(|name| sys::read_file(&format!("/sys/block/{}/queue/rotational", name)).ok())
.and_then(|c| {
match c.trim().as_ref() {
"0" => Some(false),
"1" => Some(true),
_ => None,
}
});
Ok(mount)
}
}
pub fn read_all() -> Result<Vec<Mount>> {
sys::read_file("/proc/self/mountinfo")?
.trim()
.split('\n')
.map(str::parse)
.inspect(|r| {
if let Err(e) = r {
eprintln!("Error while parsing a mount line: {}", e);
}
})
.filter(Result::is_ok)
.collect()
}