feat: further work on new repo & package implementation

This commit is contained in:
Jef Roosens 2024-05-25 13:31:46 +02:00
parent 2e0c6d1fa6
commit c95feadca1
Signed by: Jef Roosens
GPG key ID: B75D4F293C7052DB
5 changed files with 113 additions and 2 deletions

View file

@ -2,10 +2,12 @@ use super::WriteEntry;
use crate::error::ArchiveError;
use crate::Entry;
use crate::Handle;
use core::ffi::c_void;
use libarchive3_sys::ffi;
use std::fs;
use std::io;
use std::io::Read;
use std::io::Write;
use std::path::Path;
pub struct FileWriter {
@ -33,6 +35,17 @@ impl FileWriter {
}
}
/// Append the given entry to the archive. After successfully calling this function, writing to
/// the archive now writes to this entry.
pub fn append_entry(&mut self, entry: &mut WriteEntry) -> crate::Result<()> {
unsafe {
match ffi::archive_write_header(self.handle_mut(), entry.entry_mut()) {
ffi::ARCHIVE_OK => Ok(()),
_ => Err(ArchiveError::from(self as &dyn Handle).into()),
}
}
}
pub fn append_data<R: Read>(&mut self, entry: &mut WriteEntry, r: &mut R) -> crate::Result<()> {
unsafe {
match ffi::archive_write_header(self.handle_mut(), entry.entry_mut()) {
@ -109,3 +122,23 @@ impl Drop for FileWriter {
}
}
}
impl Write for FileWriter {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
let res = unsafe {
ffi::archive_write_data(self.handle_mut(), buf.as_ptr() as *const c_void, buf.len())
} as isize;
if res < 0 {
Err(ArchiveError::from(self as &dyn Handle).into())
} else {
// Unwrap is safe as we check if the value is negative in the if statement
Ok(res.try_into().unwrap())
}
}
fn flush(&mut self) -> io::Result<()> {
// Libarchive doesn't seem to provide a flush mechanism
Ok(())
}
}

View file

@ -3,6 +3,7 @@ mod file;
use crate::Entry;
pub use builder::Builder;
pub use file::FileWriter;
use libarchive3_sys::ffi;
pub struct WriteEntry {