1use crate::libarchive;
2use std::ffi::CStr;
3
4#[derive(thiserror::Error, Debug)]
5pub enum Error {
6 #[error("IO error: {0:?}")]
7 Io(#[from] std::io::Error),
8 #[error("Extraction error: {0}")]
10 Extraction(String),
11 #[error("Archive path cannot be converted to utf8")]
14 PathNotUtf8,
15 #[error("Entry name cannot be decoded with given encoding")]
18 Encoding,
19 #[error("Failed to convert string to cstring: {0:?}")]
20 StringError(#[from] std::ffi::NulError),
21 #[error("Unknown error happened")]
23 Unknown,
24}
25
26#[cfg(test)]
27impl PartialEq for Error {
28 fn eq(&self, other: &Self) -> bool {
29 match (self, other) {
30 (Error::Extraction(my_msg), Error::Extraction(other_msg)) => my_msg == other_msg,
31 (Error::Io(my_err), Error::Io(other_err)) => my_err.kind() == other_err.kind(),
32 (Error::PathNotUtf8, Error::PathNotUtf8)
33 | (Error::Unknown, Error::Unknown)
34 | (Error::StringError(_), Error::StringError(_)) => true,
35 _ => false,
36 }
37 }
38}
39
40pub type Result<T, E = Error> = std::result::Result<T, E>;
41
42pub(crate) fn analyze_result(
43 result: std::os::raw::c_int,
44 handle: *mut libarchive::archive,
45) -> Result<()> {
46 match result {
47 libarchive::ARCHIVE_OK | libarchive::ARCHIVE_WARN => Ok(()),
48 _ => unsafe {
49 let error_string = libarchive::archive_error_string(handle);
50 if !error_string.is_null() {
51 return Err(Error::Extraction(
52 CStr::from_ptr(error_string).to_string_lossy().to_string(),
53 ));
54 }
55 let error_code = libarchive::archive_errno(handle);
56 if error_code != 0 {
57 Err(std::io::Error::from_raw_os_error(error_code).into())
58 } else {
59 Err(Error::Unknown)
60 }
61 },
62 }
63}
64
65pub(crate) fn path_does_not_exist<S: Into<String>>(message: S) -> Error {
66 Error::Io(std::io::Error::new(
67 std::io::ErrorKind::NotFound,
68 message.into(),
69 ))
70}
71
72pub(crate) fn invalid_data<S: Into<String>>(message: S) -> Error {
73 Error::Io(std::io::Error::new(
74 std::io::ErrorKind::InvalidData,
75 message.into(),
76 ))
77}