Skip to content

Commit 4babf71

Browse files
cgzonesotavio
authored andcommitted
Forward name decode failures
1 parent a87ce26 commit 4babf71

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
* Change to 2021 edition
99
* Drop lifetime annotations of reader parameter in `ArchiveIterator::from_read`
1010
and `ArchiveIterator::from_read_with_encoding` [#90]
11+
* Forward name decode failures in `ArchiveIterator::from_read` and
12+
`ArchiveIterator::from_read_with_encoding` instead of panicking [#91]
1113

1214
## [0.13.0] - 2022-08-03
1315

src/iterator.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,10 @@ impl<R: Read + Seek> ArchiveIterator<R> {
269269
ffi::ARCHIVE_OK | ffi::ARCHIVE_WARN => {
270270
let _utf8_guard = ffi::WindowsUTF8LocaleGuard::new();
271271
let cstr = CStr::from_ptr(ffi::archive_entry_pathname(self.archive_entry));
272-
let file_name = (self.decode)(cstr.to_bytes()).unwrap();
272+
let file_name = match (self.decode)(cstr.to_bytes()) {
273+
Ok(f) => f,
274+
Err(e) => return ArchiveContents::Err(e),
275+
};
273276
let stat = *ffi::archive_entry_stat(self.archive_entry);
274277
ArchiveContents::StartOfEntry(file_name, stat)
275278
}

tests/integration_test.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use compress_tools::*;
66
use std::{
7-
io::{Cursor, Read},
7+
io::{Cursor, ErrorKind, Read},
88
path::Path,
99
};
1010

@@ -686,3 +686,19 @@ fn uncompress_archive_absolute_path() {
686686
assert!(correct_dest.exists());
687687
assert!(!Path::new(incorrect_dest).exists());
688688
}
689+
690+
#[test]
691+
fn decode_failure() {
692+
let source = std::fs::File::open("tests/fixtures/file.txt.gz").unwrap();
693+
let decode_fail = |_bytes: &[u8]| Err(Error::Io(std::io::Error::from(ErrorKind::BrokenPipe)));
694+
695+
for content in ArchiveIterator::from_read_with_encoding(source, decode_fail).unwrap() {
696+
if let ArchiveContents::Err(Error::Io(err)) = content {
697+
if err.kind() == ErrorKind::BrokenPipe {
698+
return;
699+
}
700+
}
701+
}
702+
703+
panic!("Did not find expected error");
704+
}

0 commit comments

Comments
 (0)