diff --git a/src/fingerprint.rs b/src/fingerprint.rs index f8e7a8e..026ac27 100644 --- a/src/fingerprint.rs +++ b/src/fingerprint.rs @@ -390,26 +390,16 @@ fn rustup_toolchain_list() -> Option> { } } -pub fn remove_not_built_with( - dir: &Path, - rust_version_to_keep: &[String], - dry_run: bool, -) -> Result { - debug!("cleaning: {:?} with remove_not_built_with", dir); - let mut total_disk_space = 0; - let hashed_rust_version_to_keep = if !rust_version_to_keep.is_empty() { - info!( - "Using specified installed toolchains: {:?}", - rust_version_to_keep - ); - lookup_from_names(rust_version_to_keep.iter().map(Some))? +pub fn hash_toolchains(rust_versions: Option<&Vec>) -> Result, Error> { + let hashed_versions = if let Some(versions) = rust_versions { + info!("Using specified installed toolchains: {:?}", versions); + lookup_from_names(versions.iter().map(Some))? } else { match rustup_toolchain_list() { Some(list) => { info!("Using all installed toolchains: {:?}", list); lookup_from_names(list.iter().map(Some))? } - None => { info!("Couldn't identify the installed toolchains, using bare `rustc` call"); let list: Vec> = vec![None]; @@ -417,9 +407,20 @@ pub fn remove_not_built_with( } } }; + + Ok(hashed_versions) +} + +pub fn remove_not_built_with( + dir: &Path, + hashed_rust_version_to_keep: &HashSet, + dry_run: bool, +) -> Result { + debug!("cleaning: {:?} with remove_not_built_with", dir); + let mut total_disk_space = 0; for fing in lookup_all_fingerprint_dirs(dir) { let path = fing.into_path(); - let keep = load_all_fingerprints_built_with(&path, &hashed_rust_version_to_keep)?; + let keep = load_all_fingerprints_built_with(&path, hashed_rust_version_to_keep)?; total_disk_space += remove_not_built_with_in_a_profile(path.parent().unwrap(), &keep, dry_run)?; } diff --git a/src/main.rs b/src/main.rs index 68b8312..b5e166b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,7 +17,9 @@ mod stamp; mod util; use self::cli::Criterion; -use self::fingerprint::{remove_not_built_with, remove_older_than, remove_older_until_fits}; +use self::fingerprint::{ + hash_toolchains, remove_not_built_with, remove_older_than, remove_older_until_fits, +}; use self::stamp::Timestamp; use self::util::format_bytes; @@ -164,13 +166,15 @@ fn main() -> anyhow::Result<()> { }; let toolchains = match &criterion { - Criterion::Installed => Some(vec![]), - Criterion::Toolchains(vec) => Some(vec).cloned(), + Criterion::Installed => Some(None), + Criterion::Toolchains(vec) => Some(Some(vec.clone())), _ => None, }; if let Some(toolchains) = toolchains { + let hashed_toolchains = hash_toolchains(toolchains.as_ref())?; + for project_path in &paths { - match remove_not_built_with(project_path, &toolchains, dry_run) { + match remove_not_built_with(project_path, &hashed_toolchains, dry_run) { Ok(cleaned_amount) if dry_run => { info!( "Would clean: {} from {project_path:?}",