refactor(libarchive): tweak some error handling

main
Jef Roosens 2023-07-14 12:31:06 +02:00
parent 1149061618
commit 6eb95aabbf
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
1 changed files with 15 additions and 47 deletions

View File

@ -1,4 +1,5 @@
use crate::archive::Entry;
use crate::archive::Handle;
use crate::error::ArchiveError;
use crate::read::Archive;
use libarchive3_sys::ffi;
@ -13,16 +14,6 @@ pub struct ReadEntry<'a, T: Archive> {
_ignored: marker::PhantomData<&'a T>,
}
//impl<'b, T: Archive + 'b> ReadEntry<'b, T> {
// /// Create a new ReaderEntry from a raw pointer to an `archive_entry` struct
// ///
// /// # Safety
// /// The pointer must own the struct it points to, otherwise it may be freed twice
// pub unsafe fn new(archive: &'b mut T, handle: *mut ffi::Struct_archive_entry) -> Self {
// Self { archive, handle }
// }
//}
impl<'a, T: Archive> Entry for ReadEntry<'a, T> {
unsafe fn entry(&self) -> *const ffi::Struct_archive_entry {
self.entry as *const _
@ -33,18 +24,19 @@ impl<'a, T: Archive> Entry for ReadEntry<'a, T> {
}
}
pub struct Entries<'a, T: 'a + Archive> {
archive: &'a mut T, // entry: &'b mut ReadEntry<'b, T>
// entry_handle: *mut ffi::Struct_archive_entry, // entry: ReadEntry<'a, T>
impl<'a, T: Archive> Handle for ReadEntry<'a, T> {
unsafe fn handle(&self) -> *const ffi::Struct_archive {
self.archive as *const _
}
unsafe fn handle_mut(&mut self) -> *mut ffi::Struct_archive {
self.archive
}
}
// impl Default for ReadEntry {
// fn default() -> Self {
// Self {
// handle: std::ptr::null_mut(),
// }
// }
// }
pub struct Entries<'a, T: 'a + Archive> {
archive: &'a mut T,
}
impl<'a, T: 'a + Archive> Entries<'a, T> {
pub fn new(archive: &'a mut T) -> Self {
@ -52,27 +44,6 @@ impl<'a, T: 'a + Archive> Entries<'a, T> {
}
}
// impl<'a, T: 'a + Archive> EntryFields<'a, T> {
// fn read_next_header(&'a mut self) -> crate::Result<Option<ReadEntry<'a, T>>> {
// let mut entry_ptr: *mut ffi::Struct_archive_entry = std::ptr::null_mut();
// let res = unsafe {
// ffi::archive_read_next_header(self.archive.handle() as *mut _, &mut entry_ptr)
// };
// match res {
// ffi::ARCHIVE_OK | ffi::ARCHIVE_WARN => Ok(Some(unsafe {
// EntryFields::new(self.archive, entry_ptr).into_entry()
// })),
// ffi::ARCHIVE_EOF => Ok(None),
// _ => Err(ArchiveError::Sys(
// self.archive.err_code(),
// self.archive.err_msg().to_owned(),
// )),
// }
// }
// }
impl<'a, T: Archive + 'a> Iterator for Entries<'a, T> {
type Item = crate::Result<ReadEntry<'a, T>>;
@ -84,7 +55,7 @@ impl<'a, T: Archive + 'a> Iterator for Entries<'a, T> {
};
match res {
ffi::ARCHIVE_OK | ffi::ARCHIVE_WARN => {
ffi::ARCHIVE_OK => {
let entry = ReadEntry {
archive: unsafe { self.archive.handle_mut() },
entry: entry_ptr,
@ -95,10 +66,7 @@ impl<'a, T: Archive + 'a> Iterator for Entries<'a, T> {
Some(Ok(entry))
}
ffi::ARCHIVE_EOF => None,
_ => Some(Err(ArchiveError::Sys(
self.archive.err_code(),
self.archive.err_msg().to_owned(),
))),
_ => Some(Err(ArchiveError::from(self.archive as &dyn Handle))),
}
}
}
@ -116,7 +84,7 @@ impl<'a, T: Archive> Read for ReadEntry<'a, T> {
Ok(count)
} else {
Err(io::Error::new(io::ErrorKind::Other, "error"))
Err(ArchiveError::from(self as &dyn Handle).into())
}
}
}