diff --git a/Cargo.toml b/Cargo.toml index 2d1462e..f9cfcba 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,7 @@ [package] name = "libarchive" version = "0.1.1" +edition = "2021" authors = ["Jamie Winsor "] license = "Apache-2.0" repository = "https://github.com/chef/libarchive-rust" @@ -8,5 +9,5 @@ description = "A safe Rust API for authoring and extracting archives with libarc keywords = ["libarchive", "archive", "tar", "zip"] [dependencies] -libc = ">= 0.2.0" -libarchive3-sys = "0.1" +libc = "0.2.147" +libarchive3-sys = { git = "https://github.com/jerry73204/libarchive3-sys.git", branch = "rewrite-using-bindgen" } diff --git a/src/archive.rs b/src/archive.rs index 2a79870..838f14a 100644 --- a/src/archive.rs +++ b/src/archive.rs @@ -3,8 +3,8 @@ use std::ffi::{CStr, CString}; use std::path::PathBuf; use std::str; -use libarchive3_sys::ffi; -use error::ErrCode; +use crate::error::ErrCode; +use libarchive3_sys as ffi; pub enum ReadCompression { All, @@ -104,7 +104,7 @@ pub enum FileType { } pub trait Handle { - unsafe fn handle(&self) -> *mut ffi::Struct_archive; + unsafe fn handle(&self) -> *mut ffi::archive; fn err_code(&self) -> ErrCode { let code = unsafe { ffi::archive_errno(self.handle()) }; @@ -121,7 +121,7 @@ pub trait Handle { } pub trait Entry { - unsafe fn entry(&self) -> *mut ffi::Struct_archive_entry; + unsafe fn entry(&self) -> *mut ffi::archive_entry; fn filetype(&self) -> FileType { unsafe { diff --git a/src/error.rs b/src/error.rs index 7d22eff..5a2b283 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,6 +1,6 @@ +use crate::archive; use std::error; use std::fmt; -use archive; pub type ArchiveResult = Result; @@ -42,14 +42,14 @@ impl fmt::Display for ArchiveError { } } -impl<'a> From<&'a archive::Handle> for ArchiveError { - fn from(handle: &'a archive::Handle) -> ArchiveError { +impl<'a> From<&'a dyn archive::Handle> for ArchiveError { + fn from(handle: &'a dyn archive::Handle) -> ArchiveError { ArchiveError::Sys(handle.err_code(), handle.err_msg()) } } -impl<'a> From<&'a archive::Handle> for ArchiveResult<()> { - fn from(handle: &'a archive::Handle) -> ArchiveResult<()> { +impl<'a> From<&'a dyn archive::Handle> for ArchiveResult<()> { + fn from(handle: &'a dyn archive::Handle) -> ArchiveResult<()> { match handle.err_code() { ErrCode(0) => Ok(()), _ => Err(ArchiveError::from(handle)), diff --git a/src/lib.rs b/src/lib.rs index c24af2f..a61d852 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,3 @@ -extern crate libc; -extern crate libarchive3_sys; - pub mod archive; pub mod error; pub mod reader; diff --git a/src/reader.rs b/src/reader.rs index c913b1c..ccd1126 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -1,6 +1,5 @@ use std::any::Any; use std::default::Default; -use std::error::Error; use std::ffi::CString; use std::io::{self, Read}; use std::mem; @@ -8,31 +7,32 @@ use std::path::Path; use std::ptr; use std::slice; +use libarchive3_sys as ffi; use libc::{c_void, ssize_t}; -use libarchive3_sys::ffi; -use archive::{Entry, ReadCompression, ReadFilter, ReadFormat, Handle}; -use error::{ArchiveResult, ArchiveError}; +use crate::archive::{Entry, Handle, ReadCompression, ReadFilter, ReadFormat}; +use crate::error::{ArchiveError, ArchiveResult}; const BLOCK_SIZE: usize = 10240; -unsafe extern "C" fn stream_read_callback(handle: *mut ffi::Struct_archive, - data: *mut c_void, - buff: *mut *const c_void) - -> ssize_t { +unsafe extern "C" fn stream_read_callback( + handle: *mut ffi::archive, + data: *mut c_void, + buff: *mut *const c_void, +) -> ssize_t { let pipe: &mut Pipe = &mut *(data as *mut Pipe); *buff = pipe.buffer.as_mut_ptr() as *mut c_void; match pipe.read_bytes() { Ok(size) => size as ssize_t, Err(e) => { - let desc = CString::new(e.description()).unwrap(); + let desc = CString::new(e.to_string()).unwrap(); ffi::archive_set_error(handle, e.raw_os_error().unwrap_or(0), desc.as_ptr()); -1 as ssize_t } } } -pub trait Reader : Handle { +pub trait Reader: Handle { fn entry(&mut self) -> &mut ReaderEntry; fn header_position(&self) -> i64 { @@ -64,27 +64,27 @@ pub trait Reader : Handle { } pub struct FileReader { - handle: *mut ffi::Struct_archive, + handle: *mut ffi::archive, entry: ReaderEntry, } pub struct StreamReader { - handle: *mut ffi::Struct_archive, + handle: *mut ffi::archive, entry: ReaderEntry, _pipe: Box, } pub struct Builder { - handle: *mut ffi::Struct_archive, + handle: *mut ffi::archive, consumed: bool, } pub struct ReaderEntry { - handle: *mut ffi::Struct_archive_entry, + handle: *mut ffi::archive_entry, } struct Pipe { - reader: Box, + reader: Box, buffer: Vec, } @@ -103,7 +103,7 @@ impl Pipe { impl FileReader { pub fn open>(mut builder: Builder, file: T) -> ArchiveResult { - try!(builder.check_consumed()); + builder.check_consumed()?; let c_file = CString::new(file.as_ref().to_string_lossy().as_bytes()).unwrap(); unsafe { match ffi::archive_read_open_filename(builder.handle(), c_file.as_ptr(), BLOCK_SIZE) { @@ -111,12 +111,12 @@ impl FileReader { builder.consume(); Ok(Self::new(builder.handle())) } - _ => Err(ArchiveError::from(&builder as &Handle)), + _ => Err(ArchiveError::from(&builder as &dyn Handle)), } } } - fn new(handle: *mut ffi::Struct_archive) -> Self { + fn new(handle: *mut ffi::archive) -> Self { FileReader { handle: handle, entry: ReaderEntry::default(), @@ -125,7 +125,7 @@ impl FileReader { } impl Handle for FileReader { - unsafe fn handle(&self) -> *mut ffi::Struct_archive { + unsafe fn handle(&self) -> *mut ffi::archive { self.handle } } @@ -149,11 +149,13 @@ impl StreamReader { unsafe { let mut pipe = Box::new(Pipe::new(src)); let pipe_ptr: *mut c_void = &mut *pipe as *mut Pipe as *mut c_void; - match ffi::archive_read_open(builder.handle(), - pipe_ptr, - None, - Some(stream_read_callback), - None) { + match ffi::archive_read_open( + builder.handle(), + pipe_ptr, + None, + Some(stream_read_callback), + None, + ) { ffi::ARCHIVE_OK => { let reader = StreamReader { handle: builder.handle(), @@ -165,7 +167,7 @@ impl StreamReader { } _ => { builder.consume(); - Err(ArchiveError::from(&builder as &Handle)) + Err(ArchiveError::from(&builder as &dyn Handle)) } } } @@ -173,7 +175,7 @@ impl StreamReader { } impl Handle for StreamReader { - unsafe fn handle(&self) -> *mut ffi::Struct_archive { + unsafe fn handle(&self) -> *mut ffi::archive { self.handle } } @@ -234,7 +236,7 @@ impl Builder { }; match result { ffi::ARCHIVE_OK => Ok(()), - _ => ArchiveResult::from(self as &Handle), + _ => ArchiveResult::from(self as &dyn Handle), } } @@ -259,10 +261,12 @@ impl Builder { ReadFilter::ProgramSignature(prog, cb, size) => { let c_prog = CString::new(prog).unwrap(); unsafe { - ffi::archive_read_support_filter_program_signature(self.handle, - c_prog.as_ptr(), - mem::transmute(cb), - size) + ffi::archive_read_support_filter_program_signature( + self.handle, + c_prog.as_ptr(), + mem::transmute(cb), + size, + ) } } ReadFilter::Rpm => unsafe { ffi::archive_read_support_filter_rpm(self.handle) }, @@ -271,7 +275,7 @@ impl Builder { }; match result { ffi::ARCHIVE_OK => Ok(()), - _ => ArchiveResult::from(self as &Handle), + _ => ArchiveResult::from(self as &dyn Handle), } } @@ -297,17 +301,17 @@ impl Builder { }; match result { ffi::ARCHIVE_OK => Ok(()), - _ => ArchiveResult::from(self as &Handle), + _ => ArchiveResult::from(self as &dyn Handle), } } pub fn open_file>(self, file: T) -> ArchiveResult { - try!(self.check_consumed()); + self.check_consumed()?; FileReader::open(self, file) } pub fn open_stream(self, src: T) -> ArchiveResult { - try!(self.check_consumed()); + self.check_consumed()?; StreamReader::open(self, src) } @@ -325,7 +329,7 @@ impl Builder { } impl Handle for Builder { - unsafe fn handle(&self) -> *mut ffi::Struct_archive { + unsafe fn handle(&self) -> *mut ffi::archive { self.handle } } @@ -356,19 +360,21 @@ impl Default for Builder { } impl ReaderEntry { - pub fn new(handle: *mut ffi::Struct_archive_entry) -> Self { + pub fn new(handle: *mut ffi::archive_entry) -> Self { ReaderEntry { handle: handle } } } impl Default for ReaderEntry { fn default() -> Self { - ReaderEntry { handle: ptr::null_mut() } + ReaderEntry { + handle: ptr::null_mut(), + } } } impl Entry for ReaderEntry { - unsafe fn entry(&self) -> *mut ffi::Struct_archive_entry { + unsafe fn entry(&self) -> *mut ffi::archive_entry { self.handle } } diff --git a/src/writer.rs b/src/writer.rs index 9218aa2..91018b4 100644 --- a/src/writer.rs +++ b/src/writer.rs @@ -1,35 +1,35 @@ use std::default::Default; +use std::ffi::CString; use std::path::Path; use std::ptr; -use std::ffi::CString; -use libarchive3_sys::ffi; +use libarchive3_sys as ffi; -use archive::{Entry, ExtractOptions, Handle, WriteFilter, WriteFormat}; -use reader::{Reader, ReaderEntry}; -use error::{ArchiveResult, ArchiveError}; +use crate::archive::{Entry, ExtractOptions, Handle, WriteFilter, WriteFormat}; +use crate::error::{ArchiveError, ArchiveResult}; +use crate::reader::{Reader, ReaderEntry}; pub struct Writer { - handle: *mut ffi::Struct_archive, + handle: *mut ffi::archive, } pub struct Disk { - handle: *mut ffi::Struct_archive, + handle: *mut ffi::archive, } pub struct Builder { - handle: *mut ffi::Struct_archive, + handle: *mut ffi::archive, consumed: bool, } impl Writer { - pub fn new(handle: *mut ffi::Struct_archive) -> Self { + pub fn new(handle: *mut ffi::archive) -> Self { Writer { handle: handle } } } impl Handle for Writer { - unsafe fn handle(&self) -> *mut ffi::Struct_archive { + unsafe fn handle(&self) -> *mut ffi::archive { self.handle } } @@ -64,7 +64,7 @@ impl Disk { unsafe { match ffi::archive_write_set_bytes_per_block(self.handle, count) { ffi::ARCHIVE_OK => Ok(()), - _ => ArchiveResult::from(self as &Handle), + _ => ArchiveResult::from(self as &dyn Handle), } } } @@ -73,7 +73,7 @@ impl Disk { unsafe { match ffi::archive_write_set_bytes_in_last_block(self.handle, count) { ffi::ARCHIVE_OK => Ok(()), - _ => ArchiveResult::from(self as &Handle), + _ => ArchiveResult::from(self as &dyn Handle), } } } @@ -83,7 +83,7 @@ impl Disk { unsafe { match ffi::archive_write_disk_set_options(self.handle, eopt.flags) { ffi::ARCHIVE_OK => Ok(()), - _ => ArchiveResult::from(self as &Handle), + _ => ArchiveResult::from(self as &dyn Handle), } } } @@ -96,7 +96,7 @@ impl Disk { unsafe { match ffi::archive_write_disk_set_standard_lookup(self.handle) { ffi::ARCHIVE_OK => Ok(()), - _ => ArchiveResult::from(self as &Handle), + _ => ArchiveResult::from(self as &dyn Handle), } } } @@ -131,14 +131,14 @@ impl Disk { } } if write_pending { - bytes += try!(self.write_data(reader)); + bytes += self.write_data(reader)?; write_pending = false; } } unsafe { match ffi::archive_write_finish_entry(self.handle()) { ffi::ARCHIVE_OK => Ok(bytes), - _ => Err(ArchiveError::from(self as &Handle)), + _ => Err(ArchiveError::from(self as &dyn Handle)), } } } @@ -147,7 +147,7 @@ impl Disk { unsafe { match ffi::archive_write_close(self.handle()) { ffi::ARCHIVE_OK => Ok(()), - _ => ArchiveResult::from(self as &Handle), + _ => ArchiveResult::from(self as &dyn Handle), } } } @@ -159,18 +159,21 @@ impl Disk { unsafe { loop { - match ffi::archive_read_data_block(reader.handle(), - &mut buff, - &mut size, - &mut offset) { + match ffi::archive_read_data_block( + reader.handle(), + &mut buff, + &mut size, + &mut offset, + ) { ffi::ARCHIVE_EOF => return Ok(size), ffi::ARCHIVE_OK => { - if ffi::archive_write_data_block(self.handle, buff, size, offset) != - ffi::ARCHIVE_OK as isize { - return Err(ArchiveError::from(self as &Handle)); + if ffi::archive_write_data_block(self.handle, buff, size, offset) + != ffi::ARCHIVE_OK as isize + { + return Err(ArchiveError::from(self as &dyn Handle)); } } - _ => return Err(ArchiveError::from(reader as &Handle)), + _ => return Err(ArchiveError::from(reader as &dyn Handle)), } } } @@ -180,14 +183,14 @@ impl Disk { unsafe { match ffi::archive_write_header(self.handle, entry.entry()) { ffi::ARCHIVE_OK => Ok(()), - _ => ArchiveResult::from(self as &Handle), + _ => ArchiveResult::from(self as &dyn Handle), } } } } impl Handle for Disk { - unsafe fn handle(&self) -> *mut ffi::Struct_archive { + unsafe fn handle(&self) -> *mut ffi::archive { self.handle } } @@ -240,7 +243,7 @@ impl Builder { }; match result { ffi::ARCHIVE_OK => Ok(()), - _ => ArchiveResult::from(self as &Handle), + _ => ArchiveResult::from(self as &dyn Handle), } } @@ -274,7 +277,7 @@ impl Builder { }; match result { ffi::ARCHIVE_OK => Ok(()), - _ => ArchiveResult::from(self as &Handle), + _ => ArchiveResult::from(self as &dyn Handle), } } @@ -289,7 +292,7 @@ impl Builder { self.consumed = true; Ok(Writer::new(self.handle)) } - _ => Err(ArchiveError::from(&self as &Handle)), + _ => Err(ArchiveError::from(&self as &dyn Handle)), } } } @@ -310,7 +313,7 @@ impl Default for Builder { } impl Handle for Builder { - unsafe fn handle(&self) -> *mut ffi::Struct_archive { + unsafe fn handle(&self) -> *mut ffi::archive { self.handle } } diff --git a/tests/lib.rs b/tests/lib.rs index 872e947..d35c537 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -2,10 +2,10 @@ extern crate libarchive; pub mod util; -use std::fs::File; use libarchive::archive::{self, ReadFilter, ReadFormat}; use libarchive::reader::{self, Reader}; use libarchive::writer; +use std::fs::File; #[test] fn reading_from_file() { @@ -37,11 +37,14 @@ fn read_archive_from_stream() { Ok(mut reader) => { assert_eq!(reader.header_position(), 0); let writer = writer::Disk::new(); - let count = writer.write(&mut reader, Some("/opt/bldr/fucks")).ok().unwrap(); + let count = writer + .write(&mut reader, Some("/opt/bldr/fucks")) + .ok() + .unwrap(); assert_eq!(count, 14); assert_eq!(reader.header_position(), 1024); assert_eq!(4, 4); - }, + } Err(e) => { println!("{:?}", e); } @@ -92,7 +95,7 @@ fn extracting_a_reader_twice() { println!("{:?}", reader.header_position()); match writer.write(&mut reader, None) { Ok(_) => println!("oops"), - Err(_) => println!("nice") + Err(_) => println!("nice"), } assert_eq!(4, 4) } diff --git a/tests/util/path.rs b/tests/util/path.rs index da579b3..882a297 100644 --- a/tests/util/path.rs +++ b/tests/util/path.rs @@ -6,7 +6,14 @@ pub fn exe_path() -> PathBuf { } pub fn root() -> PathBuf { - exe_path().parent().unwrap().parent().unwrap().parent().unwrap().join("tests") + exe_path() + .parent() + .unwrap() + .parent() + .unwrap() + .parent() + .unwrap() + .join("tests") } pub fn fixtures() -> PathBuf {