lfs_core/
stats.rs

1use crate::Inodes;
2
3/// inode & blocs information
4///
5/// The semantics is mostly the one of statvfs, with addition of
6///  bused which is necessary for volumes freely growing in containers
7#[derive(Debug, Clone)]
8pub struct Stats {
9    /// block size
10    pub bsize: u64,
11    /// number of blocks
12    pub blocks: u64,
13    /// not provided by statvfs
14    pub bused: u64,
15    /// number of free blocks
16    pub bfree: u64,
17    /// number of free blocks for underprivileged users
18    pub bavail: u64,
19    /// information relative to inodes, if available
20    pub inodes: Option<Inodes>,
21}
22
23#[derive(Debug, snafu::Snafu, Clone, Copy, PartialEq, Eq)]
24#[snafu(visibility(pub(crate)))]
25pub enum StatsError {
26    #[snafu(display("Could not stat mount point"))]
27    Unreachable,
28
29    #[snafu(display("Unconsistent stats"))]
30    Unconsistent,
31
32    /// Options made us not even try
33    #[snafu(display("Excluded"))]
34    Excluded,
35}
36
37impl Stats {
38    pub fn size(&self) -> u64 {
39        self.bsize * self.blocks
40    }
41    pub fn available(&self) -> u64 {
42        self.bsize * self.bavail
43    }
44    /// Space used in the volume (including unreadable fs metadata)
45    pub fn used(&self) -> u64 {
46        self.bsize * self.bused
47    }
48    pub fn use_share(&self) -> f64 {
49        if self.blocks == 0 {
50            0.0
51        } else {
52            (self.blocks - self.bfree) as f64 / self.blocks as f64
53        }
54    }
55}