From bd5090069dc4b4a4c9c8418936c456c6948a08b0 Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Wed, 19 Apr 2023 00:43:36 -0400 Subject: [PATCH] Add `Cleanup` struct to run all hooks --- src/cleanup/mod.rs | 34 ++++++++++++++++++++++++++++++++++ src/installer/mod.rs | 12 ++++++++++-- src/lib.rs | 2 ++ 3 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 src/cleanup/mod.rs diff --git a/src/cleanup/mod.rs b/src/cleanup/mod.rs new file mode 100644 index 0000000..1d79b01 --- /dev/null +++ b/src/cleanup/mod.rs @@ -0,0 +1,34 @@ +use { + super::Hooks, + crate::{InstallError, InstallMessage}, + rayon::prelude::{IntoParallelRefIterator, ParallelIterator}, + std::{error::Error, sync::mpsc::Sender}, +}; + +#[derive(Debug)] +pub struct Cleanup { + hooks: Vec, +} + +impl Cleanup { + pub fn run(&self, sender: Sender) -> Result<(), Box> { + self.hooks + .par_iter() + .try_for_each_with(sender, |sender, hook| { + let output = hook.run()?; + match hook { + Hooks::Man => sender.send(InstallMessage::Man)?, + Hooks::Info(s) => sender.send(InstallMessage::Info(s.clone()))?, + Hooks::GlibSchema => sender.send(InstallMessage::GlibSchemas)?, + Hooks::User(u, _) => sender.send(InstallMessage::UserCreated(u.clone()))?, + Hooks::Group(g, _) => sender.send(InstallMessage::GroupCreated(g.clone()))?, + Hooks::Pinstall(_) => { + sender.send(InstallMessage::PostInstallStdout(output.stdout))?; + sender.send(InstallMessage::PostInstallStderr(output.stderr))?; + } + } + Ok::<(), InstallError>(()) + })?; + Ok(()) + } +} diff --git a/src/installer/mod.rs b/src/installer/mod.rs index dae323e..e07b91d 100644 --- a/src/installer/mod.rs +++ b/src/installer/mod.rs @@ -23,12 +23,14 @@ use { zstd::Decoder, }; +#[derive(Debug)] /// Represents a symbolic link pub struct Link { pub path: PathBuf, pub target: PathBuf, } +#[derive(Debug)] /// Messages sent from and `Installer` to the calling thread pub enum InstallMessage { ArchiveRead, @@ -46,9 +48,15 @@ pub enum InstallMessage { /// A `Group` has been successfully created GroupCreated(Group), /// The output of the post install script sent to stdout - PostInstallStdout(String), + PostInstallStdout(Vec), /// The output of the post install script sent to stderr - PostInstallStderr(String), + PostInstallStderr(Vec), + /// Update the mandoc db + Man, + /// Update the info db + Info(String), + /// Glib schemas updated + GlibSchemas, } /// Installs a package into it's specified rootfs diff --git a/src/lib.rs b/src/lib.rs index 812961c..9d8f0c7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,6 @@ #![warn(clippy::all, clippy::pedantic)] #![allow(clippy::must_use_candidate, clippy::missing_errors_doc)] +mod cleanup; mod creator; mod db; mod hooks; @@ -14,6 +15,7 @@ mod version; use std::path::PathBuf; pub use { + cleanup::Cleanup, creator::{CreationError, Creator, Message}, db::Database, hooks::{Hooks, Pinstall},