Merge pull request #135 from Team-LJY/fix/list-preview-no-copy
목록과 미리보기의 전체 원본 사본 제거
- 작성자
- LeeJunyoung
- 작성 시각
- 커밋
bd72bbfb8f0d50e0ef92380bae20b8570057b8f5
변경된 파일 9개
+1,095-46| @@ -40,6 +40,8 @@ struct Job<W> { | ||
| 40 | 40 | watchdog: Watchdog, |
| 41 | 41 | remaining_choice: Option<ConflictChoice>, |
| 42 | 42 | source_group: Option<crate::split_seven_zip::Group>, |
| 43 | 추가: direct_source: Option<crate::spool::DirectSource>, | |
| 44 | 추가: direct_split: Option<crate::split_seven_zip::Direct>, | |
| 43 | 45 | source_verified_for_publication: bool, |
| 44 | 46 | creation_plan: Option<crate::create::Plan>, |
| 45 | 47 | recovery: Option<crate::zip_recovery::Recovery>, |
| @@ -419,6 +421,87 @@ impl<W: Write> Job<W> { | ||
| 419 | 421 | } |
| 420 | 422 | Ok(()) |
| 421 | 423 | } |
| 424 | 추가: fn direct_snapshot( | |
| 425 | 추가: &mut self, | |
| 426 | 추가: path: &std::path::Path, | |
| 427 | 추가: limit: u64, | |
| 428 | 추가: ) -> Result<crate::spool::Snapshot> { | |
| 429 | 추가: let (snapshot, guard) = | |
| 430 | 추가: crate::spool::Snapshot::open_direct(path, limit, &mut |step| match step { | |
| 431 | 추가: crate::spool::Step::Check => self.check(), | |
| 432 | 추가: crate::spool::Step::Activity => completed(Ok(()), self), | |
| 433 | 추가: crate::spool::Step::Read { bytes, total } => { | |
| 434 | 추가: self.progress(Stage::Read, bytes, Some(total), 0) | |
| 435 | 추가: } | |
| 436 | 추가: })?; | |
| 437 | 추가: self.direct_source = Some(guard); | |
| 438 | 추가: Ok(snapshot) | |
| 439 | 추가: } | |
| 440 | 추가: fn verify_direct(&mut self) -> Result<()> { | |
| 441 | 추가: if let Some(mut source) = self.direct_source.take() { | |
| 442 | 추가: let result = source.verify(&mut |step| match step { | |
| 443 | 추가: crate::spool::Step::Check => self.check(), | |
| 444 | 추가: crate::spool::Step::Activity => completed(Ok(()), self), | |
| 445 | 추가: crate::spool::Step::Read { bytes, total } => { | |
| 446 | 추가: self.progress(Stage::Verify, bytes, Some(total), 0) | |
| 447 | 추가: } | |
| 448 | 추가: }); | |
| 449 | 추가: self.direct_source = Some(source); | |
| 450 | 추가: result?; | |
| 451 | 추가: } | |
| 452 | 추가: if let Some(source) = self.direct_split.take() { | |
| 453 | 추가: let result = source.verify(&mut |step| match step { | |
| 454 | 추가: crate::spool::Step::Check => self.check(), | |
| 455 | 추가: crate::spool::Step::Activity => completed(Ok(()), self), | |
| 456 | 추가: crate::spool::Step::Read { bytes, total } => { | |
| 457 | 추가: self.progress(Stage::Verify, bytes, Some(total), 0) | |
| 458 | 추가: } | |
| 459 | 추가: }); | |
| 460 | 추가: self.direct_split = Some(source); | |
| 461 | 추가: self.split_result(result)?; | |
| 462 | 추가: } | |
| 463 | 추가: Ok(()) | |
| 464 | 추가: } | |
| 465 | 추가: fn list_direct_split(&mut self, path: &std::path::Path, limits: Limits) -> Result<()> { | |
| 466 | 추가: let metadata_limits = self.metadata_limits(); | |
| 467 | 추가: let opened = | |
| 468 | 추가: crate::split_seven_zip::open_direct(path, limits.input, metadata_limits, &mut |step| { | |
| 469 | 추가: match step { | |
| 470 | 추가: crate::spool::Step::Check => self.check(), | |
| 471 | 추가: crate::spool::Step::Activity => completed(Ok(()), self), | |
| 472 | 추가: crate::spool::Step::Read { bytes, total } => { | |
| 473 | 추가: self.progress(Stage::Read, bytes, Some(total), 0) | |
| 474 | 추가: } | |
| 475 | 추가: } | |
| 476 | 추가: }); | |
| 477 | 추가: let mut source = self.split_result(opened)?.ok_or(ErrorCode::Internal)?; | |
| 478 | 추가: let length = source.len(); | |
| 479 | 추가: let object = self.split_result(source.object())?; | |
| 480 | 추가: let hash = source.hash; | |
| 481 | 추가: let result = crate::spool::retry_full(|| { | |
| 482 | 추가: crate::seven_zip::open(&mut source, length, limits, false, self) | |
| 483 | 추가: }); | |
| 484 | 추가: self.direct_split = Some(source); | |
| 485 | 추가: let catalog = result?; | |
| 486 | 추가: for member in &catalog.members { | |
| 487 | 추가: self.check()?; | |
| 488 | 추가: completed(self.event(Message::Entry(member.entry.clone())), self)?; | |
| 489 | 추가: } | |
| 490 | 추가: self.verify_direct()?; | |
| 491 | 추가: completed( | |
| 492 | 추가: self.event(Message::ListEnd { | |
| 493 | 추가: entry_count: catalog.members.len() as u32, | |
| 494 | 추가: list_id: ListId { | |
| 495 | 추가: catalog_version: 2, | |
| 496 | 추가: format: Format::SevenZip, | |
| 497 | 추가: source_object: object, | |
| 498 | 추가: archive_bytes: length, | |
| 499 | 추가: archive_sha256: hash, | |
| 500 | 추가: }, | |
| 501 | 추가: }), | |
| 502 | 추가: self, | |
| 503 | 추가: ) | |
| 504 | 추가: } | |
| 422 | 505 | fn preview( |
| 423 | 506 | &mut self, |
| 424 | 507 | archive: NativePath, |
| @@ -436,14 +519,7 @@ impl<W: Write> Job<W> { | ||
| 436 | 519 | return Err(ErrorCode::UnsupportedFeature); |
| 437 | 520 | } |
| 438 | 521 | self.progress(Stage::Read, 0, None, 0)?; |
| 439 | 삭제: let mut snapshot = | |
| 440 | 삭제: crate::spool::Snapshot::open_reporting(&path, limits.input, &mut |step| match step { | |
| 441 | 삭제: crate::spool::Step::Check => self.check(), | |
| 442 | 삭제: crate::spool::Step::Activity => completed(Ok(()), self), | |
| 443 | 삭제: crate::spool::Step::Read { bytes, total } => { | |
| 444 | 삭제: self.progress(Stage::Read, bytes, Some(total), 0) | |
| 445 | 삭제: } | |
| 446 | 삭제: })?; | |
| 522 | 추가: let mut snapshot = self.direct_snapshot(&path, limits.input)?; | |
| 447 | 523 | if expected.catalog_version != 1 |
| 448 | 524 | || expected.source_object != snapshot.object |
| 449 | 525 | || expected.archive_bytes != snapshot.len |
| @@ -535,6 +611,7 @@ impl<W: Write> Job<W> { | ||
| 535 | 611 | } |
| 536 | 612 | let (info, bytes) = crate::preview::decode(kind, entry_id, bytes, self)?; |
| 537 | 613 | self.check()?; |
| 614 | 추가: self.verify_direct()?; | |
| 538 | 615 | let hash: [u8; 32] = Sha256::digest(&bytes).into(); |
| 539 | 616 | completed(self.event(Message::PreviewBegin(info)), self)?; |
| 540 | 617 | for (index, chunk) in bytes.chunks(MAX_PREVIEW_CHUNK_BYTES).enumerate() { |
| @@ -675,6 +752,9 @@ impl<W: Write> Job<W> { | ||
| 675 | 752 | Action::Extract { list_id, .. } => list_id.as_ref(), |
| 676 | 753 | _ => None, |
| 677 | 754 | }; |
| 755 | 추가: if !extract && crate::split_seven_zip::is_split(&path) { | |
| 756 | 추가: return self.list_direct_split(&path, limits); | |
| 757 | 추가: } | |
| 678 | 758 | let metadata_limits = self.metadata_limits(); |
| 679 | 759 | let captured = crate::split_seven_zip::open( |
| 680 | 760 | &path, |
| @@ -692,6 +772,8 @@ impl<W: Write> Job<W> { | ||
| 692 | 772 | let mut snapshot = if let Some(captured) = self.split_result(captured)? { |
| 693 | 773 | self.source_group = Some(captured.group); |
| 694 | 774 | captured.snapshot |
| 775 | 추가: } else if !extract { | |
| 776 | 추가: self.direct_snapshot(&path, limits.input)? | |
| 695 | 777 | } else { |
| 696 | 778 | crate::spool::Snapshot::open_reporting(&path, limits.input, &mut |step| { |
| 697 | 779 | match step { |
| @@ -738,6 +820,7 @@ impl<W: Write> Job<W> { | ||
| 738 | 820 | completed(self.event(Message::Entry(member.entry.clone())), self)?; |
| 739 | 821 | } |
| 740 | 822 | self.verify_split()?; |
| 823 | 추가: self.verify_direct()?; | |
| 741 | 824 | completed( |
| 742 | 825 | self.event(Message::ListEnd { |
| 743 | 826 | entry_count: catalog.members.len() as u32, |
| @@ -1237,6 +1320,8 @@ pub fn serve<R: Read + Send + 'static, W: Write + Send + 'static>( | ||
| 1237 | 1320 | watchdog: Watchdog::new(), |
| 1238 | 1321 | remaining_choice: None, |
| 1239 | 1322 | source_group: None, |
| 1323 | 추가: direct_source: None, | |
| 1324 | 추가: direct_split: None, | |
| 1240 | 1325 | source_verified_for_publication: false, |
| 1241 | 1326 | creation_plan: None, |
| 1242 | 1327 | recovery: None, |
| @@ -1253,7 +1338,18 @@ pub fn serve<R: Read + Send + 'static, W: Write + Send + 'static>( | ||
| 1253 | 1338 | detail: String::new(), |
| 1254 | 1339 | }, |
| 1255 | 1340 | }; |
| 1256 | 삭제: let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| context.run(start))); | |
| 1341 | 추가: let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { | |
| 1342 | 추가: let result = context.run(start); | |
| 1343 | 추가: if result.is_err() | |
| 1344 | 추가: && !matches!( | |
| 1345 | 추가: result, | |
| 1346 | 추가: Err(ErrorCode::None | ErrorCode::Limit | ErrorCode::Protocol | ErrorCode::Internal) | |
| 1347 | 추가: ) | |
| 1348 | 추가: { | |
| 1349 | 추가: context.verify_direct()?; | |
| 1350 | 추가: } | |
| 1351 | 추가: result | |
| 1352 | 추가: })); | |
| 1257 | 1353 | let error = match result { |
| 1258 | 1354 | Ok(r) => r.err(), |
| 1259 | 1355 | Err(_) => { |
| @@ -276,7 +276,7 @@ impl Streams { | ||
| 276 | 276 | } |
| 277 | 277 | fn ranges( |
| 278 | 278 | &self, |
| 279 | 삭제: input: &mut std::fs::File, | |
| 279 | 추가: input: &mut (impl std::io::Read + std::io::Seek), | |
| 280 | 280 | data_end: u64, |
| 281 | 281 | check: &mut impl crate::resources::Monitor, |
| 282 | 282 | ) -> Result<Vec<(u64, u64)>> { |
| @@ -303,13 +303,13 @@ impl Streams { | ||
| 303 | 303 | } |
| 304 | 304 | fn decode_file( |
| 305 | 305 | &self, |
| 306 | 삭제: input: &mut std::fs::File, | |
| 306 | 추가: input: &mut (impl std::io::Read + std::io::Seek), | |
| 307 | 307 | ranges: &[(u64, u64)], |
| 308 | 308 | output: &mut std::fs::File, |
| 309 | 309 | selected: Option<usize>, |
| 310 | 310 | check: &mut impl crate::resources::Monitor, |
| 311 | 311 | ) -> Result<()> { |
| 312 | 삭제: use std::io::{Read, Seek, SeekFrom, Write}; | |
| 312 | 추가: use std::io::{Seek, SeekFrom, Write}; | |
| 313 | 313 | let total = self |
| 314 | 314 | .folders |
| 315 | 315 | .iter() |
| @@ -407,12 +407,12 @@ impl Streams { | ||
| 407 | 407 | } |
| 408 | 408 | } |
| 409 | 409 | fn digest( |
| 410 | 삭제: file: &mut std::fs::File, | |
| 410 | 추가: file: &mut (impl std::io::Read + std::io::Seek), | |
| 411 | 411 | start: u64, |
| 412 | 412 | size: u64, |
| 413 | 413 | check: &mut impl crate::resources::Monitor, |
| 414 | 414 | ) -> Result<u32> { |
| 415 | 삭제: use std::io::{Read, Seek, SeekFrom}; | |
| 415 | 추가: use std::io::SeekFrom; | |
| 416 | 416 | file.seek(SeekFrom::Start(start)) |
| 417 | 417 | .map_err(crate::space::io_error)?; |
| 418 | 418 | let mut crc = 0; |
| @@ -589,7 +589,7 @@ fn files( | ||
| 589 | 589 | Ok((members, stream_indices)) |
| 590 | 590 | } |
| 591 | 591 | pub(crate) fn open( |
| 592 | 삭제: input: &mut std::fs::File, | |
| 592 | 추가: input: &mut (impl std::io::Read + std::io::Seek), | |
| 593 | 593 | length: u64, |
| 594 | 594 | limits: Limits, |
| 595 | 595 | decode: bool, |
| @@ -598,7 +598,7 @@ pub(crate) fn open( | ||
| 598 | 598 | open_selected(input, length, limits, decode, None, check) |
| 599 | 599 | } |
| 600 | 600 | pub(crate) fn open_selected( |
| 601 | 삭제: input: &mut std::fs::File, | |
| 601 | 추가: input: &mut (impl std::io::Read + std::io::Seek), | |
| 602 | 602 | length: u64, |
| 603 | 603 | limits: Limits, |
| 604 | 604 | decode: bool, |
| @@ -448,13 +448,13 @@ impl Group { | ||
| 448 | 448 | Ok(()) |
| 449 | 449 | } |
| 450 | 450 | } |
| 451 | 삭제: pub(crate) fn open( | |
| 451 | 추가: fn discover( | |
| 452 | 452 | path: &Path, |
| 453 | 453 | expected: Option<&ListId>, |
| 454 | 454 | limit: u64, |
| 455 | 455 | metadata_limits: crate::archive::MetadataLimits, |
| 456 | 456 | step: &mut impl FnMut(Step) -> std::result::Result<(), ErrorCode>, |
| 457 | 삭제: ) -> Result<Option<Captured>> { | |
| 457 | 추가: ) -> Result<Option<Group>> { | |
| 458 | 458 | if unsupported_split(path) { |
| 459 | 459 | return Err(ErrorCode::UnsupportedFeature.into()); |
| 460 | 460 | } |
| @@ -539,6 +539,18 @@ pub(crate) fn open( | ||
| 539 | 539 | if group.total > limit { |
| 540 | 540 | return Err(ErrorCode::Limit.into()); |
| 541 | 541 | } |
| 542 | 추가: Ok(Some(group)) | |
| 543 | 추가: } | |
| 544 | 추가: pub(crate) fn open( | |
| 545 | 추가: path: &Path, | |
| 546 | 추가: expected: Option<&ListId>, | |
| 547 | 추가: limit: u64, | |
| 548 | 추가: metadata_limits: crate::archive::MetadataLimits, | |
| 549 | 추가: step: &mut impl FnMut(Step) -> std::result::Result<(), ErrorCode>, | |
| 550 | 추가: ) -> Result<Option<Captured>> { | |
| 551 | 추가: let Some(mut group) = discover(path, expected, limit, metadata_limits, step)? else { | |
| 552 | 추가: return Ok(None); | |
| 553 | 추가: }; | |
| 542 | 554 | let (mut file, hash) = loop { |
| 543 | 555 | match group.capture(step) { |
| 544 | 556 | Err(failure) |
| @@ -591,6 +603,187 @@ pub(crate) fn open( | ||
| 591 | 603 | })) |
| 592 | 604 | } |
| 593 | 605 | |
| 606 | 추가: pub(crate) struct Direct { | |
| 607 | 추가: group: Group, | |
| 608 | 추가: changes: Vec<Option<(i64, i64)>>, | |
| 609 | 추가: position: u64, | |
| 610 | 추가: pub hash: [u8; 32], | |
| 611 | 추가: } | |
| 612 | 추가: impl Direct { | |
| 613 | 추가: pub fn len(&self) -> u64 { | |
| 614 | 추가: self.group.total | |
| 615 | 추가: } | |
| 616 | 추가: pub fn object(&self) -> Result<SourceObject> { | |
| 617 | 추가: Ok(SourceObject::SplitSevenZip { | |
| 618 | 추가: manifest_sha256: self.group.manifest()?, | |
| 619 | 추가: }) | |
| 620 | 추가: } | |
| 621 | 추가: fn matches(&self, index: usize, input: &Input) -> Result<()> { | |
| 622 | 추가: let part = &self.group.parts[index]; | |
| 623 | 추가: let data = input.file.metadata().map_err(crate::space::io_error)?; | |
| 624 | 추가: if !data.is_file() | |
| 625 | 추가: || filesystem::object(&input.file)? != part.object | |
| 626 | 추가: || data.len() != part.length | |
| 627 | 추가: || data.modified().map_err(crate::space::io_error)? != part.modified | |
| 628 | 추가: || crate::spool::change_stamp(&data) != self.changes[index] | |
| 629 | 추가: { | |
| 630 | 추가: return Err(ErrorCode::SourceChanged.into()); | |
| 631 | 추가: } | |
| 632 | 추가: Ok(()) | |
| 633 | 추가: } | |
| 634 | 추가: fn open_checked( | |
| 635 | 추가: &self, | |
| 636 | 추가: index: usize, | |
| 637 | 추가: step: &mut impl FnMut(Step) -> std::result::Result<(), ErrorCode>, | |
| 638 | 추가: ) -> Result<Input> { | |
| 639 | 추가: let input = self | |
| 640 | 추가: .group | |
| 641 | 추가: .open_part(&self.group.parts[index].name, step) | |
| 642 | 추가: .map_err(|failure| { | |
| 643 | 추가: if failure.error == ErrorCode::UnsafePath { | |
| 644 | 추가: ErrorCode::SourceChanged.into() | |
| 645 | 추가: } else { | |
| 646 | 추가: failure | |
| 647 | 추가: } | |
| 648 | 추가: })?; | |
| 649 | 추가: self.matches(index, &input)?; | |
| 650 | 추가: Ok(input) | |
| 651 | 추가: } | |
| 652 | 추가: fn unchanged( | |
| 653 | 추가: &self, | |
| 654 | 추가: step: &mut impl FnMut(Step) -> std::result::Result<(), ErrorCode>, | |
| 655 | 추가: ) -> Result<()> { | |
| 656 | 추가: for index in 0..self.group.parts.len() { | |
| 657 | 추가: step(Step::Check)?; | |
| 658 | 추가: self.open_checked(index, step)?; | |
| 659 | 추가: } | |
| 660 | 추가: Ok(()) | |
| 661 | 추가: } | |
| 662 | 추가: pub fn verify( | |
| 663 | 추가: &self, | |
| 664 | 추가: step: &mut impl FnMut(Step) -> std::result::Result<(), ErrorCode>, | |
| 665 | 추가: ) -> Result<()> { | |
| 666 | 추가: self.unchanged(step)?; | |
| 667 | 추가: self.group.verify(step)?; | |
| 668 | 추가: self.unchanged(step) | |
| 669 | 추가: } | |
| 670 | 추가: } | |
| 671 | 추가: impl Read for Direct { | |
| 672 | 추가: fn read(&mut self, output: &mut [u8]) -> std::io::Result<usize> { | |
| 673 | 추가: let mut start = 0; | |
| 674 | 추가: if output.is_empty() { | |
| 675 | 추가: return Ok(0); | |
| 676 | 추가: } | |
| 677 | 추가: let source_error = |failure: Failure| { | |
| 678 | 추가: std::io::Error::other(format!("분할 원본 읽기 실패: {:?}", failure.error)) | |
| 679 | 추가: }; | |
| 680 | 추가: for (index, part) in self.group.parts.iter().enumerate() { | |
| 681 | 추가: let end = start + part.length; | |
| 682 | 추가: if self.position < end { | |
| 683 | 추가: let count = (end - self.position).min(output.len() as u64) as usize; | |
| 684 | 추가: // 필요한 조각만 열어 핸들 사용량이 조각 수에 비례하지 않게 함. | |
| 685 | 추가: let mut input = self | |
| 686 | 추가: .open_checked(index, &mut |_| Ok(())) | |
| 687 | 추가: .map_err(source_error)?; | |
| 688 | 추가: input | |
| 689 | 추가: .file | |
| 690 | 추가: .seek(std::io::SeekFrom::Start(self.position - start))?; | |
| 691 | 추가: let read = input.file.read(&mut output[..count]); | |
| 692 | 추가: self.matches(index, &input).map_err(source_error)?; | |
| 693 | 추가: let count = read?; | |
| 694 | 추가: if count == 0 { | |
| 695 | 추가: return Err(std::io::Error::new( | |
| 696 | 추가: std::io::ErrorKind::UnexpectedEof, | |
| 697 | 추가: "분할 원본이 변경됨", | |
| 698 | 추가: )); | |
| 699 | 추가: } | |
| 700 | 추가: self.position += count as u64; | |
| 701 | 추가: return Ok(count); | |
| 702 | 추가: } | |
| 703 | 추가: start = end; | |
| 704 | 추가: } | |
| 705 | 추가: Ok(0) | |
| 706 | 추가: } | |
| 707 | 추가: } | |
| 708 | 추가: impl Seek for Direct { | |
| 709 | 추가: fn seek(&mut self, position: std::io::SeekFrom) -> std::io::Result<u64> { | |
| 710 | 추가: let next = match position { | |
| 711 | 추가: std::io::SeekFrom::Start(n) => i128::from(n), | |
| 712 | 추가: std::io::SeekFrom::Current(n) => i128::from(self.position) + i128::from(n), | |
| 713 | 추가: std::io::SeekFrom::End(n) => i128::from(self.group.total) + i128::from(n), | |
| 714 | 추가: }; | |
| 715 | 추가: self.position = u64::try_from(next).map_err(|_| { | |
| 716 | 추가: std::io::Error::new(std::io::ErrorKind::InvalidInput, "잘못된 읽기 위치") | |
| 717 | 추가: })?; | |
| 718 | 추가: Ok(self.position) | |
| 719 | 추가: } | |
| 720 | 추가: } | |
| 721 | 추가: pub(crate) fn open_direct( | |
| 722 | 추가: path: &Path, | |
| 723 | 추가: limit: u64, | |
| 724 | 추가: metadata_limits: crate::archive::MetadataLimits, | |
| 725 | 추가: step: &mut impl FnMut(Step) -> std::result::Result<(), ErrorCode>, | |
| 726 | 추가: ) -> Result<Option<Direct>> { | |
| 727 | 추가: let Some(mut group) = discover(path, None, limit, metadata_limits, step)? else { | |
| 728 | 추가: return Ok(None); | |
| 729 | 추가: }; | |
| 730 | 추가: let mut changes = Vec::new(); | |
| 731 | 추가: for part in &group.parts { | |
| 732 | 추가: let input = group.open_part(&part.name, step)?; | |
| 733 | 추가: group.matches(part, &input)?; | |
| 734 | 추가: changes.push(crate::spool::change_stamp(&metadata(&input.file)?)); | |
| 735 | 추가: } | |
| 736 | 추가: let mut whole = Sha256::new(); | |
| 737 | 추가: let mut processed = 0; | |
| 738 | 추가: let mut reported = Instant::now(); | |
| 739 | 추가: for index in 0..group.parts.len() { | |
| 740 | 추가: group.parts[index].hash = group.read_part( | |
| 741 | 추가: &group.parts[index], | |
| 742 | 추가: &mut None, | |
| 743 | 추가: &mut whole, | |
| 744 | 추가: &mut processed, | |
| 745 | 추가: &mut reported, | |
| 746 | 추가: step, | |
| 747 | 추가: )?; | |
| 748 | 추가: } | |
| 749 | 추가: step(Step::Read { | |
| 750 | 추가: bytes: processed, | |
| 751 | 추가: total: group.total, | |
| 752 | 추가: })?; | |
| 753 | 추가: let mut direct = Direct { | |
| 754 | 추가: group, | |
| 755 | 추가: changes, | |
| 756 | 추가: position: 0, | |
| 757 | 추가: hash: whole.finalize().into(), | |
| 758 | 추가: }; | |
| 759 | 추가: let mut header = [0; 32]; | |
| 760 | 추가: if direct.group.parts.iter().any(|part| part.length == 0) || direct.len() < 32 { | |
| 761 | 추가: return Err(direct.group.name.incomplete()); | |
| 762 | 추가: } | |
| 763 | 추가: let header_read = direct | |
| 764 | 추가: .read_exact(&mut header) | |
| 765 | 추가: .map_err(crate::space::io_error); | |
| 766 | 추가: direct.verify(step)?; | |
| 767 | 추가: header_read?; | |
| 768 | 추가: if &header[..6] != b"7z\xbc\xaf\x27\x1c" | |
| 769 | 추가: || zlib_rs::crc32::crc32(0, &header[12..]) | |
| 770 | 추가: != u32::from_le_bytes(header[8..12].try_into().unwrap()) | |
| 771 | 추가: { | |
| 772 | 추가: return Err(direct.group.name.incomplete()); | |
| 773 | 추가: } | |
| 774 | 추가: let offset = u64::from_le_bytes(header[12..20].try_into().unwrap()); | |
| 775 | 추가: let length = u64::from_le_bytes(header[20..28].try_into().unwrap()); | |
| 776 | 추가: if 32u64 | |
| 777 | 추가: .checked_add(offset) | |
| 778 | 추가: .and_then(|n| n.checked_add(length)) | |
| 779 | 추가: != Some(direct.len()) | |
| 780 | 추가: { | |
| 781 | 추가: return Err(direct.group.name.incomplete()); | |
| 782 | 추가: } | |
| 783 | 추가: direct.rewind().map_err(crate::space::io_error)?; | |
| 784 | 추가: Ok(Some(direct)) | |
| 785 | 추가: } | |
| 786 | 추가: | |
| 594 | 787 | #[cfg(test)] |
| 595 | 788 | #[path = "../../../tests/rust-archive-worker/split_capture.rs"] |
| 596 | 789 | mod tests; |
| @@ -271,7 +271,146 @@ pub(crate) struct Snapshot { | ||
| 271 | 271 | pub object: SourceObject, |
| 272 | 272 | pub hash: [u8; 32], |
| 273 | 273 | } |
| 274 | 추가: pub(crate) struct DirectSource { | |
| 275 | 추가: file: File, | |
| 276 | 추가: path: std::path::PathBuf, | |
| 277 | 추가: object: SourceObject, | |
| 278 | 추가: len: u64, | |
| 279 | 추가: modified: std::time::SystemTime, | |
| 280 | 추가: changed: Option<(i64, i64)>, | |
| 281 | 추가: hash: [u8; 32], | |
| 282 | 추가: } | |
| 283 | 추가: pub(crate) fn change_stamp(metadata: &std::fs::Metadata) -> Option<(i64, i64)> { | |
| 284 | 추가: #[cfg(unix)] | |
| 285 | 추가: { | |
| 286 | 추가: use std::os::unix::fs::MetadataExt; | |
| 287 | 추가: Some((metadata.ctime(), metadata.ctime_nsec())) | |
| 288 | 추가: } | |
| 289 | 추가: #[cfg(windows)] | |
| 290 | 추가: { | |
| 291 | 추가: let _ = metadata; | |
| 292 | 추가: None | |
| 293 | 추가: } | |
| 294 | 추가: } | |
| 295 | 추가: fn source_options() -> OpenOptions { | |
| 296 | 추가: let mut options = OpenOptions::new(); | |
| 297 | 추가: options.read(true); | |
| 298 | 추가: #[cfg(unix)] | |
| 299 | 추가: { | |
| 300 | 추가: use std::os::unix::fs::OpenOptionsExt; | |
| 301 | 추가: options.custom_flags(0x20000 | 0x800); | |
| 302 | 추가: } | |
| 303 | 추가: #[cfg(windows)] | |
| 304 | 추가: { | |
| 305 | 추가: use std::os::windows::fs::OpenOptionsExt; | |
| 306 | 추가: options.share_mode(1); | |
| 307 | 추가: } | |
| 308 | 추가: options | |
| 309 | 추가: } | |
| 310 | 추가: impl DirectSource { | |
| 311 | 추가: fn unchanged(&self) -> Result<()> { | |
| 312 | 추가: let before = self.file.metadata().map_err(|_| ErrorCode::SourceChanged)?; | |
| 313 | 추가: let named = source_options() | |
| 314 | 추가: .open(&self.path) | |
| 315 | 추가: .map_err(|_| ErrorCode::SourceChanged)?; | |
| 316 | 추가: let after = named.metadata().map_err(|_| ErrorCode::SourceChanged)?; | |
| 317 | 추가: for (file, metadata) in [(&self.file, before), (&named, after)] { | |
| 318 | 추가: if !metadata.is_file() | |
| 319 | 추가: || filesystem::object(file)? != self.object | |
| 320 | 추가: || metadata.len() != self.len | |
| 321 | 추가: || metadata.modified().map_err(|_| ErrorCode::SourceChanged)? != self.modified | |
| 322 | 추가: || change_stamp(&metadata) != self.changed | |
| 323 | 추가: { | |
| 324 | 추가: return Err(ErrorCode::SourceChanged); | |
| 325 | 추가: } | |
| 326 | 추가: } | |
| 327 | 추가: Ok(()) | |
| 328 | 추가: } | |
| 329 | 추가: pub(crate) fn verify(&mut self, step: &mut impl FnMut(Step) -> Result<()>) -> Result<()> { | |
| 330 | 추가: self.unchanged()?; | |
| 331 | 추가: let hash = hash_source(&mut self.file, self.len, step)?; | |
| 332 | 추가: self.unchanged()?; | |
| 333 | 추가: if hash != self.hash { | |
| 334 | 추가: return Err(ErrorCode::SourceChanged); | |
| 335 | 추가: } | |
| 336 | 추가: Ok(()) | |
| 337 | 추가: } | |
| 338 | 추가: } | |
| 339 | 추가: fn hash_source( | |
| 340 | 추가: file: &mut File, | |
| 341 | 추가: length: u64, | |
| 342 | 추가: step: &mut impl FnMut(Step) -> Result<()>, | |
| 343 | 추가: ) -> Result<[u8; 32]> { | |
| 344 | 추가: file.rewind().map_err(|_| ErrorCode::Io)?; | |
| 345 | 추가: let mut hash = Sha256::new(); | |
| 346 | 추가: let mut bytes = 0u64; | |
| 347 | 추가: let mut buffer = zeroize::Zeroizing::new([0; 65536]); | |
| 348 | 추가: let mut reported = std::time::Instant::now(); | |
| 349 | 추가: loop { | |
| 350 | 추가: step(Step::Check)?; | |
| 351 | 추가: let count = completed(file.read(&mut buffer[..]).map_err(|_| ErrorCode::Io), step)?; | |
| 352 | 추가: if count == 0 { | |
| 353 | 추가: break; | |
| 354 | 추가: } | |
| 355 | 추가: bytes = bytes | |
| 356 | 추가: .checked_add(count as u64) | |
| 357 | 추가: .filter(|n| *n <= length) | |
| 358 | 추가: .ok_or(ErrorCode::SourceChanged)?; | |
| 359 | 추가: hash.update(&buffer[..count]); | |
| 360 | 추가: if reported.elapsed() >= std::time::Duration::from_millis(200) { | |
| 361 | 추가: step(Step::Read { | |
| 362 | 추가: bytes, | |
| 363 | 추가: total: length, | |
| 364 | 추가: })?; | |
| 365 | 추가: reported = std::time::Instant::now(); | |
| 366 | 추가: } | |
| 367 | 추가: } | |
| 368 | 추가: if bytes != length { | |
| 369 | 추가: return Err(ErrorCode::SourceChanged); | |
| 370 | 추가: } | |
| 371 | 추가: step(Step::Read { | |
| 372 | 추가: bytes, | |
| 373 | 추가: total: length, | |
| 374 | 추가: })?; | |
| 375 | 추가: file.rewind().map_err(|_| ErrorCode::Io)?; | |
| 376 | 추가: Ok(hash.finalize().into()) | |
| 377 | 추가: } | |
| 274 | 378 | impl Snapshot { |
| 379 | 추가: /// 열린 원본을 직접 읽고 작업 종료까지 같은 객체의 핸들을 유지함. | |
| 380 | 추가: pub(crate) fn open_direct( | |
| 381 | 추가: path: &Path, | |
| 382 | 추가: limit: u64, | |
| 383 | 추가: step: &mut impl FnMut(Step) -> Result<()>, | |
| 384 | 추가: ) -> Result<(Self, DirectSource)> { | |
| 385 | 추가: let mut file = completed(source_options().open(path).map_err(|_| ErrorCode::Io), step)?; | |
| 386 | 추가: let metadata = file.metadata().map_err(|_| ErrorCode::Io)?; | |
| 387 | 추가: if !metadata.is_file() { | |
| 388 | 추가: return Err(ErrorCode::InvalidRequest); | |
| 389 | 추가: } | |
| 390 | 추가: if metadata.len() > limit { | |
| 391 | 추가: return Err(ErrorCode::Limit); | |
| 392 | 추가: } | |
| 393 | 추가: let mut guard = DirectSource { | |
| 394 | 추가: file: file.try_clone().map_err(|_| ErrorCode::Io)?, | |
| 395 | 추가: path: path.to_path_buf(), | |
| 396 | 추가: object: filesystem::object(&file)?, | |
| 397 | 추가: len: metadata.len(), | |
| 398 | 추가: modified: metadata.modified().map_err(|_| ErrorCode::Io)?, | |
| 399 | 추가: changed: change_stamp(&metadata), | |
| 400 | 추가: hash: [0; 32], | |
| 401 | 추가: }; | |
| 402 | 추가: guard.hash = hash_source(&mut file, guard.len, step)?; | |
| 403 | 추가: guard.unchanged()?; | |
| 404 | 추가: Ok(( | |
| 405 | 추가: Self { | |
| 406 | 추가: file, | |
| 407 | 추가: len: guard.len, | |
| 408 | 추가: object: guard.object.clone(), | |
| 409 | 추가: hash: guard.hash, | |
| 410 | 추가: }, | |
| 411 | 추가: guard, | |
| 412 | 추가: )) | |
| 413 | 추가: } | |
| 275 | 414 | /// 원본을 임시 파일로 복사하는 동안 읽은 바이트와 전체 크기를 함께 알림. |
| 276 | 415 | pub fn open_reporting( |
| 277 | 416 | path: &Path, |
| @@ -400,3 +539,7 @@ impl Snapshot { | ||
| 400 | 539 | }) |
| 401 | 540 | } |
| 402 | 541 | } |
| 542 | 추가: | |
| 543 | 추가: #[cfg(test)] | |
| 544 | 추가: #[path = "../../../tests/rust-archive-worker/direct_source.rs"] | |
| 545 | 추가: mod direct_source_tests; |
| @@ -0,0 +1,115 @@ | ||
| 1 | 추가: use super::*; | |
| 2 | 추가: use std::sync::atomic::{AtomicU64, Ordering}; | |
| 3 | 추가: | |
| 4 | 추가: static NEXT: AtomicU64 = AtomicU64::new(0); | |
| 5 | 추가: struct Source(std::path::PathBuf); | |
| 6 | 추가: impl Source { | |
| 7 | 추가: fn new() -> Self { | |
| 8 | 추가: let root = std::env::var_os("TMPDIR") | |
| 9 | 추가: .map(std::path::PathBuf::from) | |
| 10 | 추가: .unwrap_or_else(|| { | |
| 11 | 추가: std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../tests/work/tmp") | |
| 12 | 추가: }); | |
| 13 | 추가: std::fs::create_dir_all(&root).unwrap(); | |
| 14 | 추가: let path = root.join(format!( | |
| 15 | 추가: "direct-source-{}-{}", | |
| 16 | 추가: std::process::id(), | |
| 17 | 추가: NEXT.fetch_add(1, Ordering::Relaxed) | |
| 18 | 추가: )); | |
| 19 | 추가: std::fs::create_dir(&path).unwrap(); | |
| 20 | 추가: std::fs::write(path.join("input"), vec![42; 1024 * 1024]).unwrap(); | |
| 21 | 추가: Self(path) | |
| 22 | 추가: } | |
| 23 | 추가: fn path(&self) -> std::path::PathBuf { | |
| 24 | 추가: self.0.join("input") | |
| 25 | 추가: } | |
| 26 | 추가: } | |
| 27 | 추가: impl Drop for Source { | |
| 28 | 추가: fn drop(&mut self) { | |
| 29 | 추가: std::fs::remove_dir_all(&self.0).unwrap(); | |
| 30 | 추가: } | |
| 31 | 추가: } | |
| 32 | 추가: | |
| 33 | 추가: #[test] | |
| 34 | 추가: fn direct_source_matches_copied_identity_length_and_hash() { | |
| 35 | 추가: let source = Source::new(); | |
| 36 | 추가: let (direct, mut guard) = | |
| 37 | 추가: Snapshot::open_direct(&source.path(), u64::MAX, &mut |_| Ok(())).unwrap(); | |
| 38 | 추가: let copied = Snapshot::open_reporting(&source.path(), u64::MAX, &mut |_| Ok(())).unwrap(); | |
| 39 | 추가: assert_eq!(direct.object, copied.object); | |
| 40 | 추가: assert_eq!(direct.len, copied.len); | |
| 41 | 추가: assert_eq!(direct.hash, copied.hash); | |
| 42 | 추가: guard.verify(&mut |_| Ok(())).unwrap(); | |
| 43 | 추가: } | |
| 44 | 추가: | |
| 45 | 추가: #[cfg(unix)] | |
| 46 | 추가: #[test] | |
| 47 | 추가: fn direct_source_detects_same_size_and_restored_mtime_changes() { | |
| 48 | 추가: let source = Source::new(); | |
| 49 | 추가: let (_, mut guard) = Snapshot::open_direct(&source.path(), u64::MAX, &mut |_| Ok(())).unwrap(); | |
| 50 | 추가: let modified = std::fs::metadata(source.path()) | |
| 51 | 추가: .unwrap() | |
| 52 | 추가: .modified() | |
| 53 | 추가: .unwrap(); | |
| 54 | 추가: let mut file = OpenOptions::new().write(true).open(source.path()).unwrap(); | |
| 55 | 추가: file.seek(SeekFrom::Start(512)).unwrap(); | |
| 56 | 추가: file.write_all(b"x").unwrap(); | |
| 57 | 추가: file.set_times(std::fs::FileTimes::new().set_modified(modified)) | |
| 58 | 추가: .unwrap(); | |
| 59 | 추가: assert!(matches!( | |
| 60 | 추가: guard.verify(&mut |_| Ok(())), | |
| 61 | 추가: Err(ErrorCode::SourceChanged) | |
| 62 | 추가: )); | |
| 63 | 추가: } | |
| 64 | 추가: | |
| 65 | 추가: #[cfg(unix)] | |
| 66 | 추가: #[test] | |
| 67 | 추가: fn direct_source_detects_named_replacement_even_when_open_handle_survives() { | |
| 68 | 추가: let source = Source::new(); | |
| 69 | 추가: let (_, mut guard) = Snapshot::open_direct(&source.path(), u64::MAX, &mut |_| Ok(())).unwrap(); | |
| 70 | 추가: std::fs::rename(source.path(), source.0.join("old")).unwrap(); | |
| 71 | 추가: std::fs::write(source.path(), vec![42; 1024 * 1024]).unwrap(); | |
| 72 | 추가: assert!(matches!( | |
| 73 | 추가: guard.verify(&mut |_| Ok(())), | |
| 74 | 추가: Err(ErrorCode::SourceChanged) | |
| 75 | 추가: )); | |
| 76 | 추가: } | |
| 77 | 추가: | |
| 78 | 추가: #[cfg(windows)] | |
| 79 | 추가: #[test] | |
| 80 | 추가: fn direct_source_allows_existing_readers_and_blocks_writers_until_closed() { | |
| 81 | 추가: use std::os::windows::fs::OpenOptionsExt; | |
| 82 | 추가: let source = Source::new(); | |
| 83 | 추가: let reader = OpenOptions::new() | |
| 84 | 추가: .read(true) | |
| 85 | 추가: .share_mode(3) | |
| 86 | 추가: .open(source.path()) | |
| 87 | 추가: .unwrap(); | |
| 88 | 추가: let (snapshot, guard) = | |
| 89 | 추가: Snapshot::open_direct(&source.path(), u64::MAX, &mut |_| Ok(())).unwrap(); | |
| 90 | 추가: assert!(OpenOptions::new().read(true).open(source.path()).is_ok()); | |
| 91 | 추가: assert!(OpenOptions::new().write(true).open(source.path()).is_err()); | |
| 92 | 추가: drop((snapshot, guard, reader)); | |
| 93 | 추가: assert!(OpenOptions::new().write(true).open(source.path()).is_ok()); | |
| 94 | 추가: } | |
| 95 | 추가: | |
| 96 | 추가: #[cfg(unix)] | |
| 97 | 추가: #[test] | |
| 98 | 추가: fn direct_source_detects_transient_change_even_after_bytes_and_mtime_are_restored() { | |
| 99 | 추가: let source = Source::new(); | |
| 100 | 추가: let (_, mut guard) = Snapshot::open_direct(&source.path(), u64::MAX, &mut |_| Ok(())).unwrap(); | |
| 101 | 추가: let modified = std::fs::metadata(source.path()) | |
| 102 | 추가: .unwrap() | |
| 103 | 추가: .modified() | |
| 104 | 추가: .unwrap(); | |
| 105 | 추가: let mut file = OpenOptions::new().write(true).open(source.path()).unwrap(); | |
| 106 | 추가: file.write_all(b"x").unwrap(); | |
| 107 | 추가: file.rewind().unwrap(); | |
| 108 | 추가: file.write_all(b"*").unwrap(); | |
| 109 | 추가: file.set_times(std::fs::FileTimes::new().set_modified(modified)) | |
| 110 | 추가: .unwrap(); | |
| 111 | 추가: assert!(matches!( | |
| 112 | 추가: guard.verify(&mut |_| Ok(())), | |
| 113 | 추가: Err(ErrorCode::SourceChanged) | |
| 114 | 추가: )); | |
| 115 | 추가: } |
| @@ -483,3 +483,257 @@ fn scratch_abrupt_exit_leaves_parts_only_under_tmpdir() { | ||
| 483 | 483 | }; |
| 484 | 484 | assert!(!path.exists()); |
| 485 | 485 | } |
| 486 | 추가: | |
| 487 | 추가: #[test] | |
| 488 | 추가: fn direct_reader_preserves_copy_hash_manifest_and_reads_across_volume_boundaries() { | |
| 489 | 추가: use std::io::SeekFrom; | |
| 490 | 추가: let s = Scratch::new(); | |
| 491 | 추가: let original = s.split(11); | |
| 492 | 추가: let copied = s.capture(); | |
| 493 | 추가: let mut direct = open_direct( | |
| 494 | 추가: &s.first(), | |
| 495 | 추가: u64::MAX, | |
| 496 | 추가: crate::archive::MetadataLimits::default(), | |
| 497 | 추가: &mut |_| Ok(()), | |
| 498 | 추가: ) | |
| 499 | 추가: .unwrap() | |
| 500 | 추가: .unwrap(); | |
| 501 | 추가: assert_eq!(direct.hash, copied.snapshot.hash); | |
| 502 | 추가: assert_eq!(direct.object().unwrap(), copied.snapshot.object); | |
| 503 | 추가: assert_eq!(direct.len(), original.len() as u64); | |
| 504 | 추가: for offset in [ | |
| 505 | 추가: 0, | |
| 506 | 추가: 1, | |
| 507 | 추가: original.len() / 11 - 1, | |
| 508 | 추가: original.len() / 2, | |
| 509 | 추가: original.len() - 1, | |
| 510 | 추가: ] { | |
| 511 | 추가: direct.seek(SeekFrom::Start(offset as u64)).unwrap(); | |
| 512 | 추가: let mut bytes = vec![0; (original.len() - offset).min(64)]; | |
| 513 | 추가: direct.read_exact(&mut bytes).unwrap(); | |
| 514 | 추가: assert_eq!(bytes, original[offset..offset + bytes.len()]); | |
| 515 | 추가: } | |
| 516 | 추가: direct.seek(SeekFrom::End(1)).unwrap(); | |
| 517 | 추가: assert_eq!(direct.read(&mut [0; 1]).unwrap(), 0); | |
| 518 | 추가: assert!(direct.seek(SeekFrom::Start(0)).is_ok()); | |
| 519 | 추가: assert!(direct.seek(SeekFrom::Current(-1)).is_err()); | |
| 520 | 추가: direct.verify(&mut |_| Ok(())).unwrap(); | |
| 521 | 추가: } | |
| 522 | 추가: | |
| 523 | 추가: #[cfg(unix)] | |
| 524 | 추가: #[test] | |
| 525 | 추가: fn direct_reader_detects_transient_middle_volume_changes() { | |
| 526 | 추가: use std::io::{SeekFrom, Write}; | |
| 527 | 추가: let s = Scratch::new(); | |
| 528 | 추가: s.split(11); | |
| 529 | 추가: let direct = open_direct( | |
| 530 | 추가: &s.first(), | |
| 531 | 추가: u64::MAX, | |
| 532 | 추가: crate::archive::MetadataLimits::default(), | |
| 533 | 추가: &mut |_| Ok(()), | |
| 534 | 추가: ) | |
| 535 | 추가: .unwrap() | |
| 536 | 추가: .unwrap(); | |
| 537 | 추가: let path = s.0.join("sample.7z.006"); | |
| 538 | 추가: let original = std::fs::read(&path).unwrap(); | |
| 539 | 추가: let modified = std::fs::metadata(&path).unwrap().modified().unwrap(); | |
| 540 | 추가: let mut file = std::fs::OpenOptions::new().write(true).open(path).unwrap(); | |
| 541 | 추가: file.write_all(&[original[0] ^ 1]).unwrap(); | |
| 542 | 추가: file.seek(SeekFrom::Start(0)).unwrap(); | |
| 543 | 추가: file.write_all(&original[..1]).unwrap(); | |
| 544 | 추가: file.set_times(std::fs::FileTimes::new().set_modified(modified)) | |
| 545 | 추가: .unwrap(); | |
| 546 | 추가: assert_eq!( | |
| 547 | 추가: code(direct.verify(&mut |_| Ok(()))), | |
| 548 | 추가: ErrorCode::SourceChanged | |
| 549 | 추가: ); | |
| 550 | 추가: } | |
| 551 | 추가: | |
| 552 | 추가: #[cfg(unix)] | |
| 553 | 추가: #[test] | |
| 554 | 추가: fn direct_reader_detects_transient_change_during_final_verification() { | |
| 555 | 추가: use std::io::{SeekFrom, Write}; | |
| 556 | 추가: let s = Scratch::new(); | |
| 557 | 추가: s.split(11); | |
| 558 | 추가: let direct = open_direct( | |
| 559 | 추가: &s.first(), | |
| 560 | 추가: u64::MAX, | |
| 561 | 추가: crate::archive::MetadataLimits::default(), | |
| 562 | 추가: &mut |_| Ok(()), | |
| 563 | 추가: ) | |
| 564 | 추가: .unwrap() | |
| 565 | 추가: .unwrap(); | |
| 566 | 추가: let path = s.first(); | |
| 567 | 추가: let original = std::fs::read(&path).unwrap(); | |
| 568 | 추가: let modified = std::fs::metadata(&path).unwrap().modified().unwrap(); | |
| 569 | 추가: let mut changed = false; | |
| 570 | 추가: let result = direct.verify(&mut |step| { | |
| 571 | 추가: if !changed && matches!(step, Step::Activity) { | |
| 572 | 추가: let mut file = std::fs::OpenOptions::new().write(true).open(&path).unwrap(); | |
| 573 | 추가: file.write_all(&[original[0] ^ 1]).unwrap(); | |
| 574 | 추가: file.seek(SeekFrom::Start(0)).unwrap(); | |
| 575 | 추가: file.write_all(&original[..1]).unwrap(); | |
| 576 | 추가: file.set_times(std::fs::FileTimes::new().set_modified(modified)) | |
| 577 | 추가: .unwrap(); | |
| 578 | 추가: changed = true; | |
| 579 | 추가: } | |
| 580 | 추가: Ok(()) | |
| 581 | 추가: }); | |
| 582 | 추가: assert!(changed); | |
| 583 | 추가: assert_eq!(code(result), ErrorCode::SourceChanged); | |
| 584 | 추가: } | |
| 585 | 추가: | |
| 586 | 추가: #[cfg(target_os = "linux")] | |
| 587 | 추가: #[test] | |
| 588 | 추가: fn direct_reader_low_fd_child() { | |
| 589 | 추가: if std::env::var_os("ZIPELO_SPLIT_LOW_FD_CHILD").is_none() { | |
| 590 | 추가: return; | |
| 591 | 추가: } | |
| 592 | 추가: use std::io::SeekFrom; | |
| 593 | 추가: let s = Scratch::new(); | |
| 594 | 추가: let original = s.split(11); | |
| 595 | 추가: let mut copied = s.capture(); | |
| 596 | 추가: let expected = token(&copied); | |
| 597 | 추가: let catalog = crate::seven_zip::open( | |
| 598 | 추가: &mut copied.snapshot.file, | |
| 599 | 추가: expected.archive_bytes, | |
| 600 | 추가: Limits::default(), | |
| 601 | 추가: false, | |
| 602 | 추가: &mut || Ok(()), | |
| 603 | 추가: ) | |
| 604 | 추가: .unwrap(); | |
| 605 | 추가: let entries = catalog | |
| 606 | 추가: .members | |
| 607 | 추가: .iter() | |
| 608 | 추가: .map(|m| m.entry.clone()) | |
| 609 | 추가: .collect::<Vec<_>>(); | |
| 610 | 추가: drop((catalog, copied)); | |
| 611 | 추가: let mut direct = open_direct(&s.first(), u64::MAX, Default::default(), &mut |_| Ok(())) | |
| 612 | 추가: .unwrap() | |
| 613 | 추가: .unwrap(); | |
| 614 | 추가: assert_eq!(direct.hash, expected.archive_sha256); | |
| 615 | 추가: assert_eq!(direct.object().unwrap(), expected.source_object); | |
| 616 | 추가: assert_eq!(direct.len(), expected.archive_bytes); | |
| 617 | 추가: let mut bytes = Vec::new(); | |
| 618 | 추가: direct.read_to_end(&mut bytes).unwrap(); | |
| 619 | 추가: assert_eq!(bytes, original); | |
| 620 | 추가: direct.seek(SeekFrom::End(-12)).unwrap(); | |
| 621 | 추가: let mut last = [0; 12]; | |
| 622 | 추가: direct.read_exact(&mut last).unwrap(); | |
| 623 | 추가: assert_eq!(last, original[original.len() - 12..]); | |
| 624 | 추가: let catalog = crate::seven_zip::open( | |
| 625 | 추가: &mut direct, | |
| 626 | 추가: expected.archive_bytes, | |
| 627 | 추가: Limits::default(), | |
| 628 | 추가: false, | |
| 629 | 추가: &mut || Ok(()), | |
| 630 | 추가: ) | |
| 631 | 추가: .unwrap(); | |
| 632 | 추가: assert_eq!( | |
| 633 | 추가: catalog | |
| 634 | 추가: .members | |
| 635 | 추가: .iter() | |
| 636 | 추가: .map(|m| m.entry.clone()) | |
| 637 | 추가: .collect::<Vec<_>>(), | |
| 638 | 추가: entries | |
| 639 | 추가: ); | |
| 640 | 추가: drop(catalog); | |
| 641 | 추가: direct.verify(&mut |_| Ok(())).unwrap(); | |
| 642 | 추가: assert_eq!(std::fs::read_dir(&s.0).unwrap().count(), 11); | |
| 643 | 추가: } | |
| 644 | 추가: | |
| 645 | 추가: #[cfg(target_os = "linux")] | |
| 646 | 추가: #[test] | |
| 647 | 추가: fn direct_reader_with_sixteen_file_descriptors_lists_eleven_volumes() { | |
| 648 | 추가: use std::os::unix::process::CommandExt; | |
| 649 | 추가: #[repr(C)] | |
| 650 | 추가: struct Limit { | |
| 651 | 추가: soft: u64, | |
| 652 | 추가: hard: u64, | |
| 653 | 추가: } | |
| 654 | 추가: unsafe extern "C" { | |
| 655 | 추가: fn setrlimit(resource: i32, limit: *const Limit) -> i32; | |
| 656 | 추가: } | |
| 657 | 추가: let s = Scratch::new(); | |
| 658 | 추가: let mut command = std::process::Command::new(std::env::current_exe().unwrap()); | |
| 659 | 추가: command | |
| 660 | 추가: .args([ | |
| 661 | 추가: "--exact", | |
| 662 | 추가: "split_seven_zip::tests::direct_reader_low_fd_child", | |
| 663 | 추가: "--test-threads=1", | |
| 664 | 추가: ]) | |
| 665 | 추가: .env("TMPDIR", &s.0) | |
| 666 | 추가: .env("ZIPELO_SPLIT_LOW_FD_CHILD", "1"); | |
| 667 | 추가: // 자식 프로세스의 파일 핸들 상한만 16으로 제한함. | |
| 668 | 추가: unsafe { | |
| 669 | 추가: command.pre_exec(|| { | |
| 670 | 추가: if setrlimit(7, &Limit { soft: 16, hard: 16 }) == 0 { | |
| 671 | 추가: Ok(()) | |
| 672 | 추가: } else { | |
| 673 | 추가: Err(std::io::Error::last_os_error()) | |
| 674 | 추가: } | |
| 675 | 추가: }); | |
| 676 | 추가: } | |
| 677 | 추가: let result = command.output().unwrap(); | |
| 678 | 추가: assert!( | |
| 679 | 추가: result.status.success(), | |
| 680 | 추가: "stdout={} stderr={}", | |
| 681 | 추가: String::from_utf8_lossy(&result.stdout), | |
| 682 | 추가: String::from_utf8_lossy(&result.stderr) | |
| 683 | 추가: ); | |
| 684 | 추가: assert_eq!(std::fs::read_dir(&s.0).unwrap().count(), 0); | |
| 685 | 추가: } | |
| 686 | 추가: | |
| 687 | 추가: #[test] | |
| 688 | 추가: fn direct_reader_reopens_volumes_and_rejects_same_content_replacement_before_read() { | |
| 689 | 추가: use std::io::SeekFrom; | |
| 690 | 추가: let s = Scratch::new(); | |
| 691 | 추가: let original = s.split(11); | |
| 692 | 추가: let mut direct = open_direct(&s.first(), u64::MAX, Default::default(), &mut |_| Ok(())) | |
| 693 | 추가: .unwrap() | |
| 694 | 추가: .unwrap(); | |
| 695 | 추가: let path = s.0.join("sample.7z.006"); | |
| 696 | 추가: let bytes = std::fs::read(&path).unwrap(); | |
| 697 | 추가: let modified = std::fs::metadata(&path).unwrap().modified().unwrap(); | |
| 698 | 추가: std::fs::rename(&path, s.0.join("previous")).unwrap(); | |
| 699 | 추가: std::fs::write(&path, bytes).unwrap(); | |
| 700 | 추가: std::fs::OpenOptions::new() | |
| 701 | 추가: .write(true) | |
| 702 | 추가: .open(&path) | |
| 703 | 추가: .unwrap() | |
| 704 | 추가: .set_times(std::fs::FileTimes::new().set_modified(modified)) | |
| 705 | 추가: .unwrap(); | |
| 706 | 추가: direct | |
| 707 | 추가: .seek(SeekFrom::Start((5 * original.len() / 11) as u64)) | |
| 708 | 추가: .unwrap(); | |
| 709 | 추가: assert!(direct.read(&mut [0; 1]).is_err()); | |
| 710 | 추가: assert_eq!( | |
| 711 | 추가: code(direct.verify(&mut |_| Ok(()))), | |
| 712 | 추가: ErrorCode::SourceChanged | |
| 713 | 추가: ); | |
| 714 | 추가: } | |
| 715 | 추가: | |
| 716 | 추가: #[test] | |
| 717 | 추가: fn direct_reader_classifies_non_file_replacement_as_source_changed() { | |
| 718 | 추가: let s = Scratch::new(); | |
| 719 | 추가: s.split(11); | |
| 720 | 추가: let direct = open_direct(&s.first(), u64::MAX, Default::default(), &mut |_| Ok(())) | |
| 721 | 추가: .unwrap() | |
| 722 | 추가: .unwrap(); | |
| 723 | 추가: let path = s.0.join("sample.7z.006"); | |
| 724 | 추가: std::fs::rename(&path, s.0.join("previous")).unwrap(); | |
| 725 | 추가: std::fs::create_dir(&path).unwrap(); | |
| 726 | 추가: assert_eq!( | |
| 727 | 추가: code(direct.verify(&mut |_| Ok(()))), | |
| 728 | 추가: ErrorCode::SourceChanged | |
| 729 | 추가: ); | |
| 730 | 추가: #[cfg(unix)] | |
| 731 | 추가: { | |
| 732 | 추가: std::fs::remove_dir(&path).unwrap(); | |
| 733 | 추가: std::os::unix::fs::symlink(s.0.join("previous"), &path).unwrap(); | |
| 734 | 추가: assert_eq!( | |
| 735 | 추가: code(direct.verify(&mut |_| Ok(()))), | |
| 736 | 추가: ErrorCode::SourceChanged | |
| 737 | 추가: ); | |
| 738 | 추가: } | |
| 739 | 추가: } |
| @@ -76,7 +76,7 @@ impl Case { | ||
| 76 | 76 | if phase == "home" { |
| 77 | 77 | command.env_remove("XDG_CACHE_HOME"); |
| 78 | 78 | } |
| 79 | 삭제: if phase == "changed" { | |
| 79 | 추가: if phase.ends_with("changed") { | |
| 80 | 80 | command.env( |
| 81 | 81 | "ZIPELO_SPOOL_SOURCE", |
| 82 | 82 | match &action { |
| @@ -299,7 +299,7 @@ fn cache_source_and_system_locations_retry_creation_space_and_partial_quota_writ | ||
| 299 | 299 | } |
| 300 | 300 | } |
| 301 | 301 | #[test] |
| 302 | 삭제: fn split_seven_zip_retries_all_locations_after_partial_quota_writes() { | |
| 302 | 추가: fn split_seven_zip_lists_without_archive_sized_temporary_storage() { | |
| 303 | 303 | use sha2::{Digest, Sha256}; |
| 304 | 304 | for named in [false, true] { |
| 305 | 305 | for phase in ["write", "space", "create"] { |
| @@ -310,7 +310,7 @@ fn split_seven_zip_retries_all_locations_after_partial_quota_writes() { | ||
| 310 | 310 | let bytes = fs::read(c.archive("7z").to_path().unwrap()).unwrap(); |
| 311 | 311 | let zip = fs::read(c.archive("zip").to_path().unwrap()).unwrap(); |
| 312 | 312 | let events = c.run(Action::List { archive }, phase, mask, named); |
| 313 | 삭제: if mask == 7 { | |
| 313 | 추가: if mask == 7 && phase == "create" { | |
| 314 | 314 | assert_eq!( |
| 315 | 315 | terminal(&events).outcome, |
| 316 | 316 | Outcome::Failed, |
| @@ -347,15 +347,16 @@ fn split_seven_zip_retries_all_locations_after_partial_quota_writes() { | ||
| 347 | 347 | )); |
| 348 | 348 | } |
| 349 | 349 | let trace = fs::read_to_string(c.scratch.0.join("trace")).unwrap(); |
| 350 | 삭제: for at in if mask == 1 { | |
| 351 | 삭제: &['0', '1'][..] | |
| 350 | 추가: if phase == "create" { | |
| 351 | 추가: for at in if mask == 1 { | |
| 352 | 추가: &['0', '1'][..] | |
| 353 | 추가: } else { | |
| 354 | 추가: &['0', '1', '2'][..] | |
| 355 | 추가: } { | |
| 356 | 추가: assert!(trace.contains(*at), "{phase}/{mask}/{named}: {trace}"); | |
| 357 | 추가: } | |
| 352 | 358 | } else { |
| 353 | 삭제: &['0', '1', '2'][..] | |
| 354 | 삭제: } { | |
| 355 | 삭제: assert!(trace.contains(*at), "{phase}/{mask}/{named}: {trace}"); | |
| 356 | 삭제: } | |
| 357 | 삭제: if mask == 1 { | |
| 358 | 삭제: assert!(!trace.contains('2'), "{trace}"); | |
| 359 | 추가: assert!(!trace.contains('1') && !trace.contains('2'), "{trace}"); | |
| 359 | 360 | } |
| 360 | 361 | for (path, original) in originals { |
| 361 | 362 | assert_eq!(fs::read(path).unwrap(), original); |
| @@ -368,7 +369,7 @@ fn split_seven_zip_retries_all_locations_after_partial_quota_writes() { | ||
| 368 | 369 | } |
| 369 | 370 | |
| 370 | 371 | #[test] |
| 371 | 삭제: fn split_source_change_during_failed_copy_prevents_retry_success() { | |
| 372 | 추가: fn split_source_change_during_direct_read_prevents_success() { | |
| 372 | 373 | for named in [false, true] { |
| 373 | 374 | let mut c = Case::new(); |
| 374 | 375 | let (archive, originals) = c.split(); |
| @@ -389,12 +390,205 @@ fn split_source_change_during_failed_copy_prevents_retry_success() { | ||
| 389 | 390 | } |
| 390 | 391 | } |
| 391 | 392 | |
| 393 | 추가: #[test] | |
| 394 | 추가: fn archive_larger_than_every_temporary_quota_lists_previews_and_extracts_with_same_id() { | |
| 395 | 추가: use sha2::{Digest, Sha256}; | |
| 396 | 추가: use std::os::unix::fs::MetadataExt; | |
| 397 | 추가: for named in [false, true] { | |
| 398 | 추가: let mut c = Case::new(); | |
| 399 | 추가: let solid = c.list("7z", "quota", 7, named); | |
| 400 | 추가: assert_eq!(terminal(&solid).outcome, Outcome::Completed, "{solid:?}"); | |
| 401 | 추가: assert!( | |
| 402 | 추가: Command::new("python3") | |
| 403 | 추가: .arg("-B") | |
| 404 | 추가: .arg( | |
| 405 | 추가: PathBuf::from(env!("CARGO_MANIFEST_DIR")) | |
| 406 | 추가: .join("../../tests/rust-archive-worker/spool_fallback_cases.py") | |
| 407 | 추가: ) | |
| 408 | 추가: .arg(&c.scratch.0) | |
| 409 | 추가: .arg("non-solid") | |
| 410 | 추가: .status() | |
| 411 | 추가: .unwrap() | |
| 412 | 추가: .success() | |
| 413 | 추가: ); | |
| 414 | 추가: for format in ["zip", "7z"] { | |
| 415 | 추가: c.assert_preview(format, "quota", 7, named); | |
| 416 | 추가: let events = c.list(format, "quota", 7, named); | |
| 417 | 추가: let id = events | |
| 418 | 추가: .iter() | |
| 419 | 추가: .find_map(|event| match event { | |
| 420 | 추가: Message::ListEnd { list_id, .. } => Some(list_id.clone()), | |
| 421 | 추가: _ => None, | |
| 422 | 추가: }) | |
| 423 | 추가: .unwrap(); | |
| 424 | 추가: let path = c.archive(format).to_path().unwrap(); | |
| 425 | 추가: let bytes = fs::read(&path).unwrap(); | |
| 426 | 추가: let metadata = fs::metadata(path).unwrap(); | |
| 427 | 추가: assert!(bytes.len() > 16 * 1024 * 1024); | |
| 428 | 추가: assert_eq!(id.catalog_version, 1); | |
| 429 | 추가: assert_eq!(id.archive_bytes, bytes.len() as u64); | |
| 430 | 추가: assert_eq!( | |
| 431 | 추가: id.archive_sha256.as_slice(), | |
| 432 | 추가: Sha256::digest(&bytes).as_slice() | |
| 433 | 추가: ); | |
| 434 | 추가: assert_eq!( | |
| 435 | 추가: id.source_object, | |
| 436 | 추가: SourceObject::Unix { | |
| 437 | 추가: device: metadata.dev(), | |
| 438 | 추가: inode: metadata.ino() | |
| 439 | 추가: } | |
| 440 | 추가: ); | |
| 441 | 추가: let output = c.scratch.0.join(format!("output-{format}")); | |
| 442 | 추가: fs::DirBuilder::new().mode(0o700).create(&output).unwrap(); | |
| 443 | 추가: let extracted = execute(Start { | |
| 444 | 추가: action: Action::Extract { | |
| 445 | 추가: archive: c.archive(format), | |
| 446 | 추가: destination: NativePath::from_path(&output), | |
| 447 | 추가: selection: Selection::EntryIds(vec![0]), | |
| 448 | 추가: list_id: Some(id), | |
| 449 | 추가: gzip_output_name: None, | |
| 450 | 추가: }, | |
| 451 | 추가: limits: Limits::default(), | |
| 452 | 추가: }); | |
| 453 | 추가: assert_eq!( | |
| 454 | 추가: terminal(&extracted).outcome, | |
| 455 | 추가: Outcome::Completed, | |
| 456 | 추가: "{extracted:?}" | |
| 457 | 추가: ); | |
| 458 | 추가: assert_eq!(fs::read(output.join("preview.txt")).unwrap(), PAYLOAD); | |
| 459 | 추가: } | |
| 460 | 추가: let (archive, originals) = c.split(); | |
| 461 | 추가: let events = c.run( | |
| 462 | 추가: Action::List { | |
| 463 | 추가: archive: archive.clone(), | |
| 464 | 추가: }, | |
| 465 | 추가: "quota", | |
| 466 | 추가: 7, | |
| 467 | 추가: named, | |
| 468 | 추가: ); | |
| 469 | 추가: assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); | |
| 470 | 추가: let id = events | |
| 471 | 추가: .iter() | |
| 472 | 추가: .find_map(|event| match event { | |
| 473 | 추가: Message::ListEnd { list_id, .. } => Some(list_id.clone()), | |
| 474 | 추가: _ => None, | |
| 475 | 추가: }) | |
| 476 | 추가: .unwrap(); | |
| 477 | 추가: let output = c.scratch.0.join("output-split"); | |
| 478 | 추가: fs::DirBuilder::new().mode(0o700).create(&output).unwrap(); | |
| 479 | 추가: let extracted = execute(Start { | |
| 480 | 추가: action: Action::Extract { | |
| 481 | 추가: archive: archive.clone(), | |
| 482 | 추가: destination: NativePath::from_path(&output), | |
| 483 | 추가: selection: Selection::EntryIds(vec![0]), | |
| 484 | 추가: list_id: Some(id.clone()), | |
| 485 | 추가: gzip_output_name: None, | |
| 486 | 추가: }, | |
| 487 | 추가: limits: Limits::default(), | |
| 488 | 추가: }); | |
| 489 | 추가: assert_eq!( | |
| 490 | 추가: terminal(&extracted).outcome, | |
| 491 | 추가: Outcome::Completed, | |
| 492 | 추가: "{extracted:?}" | |
| 493 | 추가: ); | |
| 494 | 추가: assert_eq!(fs::read(output.join("preview.txt")).unwrap(), PAYLOAD); | |
| 495 | 추가: let preview = c.run( | |
| 496 | 추가: Action::Preview { | |
| 497 | 추가: archive, | |
| 498 | 추가: list_id: id, | |
| 499 | 추가: entry_id: 0, | |
| 500 | 추가: }, | |
| 501 | 추가: "quota", | |
| 502 | 추가: 7, | |
| 503 | 추가: named, | |
| 504 | 추가: ); | |
| 505 | 추가: assert_eq!(terminal(&preview).error, ErrorCode::UnsupportedFeature); | |
| 506 | 추가: for (path, bytes) in originals { | |
| 507 | 추가: assert_eq!(fs::read(path).unwrap(), bytes); | |
| 508 | 추가: } | |
| 509 | 추가: } | |
| 510 | 추가: } | |
| 511 | 추가: | |
| 512 | 추가: #[test] | |
| 513 | 추가: fn source_change_after_initial_hash_rejects_list_and_preview_results() { | |
| 514 | 추가: for named in [false, true] { | |
| 515 | 추가: for format in ["zip", "7z"] { | |
| 516 | 추가: let c = Case::new(); | |
| 517 | 추가: let events = c.list(format, "late-changed", 7, named); | |
| 518 | 추가: assert_eq!( | |
| 519 | 추가: terminal(&events).error, | |
| 520 | 추가: ErrorCode::SourceChanged, | |
| 521 | 추가: "{events:?}" | |
| 522 | 추가: ); | |
| 523 | 추가: assert!( | |
| 524 | 추가: !events | |
| 525 | 추가: .iter() | |
| 526 | 추가: .any(|event| matches!(event, Message::ListEnd { .. })) | |
| 527 | 추가: ); | |
| 528 | 추가: let c = Case::new(); | |
| 529 | 추가: let events = c.list(format, "quota", 7, named); | |
| 530 | 추가: let id = events | |
| 531 | 추가: .iter() | |
| 532 | 추가: .find_map(|event| match event { | |
| 533 | 추가: Message::ListEnd { list_id, .. } => Some(list_id.clone()), | |
| 534 | 추가: _ => None, | |
| 535 | 추가: }) | |
| 536 | 추가: .unwrap(); | |
| 537 | 추가: let events = c.run( | |
| 538 | 추가: Action::Preview { | |
| 539 | 추가: archive: c.archive(format), | |
| 540 | 추가: list_id: id, | |
| 541 | 추가: entry_id: 0, | |
| 542 | 추가: }, | |
| 543 | 추가: "late-changed", | |
| 544 | 추가: 7, | |
| 545 | 추가: named, | |
| 546 | 추가: ); | |
| 547 | 추가: assert_eq!( | |
| 548 | 추가: terminal(&events).error, | |
| 549 | 추가: ErrorCode::SourceChanged, | |
| 550 | 추가: "{events:?}" | |
| 551 | 추가: ); | |
| 552 | 추가: assert!(!events.iter().any(|event| matches!( | |
| 553 | 추가: event, | |
| 554 | 추가: Message::PreviewBegin(_) | Message::PreviewEnd { .. } | |
| 555 | 추가: ))); | |
| 556 | 추가: } | |
| 557 | 추가: let mut c = Case::new(); | |
| 558 | 추가: let (archive, _) = c.split(); | |
| 559 | 추가: let events = c.run(Action::List { archive }, "late-changed", 7, named); | |
| 560 | 추가: assert_eq!( | |
| 561 | 추가: terminal(&events).error, | |
| 562 | 추가: ErrorCode::SourceChanged, | |
| 563 | 추가: "{events:?}" | |
| 564 | 추가: ); | |
| 565 | 추가: assert!( | |
| 566 | 추가: !events | |
| 567 | 추가: .iter() | |
| 568 | 추가: .any(|event| matches!(event, Message::ListEnd { .. })) | |
| 569 | 추가: ); | |
| 570 | 추가: } | |
| 571 | 추가: } | |
| 572 | 추가: | |
| 392 | 573 | #[test] |
| 393 | 574 | fn disk_full_requires_exhausting_all_three_locations_and_io_errors_stay_io() { |
| 394 | 575 | for phase in ["create", "space", "write"] { |
| 395 | 576 | let c = Case::new(); |
| 396 | 577 | for format in ["zip", "7z"] { |
| 397 | 삭제: let events = c.list(format, phase, 7, false); | |
| 578 | 추가: let events = if phase == "create" { | |
| 579 | 추가: c.list(format, phase, 7, false) | |
| 580 | 추가: } else { | |
| 581 | 추가: c.preview( | |
| 582 | 추가: format, | |
| 583 | 추가: if phase == "space" { | |
| 584 | 추가: "stage-space" | |
| 585 | 추가: } else { | |
| 586 | 추가: phase | |
| 587 | 추가: }, | |
| 588 | 추가: 7, | |
| 589 | 추가: false, | |
| 590 | 추가: ) | |
| 591 | 추가: }; | |
| 398 | 592 | assert_eq!( |
| 399 | 593 | terminal(&events).error, |
| 400 | 594 | ErrorCode::DiskFull, |
| @@ -410,7 +604,11 @@ fn disk_full_requires_exhausting_all_three_locations_and_io_errors_stay_io() { | ||
| 410 | 604 | } |
| 411 | 605 | let c = Case::new(); |
| 412 | 606 | for phase in ["io", "denied"] { |
| 413 | 삭제: let events = c.list("zip", phase, 7, false); | |
| 607 | 추가: let events = if phase == "io" { | |
| 608 | 추가: c.preview("zip", phase, 7, false) | |
| 609 | 추가: } else { | |
| 610 | 추가: c.list("zip", phase, 7, false) | |
| 611 | 추가: }; | |
| 414 | 612 | assert_eq!(terminal(&events).error, ErrorCode::Io, "{events:?}"); |
| 415 | 613 | } |
| 416 | 614 | } |
| @@ -478,7 +676,7 @@ fn default_home_cache_is_created_privately() { | ||
| 478 | 676 | } |
| 479 | 677 | |
| 480 | 678 | #[test] |
| 481 | 삭제: fn source_change_during_failed_copy_prevents_retry_success() { | |
| 679 | 추가: fn source_change_during_direct_read_prevents_success() { | |
| 482 | 680 | let c = Case::new(); |
| 483 | 681 | let events = c.list("zip", "changed", 1, false); |
| 484 | 682 | assert_eq!(terminal(&events).outcome, Outcome::Failed, "{events:?}"); |
| @@ -487,11 +685,7 @@ fn source_change_during_failed_copy_prevents_retry_success() { | ||
| 487 | 685 | ErrorCode::SourceChanged, |
| 488 | 686 | "{events:?}" |
| 489 | 687 | ); |
| 490 | 삭제: let trace = fs::read_to_string(c.scratch.0.join("trace")).unwrap(); | |
| 491 | 삭제: assert!( | |
| 492 | 삭제: trace.contains('0') && !trace.contains('1') && !trace.contains('2'), | |
| 493 | 삭제: "{trace}" | |
| 494 | 삭제: ); | |
| 688 | 추가: assert!(!c.scratch.0.join("trace").exists()); | |
| 495 | 689 | } |
| 496 | 690 | |
| 497 | 691 | #[test] |
| @@ -3,7 +3,9 @@ from pathlib import Path | ||
| 3 | 3 | import sys |
| 4 | 4 | import zipfile |
| 5 | 5 | |
| 6 | 삭제: from seven_zip_fixtures import archive | |
| 6 | 추가: from seven_zip_fixtures import archive, integer, property_bytes, packed, streams, SIGNATURE | |
| 7 | 추가: import struct | |
| 8 | 추가: import zlib | |
| 7 | 9 | |
| 8 | 10 | root = Path(sys.argv[1]) |
| 9 | 11 | payload = b"spool fallback preview\n" |
| @@ -16,3 +18,24 @@ with zipfile.ZipFile( | ||
| 16 | 18 | (root / "archive" / "input.7z").write_bytes( |
| 17 | 19 | archive([("preview.txt", payload, 0x20), ("filler.bin", filler, 0x20)], method="copy") |
| 18 | 20 | ) |
| 21 | 추가: | |
| 22 | 추가: if len(sys.argv) > 2 and sys.argv[2] == "non-solid": | |
| 23 | 추가: entries = [("preview.txt", payload), ("filler.bin", filler)] | |
| 24 | 추가: packed_bytes = b"".join(data for _, data in entries) | |
| 25 | 추가: sizes = b"".join(integer(len(data)) for _, data in entries) | |
| 26 | 추가: digests = b"".join(struct.pack("<I", zlib.crc32(data)) for _, data in entries) | |
| 27 | 추가: info = b"\x06\x00\x02\x09" + sizes + b"\x0a\x01" + digests + b"\x00" | |
| 28 | 추가: info += b"\x07\x0b\x02\x00" + b"\x01\x01\x00" * 2 | |
| 29 | 추가: info += b"\x0c" + sizes + b"\x0a\x01" + digests + b"\x00\x08\x00\x00" | |
| 30 | 추가: files = b"\x05\x02" + property_bytes( | |
| 31 | 추가: 17, b"\x00" + b"".join(name.encode("utf-16le") + b"\x00\x00" for name, _ in entries) | |
| 32 | 추가: ) | |
| 33 | 추가: files += property_bytes(21, b"\x01\x00" + struct.pack("<II", 0x20, 0x20)) | |
| 34 | 추가: header = b"\x01\x04" + info + files + b"\x00\x00" | |
| 35 | 추가: encoded, coder = packed(header, "lzma") | |
| 36 | 추가: next_header = b"\x17" + streams(len(packed_bytes), encoded, coder, header, [header]) | |
| 37 | 추가: packed_bytes += encoded | |
| 38 | 추가: start = struct.pack("<QQI", len(packed_bytes), len(next_header), zlib.crc32(next_header)) | |
| 39 | 추가: (root / "archive" / "input.7z").write_bytes( | |
| 40 | 추가: SIGNATURE + struct.pack("<I", zlib.crc32(start)) + start + packed_bytes + next_header | |
| 41 | 추가: ) |
| @@ -7,6 +7,9 @@ | ||
| 7 | 7 | #include <stdlib.h> |
| 8 | 8 | #include <string.h> |
| 9 | 9 | #include <sys/statvfs.h> |
| 10 | 추가: #include <sys/stat.h> | |
| 11 | 추가: #include <sys/resource.h> | |
| 12 | 추가: #include <signal.h> | |
| 10 | 13 | #include <unistd.h> |
| 11 | 14 | |
| 12 | 15 | static int (*original_openat)(int, const char *, int, ...); |
| @@ -14,11 +17,19 @@ static int (*original_open64)(const char *, int, ...); | ||
| 14 | 17 | static ssize_t (*original_write)(int, const void *, size_t); |
| 15 | 18 | static int written[3]; |
| 16 | 19 | static int snapshot_fd = -1; |
| 20 | 추가: static int source_changed; | |
| 21 | 추가: static unsigned long long source_read; | |
| 17 | 22 | |
| 18 | 23 | __attribute__((constructor)) static void initialize(void) { |
| 19 | 24 | original_openat = dlsym(RTLD_NEXT, "openat"); |
| 20 | 25 | original_open64 = dlsym(RTLD_NEXT, "open64"); |
| 21 | 26 | original_write = dlsym(RTLD_NEXT, "write"); |
| 27 | 추가: const char *phase = getenv("ZIPELO_SPOOL_PHASE"); | |
| 28 | 추가: if (phase && !strcmp(phase, "quota")) { | |
| 29 | 추가: struct rlimit limit = {65536, 65536}; | |
| 30 | 추가: signal(SIGXFSZ, SIG_IGN); | |
| 31 | 추가: if (setrlimit(RLIMIT_FSIZE, &limit)) _exit(99); | |
| 32 | 추가: } | |
| 22 | 33 | } |
| 23 | 34 | static int location(const char *path) { |
| 24 | 35 | if (strstr(path, "/cache/")) return 0; |
| @@ -109,13 +120,6 @@ ssize_t write(int fd, const void *bytes, size_t count) { | ||
| 109 | 120 | || !strcmp(phase, "changed") || !strcmp(phase, "io"))) { |
| 110 | 121 | if (!strcmp(phase, "io")) { errno = EIO; return -1; } |
| 111 | 122 | if (!written[at]++) return original_write(fd, bytes, count < 7 ? count : 7); |
| 112 | 삭제: if (!strcmp(phase, "changed")) { | |
| 113 | 삭제: int source = original_openat(AT_FDCWD, getenv("ZIPELO_SPOOL_SOURCE"), O_WRONLY | O_APPEND); | |
| 114 | 삭제: if (source >= 0) { | |
| 115 | 삭제: original_write(source, "x", 1); | |
| 116 | 삭제: close(source); | |
| 117 | 삭제: } | |
| 118 | 삭제: } | |
| 119 | 123 | errno = EDQUOT; |
| 120 | 124 | return -1; |
| 121 | 125 | } |
| @@ -132,5 +136,32 @@ int fstatvfs(int fd, struct statvfs *value) { | ||
| 132 | 136 | value->f_frsize = 1; |
| 133 | 137 | value->f_bavail = stage ? 0 : 1024; |
| 134 | 138 | } |
| 139 | 추가: if (!result && affected(at) && phase && !strcmp(phase, "quota")) { | |
| 140 | 추가: value->f_frsize = 1; | |
| 141 | 추가: value->f_bavail = 65536; | |
| 142 | 추가: } | |
| 135 | 143 | return result; |
| 136 | 144 | } |
| 145 | 추가: | |
| 146 | 추가: ssize_t read(int fd, void *bytes, size_t count) { | |
| 147 | 추가: ssize_t (*original)(int, void *, size_t) = dlsym(RTLD_NEXT, "read"); | |
| 148 | 추가: ssize_t length = original(fd, bytes, count); | |
| 149 | 추가: const char *phase = getenv("ZIPELO_SPOOL_PHASE"); | |
| 150 | 추가: const char *source = getenv("ZIPELO_SPOOL_SOURCE"); | |
| 151 | 추가: char path[4096]; | |
| 152 | 추가: descriptor(fd, path, sizeof(path)); | |
| 153 | 추가: struct stat data; | |
| 154 | 추가: if (length > 0 && source && !strcmp(path, source)) source_read += length; | |
| 155 | 추가: int late = phase && !strcmp(phase, "late-changed") && !fstat(fd, &data) | |
| 156 | 추가: && source_read > (unsigned long long)data.st_size; | |
| 157 | 추가: if (length > 0 && !source_changed && phase && (!strcmp(phase, "changed") || late) | |
| 158 | 추가: && source && !strcmp(path, source)) { | |
| 159 | 추가: source_changed = 1; | |
| 160 | 추가: int output = original_openat(AT_FDCWD, source, O_WRONLY | O_APPEND); | |
| 161 | 추가: if (output >= 0) { | |
| 162 | 추가: original_write(output, "x", 1); | |
| 163 | 추가: close(output); | |
| 164 | 추가: } | |
| 165 | 추가: } | |
| 166 | 추가: return length; | |
| 167 | 추가: } |