Files
packie/src/install.rs
T
2026-11-16 11:08:41 +08:00

233 lines
7.2 KiB
Rust

use crate::{
configure::ConfigureOptions,
local::{DependencyRecord, InstallFlags, Installation},
package::{Package, PackageError, PkgIdent, PkgSpec},
repo::RepoError,
};
use rust_i18n::t;
use std::path::Path;
#[derive(Debug, Clone)]
pub struct InstallOptions {
/// Package spec to record for this installation.
pub install_pkgspec: Option<PkgSpec>,
/// Whether we should run configure scripts in interactive mode.
pub interactive_configure: bool,
/// Whether this package is auto-installed.
pub auto_installed: bool,
/// Mark this installation as an upgradation of an installed package.
pub is_upgradation_of: Option<PkgIdent>,
}
impl InstallOptions {
pub fn new() -> Self {
Self {
install_pkgspec: None,
interactive_configure: true,
auto_installed: false,
is_upgradation_of: None,
}
}
}
impl Default for InstallOptions {
fn default() -> Self {
Self::new()
}
}
impl super::Packie {
/// Installs a package, from a local package file.
pub fn install_package_file<P: AsRef<Path>>(
&mut self,
path: P,
options: &InstallOptions,
) -> Result<(), InstallError> {
let package = Package::open(path)?;
self.install_package(&package, options)
}
/// Installs a package, from a constructed [`Package`].
pub fn install_package(
&mut self,
package: &Package,
options: &InstallOptions,
) -> Result<(), InstallError> {
// Collect necessary information
let pkg_manifest = package.manifest()?;
let pkg_ident = pkg_manifest.pkg_ident();
let installed_size = package.installed_size()?;
let install_pkgspec = options
.install_pkgspec
.clone()
.unwrap_or_else(|| default_install_pkgspec(pkg_ident.clone()));
// Check if the package is previously installed
if !self
.search_installation(&pkg_ident.clone().into())?
.is_empty()
{
return Err(InstallError::AlreadyInstalled);
}
// Check if the package spec is valid
let real_matches = install_pkgspec.matches(&pkg_ident);
let concept_matches = pkg_manifest
.provides
.iter()
.any(|x| install_pkgspec.concept_matches(x));
if !(real_matches || concept_matches) {
return Err(InstallError::PkgSpec);
}
// Check & Collect "real" dependencies
let mut real_dependencies = Vec::with_capacity(pkg_manifest.dependencies.len());
for pkg_spec_b in pkg_manifest.dependencies.iter() {
let candidates = self.search_installation(pkg_spec_b)?;
if candidates.is_empty() {
return Err(InstallError::DependencyNotSatisfied(pkg_spec_b.clone()));
}
let prior = crate::graph::prior_pkg(
candidates.iter().map(|x| x.pkg_manifest.pkg_ident()),
&self.profile().host_arch,
);
real_dependencies.push((pkg_spec_b.clone(), prior));
}
// Copy package files
let dest_dir = self.profile.pkg_dir.join(pkg_ident.prefix_name());
crate::common::copy_dir(package.bundle_dir(), dest_dir).map_err(InstallError::Copy)?;
// Record installation in the database
let mut install_flags = InstallFlags::default();
if options.auto_installed {
install_flags.set(InstallFlags::AUTO_INSTALL);
}
let installation = Installation {
pkg_manifest: pkg_manifest.clone(),
install_pkgspec,
install_date: crate::common::timestamp_secs(),
update_date: crate::common::timestamp_secs(),
installed_size,
install_flags,
};
self.local_db.insert_installation(&installation)?;
// Record "real" dependencies in the database
for (pkg_spec_b, pkg_ident_b) in real_dependencies {
self.local_db.insert_dependency(&DependencyRecord {
pkg_ident_a: pkg_ident.clone(),
pkg_spec_b,
pkg_ident_b,
})?;
}
// Record concept provision in the database
for i in installation.pkg_manifest.provides.iter() {
self.local_db.insert_concept(&i, &pkg_ident)?;
}
// Copy links file
if let Some(links) = package.links() {
std::fs::copy(&links.0, self.local_data.links_file(&pkg_ident))
.map_err(|err| InstallError::Database(Box::new(err)))?;
}
// Copy configure files
for i in std::fs::read_dir(package.configure_scripts())
.into_iter()
.flatten()
.filter_map(Result::ok)
{
let dst = self
.local_data
.configure_script(&pkg_ident, &i.file_name().to_string_lossy());
std::fs::copy(i.path(), dst).map_err(|err| InstallError::Database(Box::new(err)))?;
}
// Process upgradation
if let Some(original) = &options.is_upgradation_of {
self.local_db
.update_dependency(None, original, &pkg_ident)?;
self.remove(&PkgSpec::from(original.clone()))
.map_err(InstallError::Remove)?;
}
// Enable default links
if let Some(links) = package.links() {
for linkdes in links.iter().ok().into_iter().flatten() {
let Ok(linkdes) = linkdes else {
continue;
};
if !linkdes.default {
continue;
}
_ = crate::link::activate_by_description(self, &pkg_ident, &linkdes);
}
}
// Run configuration
_ = self.run_configure(
&pkg_ident,
None,
&ConfigureOptions {
action: crate::configure::Action::Install,
interactive: options.interactive_configure,
prefix: None,
extra_args: Vec::new(),
},
);
Ok(())
}
}
#[derive(Debug, thiserror::Error)]
pub enum InstallError {
#[error("{}", t!("install.InstallError.DependencyCycle"))]
DependencyCycle,
#[error("{}", t!("install.InstallError.DependencyNotSatisfied", pkgspec = .0))]
DependencyNotSatisfied(PkgSpec),
#[error("{0}")]
Package(PackageError),
#[error("incompatible pkgspec")]
PkgSpec,
#[error("{0}")]
Copy(crate::common::CopyError),
#[error("{}", t!("install.InstallError.AlreadyInstalled"))]
AlreadyInstalled,
#[error("{0}")]
Repo(RepoError),
#[error("{0}")]
Remove(crate::remove::RemoveError),
#[error("{}", t!("install.InstallError.Database", error = .0))]
Database(Box<dyn std::error::Error + Send + Sync>),
}
impl From<PackageError> for InstallError {
fn from(value: PackageError) -> Self {
Self::Package(value)
}
}
impl From<rusqlite::Error> for InstallError {
fn from(value: rusqlite::Error) -> Self {
Self::Database(Box::new(value))
}
}
fn default_install_pkgspec(pkg_ident: PkgIdent) -> PkgSpec {
PkgSpec {
name: pkg_ident.name,
version: crate::version::VersionFilter::Any,
arch: None,
}
}