From 1b13dac0038a48542df1afbddc6adf4e936dc037 Mon Sep 17 00:00:00 2001 From: Otavio Salvador Date: Sun, 7 Mar 2021 22:09:49 -0300 Subject: [PATCH] Change `run_with_archive` to be seekable The rework the code from `run_with_archive` to use a seekable read and fix the 7zip uncompression with `uncompress_archive` method. Fixes: #53. Signed-off-by: Otavio Salvador --- CHANGES.md | 4 ++++ src/lib.rs | 13 +++++++++---- tests/integration_test.rs | 26 ++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 50a818d..94454dd 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -4,6 +4,10 @@ ## [Unreleased] - ReleaseDate +* Fix when uncompressing 7z archive to a directory. [#53] + +[#53]: https://github.com/OSSystems/compress-tools-rs/issues/53 + ## [0.11.0] - 2021-03-03 ### Fixed diff --git a/src/lib.rs b/src/lib.rs index c101e46..e8966f4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -294,7 +294,7 @@ where fn run_with_archive(ownership: Ownership, mut reader: R, f: F) -> Result where F: FnOnce(*mut ffi::archive, *mut ffi::archive, *mut ffi::archive_entry) -> Result, - R: Read, + R: Read + Seek, { let _utf8_guard = ffi::UTF8LocaleGuard::new(); unsafe { @@ -313,6 +313,11 @@ where archive_reader, )?; + archive_result( + ffi::archive_read_set_seek_callback(archive_reader, Some(libarchive_seek_callback)), + archive_reader, + )?; + let mut writer_flags = ffi::ARCHIVE_EXTRACT_TIME | ffi::ARCHIVE_EXTRACT_PERM | ffi::ARCHIVE_EXTRACT_ACL @@ -340,7 +345,7 @@ where return Err(Error::NullArchive); } - let mut pipe = ReaderPipe { + let mut pipe = SeekableReaderPipe { reader: &mut reader, buffer: &mut [0; READER_BUFFER_SIZE], }; @@ -348,9 +353,9 @@ where archive_result( ffi::archive_read_open( archive_reader, - (&mut pipe as *mut ReaderPipe) as *mut c_void, + (&mut pipe as *mut SeekableReaderPipe) as *mut c_void, None, - Some(libarchive_read_callback), + Some(libarchive_seekable_read_callback), None, ), archive_reader, diff --git a/tests/integration_test.rs b/tests/integration_test.rs index 5b553f1..a1b3bf4 100644 --- a/tests/integration_test.rs +++ b/tests/integration_test.rs @@ -309,6 +309,32 @@ fn uncompress_to_dir_not_preserve_owner() { ); } +#[test] +fn uncompress_7z_to_dir_not_preserve_owner() { + let dir = tempfile::TempDir::new().expect("Failed to create the tmp directory"); + let mut source = std::fs::File::open("tests/fixtures/tree.7z").unwrap(); + + uncompress_archive(&mut source, dir.path(), Ownership::Ignore) + .expect("Failed to uncompress the file"); + + assert_eq!( + dir.path().join("tree/branch1/leaf").exists(), + true, + "the path doesn't exist" + ); + assert_eq!( + dir.path().join("tree/branch2/leaf").exists(), + true, + "the path doesn't exist" + ); + + let contents = std::fs::read_to_string(dir.path().join("tree/branch2/leaf")).unwrap(); + assert_eq!( + contents, "Goodbye World\n", + "Uncompressed file did not match" + ); +} + #[test] fn uncompress_to_dir_with_utf8_pathname() { let dir = tempfile::TempDir::new().expect("Failed to create the tmp directory");