From 9e51c2428eec1deddce42f959efaa94e2a463721 Mon Sep 17 00:00:00 2001 From: Olivier 'reivilibre Date: Sat, 4 Jun 2022 12:39:53 +0100 Subject: [PATCH] Report the size used by the pile itself in the report --- datman/src/commands/report.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/datman/src/commands/report.rs b/datman/src/commands/report.rs index f9b92f6..37324ab 100644 --- a/datman/src/commands/report.rs +++ b/datman/src/commands/report.rs @@ -13,6 +13,7 @@ use std::io::Read; use std::mem; use std::mem::size_of; use std::os::unix::ffi::OsStrExt; +use std::os::unix::fs::MetadataExt; use std::path::Path; use yama::chunking::RecursiveUnchunker; use yama::commands::{load_pile_descriptor, open_pile, retrieve_tree_node}; @@ -371,7 +372,22 @@ fn format_size(chunks: u32, average_chunk_size: Option) -> String { format!("{} c{}", chunks, est_size_suffix) } +fn calculate_total_filesize_of_dir(dir: &Path) -> anyhow::Result { + let mut total = 0; + for file in std::fs::read_dir(dir)? { + let file = file?; + let metadata = file.metadata()?; + total += metadata.size(); + if metadata.is_dir() { + total += calculate_total_filesize_of_dir(&file.path())?; + } + } + Ok(total) +} + pub fn print_filesystem_space(pile_path: &Path) -> anyhow::Result<()> { + let usage_for_pile = calculate_total_filesize_of_dir(&pile_path)?; + let path_c = CString::new(pile_path.as_os_str().as_bytes()).unwrap(); let stats = unsafe { let mut stats: libc::statfs = mem::zeroed(); @@ -409,6 +425,7 @@ pub fn print_filesystem_space(pile_path: &Path) -> anyhow::Result<()> { Cell::new("Theoretical Size").fg(Color::Cyan), Cell::new("Usable Size").fg(Color::Cyan), Cell::new("Used").fg(Color::Cyan), + Cell::new("Used for Pile").fg(Color::Cyan), Cell::new("Available").fg(Color::Cyan), ]); @@ -428,6 +445,7 @@ pub fn print_filesystem_space(pile_path: &Path) -> anyhow::Result<()> { .fg(Color::Blue), Cell::new(format!("{:>9}", usable_bytes.file_size(&format).unwrap())).fg(Color::Blue), Cell::new(format!("{:>9}", used_bytes.file_size(&format).unwrap())).fg(Color::Blue), + Cell::new(format!("{:>9}", usage_for_pile.file_size(&format).unwrap())).fg(Color::Blue), Cell::new(format!("{:>9}", avail_bytes.file_size(&format).unwrap())) .fg(available_space_colour), ]);