| 1 | use std::path::{Path, PathBuf}; |
| 2 | |
| 3 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 4 | pub enum NativePath { |
| 5 | Unix(Vec<u8>), |
| 6 | Windows(Vec<u16>), |
| 7 | } |
| 8 | impl 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)] |
| 52 | pub struct Limits { |
| 53 | pub input: u64, |
| 54 | pub output: u64, |
| 55 | pub entries: u32, |
| 56 | } |
| 57 | impl 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 | } |
| 66 | impl 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, Debug, PartialEq, Eq)] |
| 86 | pub struct GuardedStart { |
| 87 | pub start: Start, |
| 88 | pub ceiling: Limits, |
| 89 | pub interactive: bool, |
| 90 | } |
| 91 | impl GuardedStart { |
| 92 | pub fn valid(&self) -> bool { |
| 93 | self.start.limits.valid() |
| 94 | && self.ceiling.input == self.start.limits.input |
| 95 | && self.ceiling.output >= self.start.limits.output |
| 96 | && self.ceiling.output <= 64 * 1024 * 1024 * 1024 |
| 97 | && self.ceiling.entries >= self.start.limits.entries |
| 98 | && self.ceiling.entries <= 200_000 |
| 99 | && (!self.interactive || matches!(self.start.action, Action::Extract { .. })) |
| 100 | } |
| 101 | } |
| 102 | #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] |
| 103 | pub struct ResourceScope { |
| 104 | pub output: bool, |
| 105 | pub entries: bool, |
| 106 | pub ratio: bool, |
| 107 | } |
| 108 | impl ResourceScope { |
| 109 | pub fn any(self) -> bool { |
| 110 | self.output || self.entries || self.ratio |
| 111 | } |
| 112 | } |
| 113 | #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] |
| 114 | pub struct ResourceUsage { |
| 115 | pub estimated_bytes: Option<u64>, |
| 116 | pub actual_bytes: Option<u64>, |
| 117 | pub estimated_entries: Option<u32>, |
| 118 | pub actual_entries: Option<u32>, |
| 119 | pub compressed_bytes: Option<u64>, |
| 120 | pub ratio_output_bytes: Option<u64>, |
| 121 | pub available_bytes: Option<u64>, |
| 122 | } |
| 123 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 124 | pub enum Format { |
| 125 | Zip, |
| 126 | Tar, |
| 127 | Gzip, |
| 128 | TarGzip, |
| 129 | SevenZip, |
| 130 | } |
| 131 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 132 | pub enum SourceObject { |
| 133 | Unix { |
| 134 | device: u64, |
| 135 | inode: u64, |
| 136 | }, |
| 137 | Windows { |
| 138 | volume_serial: u64, |
| 139 | file_id: [u8; 16], |
| 140 | }, |
| 141 | } |
| 142 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 143 | pub struct ListId { |
| 144 | pub catalog_version: u16, |
| 145 | pub format: Format, |
| 146 | pub source_object: SourceObject, |
| 147 | pub archive_bytes: u64, |
| 148 | pub archive_sha256: [u8; 32], |
| 149 | } |
| 150 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 151 | pub enum Selection { |
| 152 | All, |
| 153 | EntryIds(Vec<u32>), |
| 154 | } |
| 155 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 156 | pub enum Method { |
| 157 | Store, |
| 158 | Deflate, |
| 159 | } |
| 160 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 161 | pub enum Encryption { |
| 162 | None, |
| 163 | Aes256, |
| 164 | } |
| 165 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 166 | pub struct Source { |
| 167 | pub path: NativePath, |
| 168 | pub archive_name: String, |
| 169 | } |
| 170 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 171 | pub enum Action { |
| 172 | List { |
| 173 | archive: NativePath, |
| 174 | }, |
| 175 | Extract { |
| 176 | archive: NativePath, |
| 177 | destination: NativePath, |
| 178 | selection: Selection, |
| 179 | list_id: Option<ListId>, |
| 180 | gzip_output_name: Option<String>, |
| 181 | }, |
| 182 | CreateZip { |
| 183 | sources: Vec<Source>, |
| 184 | output_file: NativePath, |
| 185 | method: Method, |
| 186 | encryption: Encryption, |
| 187 | }, |
| 188 | } |
| 189 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 190 | pub enum ActionKind { |
| 191 | List, |
| 192 | Extract, |
| 193 | CreateZip, |
| 194 | } |
| 195 | impl Action { |
| 196 | pub fn kind(&self) -> ActionKind { |
| 197 | match self { |
| 198 | Self::List { .. } => ActionKind::List, |
| 199 | Self::Extract { .. } => ActionKind::Extract, |
| 200 | Self::CreateZip { .. } => ActionKind::CreateZip, |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 205 | pub struct Start { |
| 206 | pub action: Action, |
| 207 | pub limits: Limits, |
| 208 | } |
| 209 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 210 | pub enum EntryKind { |
| 211 | File, |
| 212 | Directory, |
| 213 | Link, |
| 214 | Other, |
| 215 | } |
| 216 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 217 | pub struct Entry { |
| 218 | pub entry_id: u32, |
| 219 | pub name: String, |
| 220 | pub kind: EntryKind, |
| 221 | pub size: u64, |
| 222 | pub compressed_size: u64, |
| 223 | pub encrypted: bool, |
| 224 | } |
| 225 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 226 | pub enum Stage { |
| 227 | Scan, |
| 228 | Read, |
| 229 | Decode, |
| 230 | Write, |
| 231 | Verify, |
| 232 | Commit, |
| 233 | } |
| 234 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 235 | pub enum Disposition { |
| 236 | Created, |
| 237 | PartialRetained, |
| 238 | Removed, |
| 239 | CommitUncertain, |
| 240 | } |
| 241 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 242 | pub enum Outcome { |
| 243 | Completed, |
| 244 | Failed, |
| 245 | Cancelled, |
| 246 | } |
| 247 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 248 | pub enum ErrorCode { |
| 249 | None, |
| 250 | Integrity, |
| 251 | UnsupportedFormat, |
| 252 | UnsupportedFeature, |
| 253 | WrongPassword, |
| 254 | UnsafePath, |
| 255 | Conflict, |
| 256 | Limit, |
| 257 | Io, |
| 258 | Protocol, |
| 259 | Internal, |
| 260 | SourceChanged, |
| 261 | InvalidRequest, |
| 262 | DiskFull, |
| 263 | } |
| 264 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 265 | pub enum Publication { |
| 266 | None, |
| 267 | Partial, |
| 268 | Complete, |
| 269 | Uncertain, |
| 270 | } |
| 271 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 272 | pub enum Durability { |
| 273 | NotApplicable, |
| 274 | Synced, |
| 275 | Unconfirmed, |
| 276 | Failed, |
| 277 | } |
| 278 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 279 | pub enum Cleanup { |
| 280 | Complete, |
| 281 | Failed, |
| 282 | Unconfirmed, |
| 283 | } |
| 284 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 285 | pub enum CancelStatus { |
| 286 | Requested, |
| 287 | AlreadyRequested, |
| 288 | TooLate, |
| 289 | Finished, |
| 290 | } |
| 291 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 292 | pub enum PasswordPurpose { |
| 293 | Decrypt, |
| 294 | Encrypt, |
| 295 | } |
| 296 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 297 | pub enum ConflictChoice { |
| 298 | Overwrite, |
| 299 | Skip, |
| 300 | Rename, |
| 301 | } |
| 302 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 303 | pub struct Terminal { |
| 304 | pub outcome: Outcome, |
| 305 | pub error: ErrorCode, |
| 306 | pub publication: Publication, |
| 307 | pub created_count: u64, |
| 308 | pub retained_partial_count: u64, |
| 309 | pub durability: Durability, |
| 310 | pub cleanup: Cleanup, |
| 311 | pub detail: String, |
| 312 | } |
| 313 | pub struct Secret(pub Vec<u8>); |
| 314 | impl std::fmt::Debug for Secret { |
| 315 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 316 | f.write_str("[redacted]") |
| 317 | } |
| 318 | } |
| 319 | impl Drop for Secret { |
| 320 | fn drop(&mut self) { |
| 321 | zeroize::Zeroize::zeroize(&mut self.0); |
| 322 | } |
| 323 | } |
| 324 | #[derive(Debug)] |
| 325 | pub enum Message { |
| 326 | Start(Start), |
| 327 | Password { |
| 328 | challenge_id: u64, |
| 329 | secret: Secret, |
| 330 | }, |
| 331 | Cancel, |
| 332 | StartInteractive(Start), |
| 333 | StartGuarded(GuardedStart), |
| 334 | ResourceReply { |
| 335 | challenge_id: u64, |
| 336 | scope: ResourceScope, |
| 337 | allow: bool, |
| 338 | }, |
| 339 | ResourceRequired { |
| 340 | challenge_id: u64, |
| 341 | action: ActionKind, |
| 342 | scope: ResourceScope, |
| 343 | usage: ResourceUsage, |
| 344 | soft_limits: Limits, |
| 345 | ceiling: Limits, |
| 346 | }, |
| 347 | ConflictReply { |
| 348 | challenge_id: u64, |
| 349 | choice: ConflictChoice, |
| 350 | apply_to_remaining: bool, |
| 351 | }, |
| 352 | Accepted { |
| 353 | action: ActionKind, |
| 354 | encryption: bool, |
| 355 | }, |
| 356 | Entry(Entry), |
| 357 | ListEnd { |
| 358 | entry_count: u32, |
| 359 | list_id: ListId, |
| 360 | }, |
| 361 | PasswordRequired { |
| 362 | challenge_id: u64, |
| 363 | attempt: u32, |
| 364 | purpose: PasswordPurpose, |
| 365 | }, |
| 366 | Progress { |
| 367 | stage: Stage, |
| 368 | processed_bytes: u64, |
| 369 | total_bytes: Option<u64>, |
| 370 | completed_entries: u32, |
| 371 | }, |
| 372 | Output { |
| 373 | entry_id: Option<u32>, |
| 374 | relative_name: String, |
| 375 | disposition: Disposition, |
| 376 | }, |
| 377 | Terminal(Terminal), |
| 378 | CancelAck { |
| 379 | request_sequence: u64, |
| 380 | status: CancelStatus, |
| 381 | }, |
| 382 | ConflictRequired { |
| 383 | challenge_id: u64, |
| 384 | entry_id: u32, |
| 385 | relative_name: String, |
| 386 | incoming_size: u64, |
| 387 | existing_size: u64, |
| 388 | }, |
| 389 | ConflictResolved { |
| 390 | entry_id: u32, |
| 391 | relative_name: String, |
| 392 | choice: ConflictChoice, |
| 393 | }, |
| 394 | } |