feat: working package archive parsing
parent
70b6b326f1
commit
f5de535313
|
@ -53,27 +53,9 @@ async fn post_package_archive(
|
|||
.await?;
|
||||
}
|
||||
|
||||
let mut builder = Builder::new();
|
||||
builder
|
||||
.support_format(libarchive::archive::ReadFormat::Tar)
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
builder
|
||||
.support_filter(libarchive::archive::ReadFilter::All)
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
let mut ar = builder
|
||||
.open_file(&path)
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
let pkg = tokio::task::spawn_blocking(move || package::Package::open(&path)).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR).await?;
|
||||
|
||||
for entry in ar.entries() {
|
||||
let mut entry = entry.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
|
||||
|
||||
if entry.pathname() == ".PKGINFO" {
|
||||
let mut buffer = String::new();
|
||||
entry.read_to_string(&mut buffer);
|
||||
|
||||
println!("{}", buffer);
|
||||
}
|
||||
}
|
||||
println!("{:?}", pkg);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -3,9 +3,11 @@ use libarchive::{Entry, ReadFilter};
|
|||
use std::fmt;
|
||||
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"];
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Package {
|
||||
path: PathBuf,
|
||||
info: PkgInfo,
|
||||
|
@ -13,14 +15,15 @@ pub struct Package {
|
|||
compression: ReadFilter,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Default)]
|
||||
pub struct PkgInfo {
|
||||
name: String,
|
||||
base: String,
|
||||
version: String,
|
||||
description: String,
|
||||
size: i64,
|
||||
csize: i64,
|
||||
size: u64,
|
||||
csize: u64,
|
||||
url: String,
|
||||
arch: String,
|
||||
build_date: i64,
|
||||
|
@ -62,7 +65,7 @@ impl PkgInfo {
|
|||
let line = line.as_ref();
|
||||
|
||||
if !line.starts_with('#') {
|
||||
if let Some((key, value)) = line.split_once(',').map(|(k, v)| (k.trim(), v.trim())) {
|
||||
if let Some((key, value)) = line.split_once('=').map(|(k, v)| (k.trim(), v.trim())) {
|
||||
match key {
|
||||
"pkgname" => self.name = value.to_string(),
|
||||
"pkgbase" => self.base = value.to_string(),
|
||||
|
@ -83,7 +86,7 @@ impl PkgInfo {
|
|||
"pgpsigsize" => {
|
||||
self.pgpsigsize = value
|
||||
.parse()
|
||||
.map_err(|_| ParsePkgInfoError::InvalidBuildDate)?
|
||||
.map_err(|_| ParsePkgInfoError::InvalidPgpSigSize)?
|
||||
}
|
||||
"group" => self.groups.push(value.to_string()),
|
||||
"license" => self.licenses.push(value.to_string()),
|
||||
|
@ -120,11 +123,19 @@ impl Package {
|
|||
pub fn open<P: AsRef<Path>>(path: P) -> io::Result<Self> {
|
||||
let mut builder = Builder::new();
|
||||
builder.support_format(libarchive::ReadFormat::Tar)?;
|
||||
builder.support_filter(libarchive::ReadFilter::All)?;
|
||||
|
||||
let mut ar = builder.open_file(path)?;
|
||||
// There are chosen kind of arbitrarily, most likely only zstd, gzip and xz will ever be
|
||||
// used
|
||||
builder.support_filter(libarchive::ReadFilter::Zstd)?;
|
||||
builder.support_filter(libarchive::ReadFilter::Gzip)?;
|
||||
builder.support_filter(libarchive::ReadFilter::Xz)?;
|
||||
builder.support_filter(libarchive::ReadFilter::Bzip2)?;
|
||||
builder.support_filter(libarchive::ReadFilter::Lzma)?;
|
||||
|
||||
let mut info: PkgInfo;
|
||||
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 mut info: Option<PkgInfo> = None;
|
||||
let mut files: Vec<PathBuf> = Vec::new();
|
||||
|
||||
for entry in ar.entries() {
|
||||
|
@ -136,12 +147,21 @@ impl Package {
|
|||
}
|
||||
|
||||
if path_name == ".PKGINFO" {
|
||||
let size = entry.size();
|
||||
info = PkgInfo::parse(entry)?;
|
||||
info.size = size;
|
||||
info = Some(PkgInfo::parse(entry)?);
|
||||
}
|
||||
}
|
||||
|
||||
todo!()
|
||||
if let Some(mut info) = info {
|
||||
info.csize = fs::metadata(path.as_ref())?.len();
|
||||
|
||||
Ok(Package{
|
||||
path: path.as_ref().to_path_buf(),
|
||||
info,
|
||||
compression,
|
||||
files
|
||||
})
|
||||
} else {
|
||||
Err(io::Error::new(io::ErrorKind::Other, "No .PKGINFO file found."))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue