cli/commands/
cache.rs

1/*
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 */
6
7use 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}