use super::*;
pub fn execute_clean(options: &CleanOptions) -> io::Result<CleanReport> {
let root: &Path = options.get_root_dir();
let dry_run: bool = options.get_dry_run();
let ota_store_dir: PathBuf = load_euv_config(root)
.ok()
.flatten()
.and_then(|loaded: LoadedEuvConfig| loaded.get_config().get_ota().try_get_store().clone())
.map(|configured: PathBuf| {
if configured.is_absolute() {
configured
} else {
root.join(configured)
}
})
.unwrap_or_else(|| root.join(DEFAULT_UPDATE_STORE_DIR));
let targets: [(PathBuf, &str); 4] = [
(root.join(CLEAN_DIR_DIST), CLEAN_CAT_DIST),
(
root.join(CLEAN_DIR_TARGET).join(CLEAN_DIR_MOBILE_HOSTS),
CLEAN_CAT_MOBILE_HOSTS,
),
(
root.join(CLEAN_DIR_TARGET).join(CLEAN_DIR_ICON_CACHE),
CLEAN_CAT_ICON_CACHE,
),
(ota_store_dir.join(STORE_DIR_STAGING), CLEAN_CAT_OTA_STAGING),
];
let mut entries: Vec<CleanEntry> = Vec::new();
let mut total_bytes: u64 = 0;
for (dir_path, category) in targets.iter() {
if !dir_path.exists() {
continue;
}
let size_bytes: u64 = compute_dir_size_bytes(dir_path);
let removed: bool = if dry_run {
false
} else {
if dir_path.is_dir() {
fs::remove_dir_all(dir_path)?;
} else {
fs::remove_file(dir_path)?;
}
true
};
total_bytes = total_bytes.saturating_add(size_bytes);
entries.push(CleanEntry::new(
dir_path.display().to_string(),
(*category).to_string(),
size_bytes,
removed,
));
}
let mut cargo_clean_ran: bool = false;
if options.get_all() {
let cargo_manifest: PathBuf = root.join(CARGO_TOML_NAME);
let target_dir: PathBuf = root.join(CLEAN_DIR_TARGET);
if cargo_manifest.is_file() && target_dir.exists() {
let target_bytes: u64 = compute_dir_size_bytes(&target_dir);
if !dry_run {
let args: Vec<String> = vec![
ARG_CLEAN.to_string(),
ARG_MANIFEST_PATH.to_string(),
cargo_manifest.display().to_string(),
];
let _: CommandRunMetrics =
run_command_streaming(BIN_CARGO, &args, Some(root), &[], false)?;
cargo_clean_ran = true;
}
total_bytes = total_bytes.saturating_add(target_bytes);
entries.push(CleanEntry::new(
target_dir.display().to_string(),
CLEAN_CAT_CARGO_TARGET.to_string(),
target_bytes,
cargo_clean_ran,
));
}
}
Ok(CleanReport::new(
root.display().to_string(),
dry_run,
entries,
total_bytes,
cargo_clean_ran,
))
}
#[must_use]
pub fn format_clean_report(report: &CleanReport) -> String {
if report.get_entries().is_empty() {
return CLEAN_EMPTY_MSG.to_string();
}
let mut table: TerminalTable = TerminalTable::with_headers(vec![
HEADER_STATUS.to_string(),
DOCTOR_HDR_CATEGORY.to_string(),
CLEAN_HDR_SIZE.to_string(),
CLEAN_HDR_PATH.to_string(),
]);
for entry in report.get_entries().iter() {
let status: &str = if entry.get_removed() {
CLEAN_MODE_REMOVED
} else {
CLEAN_MODE_DRY_RUN
};
table.push_row(vec![
status.to_string(),
entry.get_category().clone(),
format_bytes_human(entry.get_bytes_reclaimed()),
entry.get_path().clone(),
]);
}
let mut out: String = table.render();
let verb: &str = if report.get_dry_run() {
CLEAN_MODE_DRY_RUN
} else {
CLEAN_MODE_REMOVED
};
out.push_str(&format!(
"Total ({verb}): {} across {} location(s)\n",
format_bytes_human(report.get_total_bytes_reclaimed()),
report.get_entries().len()
));
out
}
pub(crate) fn compute_dir_size_bytes(path: &Path) -> u64 {
let Ok(meta) = fs::symlink_metadata(path) else {
return 0;
};
if meta.file_type().is_symlink() {
return 0;
}
if meta.is_file() {
return meta.len();
}
if !meta.is_dir() {
return 0;
}
let mut total: u64 = 0;
if let Ok(entries) = fs::read_dir(path) {
for entry in entries.flatten() {
total = total.saturating_add(compute_dir_size_bytes(&entry.path()));
}
}
total
}