feat: start on install section

This commit is contained in:
Strix 2023-10-14 22:39:43 +02:00
parent 092c616ca4
commit 4ecc1eb1e8
No known key found for this signature in database
GPG key ID: 49B2E37B8915B774
4 changed files with 68 additions and 26 deletions

View file

@ -8,6 +8,10 @@ impl PKGFile {
pub fn new(manifest: String, data: Vec<u8>) -> PKGFile { pub fn new(manifest: String, data: Vec<u8>) -> PKGFile {
PKGFile { manifest, data } PKGFile { manifest, data }
} }
pub fn has_data(&self) -> bool {
!self.data.is_empty()
}
} }
impl Default for PKGFile { impl Default for PKGFile {

View file

@ -72,7 +72,8 @@ impl Command {
() ()
}, },
Err(e) => { 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(); let unix_end = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap();

View file

@ -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(),
}
}
}

View file

@ -1,32 +1,18 @@
use libc::fork; use std::fmt::Display;
use log::{debug, trace}; use log::{debug, trace};
use errors::{BinError, BuildError, InstallError};
use manifest::Manifest; use manifest::Manifest;
use pkgfile::PKGFile; use pkgfile::PKGFile;
use crate::tmp::TempDir; use crate::tmp::TempDir;
mod errors;
#[derive(Debug)] #[derive(Debug)]
pub enum InstallType { pub enum InstallType {
Build, Build,
Bin 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)] #[derive(Debug)]
pub struct PackageInstaller { pub struct PackageInstaller {
manifest: Manifest, manifest: Manifest,
@ -43,22 +29,31 @@ impl PackageInstaller {
} }
} }
fn extract_to<S: Into<String>>(&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()) tar::Archive::new(self.pkgfile.data.as_slice())
.unpack(path.into()) .unpack(&path)
.map_err(|e| e.to_string()) .map_err(|e| BinError::UnpackError(e.to_string()))
} }
fn bin(&self) -> Result<(), String> { fn bin(&self) -> Result<(), BinError> {
let mut tmpdir = TempDir::default(); let mut tmpdir = TempDir::default();
tmpdir.push(&self.manifest.package.name); tmpdir.push(&self.manifest.package.name);
trace!("extracting package into: {}", tmpdir.to_string()); 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()); debug!("extracted package in: {}", tmpdir.to_string());
Ok(()) Ok(())
} }
fn build(&self) -> Result<(), String> { fn build(&self) -> Result<(), BuildError> {
Ok(()) Ok(())
} }