Skip to content

Commit

Permalink
Merge pull request #14 from infinity0/master
Browse files Browse the repository at this point in the history
Fix some atrocious bugs
  • Loading branch information
mdsteele authored Aug 31, 2019
2 parents 89b52bc + 001855b commit 2417d06
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ar"
version = "0.7.0"
version = "0.8.0"
authors = ["Matthew D. Steele <[email protected]>"]
description = "A library for encoding/decoding Unix archive files."
repository = "https://github.com/mdsteele/rust-ar"
Expand Down
28 changes: 24 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ pub enum Variant {
// ========================================================================= //

/// Representation of an archive entry header.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Header {
identifier: Vec<u8>,
mtime: u64,
Expand Down Expand Up @@ -674,14 +675,14 @@ impl<R: Read + Seek> Archive<R> {
let msg = "Entry index out of bounds";
return Err(Error::new(ErrorKind::InvalidInput, msg));
}
if index != self.next_entry_index {
let offset = self.entry_headers[index].data_start;
self.reader.seek(SeekFrom::Start(offset))?;
}
let offset = self.entry_headers[index].data_start;
self.reader.seek(SeekFrom::Start(offset))?;
let header = &self.entry_headers[index].header;
let size = header.size();
if size % 2 != 0 {
self.padding = true;
} else {
self.padding = false;
}
self.next_entry_index = index + 1;
Ok(Entry {
Expand Down Expand Up @@ -1862,6 +1863,25 @@ mod tests {
entry.read_to_end(&mut buffer).unwrap();
assert_eq!(&buffer as &[u8], "Hello, world!\n".as_bytes());
}
{
// Read the next entry, which should be the second one again.
let mut entry = archive.jump_to_entry(1).unwrap();
assert_eq!(
entry.header().identifier(),
"this_is_a_very_long_filename.txt".as_bytes()
);
let mut buffer = Vec::new();
entry.read_to_end(&mut buffer).unwrap();
assert_eq!(&buffer as &[u8], "foobar\n".as_bytes());
}
{
// Jump back to the first entry and check its contents.
let mut entry = archive.jump_to_entry(0).unwrap();
assert_eq!(entry.header().identifier(), "hello.txt".as_bytes());
let mut buffer = Vec::new();
entry.read_to_end(&mut buffer).unwrap();
assert_eq!(&buffer as &[u8], "Hello, world!\n".as_bytes());
}
{
// Read the next entry, which should be the second one again.
let mut entry = archive.next_entry().unwrap().unwrap();
Expand Down

0 comments on commit 2417d06

Please sign in to comment.