Skip to content

Commit

Permalink
Merge cffc797 into 72c4745
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathas-Conceicao committed Apr 17, 2020
2 parents 72c4745 + cffc797 commit 2b59865
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
6 changes: 6 additions & 0 deletions CHANGES.md
Expand Up @@ -2,6 +2,12 @@

## [TBD] TBD

### Added

* `uncompress_file` and `uncompress_archive_file`, on success, now return the ammount of bytes they have uncompressed [#16]

[#16]: https://github.com/OSSystems/compress-tools-rs/pull/16

### Changed

* More generic read/write api (should not be a breaking change) [#14]
Expand Down
19 changes: 11 additions & 8 deletions src/lib.rs
Expand Up @@ -94,7 +94,7 @@ enum Mode {
/// # Ok(())
/// # }
/// ```
pub fn uncompress_file<R, W>(source: R, target: W) -> Result<()>
pub fn uncompress_file<R, W>(source: R, target: W) -> Result<usize>
where
R: Read,
W: Write,
Expand Down Expand Up @@ -194,7 +194,7 @@ where
/// # Ok(())
/// # }
/// ```
pub fn uncompress_archive_file<R, W>(source: R, target: W, path: &str) -> Result<()>
pub fn uncompress_archive_file<R, W>(source: R, target: W, path: &str) -> Result<usize>
where
R: Read,
W: Write,
Expand All @@ -221,9 +221,9 @@ where
)
}

fn run_with_archive<F, R>(mode: Mode, mut reader: R, f: F) -> Result<()>
fn run_with_archive<F, R, T>(mode: Mode, mut reader: R, f: F) -> Result<T>
where
F: FnOnce(*mut ffi::archive, *mut ffi::archive, *mut ffi::archive_entry) -> Result<()>,
F: FnOnce(*mut ffi::archive, *mut ffi::archive, *mut ffi::archive_entry) -> Result<T>,
R: Read,
{
let archive_reader: *mut ffi::archive;
Expand Down Expand Up @@ -300,15 +300,16 @@ where
archive_reader,
)?;

f(archive_reader, archive_writer, archive_entry)?;
let res = f(archive_reader, archive_writer, archive_entry)?;

archive_result(ffi::archive_read_close(archive_reader), archive_reader)?;
archive_result(ffi::archive_read_free(archive_reader), archive_reader)?;
archive_result(ffi::archive_write_close(archive_writer), archive_writer)?;
archive_result(ffi::archive_write_free(archive_writer), archive_writer)?;
ffi::archive_entry_free(archive_entry);

Ok(res)
}
Ok(())
}

fn libarchive_copy_data(
Expand Down Expand Up @@ -339,20 +340,22 @@ fn libarchive_copy_data(
unsafe fn libarchive_write_data_block<W>(
archive_reader: *mut ffi::archive,
mut target: W,
) -> Result<()>
) -> Result<usize>
where
W: Write,
{
let mut buffer = std::ptr::null();
let mut offset = 0;
let mut size = 0;
let mut written = 0;

loop {
match ffi::archive_read_data_block(archive_reader, &mut buffer, &mut size, &mut offset) {
ffi::ARCHIVE_EOF => return Ok(()),
ffi::ARCHIVE_EOF => return Ok(written),
ffi::ARCHIVE_OK => {
let content = slice::from_raw_parts(buffer as *const u8, size);
target.write_all(content)?;
written += size;
}
_ => return Err(Error::from(archive_reader)),
}
Expand Down
6 changes: 4 additions & 2 deletions tests/integration_test.rs
Expand Up @@ -9,26 +9,28 @@ fn get_compressed_file_content() {
let mut source = std::fs::File::open("tests/fixtures/file.txt.gz").unwrap();
let mut target = Vec::default();

uncompress_file(&mut source, &mut target).expect("Failed to uncompress the file");
let written = uncompress_file(&mut source, &mut target).expect("Failed to uncompress the file");
assert_eq!(
String::from_utf8_lossy(&target),
"some_file_content\n",
"Uncompressed file did not match",
);
assert_eq!(written, 18, "Uncompressed bytes count did not match");
}

#[test]
fn get_a_file_from_tar() {
let mut source = std::fs::File::open("tests/fixtures/tree.tar").unwrap();
let mut target = Vec::default();

uncompress_archive_file(&mut source, &mut target, &"tree/branch2/leaf")
let written = uncompress_archive_file(&mut source, &mut target, &"tree/branch2/leaf")
.expect("Failed to get the file");
assert_eq!(
String::from_utf8_lossy(&target),
"Goodbye World\n",
"Uncompressed file did not match",
);
assert_eq!(written, 14, "Uncompressed bytes count did not match");
}

#[test]
Expand Down

0 comments on commit 2b59865

Please sign in to comment.