use std::fs;
use std::path::{Path, PathBuf};
use crate::cache::caches::{Cache, RegistrySuperCache};
use crate::cache::*;
use crate::library::*;
use humansize::{file_size_opts, FileSize};
pub(crate) enum DryRunMessage<'a> {
Custom(&'a str), Default, #[allow(dead_code)]
None, }
fn parse_version(path: &Path) -> Result<(String, String), Error> {
#[allow(clippy::single_match_else)]
let filename = match path.file_stem() {
Some(name) => name.to_str().unwrap().to_string(),
None => {
return Err(Error::MalformedPackageName(path.display().to_string()));
}
};
let mut name = Vec::new();
let mut version = Vec::new();
let mut found_version = false;
filename.split('-').for_each(|seg| {
let first_char = seg.chars().next();
if let Some(char) = first_char {
if char.is_numeric() {
version.push(seg);
found_version = true;
} else if found_version {
version.push(seg);
} else {
name.push(seg);
}
}
});
let name = name.join("-");
let version = version.join("-");
Ok((name, version))
}
pub(crate) fn rm_old_crates(
amount_to_keep: u64,
dry_run: bool,
registry_src_path: &Path,
size_changed: &mut bool,
) -> Result<(), Error> {
println!();
let mut removed_size = 0;
for repo in fs::read_dir(registry_src_path).unwrap() {
let mut crate_list = fs::read_dir(&repo.unwrap().path())
.unwrap()
.map(|cratepath| cratepath.unwrap().path())
.collect::<Vec<PathBuf>>();
crate_list.sort();
crate_list.reverse();
let mut versions_of_this_package = 0;
let mut last_pkgname = String::new();
for pkgpath in &crate_list {
let (pkgname, pkgver) = parse_version(pkgpath)?;
if amount_to_keep == 0 {
removed_size += fs::metadata(pkgpath)
.unwrap_or_else(|_| {
panic!("Failed to get metadata of file '{}'", &pkgpath.display())
})
.len();
let dryrun_msg = format!(
"dry run: not actually deleting {} {} at {}",
pkgname,
pkgver,
pkgpath.display()
);
remove_file(
pkgpath,
dry_run,
size_changed,
None,
&DryRunMessage::Custom(&dryrun_msg),
None,
);
continue;
}
if last_pkgname == pkgname {
versions_of_this_package += 1;
if versions_of_this_package == amount_to_keep {
removed_size += fs::metadata(pkgpath)
.unwrap_or_else(|_| {
panic!("Failed to get metadata of file '{}'", &pkgpath.display())
})
.len();
let dryrun_msg = format!(
"dry run: not actually deleting {} {} at {}",
pkgname,
pkgver,
pkgpath.display()
);
remove_file(
pkgpath,
dry_run,
size_changed,
None,
&DryRunMessage::Custom(&dryrun_msg),
None,
);
}
} else {
versions_of_this_package = 0;
last_pkgname = pkgname;
} } }
println!(
"Removed {} of compressed crate sources.",
removed_size.file_size(file_size_opts::DECIMAL).unwrap()
);
Ok(())
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn remove_dir_via_cmdline(
directory: Option<&str>,
dry_run: bool,
ccd: &CargoCachePaths,
size_changed: &mut bool,
checkouts_cache: &mut git_checkouts::GitCheckoutCache,
bare_repos_cache: &mut git_bare_repos::GitRepoCache,
registry_index_caches: &mut registry_index::RegistryIndicesCache,
registry_pkgs_cache: &mut registry_pkg_cache::RegistryPkgCaches,
registry_sources_caches: &mut registry_sources::RegistrySourceCaches,
) -> Result<(), Error> {
let dirs_to_remove = components_from_groups(directory)?;
let mut size_removed: u64 = 0;
if dry_run {
println!(); }
for component in dirs_to_remove {
match component {
Component::RegistryCrateCache => {
let size = registry_pkgs_cache.total_size();
size_removed += size;
remove_with_default_message(
&ccd.registry_pkg_cache,
dry_run,
size_changed,
Some(size),
);
if !dry_run {
registry_pkgs_cache.invalidate();
}
}
Component::RegistrySources => {
let size = registry_sources_caches.total_size();
size_removed += size;
remove_with_default_message(
&ccd.registry_sources,
dry_run,
size_changed,
Some(size),
);
if !dry_run {
registry_sources_caches.invalidate();
}
}
Component::RegistryIndex => {
let size_of_all_indices: u64 = registry_index_caches.total_size();
size_removed += size_of_all_indices;
remove_with_default_message(
&ccd.registry_index,
dry_run,
size_changed,
Some(size_of_all_indices),
);
if !dry_run {
registry_index_caches.invalidate();
}
}
Component::GitRepos => {
let size = checkouts_cache.total_size();
size_removed += size;
remove_with_default_message(&ccd.git_checkouts, dry_run, size_changed, Some(size));
if !dry_run {
checkouts_cache.invalidate();
}
}
Component::GitDB => {
let size = bare_repos_cache.total_size();
size_removed += size;
remove_with_default_message(&ccd.git_repos_bare, dry_run, size_changed, Some(size));
if !dry_run {
bare_repos_cache.invalidate();
}
}
}
}
if dry_run {
println!(
"dry-run: would remove in total: {}",
size_removed.file_size(file_size_opts::DECIMAL).unwrap()
);
}
Ok(())
}
pub(crate) fn remove_with_default_message(
dir: &Path,
dry_run: bool,
size_changed: &mut bool,
total_size_from_cache: Option<u64>,
) {
let msg = Some(format!("removing: '{}'", dir.display()));
remove_file(
dir,
dry_run,
size_changed,
msg,
&DryRunMessage::Default,
total_size_from_cache,
);
}
pub(crate) fn remove_file(
path: &Path,
dry_run: bool,
size_changed: &mut bool,
deletion_msg: Option<String>,
dry_run_msg: &DryRunMessage<'_>,
total_size_from_cache: Option<u64>,
) {
if dry_run {
match dry_run_msg {
DryRunMessage::Custom(msg) => {
println!("{}", msg);
}
DryRunMessage::Default => {
#[allow(clippy::single_match_else)]
match total_size_from_cache {
Some(size) => {
let size_hr = size.file_size(file_size_opts::DECIMAL).unwrap();
println!("dry-run: would remove: '{}' ({})", path.display(), size_hr);
}
None => {
println!("dry-run: would remove: '{}'", path.display());
}
}
}
DryRunMessage::None => {}
}
} else {
if let Some(msg) = deletion_msg {
println!("{}", msg);
}
if path.is_file() && fs::remove_file(path).is_err() {
eprintln!("Warning: failed to remove file \"{}\".", path.display());
} else {
*size_changed = true;
}
if path.is_dir() {
if let Err(error) = remove_dir_all::remove_dir_all(path) {
eprintln!(
"Warning: failed to recursively remove directory \"{}\".",
path.display()
);
eprintln!("error: {:?}", error);
} else {
*size_changed = true;
}
}
}
}
#[cfg(test)]
mod libtests {
use super::*;
use pretty_assertions::assert_eq;
#[test]
fn test_parse_version() {
let (name, version): (String, String) =
parse_version(&PathBuf::from("heim-runtime-0.1.0-beta.1.crate")).unwrap();
assert_eq!(name, "heim-runtime");
assert_eq!(version, "0.1.0-beta.1");
let (name2, version2): (String, String) =
parse_version(&PathBuf::from("cargo-cache-0.4.3.crate")).unwrap();
assert_eq!(name2, "cargo-cache");
assert_eq!(version2, "0.4.3");
}
}