summaryrefslogtreecommitdiffstats
path: root/src/usecase/maintenance.rs
blob: 8c66ee2e118c54b7ee2b5ca1528bc7c8bdcad45d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//! Housekeeping: retention, backups, and clearing the attention list.

use std::path::{Path, PathBuf};

use crate::config::Config;
use crate::db::{self, Db};
use crate::error::Result;
use crate::ops;

/// What a garbage collection would do, before anything is deleted.
// GcPlan stays `pub`: what `usecase::plan_gc` returns to the `aur-build` binary, which is a separate crate.
pub struct GcPlan {
    pub keep: usize,
    /// Releases whose files survive.
    pub kept: usize,
    /// Releases whose files would go, with the files each one owns.
    pub evicting: Vec<(i64, Vec<PathBuf>)>,
}

/// Work out what retention would remove, without removing it.
pub fn plan_gc(cfg: &Config, db: &Db, keep: Option<usize>) -> Result<GcPlan> {
    let keep = keep.unwrap_or(ops::DEFAULT_KEEP);
    let (kept, evict) = ops::plan_retention(db, keep)?;
    let mut evicting = Vec::with_capacity(evict.len());
    for e in evict {
        evicting.push((e.release_id, ops::release_files(cfg, db, e.release_id)?));
    }
    Ok(GcPlan { keep, kept: kept.len(), evicting })
}

/// Delete what retention says should go.
pub fn run_gc(cfg: &Config, db: &Db, keep: Option<usize>) -> Result<ops::RetentionReport> {
    ops::retention(cfg, db, Some(keep.unwrap_or(ops::DEFAULT_KEEP)))
}

/// Copy the database to a single file, WAL folded in.
pub fn backup(db: &Db, dest: &Path) -> Result<PathBuf> {
    ops::backup(db, dest)
}

/// Mark a notice as seen.
pub fn acknowledge_notice(db: &Db, id: i64) -> Result<()> {
    db::acknowledge_notice(&db.writer, id)
}