Add method to create an Installer struct from an archive path
This commit is contained in:
parent
c36b3b14ab
commit
7fe884cd3e
@ -13,7 +13,7 @@ use {
|
||||
ffi::OsStr,
|
||||
fmt::Write as _,
|
||||
fs::{self, DirBuilder, File},
|
||||
io::{self, BufWriter, Write},
|
||||
io::{self, BufReader, BufWriter, Write},
|
||||
os::{
|
||||
self,
|
||||
unix::{fs::DirBuilderExt, prelude::OpenOptionsExt},
|
||||
@ -56,17 +56,31 @@ pub enum InstallMessage {
|
||||
pub struct Installer<T: io::Read> {
|
||||
/// The filesystem root under which to install this package.
|
||||
/// Normally this is "/".
|
||||
path: PathBuf,
|
||||
root: PathBuf,
|
||||
package: Package,
|
||||
reader: T,
|
||||
}
|
||||
|
||||
impl Installer<Decoder<'_, BufReader<File>>> {
|
||||
/// creates a new installer from an archive path
|
||||
pub fn new_for_file<P: AsRef<OsStr>>(
|
||||
root: P,
|
||||
package: Package,
|
||||
archive: P,
|
||||
) -> Result<Self, io::Error> {
|
||||
let root = Path::new(&root).to_path_buf();
|
||||
let fd = File::open(Path::new(&archive))?;
|
||||
let decoder = Decoder::new(fd)?;
|
||||
Ok(Self::new(root, package, decoder))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: io::Read> Installer<T> {
|
||||
/// Creates a new installer from an open archive.
|
||||
pub fn new<P: AsRef<OsStr>>(path: P, package: Package, reader: T) -> Self {
|
||||
let path = Path::new(&path).to_path_buf();
|
||||
/// Creates a new installer from an open archive stream
|
||||
pub fn new<P: AsRef<OsStr>>(root: P, package: Package, reader: T) -> Self {
|
||||
let root = Path::new(&root).to_path_buf();
|
||||
Self {
|
||||
path,
|
||||
root,
|
||||
package,
|
||||
reader,
|
||||
}
|
||||
@ -83,13 +97,13 @@ impl<T: io::Read> Installer<T> {
|
||||
};
|
||||
let len = archive.nodes.len();
|
||||
sender.send(InstallMessage::ArchiveLen(len))?;
|
||||
let mut db_pkgdir = crate::get_dbdir(Some(self.path.clone()));
|
||||
let mut db_pkgdir = crate::get_dbdir(Some(self.root.clone()));
|
||||
db_pkgdir.push(&self.package.name);
|
||||
if !db_pkgdir.exists() {
|
||||
fs::create_dir_all(&db_pkgdir)?;
|
||||
}
|
||||
pop_appstream(&mut archive, &db_pkgdir)?;
|
||||
pop_pinstall(&mut archive, &mut hooks, &self.package.name, &self.path)?;
|
||||
pop_pinstall(&mut archive, &mut hooks, &self.package.name, &self.root)?;
|
||||
let mut db_file = db_pkgdir;
|
||||
db_file.push("package.ron");
|
||||
if db_file.exists() {
|
||||
@ -98,12 +112,15 @@ impl<T: io::Read> Installer<T> {
|
||||
let fd = File::create(&db_file)?;
|
||||
let writer = BufWriter::new(fd);
|
||||
pr_node.write(writer)?;
|
||||
let path = &self.path;
|
||||
let root = &self.root;
|
||||
let package = &self.package;
|
||||
let hooks = Mutex::new(hooks);
|
||||
let s = sender.clone();
|
||||
archive.nodes.par_iter().try_for_each_with(s, |sender, node| {
|
||||
let mut path = path.clone();
|
||||
archive
|
||||
.nodes
|
||||
.par_iter()
|
||||
.try_for_each_with(s, |sender, node| {
|
||||
let mut path = root.clone();
|
||||
let fpath = node.header.file_path()?;
|
||||
if let Some(s) = node.header.prefix() {
|
||||
if s.contains("/share/man/") {
|
||||
@ -164,12 +181,12 @@ impl<T: io::Read> Installer<T> {
|
||||
if let Some(ref users) = self.package.users {
|
||||
users
|
||||
.iter()
|
||||
.for_each(|u| hooks.push((u.clone(), Some(self.path.clone())).into()));
|
||||
.for_each(|u| hooks.push((u.clone(), Some(self.root.clone())).into()));
|
||||
}
|
||||
if let Some(ref groups) = self.package.groups {
|
||||
groups
|
||||
.iter()
|
||||
.for_each(|g| hooks.push((g.clone(), Some(self.path.clone())).into()));
|
||||
.for_each(|g| hooks.push((g.clone(), Some(self.root.clone())).into()));
|
||||
}
|
||||
hooks
|
||||
}
|
||||
@ -252,6 +269,7 @@ fn pop_pinstall(
|
||||
}
|
||||
|
||||
mod error {
|
||||
use super::InstallMessage;
|
||||
use crate::Hooks;
|
||||
use hpk_package::tar;
|
||||
use std::{
|
||||
@ -263,7 +281,6 @@ mod error {
|
||||
PoisonError,
|
||||
},
|
||||
};
|
||||
use super::InstallMessage;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum InstallError {
|
||||
|
Loading…
Reference in New Issue
Block a user