Files
2026-07-06 15:25:51 +08:00

64 lines
2.0 KiB
Rust

use crate::{link::LinksFile, package::PkgSpec};
use rust_i18n::t;
impl super::Packie {
pub fn remove(&mut self, pkgspec: &PkgSpec) -> Result<(), RemoveError> {
// Find the installation to remove
let mut found = self
.search_installation(pkgspec)
.map_err(RemoveError::Database)?;
if found.is_empty() {
return Err(RemoveError::NotFound);
}
if found.len() != 1 {
return Err(RemoveError::NotUniquePkgSpec);
}
assert_eq!(found.len(), 1);
let found = found.remove(0);
let pkg_ident = found.pkg_manifest.pkg_ident();
// Remove all links
let links = LinksFile(self.local_data.links_file(&pkg_ident));
if let Ok(links) = links.iter() {
for link in links {
let Ok(link) = link else {
continue;
};
_ = crate::link::deactivate_by_description(self, &pkg_ident, &link);
}
}
// Remove package files
let bundle_dir = self.profile.pkg_dir.join(pkg_ident.to_string());
std::fs::remove_dir_all(&bundle_dir).map_err(RemoveError::RemoveFiles)?;
// Remove database records
self.local_db
.remove_installation(&pkg_ident)
.map_err(RemoveError::Database)?;
self.local_db
.remove_abspkg_provided_by(&pkg_ident)
.map_err(RemoveError::Database)?;
// Remove the links file
_ = std::fs::remove_file(self.local_data.links_file(&pkg_ident));
Ok(())
}
}
#[derive(Debug, thiserror::Error)]
pub enum RemoveError {
#[error("{}", t!("remove.RemoveError.NotFound"))]
NotFound,
#[error("{}", t!("remove.RemoveError.NotUniquePkgSpec"))]
NotUniquePkgSpec,
#[error("{}", t!("remove.RemoveError.RemoveFiles", error = .0))]
RemoveFiles(std::io::Error),
#[error("{}", t!("remove.RemoveError.Database", error = .0))]
Database(rusqlite::Error),
}