Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
CosmicHorrorDev committed Oct 30, 2022
1 parent a6d0e96 commit 604aeb5
Showing 1 changed file with 27 additions and 29 deletions.
56 changes: 27 additions & 29 deletions src/cargo/ops/cargo_clean.rs
Expand Up @@ -232,25 +232,41 @@ fn escape_glob_path(pattern: &Path) -> CargoResult<String> {
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,
Expand All @@ -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(())
}
Expand Down

0 comments on commit 604aeb5

Please sign in to comment.