Skip to main content

simple_archive/carchive/
mod.rs

1mod generated;
2
3use std::ffi::CStr;
4
5pub use generated::*;
6
7use crate::Metadata;
8
9fn entry_pathname(entry: *mut archive_entry) -> String {
10    let pathname = unsafe { archive_entry_pathname(entry) };
11    if pathname.is_null() {
12        return "".to_owned();
13    }
14
15    String::from_utf8_lossy(unsafe { CStr::from_ptr(pathname) }.to_bytes()).into()
16}
17
18fn entry_ctime(entry: *mut archive_entry) -> i64 {
19    unsafe { archive_entry_ctime(entry) }
20}
21
22fn entry_ctime_nano(entry: *mut archive_entry) -> i64 {
23    unsafe { archive_entry_ctime_nsec(entry) }
24}
25
26fn entry_atime(entry: *mut archive_entry) -> i64 {
27    unsafe { archive_entry_atime(entry) }
28}
29
30fn entry_atime_nano(entry: *mut archive_entry) -> i64 {
31    unsafe { archive_entry_atime_nsec(entry) }
32}
33
34fn entry_mtime(entry: *mut archive_entry) -> i64 {
35    unsafe { archive_entry_mtime(entry) }
36}
37
38fn entry_mtime_nano(entry: *mut archive_entry) -> i64 {
39    unsafe { archive_entry_mtime_nsec(entry) }
40}
41
42fn entry_owner(entry: *mut archive_entry) -> libc::uid_t {
43    unsafe { archive_entry_uid(entry) }
44}
45
46fn entry_group(entry: *mut archive_entry) -> libc::gid_t {
47    unsafe { archive_entry_gid(entry) }
48}
49
50pub fn entry_nodetype(entry: *mut archive_entry) -> u32 {
51    unsafe { archive_entry_filetype(entry) }
52}
53
54pub fn entry_header(archive: *mut archive) -> crate::prelude::Result<*mut archive_entry> {
55    unsafe {
56        let mut entry = std::mem::MaybeUninit::<*mut archive_entry>::uninit();
57        match archive_read_next_header(archive, entry.as_mut_ptr()) {
58            ARCHIVE_EOF => Err(crate::Error::Unknown("EOF".to_owned())),
59            ARCHIVE_OK | ARCHIVE_WARN => {
60                Ok(entry.assume_init())
61            }
62            _ => Err(crate::Error::from(archive)),
63        }
64    }
65}
66
67pub fn extract(archive: *mut archive, entry: *mut archive_entry, flags: i32) -> crate::prelude::Result<()> {
68    unsafe {
69        match archive_read_extract(archive, entry, flags) {
70            ARCHIVE_OK | ARCHIVE_WARN => Ok(()),
71            _ => Err(crate::Error::from(archive)),
72        }
73    }
74}
75
76fn entry_size(entry: *mut archive_entry) -> i64 {
77    unsafe { archive_entry_size(entry) }
78}
79
80fn entry_perm(entry: *mut archive_entry) -> mode_t {
81    unsafe { archive_entry_perm(entry) }
82}
83
84impl From<*mut archive_entry> for Metadata {
85    fn from(input: *mut archive_entry) -> Self {
86        Metadata {
87            filepath: entry_pathname(input),
88            size: entry_size(input),
89            nodetype: entry_nodetype(input),
90            perm: entry_perm(input),
91            ctime: entry_ctime(input),
92            ctime_nano: entry_ctime_nano(input),
93            atime: entry_atime(input),
94            atime_nano: entry_atime_nano(input),
95            mtime: entry_mtime(input),
96            mtime_nano: entry_mtime_nano(input),
97            owner: entry_owner(input),
98            group: entry_group(input),
99        }
100    }
101}