Skip to content

Commit b8608b4

Browse files
author
Yorhel
committed
Implement Read for Reader, wrapping archive_read_data()
1 parent 3f723cf commit b8608b4

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/reader.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,20 @@ pub trait Reader : Handle {
6363
}
6464
}
6565

66+
impl Read for Reader {
67+
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
68+
let cbuf = buf.as_mut_ptr() as *mut c_void;
69+
unsafe {
70+
match ffi::archive_read_data(self.handle(), cbuf, buf.len()) {
71+
n if n >= 0 => Ok(n as usize),
72+
// Libarchive returns OS errors, but has more specific error strings (err_msg()).
73+
// Not sure how to include that in the io::Error struct.
74+
_ => Err(io::Error::from_raw_os_error(self.err_code().0)),
75+
}
76+
}
77+
}
78+
}
79+
6680
pub struct FileReader {
6781
handle: *mut ffi::Struct_archive,
6882
entry: ReaderEntry,

0 commit comments

Comments
 (0)