use std::path::{Path, PathBuf}; use std::fs; use std::os::unix::fs::MetadataExt; use glob::Pattern; fn trim(repo: &String, path: &Path, trim_date: i64, rules: &String) -> Result<(), std::io::Error> { let metadata = fs::symlink_metadata(path)?; if metadata.is_dir() { if rules_check(repo, path, rules) { for entry in fs::read_dir(path)? { let entry = entry?; let entry_path = entry.path(); trim(repo, &entry_path, trim_date, rules)?; } } } else if metadata.is_file() { if rules_check(repo, path, rules) { let atime = metadata.atime(); if atime < trim_date { fs::remove_file(path)?; } } } else if metadata.file_type().is_symlink() { if let Ok(symlink_value) = fs::read_link(path) { let symlink_path = if symlink_value.is_relative() { path.parent().unwrap().join(&symlink_value) } else { symlink_value }; if !symlink_path.exists() { println!("remove symlink: {} {}", path.display(), symlink_path.display()); fs::remove_file(path)?; } } } Ok(()) } pub fn trim_handler(repo: &String, trim_date: i64) { let etc_path = super::get_etc_path(); let cfg_path = etc_path.join("aeropkg.md"); let rules = crate::utils::parser::get_trim_rules(&cfg_path).unwrap(); let pkg_dir = PathBuf::from("/pkg").join(repo); trim(repo, &pkg_dir, trim_date, &rules).unwrap(); } fn rules_check(repo: &String, path: &Path, rules: &String) -> bool { let mut confirm = true; for rule in rules.lines() { let mut rule = rule.trim(); let invert = rule.starts_with('!'); if invert { rule = &rule[1..]; } let wildcard_string = if rule.starts_with('/') { rule.to_string() } else { format!("/pkg/{}/{}", repo, rule) }; let path_str = path.to_str().unwrap_or(""); if let Ok(pattern) = Pattern::new(&wildcard_string) { if pattern.matches(path_str) { confirm = !invert; } } } confirm }