| 1 | use std::path::{Path, PathBuf}; |
| 2 | |
| 3 | pub const MAX_PREVIEW_TEXT_BYTES: u64 = 1024 * 1024; |
| 4 | pub const MAX_PREVIEW_IMAGE_BYTES: u64 = 8 * 1024 * 1024; |
| 5 | pub const MAX_PREVIEW_RGBA_BYTES: u64 = 64 * 1024 * 1024; |
| 6 | pub const MAX_PREVIEW_DIMENSION: u32 = 8192; |
| 7 | pub const MAX_PREVIEW_PIXELS: u64 = 16_000_000; |
| 8 | pub const MAX_PREVIEW_CHUNK_BYTES: usize = 32 * 1024; |
| 9 | |
| 10 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 11 | pub enum NativePath { |
| 12 | Unix(Vec<u8>), |
| 13 | Windows(Vec<u16>), |
| 14 | } |
| 15 | impl NativePath { |
| 16 | pub fn from_path(path: &Path) -> Self { |
| 17 | #[cfg(unix)] |
| 18 | { |
| 19 | use std::os::unix::ffi::OsStrExt; |
| 20 | Self::Unix(path.as_os_str().as_bytes().to_vec()) |
| 21 | } |
| 22 | #[cfg(windows)] |
| 23 | { |
| 24 | use std::os::windows::ffi::OsStrExt; |
| 25 | Self::Windows(path.as_os_str().encode_wide().collect()) |
| 26 | } |
| 27 | } |
| 28 | pub fn is_basename(&self) -> bool { |
| 29 | match self { |
| 30 | Self::Unix(bytes) => { |
| 31 | !bytes.is_empty() |
| 32 | && bytes != b"." |
| 33 | && bytes != b".." |
| 34 | && !bytes.iter().any(|byte| matches!(*byte, 0 | b'/' | b'\\')) |
| 35 | } |
| 36 | Self::Windows(units) => { |
| 37 | !units.is_empty() |
| 38 | && units != &[b'.' as u16] |
| 39 | && units != &[b'.' as u16, b'.' as u16] |
| 40 | && !units.iter().any(|unit| matches!(*unit, 0 | 47 | 92 | 58)) |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | pub fn to_path(&self) -> Result<PathBuf, ErrorCode> { |
| 45 | #[cfg(unix)] |
| 46 | if let Self::Unix(bytes) = self { |
| 47 | use std::os::unix::ffi::OsStringExt; |
| 48 | if bytes.contains(&0) { |
| 49 | return Err(ErrorCode::InvalidRequest); |
| 50 | } |
| 51 | let path = PathBuf::from(std::ffi::OsString::from_vec(bytes.clone())); |
| 52 | return if path.is_absolute() { |
| 53 | Ok(path) |
| 54 | } else { |
| 55 | Err(ErrorCode::InvalidRequest) |
| 56 | }; |
| 57 | } |
| 58 | #[cfg(windows)] |
| 59 | if let Self::Windows(units) = self { |
| 60 | use std::os::windows::ffi::OsStringExt; |
| 61 | if units.contains(&0) { |
| 62 | return Err(ErrorCode::InvalidRequest); |
| 63 | } |
| 64 | let path = PathBuf::from(std::ffi::OsString::from_wide(units)); |
| 65 | let text = path.to_string_lossy(); |
| 66 | if path.is_absolute() && !text.starts_with("\\\\") && !text.starts_with("//") { |
| 67 | return Ok(path); |
| 68 | } |
| 69 | return Err(ErrorCode::InvalidRequest); |
| 70 | } |
| 71 | Err(ErrorCode::UnsupportedFeature) |
| 72 | } |
| 73 | } |
| 74 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 75 | pub struct Limits { |
| 76 | pub input: u64, |
| 77 | pub output: u64, |
| 78 | pub entries: u32, |
| 79 | } |
| 80 | impl Default for Limits { |
| 81 | fn default() -> Self { |
| 82 | Self { |
| 83 | input: u64::MAX, |
| 84 | output: 16 * 1024 * 1024 * 1024, |
| 85 | entries: 10_000, |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | impl Limits { |
| 90 | pub(crate) fn legacy(self) -> Self { |
| 91 | Self { |
| 92 | input: self.input.min(64 * 1024 * 1024), |
| 93 | output: self.output.min(128 * 1024 * 1024), |
| 94 | entries: self.entries, |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | pub fn valid(self) -> bool { |
| 99 | let hard = Self::default(); |
| 100 | self.input > 0 |
| 101 | && self.input <= hard.input |
| 102 | && self.output > 0 |
| 103 | && self.output <= hard.output |
| 104 | && self.entries > 0 |
| 105 | && self.entries <= hard.entries |
| 106 | } |
| 107 | } |
| 108 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 109 | pub struct GuardedStart { |
| 110 | pub start: Start, |
| 111 | pub ceiling: Limits, |
| 112 | pub interactive: bool, |
| 113 | } |
| 114 | impl GuardedStart { |
| 115 | pub fn valid(&self) -> bool { |
| 116 | self.start.limits.valid() |
| 117 | && self.ceiling.input == self.start.limits.input |
| 118 | && self.ceiling.output >= self.start.limits.output |
| 119 | && self.ceiling.output <= 64 * 1024 * 1024 * 1024 |
| 120 | && self.ceiling.entries >= self.start.limits.entries |
| 121 | && self.ceiling.entries <= 200_000 |
| 122 | && (!self.interactive |
| 123 | || matches!( |
| 124 | self.start.action, |
| 125 | Action::Extract { .. } | Action::Preview { .. } |
| 126 | )) |
| 127 | && (!matches!(self.start.action, Action::Preview { .. }) || self.interactive) |
| 128 | } |
| 129 | } |
| 130 | #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] |
| 131 | pub struct ResourceScope { |
| 132 | pub output: bool, |
| 133 | pub entries: bool, |
| 134 | pub ratio: bool, |
| 135 | } |
| 136 | impl ResourceScope { |
| 137 | pub fn any(self) -> bool { |
| 138 | self.output || self.entries || self.ratio |
| 139 | } |
| 140 | } |
| 141 | #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] |
| 142 | pub struct ResourceUsage { |
| 143 | pub estimated_bytes: Option<u64>, |
| 144 | pub actual_bytes: Option<u64>, |
| 145 | pub estimated_entries: Option<u32>, |
| 146 | pub actual_entries: Option<u32>, |
| 147 | pub compressed_bytes: Option<u64>, |
| 148 | pub ratio_output_bytes: Option<u64>, |
| 149 | pub available_bytes: Option<u64>, |
| 150 | } |
| 151 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 152 | pub enum Format { |
| 153 | Zip, |
| 154 | Tar, |
| 155 | Gzip, |
| 156 | TarGzip, |
| 157 | SevenZip, |
| 158 | } |
| 159 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 160 | pub enum SourceObject { |
| 161 | Unix { |
| 162 | device: u64, |
| 163 | inode: u64, |
| 164 | }, |
| 165 | Windows { |
| 166 | volume_serial: u64, |
| 167 | file_id: [u8; 16], |
| 168 | }, |
| 169 | SplitSevenZip { |
| 170 | manifest_sha256: [u8; 32], |
| 171 | }, |
| 172 | } |
| 173 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 174 | pub struct ListId { |
| 175 | pub catalog_version: u16, |
| 176 | pub format: Format, |
| 177 | pub source_object: SourceObject, |
| 178 | pub archive_bytes: u64, |
| 179 | pub archive_sha256: [u8; 32], |
| 180 | } |
| 181 | impl ListId { |
| 182 | pub fn valid(&self) -> bool { |
| 183 | matches!( |
| 184 | (self.catalog_version, self.format, &self.source_object), |
| 185 | ( |
| 186 | 1, |
| 187 | _, |
| 188 | SourceObject::Unix { .. } | SourceObject::Windows { .. } |
| 189 | ) | (2, Format::SevenZip, SourceObject::SplitSevenZip { .. }) |
| 190 | ) |
| 191 | } |
| 192 | } |
| 193 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 194 | pub enum Selection { |
| 195 | All, |
| 196 | EntryIds(Vec<u32>), |
| 197 | } |
| 198 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 199 | pub enum Method { |
| 200 | Store, |
| 201 | Deflate, |
| 202 | } |
| 203 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 204 | pub enum SevenZipMethod { |
| 205 | Lzma2, |
| 206 | } |
| 207 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 208 | pub enum Encryption { |
| 209 | None, |
| 210 | Aes256, |
| 211 | } |
| 212 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 213 | pub struct Source { |
| 214 | pub path: NativePath, |
| 215 | pub archive_name: String, |
| 216 | } |
| 217 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 218 | pub enum Action { |
| 219 | List { |
| 220 | archive: NativePath, |
| 221 | }, |
| 222 | Extract { |
| 223 | archive: NativePath, |
| 224 | destination: NativePath, |
| 225 | selection: Selection, |
| 226 | list_id: Option<ListId>, |
| 227 | gzip_output_name: Option<String>, |
| 228 | }, |
| 229 | CreateZip { |
| 230 | sources: Vec<Source>, |
| 231 | output_file: NativePath, |
| 232 | method: Method, |
| 233 | encryption: Encryption, |
| 234 | }, |
| 235 | Preview { |
| 236 | archive: NativePath, |
| 237 | entry_id: u32, |
| 238 | list_id: ListId, |
| 239 | }, |
| 240 | RecoverZip { |
| 241 | archive: NativePath, |
| 242 | output_file: NativePath, |
| 243 | }, |
| 244 | CreateSevenZip { |
| 245 | sources: Vec<Source>, |
| 246 | output_file: NativePath, |
| 247 | method: SevenZipMethod, |
| 248 | solid: bool, |
| 249 | encryption: Encryption, |
| 250 | }, |
| 251 | } |
| 252 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 253 | pub enum ActionKind { |
| 254 | List, |
| 255 | Extract, |
| 256 | CreateZip, |
| 257 | Preview, |
| 258 | CreateSevenZip, |
| 259 | RecoverZip, |
| 260 | } |
| 261 | impl Action { |
| 262 | pub fn kind(&self) -> ActionKind { |
| 263 | match self { |
| 264 | Self::List { .. } => ActionKind::List, |
| 265 | Self::Extract { .. } => ActionKind::Extract, |
| 266 | Self::CreateZip { .. } => ActionKind::CreateZip, |
| 267 | Self::Preview { .. } => ActionKind::Preview, |
| 268 | Self::CreateSevenZip { .. } => ActionKind::CreateSevenZip, |
| 269 | Self::RecoverZip { .. } => ActionKind::RecoverZip, |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 274 | pub struct Start { |
| 275 | pub action: Action, |
| 276 | pub limits: Limits, |
| 277 | } |
| 278 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 279 | pub enum EntryKind { |
| 280 | File, |
| 281 | Directory, |
| 282 | Link, |
| 283 | Other, |
| 284 | } |
| 285 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 286 | pub struct Entry { |
| 287 | pub entry_id: u32, |
| 288 | pub name: String, |
| 289 | pub kind: EntryKind, |
| 290 | pub size: u64, |
| 291 | pub compressed_size: u64, |
| 292 | pub encrypted: bool, |
| 293 | } |
| 294 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 295 | pub enum PreviewFormat { |
| 296 | Utf8Text, |
| 297 | Rgba8, |
| 298 | } |
| 299 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 300 | pub struct PreviewInfo { |
| 301 | pub entry_id: u32, |
| 302 | pub format: PreviewFormat, |
| 303 | pub total_bytes: u64, |
| 304 | pub width: u32, |
| 305 | pub height: u32, |
| 306 | } |
| 307 | impl PreviewInfo { |
| 308 | pub fn valid(&self) -> bool { |
| 309 | match self.format { |
| 310 | PreviewFormat::Utf8Text => { |
| 311 | self.width == 0 && self.height == 0 && self.total_bytes <= MAX_PREVIEW_TEXT_BYTES |
| 312 | } |
| 313 | PreviewFormat::Rgba8 => { |
| 314 | if !(1..=MAX_PREVIEW_DIMENSION).contains(&self.width) |
| 315 | || !(1..=MAX_PREVIEW_DIMENSION).contains(&self.height) |
| 316 | { |
| 317 | return false; |
| 318 | } |
| 319 | let pixels = u64::from(self.width) * u64::from(self.height); |
| 320 | pixels <= MAX_PREVIEW_PIXELS |
| 321 | && pixels.checked_mul(4) == Some(self.total_bytes) |
| 322 | && self.total_bytes <= MAX_PREVIEW_RGBA_BYTES |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 328 | pub enum Stage { |
| 329 | Scan, |
| 330 | Read, |
| 331 | Decode, |
| 332 | Write, |
| 333 | Verify, |
| 334 | Commit, |
| 335 | } |
| 336 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 337 | pub enum Disposition { |
| 338 | Created, |
| 339 | PartialRetained, |
| 340 | Removed, |
| 341 | CommitUncertain, |
| 342 | } |
| 343 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 344 | pub enum Outcome { |
| 345 | Completed, |
| 346 | Failed, |
| 347 | Cancelled, |
| 348 | } |
| 349 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 350 | pub enum ErrorCode { |
| 351 | None, |
| 352 | Integrity, |
| 353 | UnsupportedFormat, |
| 354 | UnsupportedFeature, |
| 355 | WrongPassword, |
| 356 | UnsafePath, |
| 357 | Conflict, |
| 358 | Limit, |
| 359 | Io, |
| 360 | Protocol, |
| 361 | Internal, |
| 362 | SourceChanged, |
| 363 | InvalidRequest, |
| 364 | DiskFull, |
| 365 | MissingVolume, |
| 366 | RecoveryIncomplete, |
| 367 | RecoveryImpossible, |
| 368 | } |
| 369 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 370 | pub enum VolumeProblemReason { |
| 371 | FirstVolumeRequired, |
| 372 | MissingVolume, |
| 373 | IncompleteSet, |
| 374 | } |
| 375 | impl VolumeProblemReason { |
| 376 | pub fn terminal_error(self) -> ErrorCode { |
| 377 | match self { |
| 378 | Self::FirstVolumeRequired => ErrorCode::InvalidRequest, |
| 379 | Self::MissingVolume => ErrorCode::MissingVolume, |
| 380 | Self::IncompleteSet => ErrorCode::Integrity, |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 385 | pub enum Publication { |
| 386 | None, |
| 387 | Partial, |
| 388 | Complete, |
| 389 | Uncertain, |
| 390 | } |
| 391 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 392 | pub enum Durability { |
| 393 | NotApplicable, |
| 394 | Synced, |
| 395 | Unconfirmed, |
| 396 | Failed, |
| 397 | } |
| 398 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 399 | pub enum Cleanup { |
| 400 | Complete, |
| 401 | Failed, |
| 402 | Unconfirmed, |
| 403 | } |
| 404 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 405 | pub enum CancelStatus { |
| 406 | Requested, |
| 407 | AlreadyRequested, |
| 408 | TooLate, |
| 409 | Finished, |
| 410 | } |
| 411 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 412 | pub enum PasswordPurpose { |
| 413 | Decrypt, |
| 414 | Encrypt, |
| 415 | } |
| 416 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 417 | pub enum ConflictChoice { |
| 418 | Overwrite, |
| 419 | Skip, |
| 420 | Rename, |
| 421 | } |
| 422 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 423 | pub struct Terminal { |
| 424 | pub outcome: Outcome, |
| 425 | pub error: ErrorCode, |
| 426 | pub publication: Publication, |
| 427 | pub created_count: u64, |
| 428 | pub retained_partial_count: u64, |
| 429 | pub durability: Durability, |
| 430 | pub cleanup: Cleanup, |
| 431 | pub detail: String, |
| 432 | } |
| 433 | pub struct Secret(pub Vec<u8>); |
| 434 | impl std::fmt::Debug for Secret { |
| 435 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 436 | f.write_str("[redacted]") |
| 437 | } |
| 438 | } |
| 439 | impl Drop for Secret { |
| 440 | fn drop(&mut self) { |
| 441 | zeroize::Zeroize::zeroize(&mut self.0); |
| 442 | } |
| 443 | } |
| 444 | #[derive(Debug)] |
| 445 | pub enum Message { |
| 446 | Start(Start), |
| 447 | Password { |
| 448 | challenge_id: u64, |
| 449 | secret: Secret, |
| 450 | }, |
| 451 | Cancel, |
| 452 | StartInteractive(Start), |
| 453 | StartGuarded(GuardedStart), |
| 454 | ResourceReply { |
| 455 | challenge_id: u64, |
| 456 | scope: ResourceScope, |
| 457 | allow: bool, |
| 458 | }, |
| 459 | ResourceRequired { |
| 460 | challenge_id: u64, |
| 461 | action: ActionKind, |
| 462 | scope: ResourceScope, |
| 463 | usage: ResourceUsage, |
| 464 | soft_limits: Limits, |
| 465 | ceiling: Limits, |
| 466 | }, |
| 467 | ConflictReply { |
| 468 | challenge_id: u64, |
| 469 | choice: ConflictChoice, |
| 470 | apply_to_remaining: bool, |
| 471 | }, |
| 472 | Accepted { |
| 473 | action: ActionKind, |
| 474 | encryption: bool, |
| 475 | }, |
| 476 | Entry(Entry), |
| 477 | ListEnd { |
| 478 | entry_count: u32, |
| 479 | list_id: ListId, |
| 480 | }, |
| 481 | PasswordRequired { |
| 482 | challenge_id: u64, |
| 483 | attempt: u32, |
| 484 | purpose: PasswordPurpose, |
| 485 | }, |
| 486 | Progress { |
| 487 | stage: Stage, |
| 488 | processed_bytes: u64, |
| 489 | total_bytes: Option<u64>, |
| 490 | completed_entries: u32, |
| 491 | }, |
| 492 | Output { |
| 493 | entry_id: Option<u32>, |
| 494 | relative_name: String, |
| 495 | disposition: Disposition, |
| 496 | }, |
| 497 | PreviewBegin(PreviewInfo), |
| 498 | PreviewChunk { |
| 499 | entry_id: u32, |
| 500 | offset: u64, |
| 501 | data: Vec<u8>, |
| 502 | }, |
| 503 | PreviewEnd { |
| 504 | entry_id: u32, |
| 505 | total_bytes: u64, |
| 506 | sha256: [u8; 32], |
| 507 | }, |
| 508 | VolumeProblem { |
| 509 | reason: VolumeProblemReason, |
| 510 | volume: NativePath, |
| 511 | }, |
| 512 | RecoveryEntry(RecoveryEntry), |
| 513 | RecoveryEnd(RecoveryEnd), |
| 514 | Terminal(Terminal), |
| 515 | CancelAck { |
| 516 | request_sequence: u64, |
| 517 | status: CancelStatus, |
| 518 | }, |
| 519 | ConflictRequired { |
| 520 | challenge_id: u64, |
| 521 | entry_id: u32, |
| 522 | relative_name: String, |
| 523 | incoming_size: u64, |
| 524 | existing_size: u64, |
| 525 | }, |
| 526 | ConflictResolved { |
| 527 | entry_id: u32, |
| 528 | relative_name: String, |
| 529 | choice: ConflictChoice, |
| 530 | }, |
| 531 | } |
| 532 | |
| 533 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 534 | pub enum RecoveryMode { |
| 535 | CentralDirectory, |
| 536 | LocalChain, |
| 537 | } |
| 538 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 539 | pub enum RecoveryStatus { |
| 540 | Verified, |
| 541 | Lost, |
| 542 | Unverified, |
| 543 | } |
| 544 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 545 | pub enum RecoveryReason { |
| 546 | None, |
| 547 | HeaderRebuilt, |
| 548 | Truncated, |
| 549 | CrcMismatch, |
| 550 | DecodeFailure, |
| 551 | BoundaryUnknown, |
| 552 | UnsupportedFeature, |
| 553 | MetadataMissing, |
| 554 | } |
| 555 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 556 | pub struct RecoveryEntry { |
| 557 | pub candidate_id: u32, |
| 558 | pub source_offset: u64, |
| 559 | pub name: Option<String>, |
| 560 | pub kind: Option<EntryKind>, |
| 561 | pub status: RecoveryStatus, |
| 562 | pub reason: RecoveryReason, |
| 563 | pub declared_size: Option<u64>, |
| 564 | pub verified_size: Option<u64>, |
| 565 | pub sha256: Option<[u8; 32]>, |
| 566 | } |
| 567 | impl RecoveryEntry { |
| 568 | pub fn valid(&self) -> bool { |
| 569 | if self.name.as_ref().is_some_and(|name| name.is_empty()) { |
| 570 | return false; |
| 571 | } |
| 572 | if !matches!( |
| 573 | self.kind, |
| 574 | None | Some(EntryKind::File | EntryKind::Directory) |
| 575 | ) { |
| 576 | return false; |
| 577 | } |
| 578 | match self.status { |
| 579 | RecoveryStatus::Verified => { |
| 580 | if self.name.as_ref().is_none_or(|name| name.is_empty()) |
| 581 | || !matches!( |
| 582 | self.reason, |
| 583 | RecoveryReason::None |
| 584 | | RecoveryReason::HeaderRebuilt |
| 585 | | RecoveryReason::MetadataMissing |
| 586 | ) |
| 587 | || self.declared_size.is_none() |
| 588 | || self.declared_size != self.verified_size |
| 589 | { |
| 590 | return false; |
| 591 | } |
| 592 | match self.kind { |
| 593 | Some(EntryKind::File) => self.sha256.is_some(), |
| 594 | Some(EntryKind::Directory) => { |
| 595 | self.declared_size == Some(0) && self.sha256.is_none() |
| 596 | } |
| 597 | _ => false, |
| 598 | } |
| 599 | } |
| 600 | RecoveryStatus::Lost => { |
| 601 | self.verified_size.is_none() |
| 602 | && self.sha256.is_none() |
| 603 | && matches!( |
| 604 | self.reason, |
| 605 | RecoveryReason::Truncated |
| 606 | | RecoveryReason::CrcMismatch |
| 607 | | RecoveryReason::DecodeFailure |
| 608 | ) |
| 609 | } |
| 610 | RecoveryStatus::Unverified => { |
| 611 | self.verified_size.is_none() |
| 612 | && self.sha256.is_none() |
| 613 | && matches!( |
| 614 | self.reason, |
| 615 | RecoveryReason::BoundaryUnknown |
| 616 | | RecoveryReason::UnsupportedFeature |
| 617 | | RecoveryReason::MetadataMissing |
| 618 | ) |
| 619 | } |
| 620 | } |
| 621 | } |
| 622 | } |
| 623 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 624 | pub struct RecoveryEnd { |
| 625 | pub mode: RecoveryMode, |
| 626 | pub source_bytes: u64, |
| 627 | pub source_sha256: [u8; 32], |
| 628 | pub known_entries: Option<u32>, |
| 629 | pub verified_entries: u32, |
| 630 | pub lost_entries: u32, |
| 631 | pub unverified_entries: u32, |
| 632 | pub unknown_loss: bool, |
| 633 | } |
| 634 | impl RecoveryEnd { |
| 635 | pub fn valid(&self) -> bool { |
| 636 | let total = self |
| 637 | .verified_entries |
| 638 | .checked_add(self.lost_entries) |
| 639 | .and_then(|n| n.checked_add(self.unverified_entries)); |
| 640 | let Some(total) = total else { |
| 641 | return false; |
| 642 | }; |
| 643 | match (self.mode, self.known_entries, self.unknown_loss) { |
| 644 | (RecoveryMode::CentralDirectory, Some(known), false) => known == total, |
| 645 | (_, None, true) => true, |
| 646 | _ => false, |
| 647 | } |
| 648 | } |
| 649 | pub fn complete(&self) -> bool { |
| 650 | self.valid() && !self.unknown_loss && self.lost_entries == 0 && self.unverified_entries == 0 |
| 651 | } |
| 652 | } |