From 604aeb5c24c44eebca2e5f09c88538466326c3b3 Mon Sep 17 00:00:00 2001 From: Lovecraftian Horror Date: Sat, 29 Oct 2022 23:05:19 -0600 Subject: [PATCH] Address review comments --- src/cargo/ops/cargo_clean.rs | 56 +++++++++++++++++------------------- 1 file changed, 27 insertions(+), 29 deletions(-) diff --git a/src/cargo/ops/cargo_clean.rs b/src/cargo/ops/cargo_clean.rs index b55c2726e31..507c2d89b1c 100644 --- a/src/cargo/ops/cargo_clean.rs +++ b/src/cargo/ops/cargo_clean.rs @@ -232,25 +232,41 @@ fn escape_glob_path(pattern: &Path) -> CargoResult { Ok(glob::Pattern::escape(pattern)) } +/// Glob remove artifacts for the provided `package` +/// +/// Make sure the artifact is for `package` and not another crate that is prefixed by +/// `package` by getting the original name stripped of the trailing hash and possible +/// extension fn rm_rf_package_glob_containing_hash( package: &str, pattern: &Path, config: &Config, progress: &mut dyn CleaningProgressBar, ) -> CargoResult<()> { - rm_rf_glob_helper(Some(package), pattern, config, progress) -} + // TODO: Display utf8 warning to user? Or switch to globset? + let pattern = pattern + .to_str() + .ok_or_else(|| anyhow::anyhow!("expected utf-8 path"))?; + for path in glob::glob(pattern)? { + let path = path?; -fn rm_rf_glob( - pattern: &Path, - config: &Config, - progress: &mut dyn CleaningProgressBar, -) -> CargoResult<()> { - rm_rf_glob_helper(None, pattern, config, progress) + let pkg_name = path + .file_name() + .and_then(std::ffi::OsStr::to_str) + .and_then(|artifact| artifact.rsplit_once('-')) + .ok_or_else(|| anyhow::anyhow!("expected utf-8 path"))? + .0; + + if pkg_name != package { + continue; + } + + rm_rf(&path, config, progress)?; + } + Ok(()) } -fn rm_rf_glob_helper( - package: Option<&str>, +fn rm_rf_glob( pattern: &Path, config: &Config, progress: &mut dyn CleaningProgressBar, @@ -260,25 +276,7 @@ fn rm_rf_glob_helper( .to_str() .ok_or_else(|| anyhow::anyhow!("expected utf-8 path"))?; for path in glob::glob(pattern)? { - let path = path?; - - // Make sure the artifact is for `package` and not another crate that is prefixed by - // `package` by getting the original name stripped of the trialing hash and possible - // extension - if let Some(package) = package { - let pkg_name = path - .file_name() - .and_then(std::ffi::OsStr::to_str) - .and_then(|artifact| artifact.rsplit_once('-')) - .expect("artifact name is valid UTF-8 and contains at least one hyphen") - .0; - - if pkg_name != package { - continue; - } - } - - rm_rf(&path, config, progress)?; + rm_rf(&path?, config, progress)?; } Ok(()) }