feat(repo): write db archive parser

feat/mirror-api
Jef Roosens 2024-07-21 21:09:05 +02:00
parent 986162e926
commit d38fd5ca74
Signed by: Jef Roosens
GPG Key ID: 02D4C0997E74717B
5 changed files with 84 additions and 7 deletions

View File

@ -5,6 +5,6 @@ pub mod write;
pub use archive::{
Entry, ExtractOption, ExtractOptions, Handle, ReadCompression, ReadFilter, ReadFormat,
WriteFilter, WriteFormat,
WriteFilter, WriteFormat, FileType,
};
pub use error::Result;

View File

@ -6,9 +6,10 @@ pub use builder::Builder;
use crate::archive::Handle;
use crate::ReadFilter;
use entries::Entries;
pub use entries::{Entries, ReadEntry};
use libarchive3_sys::ffi;
use std::path::Path;
pub use file::FileReader;
// Represents a read view of an archive
pub trait Archive: Handle + Sized {

View File

@ -0,0 +1,3 @@
mod parser;
pub use parser::{DbArchiveParser, DbArchiveEntry};

View File

@ -0,0 +1,75 @@
use std::{
io::{self, BufRead},
path::Path,
};
use libarchive::{
read::{Archive, Builder, Entries, FileReader, ReadEntry},
Entry,
};
pub struct DbArchiveParser<'a, T: 'a + Archive> {
entries: Entries<'a, T>,
}
pub struct DbArchiveEntry {
name: String,
version: String,
filename: String,
}
impl<'a, T: Archive> DbArchiveParser<'a, T> {
pub fn new(ar: &'a mut T) -> Self {
Self {
entries: Entries::new(ar),
}
}
// parse a given entry. If the entry's not a regular file, the function returns None.
fn parse_entry(entry: ReadEntry<'a, T>) -> io::Result<DbArchiveEntry> {
let reader = io::BufReader::new(entry);
let mut lines = reader.lines();
let mut name: Option<String> = None;
let mut version: Option<String> = None;
let mut filename: Option<String> = None;
while let Some(line) = lines.next().transpose()? {
match line.as_str() {
"%NAME%" => name = lines.next().transpose()?,
"%VERSION%" => version = lines.next().transpose()?,
"%FILENAME%" => filename = lines.next().transpose()?,
_ => {}
}
}
if name.is_some() && version.is_some() && filename.is_some() {
Ok(DbArchiveEntry {
name: name.unwrap(),
version: version.unwrap(),
filename: filename.unwrap(),
})
} else {
Err(io::Error::other("Missing fields in entry file"))
}
}
}
impl<'a, T: Archive> Iterator for DbArchiveParser<'a, T> {
type Item = io::Result<DbArchiveEntry>;
fn next(&mut self) -> Option<Self::Item> {
while let Some(entry) = self.entries.next() {
match entry {
Ok(entry) => {
if entry.filetype() == libarchive::FileType::RegularFile {
return Some(Self::parse_entry(entry));
}
}
Err(err) => return Some(Err(err.into())),
}
}
None
}
}

View File

@ -1,6 +1,7 @@
mod actor;
mod archive;
mod handle;
mod mirror;
pub mod package;
pub use actor::Actor;
@ -38,10 +39,7 @@ pub struct SharedState {
}
impl SharedState {
pub fn new(
repos_dir: impl AsRef<Path>,
conn: DbConn,
) -> Self {
pub fn new(repos_dir: impl AsRef<Path>, conn: DbConn) -> Self {
let (tx, rx) = unbounded_channel();
Self {