Skip to main content

ex_cli/zip/
clone.rs

1use crate::error::MyResult;
2use crate::fs::entry::Entry;
3use crate::fs::file::Signature;
4use crate::fs::flags::FileFlags;
5use std::ffi::OsStr;
6use std::path::{Path, PathBuf};
7use std::rc::Rc;
8use std::time::SystemTime;
9#[cfg(unix)]
10use uzers::{gid_t, uid_t};
11
12pub struct CloneEntry {
13    file_depth: usize,
14    inner_depth: Option<usize>,
15    file_flags: FileFlags,
16    file_mode: u32,
17    #[cfg(unix)]
18    owner_uid: uid_t,
19    #[cfg(unix)]
20    owner_gid: gid_t,
21    file_size: u64,
22    file_time: SystemTime,
23}
24
25impl CloneEntry {
26    #[cfg(unix)]
27    pub fn from_entry(entry: &dyn Entry) -> Rc<Box<dyn Entry>> {
28        let file_depth = entry.file_depth();
29        let inner_depth = entry.inner_depth();
30        let file_flags = entry.file_flags();
31        let file_mode = entry.file_mode();
32        let owner_uid = entry.owner_uid();
33        let owner_gid = entry.owner_gid();
34        let file_size = entry.file_size();
35        let file_time = entry.file_time();
36        let entry = Self {
37            file_depth,
38            inner_depth,
39            file_flags,
40            file_mode,
41            owner_uid,
42            owner_gid,
43            file_size,
44            file_time,
45        };
46        Rc::new(Box::new(entry))
47    }
48
49    #[cfg(not(unix))]
50    pub fn from_entry(entry: &dyn Entry) -> Rc<Box<dyn Entry>> {
51        let file_depth = entry.file_depth();
52        let inner_depth = entry.inner_depth();
53        let file_flags = entry.file_flags();
54        let file_mode = entry.file_mode();
55        let file_size = entry.file_size();
56        let file_time = entry.file_time();
57        let entry = Self {
58            file_depth,
59            inner_depth,
60            file_flags,
61            file_mode,
62            file_size,
63            file_time,
64        };
65        Rc::new(Box::new(entry))
66    }
67}
68
69impl Entry for CloneEntry {
70    fn file_path(&self) -> &Path {
71        static PATH: once_cell::sync::OnceCell<PathBuf> = once_cell::sync::OnceCell::new();
72        PATH.get_or_init(|| PathBuf::new()).as_path()
73    }
74
75    fn file_name(&self) -> &OsStr {
76        self.file_path().file_name().unwrap_or_default()
77    }
78
79    fn file_depth(&self) -> usize {
80        self.file_depth
81    }
82
83    fn inner_path(&self) -> Option<&Path> {
84        None
85    }
86
87    fn inner_depth(&self) -> Option<usize> {
88        self.inner_depth
89    }
90
91    fn file_flags(&self) -> FileFlags {
92        self.file_flags
93    }
94
95    fn read_crc(&self) -> u32 {
96        0
97    }
98
99    fn read_sig(&self) -> Option<Signature> {
100        None
101    }
102
103    #[cfg(windows)]
104    fn read_version(&self) -> Option<String> {
105        None
106    }
107
108    fn read_link(&self) -> MyResult<Option<PathBuf>> {
109        Ok(None)
110    }
111
112    fn copy_metadata(&self, _other: &dyn Entry) {
113    }
114
115    fn reset_metadata(&self) {
116    }
117
118    fn file_mode(&self) -> u32 {
119        self.file_mode
120    }
121
122    #[cfg(unix)]
123    fn owner_uid(&self) -> uid_t {
124        self.owner_uid
125    }
126
127    #[cfg(unix)]
128    fn owner_gid(&self) -> gid_t {
129        self.owner_gid
130    }
131
132    fn file_size(&self) -> u64 {
133        self.file_size
134    }
135
136    fn file_time(&self) -> SystemTime {
137        self.file_time
138    }
139}