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
use clap::ArgMatches;
use nix::sys::statvfs::statvfs;
use std::cmp;
use std::fs::File;
use std::io::BufRead;
use std::io::BufReader;
use std::process;
use crate::procfields::ProcFields;
use crate::stats::Stats;
pub struct Reader;
impl Reader {
pub fn read(args: &ArgMatches) -> (Vec<Stats>, usize) {
let file = match File::open("/proc/mounts") {
Ok(f) => f,
Err(e) => {
println!("Error: Could not open /proc/mounts - {e}");
process::exit(1);
}
};
let reader = BufReader::new(&file);
let mut stats: Vec<Stats> = Vec::new();
let mut max_width = 0;
for line in reader.lines() {
match line {
Ok(line) => {
let fields: Vec<&str> = line.split_whitespace().collect();
let statvfs = match statvfs(fields[ProcFields::Mountpoint.upcast()]) {
Ok(s) => s,
Err(_) => continue, };
if statvfs.blocks() == 0 {
continue;
}
let s = Stats::new(
fields[ProcFields::Filesystem.upcast()],
fields[ProcFields::Mountpoint.upcast()],
statvfs,
args,
);
max_width = cmp::max(max_width, s.filesystem.len());
stats.push(s);
}
Err(err) => println!("Error: {err}"),
}
}
stats.sort();
(stats, max_width)
}
}