use std::fs;
use std::path::Path;
pub(super) fn generate_snapshot_id() -> String {
uuid::Uuid::new_v4().to_string().replace('-', "")
}
pub(super) fn calculate_dir_size(path: &Path) -> u64 {
if !path.exists() {
return 0;
}
let mut size: u64 = 0;
if let Ok(entries) = fs::read_dir(path) {
for entry in entries.flatten() {
let path = entry.path();
if path.is_file() {
if let Ok(metadata) = fs::metadata(&path) {
size += metadata.len();
}
} else if path.is_dir() {
size += calculate_dir_size(&path);
}
}
}
size
}
pub(super) fn format_size(bytes: u64) -> String {
const KB: u64 = 1024;
const MB: u64 = KB * 1024;
const GB: u64 = MB * 1024;
if bytes >= GB {
format!("{:.2} GB", bytes as f64 / GB as f64)
} else if bytes >= MB {
format!("{:.2} MB", bytes as f64 / MB as f64)
} else if bytes >= KB {
format!("{:.2} KB", bytes as f64 / KB as f64)
} else {
format!("{bytes} bytes")
}
}