From d36b3bc1b265601c29d5d80d5db5aac40c7a8888 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Thu, 13 Jul 2023 11:53:48 +0200 Subject: [PATCH] feat: add sha256 compression for archives --- server/Cargo.toml | 1 + server/src/repo/mod.rs | 4 +++- server/src/repo/package.rs | 26 +++++++++++++++++++------- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/server/Cargo.toml b/server/Cargo.toml index f64b642..3af27a3 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -9,6 +9,7 @@ edition = "2021" axum = "0.6.18" futures = "0.3.28" libarchive = { path = "../libarchive" } +sha256 = "1.1.4" tokio = { version = "1.29.1", features = ["full"] } tokio-util = { version = "0.7.8", features = ["io"] } tower = { version = "0.4.13", features = ["make"] } diff --git a/server/src/repo/mod.rs b/server/src/repo/mod.rs index 6b17057..2e00802 100644 --- a/server/src/repo/mod.rs +++ b/server/src/repo/mod.rs @@ -53,7 +53,9 @@ async fn post_package_archive( .await?; } - let pkg = tokio::task::spawn_blocking(move || package::Package::open(&path)).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR).await?; + let pkg = tokio::task::spawn_blocking(move || package::Package::open(&path)) + .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR) + .await?; println!("{:?}", pkg); diff --git a/server/src/repo/package.rs b/server/src/repo/package.rs index 058c9b8..d23aeee 100644 --- a/server/src/repo/package.rs +++ b/server/src/repo/package.rs @@ -1,9 +1,9 @@ use libarchive::read::{Archive, Builder}; use libarchive::{Entry, ReadFilter}; use std::fmt; +use std::fs; use std::io::{self, BufRead, BufReader, Read}; use std::path::{Path, PathBuf}; -use std::fs; const IGNORED_FILES: [&str; 5] = [".BUILDINFO", ".INSTALL", ".MTREE", ".PKGINFO", ".CHANGELOG"]; @@ -15,8 +15,7 @@ pub struct Package { compression: ReadFilter, } -#[derive(Debug)] -#[derive(Default)] +#[derive(Debug, Default)] pub struct PkgInfo { name: String, base: String, @@ -39,6 +38,7 @@ pub struct PkgInfo { optdepends: Vec, makedepends: Vec, checkdepends: Vec, + sha256sum: Option, } #[derive(Debug, PartialEq, Eq)] @@ -134,7 +134,10 @@ impl Package { let mut ar = builder.open_file(path.as_ref())?; - let compression = ar.filter(0).ok_or(io::Error::new(io::ErrorKind::Other, "Unknown compression type."))?; + let compression = ar.filter(0).ok_or(io::Error::new( + io::ErrorKind::Other, + "Unknown compression type.", + ))?; let mut info: Option = None; let mut files: Vec = Vec::new(); @@ -154,14 +157,23 @@ impl Package { if let Some(mut info) = info { info.csize = fs::metadata(path.as_ref())?.len(); - Ok(Package{ + Ok(Package { path: path.as_ref().to_path_buf(), info, compression, - files + files, }) } else { - Err(io::Error::new(io::ErrorKind::Other, "No .PKGINFO file found.")) + Err(io::Error::new( + io::ErrorKind::Other, + "No .PKGINFO file found.", + )) } } + + pub fn calculate_checksum(&mut self) -> io::Result<()> { + self.info.sha256sum = Some(sha256::try_digest(self.path.as_ref())?); + + Ok(()) + } }