1use std::fs::{metadata, read_dir};
8use std::io;
9use std::path::Path;
10
11use humansize::{BINARY, SizeFormatter};
12use runtime::cache::Cache;
13
14pub(crate) fn cache_statistics() {
15 if let Some(cache) = Cache::new() {
16 println!("Location: {}", cache.dir().display());
17 match cache_size(cache.dir()) {
18 Ok(size) => println!("Size: {}", SizeFormatter::new(size, BINARY)),
19 Err(err) => eprintln!("Error while Calculating Size: {err}"),
20 }
21 } else {
22 println!("No Cache Found");
23 }
24}
25
26fn cache_size(folder: &Path) -> io::Result<u64> {
27 let mut size = 0;
28 let metadata = metadata(folder)?;
29 if metadata.is_dir() {
30 for entry in read_dir(folder)? {
31 size += cache_size(&entry?.path())?;
32 }
33 } else {
34 size += metadata.len();
35 }
36 Ok(size)
37}