team-ljyzipelo

공개
342줄7.6 KB
1use std::path::{Path, PathBuf};
2
3#[derive(Clone, Debug, PartialEq, Eq)]
4pub enum NativePath {
5 Unix(Vec<u8>),
6 Windows(Vec<u16>),
7}
8impl NativePath {
9 pub fn from_path(path: &Path) -> Self {
10 #[cfg(unix)]
11 {
12 use std::os::unix::ffi::OsStrExt;
13 Self::Unix(path.as_os_str().as_bytes().to_vec())
14 }
15 #[cfg(windows)]
16 {
17 use std::os::windows::ffi::OsStrExt;
18 Self::Windows(path.as_os_str().encode_wide().collect())
19 }
20 }
21 pub fn to_path(&self) -> Result<PathBuf, ErrorCode> {
22 #[cfg(unix)]
23 if let Self::Unix(bytes) = self {
24 use std::os::unix::ffi::OsStringExt;
25 if bytes.contains(&0) {
26 return Err(ErrorCode::InvalidRequest);
27 }
28 let path = PathBuf::from(std::ffi::OsString::from_vec(bytes.clone()));
29 return if path.is_absolute() {
30 Ok(path)
31 } else {
32 Err(ErrorCode::InvalidRequest)
33 };
34 }
35 #[cfg(windows)]
36 if let Self::Windows(units) = self {
37 use std::os::windows::ffi::OsStringExt;
38 if units.contains(&0) {
39 return Err(ErrorCode::InvalidRequest);
40 }
41 let path = PathBuf::from(std::ffi::OsString::from_wide(units));
42 let text = path.to_string_lossy();
43 if path.is_absolute() && !text.starts_with("\\\\") && !text.starts_with("//") {
44 return Ok(path);
45 }
46 return Err(ErrorCode::InvalidRequest);
47 }
48 Err(ErrorCode::UnsupportedFeature)
49 }
50}
51#[derive(Clone, Copy, Debug, PartialEq, Eq)]
52pub struct Limits {
53 pub input: u64,
54 pub output: u64,
55 pub entries: u32,
56}
57impl Default for Limits {
58 fn default() -> Self {
59 Self {
60 input: u64::MAX,
61 output: 16 * 1024 * 1024 * 1024,
62 entries: 10_000,
63 }
64 }
65}
66impl Limits {
67 pub(crate) fn legacy(self) -> Self {
68 Self {
69 input: self.input.min(64 * 1024 * 1024),
70 output: self.output.min(128 * 1024 * 1024),
71 entries: self.entries,
72 }
73 }
74
75 pub fn valid(self) -> bool {
76 let hard = Self::default();
77 self.input > 0
78 && self.input <= hard.input
79 && self.output > 0
80 && self.output <= hard.output
81 && self.entries > 0
82 && self.entries <= hard.entries
83 }
84}
85#[derive(Clone, Copy, Debug, PartialEq, Eq)]
86pub enum Format {
87 Zip,
88 Tar,
89 Gzip,
90 TarGzip,
91 SevenZip,
92}
93#[derive(Clone, Debug, PartialEq, Eq)]
94pub enum SourceObject {
95 Unix {
96 device: u64,
97 inode: u64,
98 },
99 Windows {
100 volume_serial: u64,
101 file_id: [u8; 16],
102 },
103}
104#[derive(Clone, Debug, PartialEq, Eq)]
105pub struct ListId {
106 pub catalog_version: u16,
107 pub format: Format,
108 pub source_object: SourceObject,
109 pub archive_bytes: u64,
110 pub archive_sha256: [u8; 32],
111}
112#[derive(Clone, Debug, PartialEq, Eq)]
113pub enum Selection {
114 All,
115 EntryIds(Vec<u32>),
116}
117#[derive(Clone, Copy, Debug, PartialEq, Eq)]
118pub enum Method {
119 Store,
120 Deflate,
121}
122#[derive(Clone, Copy, Debug, PartialEq, Eq)]
123pub enum Encryption {
124 None,
125 Aes256,
126}
127#[derive(Clone, Debug, PartialEq, Eq)]
128pub struct Source {
129 pub path: NativePath,
130 pub archive_name: String,
131}
132#[derive(Clone, Debug, PartialEq, Eq)]
133pub enum Action {
134 List {
135 archive: NativePath,
136 },
137 Extract {
138 archive: NativePath,
139 destination: NativePath,
140 selection: Selection,
141 list_id: Option<ListId>,
142 gzip_output_name: Option<String>,
143 },
144 CreateZip {
145 sources: Vec<Source>,
146 output_file: NativePath,
147 method: Method,
148 encryption: Encryption,
149 },
150}
151#[derive(Clone, Copy, Debug, PartialEq, Eq)]
152pub enum ActionKind {
153 List,
154 Extract,
155 CreateZip,
156}
157impl Action {
158 pub fn kind(&self) -> ActionKind {
159 match self {
160 Self::List { .. } => ActionKind::List,
161 Self::Extract { .. } => ActionKind::Extract,
162 Self::CreateZip { .. } => ActionKind::CreateZip,
163 }
164 }
165}
166#[derive(Clone, Debug, PartialEq, Eq)]
167pub struct Start {
168 pub action: Action,
169 pub limits: Limits,
170}
171#[derive(Clone, Copy, Debug, PartialEq, Eq)]
172pub enum EntryKind {
173 File,
174 Directory,
175 Link,
176 Other,
177}
178#[derive(Clone, Debug, PartialEq, Eq)]
179pub struct Entry {
180 pub entry_id: u32,
181 pub name: String,
182 pub kind: EntryKind,
183 pub size: u64,
184 pub compressed_size: u64,
185 pub encrypted: bool,
186}
187#[derive(Clone, Copy, Debug, PartialEq, Eq)]
188pub enum Stage {
189 Scan,
190 Read,
191 Decode,
192 Write,
193 Verify,
194 Commit,
195}
196#[derive(Clone, Copy, Debug, PartialEq, Eq)]
197pub enum Disposition {
198 Created,
199 PartialRetained,
200 Removed,
201 CommitUncertain,
202}
203#[derive(Clone, Copy, Debug, PartialEq, Eq)]
204pub enum Outcome {
205 Completed,
206 Failed,
207 Cancelled,
208}
209#[derive(Clone, Copy, Debug, PartialEq, Eq)]
210pub enum ErrorCode {
211 None,
212 Integrity,
213 UnsupportedFormat,
214 UnsupportedFeature,
215 WrongPassword,
216 UnsafePath,
217 Conflict,
218 Limit,
219 Io,
220 Protocol,
221 Internal,
222 SourceChanged,
223 InvalidRequest,
224 DiskFull,
225}
226#[derive(Clone, Copy, Debug, PartialEq, Eq)]
227pub enum Publication {
228 None,
229 Partial,
230 Complete,
231 Uncertain,
232}
233#[derive(Clone, Copy, Debug, PartialEq, Eq)]
234pub enum Durability {
235 NotApplicable,
236 Synced,
237 Unconfirmed,
238 Failed,
239}
240#[derive(Clone, Copy, Debug, PartialEq, Eq)]
241pub enum Cleanup {
242 Complete,
243 Failed,
244 Unconfirmed,
245}
246#[derive(Clone, Copy, Debug, PartialEq, Eq)]
247pub enum CancelStatus {
248 Requested,
249 AlreadyRequested,
250 TooLate,
251 Finished,
252}
253#[derive(Clone, Copy, Debug, PartialEq, Eq)]
254pub enum PasswordPurpose {
255 Decrypt,
256 Encrypt,
257}
258#[derive(Clone, Copy, Debug, PartialEq, Eq)]
259pub enum ConflictChoice {
260 Overwrite,
261 Skip,
262 Rename,
263}
264#[derive(Clone, Debug, PartialEq, Eq)]
265pub struct Terminal {
266 pub outcome: Outcome,
267 pub error: ErrorCode,
268 pub publication: Publication,
269 pub created_count: u64,
270 pub retained_partial_count: u64,
271 pub durability: Durability,
272 pub cleanup: Cleanup,
273 pub detail: String,
274}
275pub struct Secret(pub Vec<u8>);
276impl std::fmt::Debug for Secret {
277 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
278 f.write_str("[redacted]")
279 }
280}
281impl Drop for Secret {
282 fn drop(&mut self) {
283 zeroize::Zeroize::zeroize(&mut self.0);
284 }
285}
286#[derive(Debug)]
287pub enum Message {
288 Start(Start),
289 Password {
290 challenge_id: u64,
291 secret: Secret,
292 },
293 Cancel,
294 StartInteractive(Start),
295 ConflictReply {
296 challenge_id: u64,
297 choice: ConflictChoice,
298 apply_to_remaining: bool,
299 },
300 Accepted {
301 action: ActionKind,
302 encryption: bool,
303 },
304 Entry(Entry),
305 ListEnd {
306 entry_count: u32,
307 list_id: ListId,
308 },
309 PasswordRequired {
310 challenge_id: u64,
311 attempt: u32,
312 purpose: PasswordPurpose,
313 },
314 Progress {
315 stage: Stage,
316 processed_bytes: u64,
317 total_bytes: Option<u64>,
318 completed_entries: u32,
319 },
320 Output {
321 entry_id: Option<u32>,
322 relative_name: String,
323 disposition: Disposition,
324 },
325 Terminal(Terminal),
326 CancelAck {
327 request_sequence: u64,
328 status: CancelStatus,
329 },
330 ConflictRequired {
331 challenge_id: u64,
332 entry_id: u32,
333 relative_name: String,
334 incoming_size: u64,
335 existing_size: u64,
336 },
337 ConflictResolved {
338 entry_id: u32,
339 relative_name: String,
340 choice: ConflictChoice,
341 },
342}