team-ljyzipelo

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