-
Notifications
You must be signed in to change notification settings - Fork 75
/
disk_usage.rs
62 lines (49 loc) · 1.22 KB
/
disk_usage.rs
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
use std::io;
use std::path::Path;
use nix::sys;
use crate::utils::invalid_data;
use crate::{Bytes, Percent};
#[derive(Clone, Debug)]
pub struct DiskUsage {
pub(crate) total: Bytes,
pub(crate) used: Bytes,
pub(crate) free: Bytes,
pub(crate) percent: Percent,
}
impl DiskUsage {
/// Total disk size in bytes.
pub fn total(&self) -> Bytes {
self.total
}
/// Number of bytes used.
pub fn used(&self) -> Bytes {
self.used
}
/// Number of bytes free.
pub fn free(&self) -> Bytes {
self.free
}
/// Percentage of disk used.
pub fn percent(&self) -> Percent {
self.percent
}
}
pub fn disk_usage<P>(path: P) -> io::Result<DiskUsage>
where
P: AsRef<Path>,
{
let statvfs = sys::statvfs::statvfs(path.as_ref())
.map_err(|_| invalid_data("failed to use statvfs: statvfs return an error code"))?;
let total = statvfs.blocks() as u64 * statvfs.fragment_size() as u64;
let avail_to_root = statvfs.blocks_free() as u64 * statvfs.fragment_size() as u64;
let used = total - avail_to_root;
let free = statvfs.blocks_available() as u64 * statvfs.fragment_size() as u64;
let total_user = used + free;
let percent = ((used as f64 / total_user as f64) * 100.0) as f32;
Ok(DiskUsage {
total,
used,
free,
percent,
})
}