Skip to content

Commit

Permalink
Change run_with_archive to be seekable
Browse files Browse the repository at this point in the history
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 <otavio@ossystems.com.br>
  • Loading branch information
otavio committed Mar 8, 2021
1 parent eceb932 commit 1b13dac
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 9 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ where
fn run_with_archive<F, R, T>(ownership: Ownership, mut reader: R, f: F) -> Result<T>
where
F: FnOnce(*mut ffi::archive, *mut ffi::archive, *mut ffi::archive_entry) -> Result<T>,
R: Read,
R: Read + Seek,
{
let _utf8_guard = ffi::UTF8LocaleGuard::new();
unsafe {
Expand All @@ -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
Expand Down Expand Up @@ -340,17 +345,17 @@ where
return Err(Error::NullArchive);
}

let mut pipe = ReaderPipe {
let mut pipe = SeekableReaderPipe {
reader: &mut reader,
buffer: &mut [0; READER_BUFFER_SIZE],
};

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,
Expand Down
26 changes: 26 additions & 0 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 1b13dac

Please sign in to comment.