team-ljyzipelo

공개
309줄6.8 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: 64 * 1024 * 1024,
61 output: 128 * 1024 * 1024,
62 entries: 10_000,
63 }
64 }
65}
66impl Limits {
67 pub fn valid(self) -> bool {
68 let hard = Self::default();
69 self.input > 0
70 && self.input <= hard.input
71 && self.output > 0
72 && self.output <= hard.output
73 && self.entries > 0
74 && self.entries <= hard.entries
75 }
76}
77#[derive(Clone, Copy, Debug, PartialEq, Eq)]
78pub enum Format {
79 Zip,
80 Tar,
81 Gzip,
82 TarGzip,
83 SevenZip,
84}
85#[derive(Clone, Debug, PartialEq, Eq)]
86pub enum SourceObject {
87 Unix {
88 device: u64,
89 inode: u64,
90 },
91 Windows {
92 volume_serial: u64,
93 file_id: [u8; 16],
94 },
95}
96#[derive(Clone, Debug, PartialEq, Eq)]
97pub struct ListId {
98 pub catalog_version: u16,
99 pub format: Format,
100 pub source_object: SourceObject,
101 pub archive_bytes: u64,
102 pub archive_sha256: [u8; 32],
103}
104#[derive(Clone, Debug, PartialEq, Eq)]
105pub enum Selection {
106 All,
107 EntryIds(Vec<u32>),
108}
109#[derive(Clone, Copy, Debug, PartialEq, Eq)]
110pub enum Method {
111 Store,
112 Deflate,
113}
114#[derive(Clone, Copy, Debug, PartialEq, Eq)]
115pub enum Encryption {
116 None,
117 Aes256,
118}
119#[derive(Clone, Debug, PartialEq, Eq)]
120pub struct Source {
121 pub path: NativePath,
122 pub archive_name: String,
123}
124#[derive(Clone, Debug, PartialEq, Eq)]
125pub enum Action {
126 List {
127 archive: NativePath,
128 },
129 Extract {
130 archive: NativePath,
131 destination: NativePath,
132 selection: Selection,
133 list_id: Option<ListId>,
134 gzip_output_name: Option<String>,
135 },
136 CreateZip {
137 sources: Vec<Source>,
138 output_file: NativePath,
139 method: Method,
140 encryption: Encryption,
141 },
142}
143#[derive(Clone, Copy, Debug, PartialEq, Eq)]
144pub enum ActionKind {
145 List,
146 Extract,
147 CreateZip,
148}
149impl Action {
150 pub fn kind(&self) -> ActionKind {
151 match self {
152 Self::List { .. } => ActionKind::List,
153 Self::Extract { .. } => ActionKind::Extract,
154 Self::CreateZip { .. } => ActionKind::CreateZip,
155 }
156 }
157}
158#[derive(Clone, Debug, PartialEq, Eq)]
159pub struct Start {
160 pub action: Action,
161 pub limits: Limits,
162}
163#[derive(Clone, Copy, Debug, PartialEq, Eq)]
164pub enum EntryKind {
165 File,
166 Directory,
167 Link,
168 Other,
169}
170#[derive(Clone, Debug, PartialEq, Eq)]
171pub struct Entry {
172 pub entry_id: u32,
173 pub name: String,
174 pub kind: EntryKind,
175 pub size: u64,
176 pub compressed_size: u64,
177 pub encrypted: bool,
178}
179#[derive(Clone, Copy, Debug, PartialEq, Eq)]
180pub enum Stage {
181 Scan,
182 Read,
183 Decode,
184 Write,
185 Verify,
186 Commit,
187}
188#[derive(Clone, Copy, Debug, PartialEq, Eq)]
189pub enum Disposition {
190 Created,
191 PartialRetained,
192 Removed,
193 CommitUncertain,
194}
195#[derive(Clone, Copy, Debug, PartialEq, Eq)]
196pub enum Outcome {
197 Completed,
198 Failed,
199 Cancelled,
200}
201#[derive(Clone, Copy, Debug, PartialEq, Eq)]
202pub enum ErrorCode {
203 None,
204 Integrity,
205 UnsupportedFormat,
206 UnsupportedFeature,
207 WrongPassword,
208 UnsafePath,
209 Conflict,
210 Limit,
211 Io,
212 Protocol,
213 Internal,
214 SourceChanged,
215 InvalidRequest,
216}
217#[derive(Clone, Copy, Debug, PartialEq, Eq)]
218pub enum Publication {
219 None,
220 Partial,
221 Complete,
222 Uncertain,
223}
224#[derive(Clone, Copy, Debug, PartialEq, Eq)]
225pub enum Durability {
226 NotApplicable,
227 Synced,
228 Unconfirmed,
229 Failed,
230}
231#[derive(Clone, Copy, Debug, PartialEq, Eq)]
232pub enum Cleanup {
233 Complete,
234 Failed,
235 Unconfirmed,
236}
237#[derive(Clone, Copy, Debug, PartialEq, Eq)]
238pub enum CancelStatus {
239 Requested,
240 AlreadyRequested,
241 TooLate,
242 Finished,
243}
244#[derive(Clone, Copy, Debug, PartialEq, Eq)]
245pub enum PasswordPurpose {
246 Decrypt,
247 Encrypt,
248}
249#[derive(Clone, Debug, PartialEq, Eq)]
250pub struct Terminal {
251 pub outcome: Outcome,
252 pub error: ErrorCode,
253 pub publication: Publication,
254 pub created_count: u64,
255 pub retained_partial_count: u64,
256 pub durability: Durability,
257 pub cleanup: Cleanup,
258 pub detail: String,
259}
260pub struct Secret(pub Vec<u8>);
261impl std::fmt::Debug for Secret {
262 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
263 f.write_str("[redacted]")
264 }
265}
266impl Drop for Secret {
267 fn drop(&mut self) {
268 self.0.fill(0);
269 }
270}
271#[derive(Debug)]
272pub enum Message {
273 Start(Start),
274 Password {
275 challenge_id: u64,
276 secret: Secret,
277 },
278 Cancel,
279 Accepted {
280 action: ActionKind,
281 encryption: bool,
282 },
283 Entry(Entry),
284 ListEnd {
285 entry_count: u32,
286 list_id: ListId,
287 },
288 PasswordRequired {
289 challenge_id: u64,
290 attempt: u32,
291 purpose: PasswordPurpose,
292 },
293 Progress {
294 stage: Stage,
295 processed_bytes: u64,
296 total_bytes: Option<u64>,
297 completed_entries: u32,
298 },
299 Output {
300 entry_id: Option<u32>,
301 relative_name: String,
302 disposition: Disposition,
303 },
304 Terminal(Terminal),
305 CancelAck {
306 request_sequence: u64,
307 status: CancelStatus,
308 },
309}