From 6eb95aabbff23fe46d38c7a03ecd46107a0dcf3d Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Fri, 14 Jul 2023 12:31:06 +0200 Subject: [PATCH] refactor(libarchive): tweak some error handling --- libarchive/src/read/entries.rs | 62 ++++++++-------------------------- 1 file changed, 15 insertions(+), 47 deletions(-) diff --git a/libarchive/src/read/entries.rs b/libarchive/src/read/entries.rs index 8a28043..f860ae3 100644 --- a/libarchive/src/read/entries.rs +++ b/libarchive/src/read/entries.rs @@ -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>> { -// 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>; @@ -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()) } } }