From 4ecc1eb1e867e923116e677610ab591ccb424aed Mon Sep 17 00:00:00 2001 From: Raine Date: Sat, 14 Oct 2023 22:39:43 +0200 Subject: [PATCH] feat: start on install section --- pkgfile/src/lib.rs | 4 ++ pkgr/src/{commands/mod.rs => commands.rs} | 3 +- pkgr/src/package/builder/errors.rs | 42 +++++++++++++++++++++ pkgr/src/package/builder/mod.rs | 45 ++++++++++------------- 4 files changed, 68 insertions(+), 26 deletions(-) rename pkgr/src/{commands/mod.rs => commands.rs} (98%) create mode 100644 pkgr/src/package/builder/errors.rs diff --git a/pkgfile/src/lib.rs b/pkgfile/src/lib.rs index 15249e7..f7768b0 100644 --- a/pkgfile/src/lib.rs +++ b/pkgfile/src/lib.rs @@ -8,6 +8,10 @@ impl PKGFile { pub fn new(manifest: String, data: Vec) -> PKGFile { PKGFile { manifest, data } } + + pub fn has_data(&self) -> bool { + !self.data.is_empty() + } } impl Default for PKGFile { diff --git a/pkgr/src/commands/mod.rs b/pkgr/src/commands.rs similarity index 98% rename from pkgr/src/commands/mod.rs rename to pkgr/src/commands.rs index bcbef89..e0f7ec0 100644 --- a/pkgr/src/commands/mod.rs +++ b/pkgr/src/commands.rs @@ -72,7 +72,8 @@ impl Command { () }, Err(e) => { - error!("{}", e.to_string()) + error!("{}", e.to_string()); + exit(1); } } let unix_end = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap(); diff --git a/pkgr/src/package/builder/errors.rs b/pkgr/src/package/builder/errors.rs new file mode 100644 index 0000000..e5f62d6 --- /dev/null +++ b/pkgr/src/package/builder/errors.rs @@ -0,0 +1,42 @@ +use std::fmt::Display; + +#[derive(Debug)] +pub enum BinError { + UnpackError(String), +} + +impl Display for BinError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + BinError::UnpackError(e) => write!(f, "Unpack error: {}", e), + } + } +} + +#[derive(Debug)] +pub enum BuildError {} + +impl Display for BuildError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + _ => write!(f, "Build error"), + } + } +} + +#[derive(Debug)] +pub enum InstallError { + BuildError(BuildError), + BinError(BinError), + InstallError +} + +impl ToString for InstallError { + fn to_string(&self) -> String { + match self { + InstallError::BuildError(e) => format!("Build error: {}", e), + InstallError::BinError(e) => format!("Bin error: {}", e), + InstallError::InstallError => "Install error".to_string(), + } + } +} diff --git a/pkgr/src/package/builder/mod.rs b/pkgr/src/package/builder/mod.rs index 50166e1..b841a78 100644 --- a/pkgr/src/package/builder/mod.rs +++ b/pkgr/src/package/builder/mod.rs @@ -1,32 +1,18 @@ -use libc::fork; +use std::fmt::Display; use log::{debug, trace}; +use errors::{BinError, BuildError, InstallError}; use manifest::Manifest; use pkgfile::PKGFile; use crate::tmp::TempDir; +mod errors; + #[derive(Debug)] pub enum InstallType { Build, Bin } -#[derive(Debug)] -pub enum InstallError { - BuildError(String), - BinError(String), - InstallError -} - -impl ToString for InstallError { - fn to_string(&self) -> String { - match self { - InstallError::BuildError(e) => format!("Build error: {}", e), - InstallError::BinError(e) => format!("Bin error: {}", e), - InstallError::InstallError => "Install error".to_string(), - } - } -} - #[derive(Debug)] pub struct PackageInstaller { manifest: Manifest, @@ -43,22 +29,31 @@ impl PackageInstaller { } } - fn extract_to>(&self, path: S) -> Result<(), String> { + fn extract_to(&self, path: String) -> Result<(), BinError> { + if !self.pkgfile.has_data() { + return Err(BinError::UnpackError("package has no data".to_string())); + } + if std::path::Path::new(&path).exists() { + return Err(BinError::UnpackError("path already exists".to_string())); + } tar::Archive::new(self.pkgfile.data.as_slice()) - .unpack(path.into()) - .map_err(|e| e.to_string()) + .unpack(&path) + .map_err(|e| BinError::UnpackError(e.to_string())) } - fn bin(&self) -> Result<(), String> { + fn bin(&self) -> Result<(), BinError> { let mut tmpdir = TempDir::default(); tmpdir.push(&self.manifest.package.name); trace!("extracting package into: {}", tmpdir.to_string()); - self.extract_to(tmpdir.to_string())?; + match self.extract_to(tmpdir.to_string()) { + Ok(_) => {}, + Err(e) => return Err(e) + } debug!("extracted package in: {}", tmpdir.to_string()); Ok(()) } - fn build(&self) -> Result<(), String> { + fn build(&self) -> Result<(), BuildError> { Ok(()) } @@ -68,4 +63,4 @@ impl PackageInstaller { InstallType::Build => self.build().map_err(|e| InstallError::BuildError(e)) } } -} \ No newline at end of file +}