1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use crate::libarchive;
use std::ffi::CStr;
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("IO error: {0:?}")]
Io(#[from] std::io::Error),
#[error("Extraction error: {0}")]
Extraction(String),
#[error("Archive path cannot be converted to utf8")]
PathNotUtf8,
#[error("Entry name cannot be decoded with given encoding")]
Encoding,
#[error("Unknown error happened")]
Unknown,
}
pub type Result<T, E = Error> = std::result::Result<T, E>;
pub(crate) fn analyze_result(
result: std::os::raw::c_int,
handle: *mut libarchive::archive,
) -> Result<()> {
match result {
libarchive::ARCHIVE_OK | libarchive::ARCHIVE_WARN => Ok(()),
_ => unsafe {
let error_string = libarchive::archive_error_string(handle);
if !error_string.is_null() {
return Err(Error::Extraction(
CStr::from_ptr(error_string).to_string_lossy().to_string(),
));
}
let error_code = libarchive::archive_errno(handle);
if error_code != 0 {
Err(std::io::Error::from_raw_os_error(error_code).into())
} else {
Err(Error::Unknown)
}
},
}
}
pub(crate) fn path_does_not_exist(message: String) -> Error {
Error::Io(std::io::Error::new(std::io::ErrorKind::NotFound, message))
}