Skip to content

Commit

Permalink
feat(-C): -u to only delete uninstalled packages
Browse files Browse the repository at this point in the history
Closes #920
  • Loading branch information
fosskers committed Sep 14, 2024
1 parent 5c1692f commit 991c698
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Aura Changelog

## Unreleased

#### Added

- `-Cc`: A new `-u` switch that causes only uninstalled packages to be checked and deleted.

## 4.0.5 (2024-09-08)

#### Changed
Expand Down
13 changes: 12 additions & 1 deletion rust/aura-pm/src/command/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub(crate) enum Error {
CurrDir(std::io::Error),
Mkdir(PathBuf, std::io::Error),
Date(time::error::Format),
Env(crate::env::Error),
}

impl Nested for Error {
Expand All @@ -64,6 +65,7 @@ impl Nested for Error {
Error::CurrDir(e) => error!("{e}"),
Error::Mkdir(_, e) => error!("{e}"),
Error::Date(e) => error!("{e}"),
Error::Env(e) => e.nested(),
}
}
}
Expand All @@ -83,6 +85,7 @@ impl Localised for Error {
Error::CurrDir(_) => fl!(fll, "C-b-curr"),
Error::Mkdir(p, _) => fl!(fll, "dir-mkdir", dir = p.utf8()),
Error::Date(_) => fl!(fll, "err-time-format"),
Error::Env(_) => fl!(fll, "err-alpm"),
}
}
}
Expand Down Expand Up @@ -259,7 +262,12 @@ pub(crate) fn search(caches: &[&Path], term: &str) -> Result<(), Error> {
}

/// Delete all but `keep`-many old tarballs for each package in the cache.
pub(crate) fn clean(env: &Env, fll: &FluentLanguageLoader, keep: usize) -> Result<(), Error> {
pub(crate) fn clean(
env: &Env,
fll: &FluentLanguageLoader,
keep: usize,
uninstalled: bool,
) -> Result<(), Error> {
let caches = env.caches();
debug!("Caches: {:?}", caches);

Expand All @@ -271,13 +279,16 @@ pub(crate) fn clean(env: &Env, fll: &FluentLanguageLoader, keep: usize) -> Resul
// Proceed if the user accepts.
proceed!(fll, env, "proceed").ok_or(Error::Cancelled)?;

let alpm = env.alpm().map_err(Error::Env)?;
let db = alpm.as_ref().localdb();
let elevation = env.sudo();

// Get all the tarball paths, sort and group them by name, and then remove them.
aura_core::cache::package_paths(&caches)
.sorted_by(|p0, p1| p1.cmp(p0)) // Forces a `collect` underneath.
.chunk_by(|pp| pp.as_package().name.clone()) // TODO Naughty clone.
.into_iter()
.filter(|(name, _)| !uninstalled || db.pkg(name.as_ref()).is_err())
.flat_map(|(_, group)| group.skip(keep)) // Thanks to the reverse-sort above, `group` is already backwards.
.for_each(|pp| {
let _ = pp.sudo_remove_with_sig(elevation); // TODO Handle this error better?
Expand Down
6 changes: 5 additions & 1 deletion rust/aura-pm/src/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1376,10 +1376,14 @@ pub struct Cache {
#[clap(group = "cache", short, long, value_name = "N", display_order = 1)]
pub clean: Option<usize>,

/// [-c] Delete only those tarballs which aren't present in a snapshot.
/// Delete tarballs which aren't present in a snapshot.
#[clap(group = "cache", long = "notsaved", short = 'n', display_order = 1)]
pub clean_unsaved: bool,

/// [-c] Delete tarballs for currently uninstalled packages.
#[clap(long = "uninstalled", short = 'u', display_order = 1)]
pub clean_uninstalled: bool,

/// Look up specific packages for info on their cache entries.
#[clap(group = "cache", short, long, value_name = "pkg(s)", num_args = 1.., display_order = 1)]
pub info: Vec<String>,
Expand Down
6 changes: 5 additions & 1 deletion rust/aura-pm/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,11 @@ fn work(args: Args, env: Env, fll: &FluentLanguageLoader) -> Result<(), Error> {
}
SubCmd::Cache(c) if c.search.is_some() => cache::search(&env.caches(), &c.search.unwrap())?,
SubCmd::Cache(c) if c.backup.is_some() => cache::backup(fll, &env, &c.backup.unwrap())?,
SubCmd::Cache(Cache { clean: Some(n), .. }) => cache::clean(&env, fll, n)?,
SubCmd::Cache(Cache {
clean: Some(n),
clean_uninstalled,
..
}) => cache::clean(&env, fll, n, clean_uninstalled)?,
SubCmd::Cache(c) if c.clean_unsaved => cache::clean_not_saved(fll, &env)?,
SubCmd::Cache(c) if c.invalid => cache::invalid(&env, fll, &env.alpm()?, &env.caches())?,
SubCmd::Cache(c) if c.list => cache::list(&env.caches())?,
Expand Down

0 comments on commit 991c698

Please sign in to comment.