diff --git a/src/data/graph.rs b/src/data/graph.rs index a9e9847..87f9eee 100644 --- a/src/data/graph.rs +++ b/src/data/graph.rs @@ -1,6 +1,6 @@ use super::*; use std::collections::HashMap; -use std::path::PathBuf; +use std::path::{Path,PathBuf}; use std::borrow::Borrow; /// Contains a graph of all paths and inodes that were successfully stat'd @@ -88,6 +88,17 @@ enum NodeKind File(u64), } +impl NodeKind +{ + pub fn len(&self) -> Option + { + match self { + Self::File(f) => Some(*f), + Self::Directory(_, o) => *o, + } + } +} + #[derive(Debug, Clone, PartialEq, Eq)] struct HierarchicalNode { @@ -95,6 +106,26 @@ struct HierarchicalNode inode: INode, } +impl HierarchicalNode +{ + #[inline(always)] pub fn len(&self) -> Option + { + self.kind.len() + } + + pub fn is_dir(&self) -> bool + { + match self.kind { + NodeKind::Directory(_, _) => true, + _ => false + } + } + #[inline] pub fn is_file(&self) -> bool + { + !self.is_dir() + } +} + /// A hierarchical graph of node sizes #[derive(Debug, Clone, PartialEq, Eq)] pub struct HierarchicalINodeGraph @@ -210,6 +241,25 @@ impl HierarchicalINodeGraph new } + + /// Get the max size and the path with this size + pub fn path_max_size_for(&self, kind: FsObjKind) -> Option<(&Path, u64)> + { + self.table.iter().filter_map(|(path, hi)| { + match kind { + FsObjKind::File if hi.is_file() => hi.len().map(|x| (path.as_path(), x)), + FsObjKind::Directory if hi.is_dir() => hi.len().map(|x| (path.as_path(), x)), + _ => None + } + }).max_by_key(|x| x.1) + } + /// Get the max size and the path with this size, whether it's a `Directory` or a `File`. + pub fn path_max_size(&self) -> Option<(&Path, u64)> + { + self.table.iter().filter_map(|(path, hi)| { + hi.len().map(|x| (path.as_path(), x)) + }).max_by_key(|x| x.1) + } } impl From for HierarchicalINodeGraph @@ -219,3 +269,11 @@ impl From for HierarchicalINodeGraph Self::create(from) } } + +/// A file or directory +#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Copy)] +pub enum FsObjKind +{ + Directory, + File, +} diff --git a/src/data/mod.rs b/src/data/mod.rs index 5a9cd7f..58587f1 100644 --- a/src/data/mod.rs +++ b/src/data/mod.rs @@ -13,7 +13,12 @@ pub const CACHE_GATE_TIMEOUT: Duration = duration!(100 ms); pub const CACHE_GATED_LAG: Duration = duration!(10 ms); mod cache; pub use cache::Cache; -pub mod graph; pub use graph::INodeInfoGraph; +pub mod graph; +pub use graph::{ + INodeInfoGraph, + HierarchicalINodeGraph, + FsObjKind as FsKind, +}; /// A raw file or directory inode number /// diff --git a/src/main.rs b/src/main.rs index 5d8b472..ca92005 100644 --- a/src/main.rs +++ b/src/main.rs @@ -52,6 +52,10 @@ async fn main() -> eyre::Result<()> { graph.compute_recursive_sizes(); println!("{:?}", graph); + + println!("Max size file: {:?}", graph.path_max_size_for(data::FsKind::File)); + println!("Max size dir: {:?}", graph.path_max_size_for(data::FsKind::Directory)); + println!("Max size all: {:?}", graph.path_max_size()); /* let max_size = graph.directories().map(|x| x.size()).max(); println!("Max size: {:?}", max_size);*/