summaryrefslogtreecommitdiffstats
path: root/src/stats.rs
diff options
context:
space:
mode:
authorrabite <rabite@posteo.de>2019-04-04 00:31:01 +0200
committerrabite <rabite@posteo.de>2019-04-04 00:31:01 +0200
commitd5ccfb0d7403bea77e027a9ca930c81bed5a2314 (patch)
tree897dce30b7909b4267cfe79bc256c3d16545f47a /src/stats.rs
parent343dd6dedabb1fc5158677fced8588e5b9a00c05 (diff)
show fs space usage and too much other stuff
Diffstat (limited to 'src/stats.rs')
-rw-r--r--src/stats.rs84
1 files changed, 84 insertions, 0 deletions
diff --git a/src/stats.rs b/src/stats.rs
new file mode 100644
index 0000000..10c83b0
--- /dev/null
+++ b/src/stats.rs
@@ -0,0 +1,84 @@
+use systemstat::{System, Platform};
+use systemstat::data::Filesystem;
+
+use std::path::{Path, PathBuf, Component};
+use std::collections::HashMap;
+
+use crate::fail::{HResult, ErrorLog};
+
+#[derive(Debug,Clone)]
+pub struct FsStat {
+ pub stats: HashMap<PathBuf, Filesystem>
+}
+
+impl FsStat {
+ pub fn new() -> HResult<FsStat> {
+ let mut stats = FsStat { stats: HashMap::new() };
+ stats.refresh().log();
+
+ Ok(stats)
+ }
+
+ pub fn refresh(&mut self) -> HResult<()> {
+ let sys = System::new();
+ let mounts = sys.mounts()?;
+
+ let stats = mounts.into_iter()
+ .fold(HashMap::new(), |mut stats, mount: Filesystem| {
+ let path = PathBuf::from(&mount.fs_mounted_on);
+ stats.insert(path, mount);
+ stats
+ });
+
+ self.stats = stats;
+
+ Ok(())
+ }
+
+ pub fn find_fs(&self, path: &Path) -> HResult<&Filesystem> {
+ let candidates = self
+ .stats
+ .keys()
+ .filter(|mount_point| path.starts_with(&mount_point))
+ .collect::<Vec<&PathBuf>>();
+
+ let deepest_match = candidates.iter()
+ .fold(PathBuf::new(), |mut deepest, path| {
+ let curren_path_len = deepest.components().count();
+ let candidate_path_len = path.components().count();
+
+ if candidate_path_len > curren_path_len {
+ deepest = path.to_path_buf();
+ }
+ deepest
+ });
+ let fs = self.stats.get(&deepest_match)?;
+ Ok(fs)
+ }
+}
+
+pub trait FsExt {
+ fn get_dev(&self) -> String;
+ fn get_total(&self) -> String;
+ fn get_free(&self) -> String;
+}
+
+impl FsExt for Filesystem {
+ fn get_dev(&self) -> String {
+ let path = PathBuf::from(&self.fs_mounted_from);
+ let dev = path.components().last().unwrap();
+ let dev = match dev {
+ Component::Normal(dev) => dev.to_string_lossy().to_string(),
+ _ => "wtf".to_string()
+ };
+ dev
+ }
+
+ fn get_total(&self) -> String {
+ self.total.to_string(false)
+ }
+
+ fn get_free(&self) -> String {
+ self.free.to_string(false)
+ }
+}