Skip to content

Commit

Permalink
fix: Support sparse entries of enormous size (#373)
Browse files Browse the repository at this point in the history
Just like the `GnuHeader::size` field, fields `GnuHeader::realsize` and
`GnuSparseHeader::{offset, numbytes}` could be encoded in a binary
format for values greater than 8Gi.
  • Loading branch information
xzfc authored Aug 22, 2024
1 parent 9f9ce52 commit 3747caf
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1250,7 +1250,7 @@ impl GnuHeader {
/// This is applicable for sparse files where the returned size here is the
/// size of the entire file after the sparse regions have been filled in.
pub fn real_size(&self) -> io::Result<u64> {
octal_from(&self.realsize).map_err(|err| {
num_field_wrapper_from(&self.realsize).map_err(|err| {
io::Error::new(
err.kind(),
format!(
Expand Down Expand Up @@ -1322,7 +1322,7 @@ impl GnuSparseHeader {
///
/// Returns `Err` for a malformed `offset` field.
pub fn offset(&self) -> io::Result<u64> {
octal_from(&self.offset).map_err(|err| {
num_field_wrapper_from(&self.offset).map_err(|err| {
io::Error::new(
err.kind(),
format!("{} when getting offset from sparse header", err),
Expand All @@ -1334,7 +1334,7 @@ impl GnuSparseHeader {
///
/// Returns `Err` for a malformed `numbytes` field.
pub fn length(&self) -> io::Result<u64> {
octal_from(&self.numbytes).map_err(|err| {
num_field_wrapper_from(&self.numbytes).map_err(|err| {
io::Error::new(
err.kind(),
format!("{} when getting length from sparse header", err),
Expand Down
12 changes: 12 additions & 0 deletions tests/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1157,6 +1157,18 @@ fn extract_sparse() {
assert!(s[0x2fa0 + 6..0x4000].chars().all(|x| x == '\u{0}'));
}

#[test]
fn large_sparse() {
let rdr = Cursor::new(tar!("sparse-large.tar"));
let mut ar = Archive::new(rdr);
let mut entries = t!(ar.entries());
// Only check the header info without extracting, as the file is very large,
// and not all filesystems support sparse files.
let a = t!(entries.next().unwrap());
let h = a.header().as_gnu().unwrap();
assert_eq!(h.real_size().unwrap(), 12626929280);
}

#[test]
fn sparse_with_trailing() {
let rdr = Cursor::new(tar!("sparse-1.tar"));
Expand Down
Binary file added tests/archives/sparse-large.tar
Binary file not shown.
7 changes: 7 additions & 0 deletions tests/header/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,13 @@ fn extended_numeric_format() {
0x80, 0, 0, 0, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
];
assert_eq!(h.as_header().mtime().unwrap(), 0x0123456789abcdef);

h.realsize = [0x80, 0, 0, 0, 0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde];
assert_eq!(h.real_size().unwrap(), 0x00123456789abcde);
h.sparse[0].offset = [0x80, 0, 0, 0, 0, 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd];
assert_eq!(h.sparse[0].offset().unwrap(), 0x000123456789abcd);
h.sparse[0].numbytes = [0x80, 0, 0, 0, 0, 0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc];
assert_eq!(h.sparse[0].length().unwrap(), 0x0000123456789abc);
}

#[test]
Expand Down

0 comments on commit 3747caf

Please sign in to comment.