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