pub mod browser_cache;
pub mod recent_files;
pub mod thumbnails;
pub mod user_logs;
pub mod xdg_cache;
use crate::{
config::Config,
fsutil::{self, PathFilter},
model::{CleanerGroup, Item, Plan, modified_epoch_seconds},
};
use anyhow::Result;
use directories::BaseDirs;
use std::path::{Path, PathBuf};
pub struct ScanContext<'a> {
pub base_dirs: BaseDirs,
pub filter: PathFilter,
pub config: &'a Config,
pub state_dir_override: Option<Option<PathBuf>>,
}
pub trait Cleaner: Send + Sync {
fn id(&self) -> &'static str;
fn description(&self) -> &'static str;
fn group(&self) -> CleanerGroup;
fn scan(&self, ctx: &ScanContext) -> Result<Plan>;
}
impl<'a> ScanContext<'a> {
pub fn state_dir(&self) -> Option<&Path> {
match &self.state_dir_override {
Some(override_opt) => override_opt.as_deref(),
None => self.base_dirs.state_dir(),
}
}
}
pub fn registry() -> Vec<Box<dyn Cleaner>> {
vec![
Box::new(xdg_cache::XdgCacheCleaner::new()),
Box::new(thumbnails::ThumbnailsCleaner::new()),
Box::new(browser_cache::ChromiumCacheCleaner::new()),
Box::new(browser_cache::FirefoxCacheCleaner::new()),
Box::new(recent_files::RecentFilesCleaner::new()),
Box::new(user_logs::UserLogsCleaner::new()),
]
}
pub fn item_from_path(
path: &Path,
cleaner_id: &str,
group: CleanerGroup,
reason: Option<&str>,
filter: &PathFilter,
) -> Result<Option<Item>> {
if !filter.allows(path) {
return Ok(None);
}
let meta = match std::fs::symlink_metadata(path) {
Ok(m) => m,
Err(_) => return Ok(None),
};
if meta.file_type().is_symlink() {
return Ok(None);
}
let is_dir = meta.is_dir();
let bytes = if is_dir {
fsutil::dir_size_bytes(path)?
} else {
meta.len()
};
Ok(Some(Item {
path: path.to_path_buf(),
bytes,
cleaner_id: cleaner_id.to_string(),
group,
reason: reason.map(|r| r.to_string()),
modified_epoch: modified_epoch_seconds(&meta),
is_dir,
}))
}