Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use tokio::{
};
use tokio_stream::*;

use crate::header::BLOCK_SIZE;
use crate::{
entry::{EntryFields, EntryIo},
error::TarError,
Expand Down Expand Up @@ -435,15 +436,15 @@ fn poll_next_raw<R: Read + Unpin>(
// Otherwise, check if we are ignoring zeros and continue, or break as if this is the
// end of the archive.
if !header.as_bytes().iter().all(|i| *i == 0) {
*next += 512;
*next += BLOCK_SIZE;
break;
}

if !archive.inner.ignore_zeros {
return Poll::Ready(None);
}

*next += 512;
*next += BLOCK_SIZE;
header_pos = *next;
}

Expand Down Expand Up @@ -486,10 +487,10 @@ fn poll_next_raw<R: Read + Unpin>(
// Store where the next entry is, rounding up by 512 bytes (the size of
// a header);
let size = size
.checked_add(511)
.checked_add(BLOCK_SIZE - 1)
.ok_or_else(|| other("size overflow"))?;
*next = next
.checked_add(size & !511)
.checked_add(size & !(BLOCK_SIZE - 1))
.ok_or_else(|| other("size overflow"))?;

Poll::Ready(Some(Ok(ret.into_entry())))
Expand Down Expand Up @@ -546,7 +547,7 @@ fn poll_parse_sparse_header<R: Read + Unpin>(
let off = block.offset()?;
let len = block.length()?;

if len != 0 && (size - remaining) % 512 != 0 {
if len != 0 && (size - remaining) % BLOCK_SIZE != 0 {
return Err(other(
"previous block in sparse file was not \
aligned to 512-byte boundary",
Expand Down Expand Up @@ -597,7 +598,7 @@ fn poll_parse_sparse_header<R: Read + Unpin>(
Err(err) => return Poll::Ready(Err(err)),
}

*next += 512;
*next += BLOCK_SIZE;
for block in ext.sparse.iter() {
add_block(block)?;
}
Expand Down
32 changes: 20 additions & 12 deletions src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ use crate::{other, EntryType};
#[cfg(any(unix, windows))]
const DETERMINISTIC_TIMESTAMP: u64 = 1153704088;

pub(crate) const BLOCK_SIZE: u64 = 512;

/// Representation of the header of an entry in an archive
#[repr(C)]
#[allow(missing_docs)]
pub struct Header {
bytes: [u8; 512],
bytes: [u8; BLOCK_SIZE as usize],
}

/// Declares the information that should be included when filling a Header
Expand Down Expand Up @@ -148,7 +150,9 @@ impl Header {
/// extensions such as long path names, long link names, and setting the
/// atime/ctime metadata attributes of files.
pub fn new_gnu() -> Header {
let mut header = Header { bytes: [0; 512] };
let mut header = Header {
bytes: [0; BLOCK_SIZE as usize],
};
unsafe {
let gnu = cast_mut::<_, GnuHeader>(&mut header);
gnu.magic = *b"ustar ";
Expand All @@ -166,7 +170,9 @@ impl Header {
///
/// UStar is also the basis used for pax archives.
pub fn new_ustar() -> Header {
let mut header = Header { bytes: [0; 512] };
let mut header = Header {
bytes: [0; BLOCK_SIZE as usize],
};
unsafe {
let gnu = cast_mut::<_, UstarHeader>(&mut header);
gnu.magic = *b"ustar\0";
Expand All @@ -183,7 +189,9 @@ impl Header {
/// format limits the path name limit and isn't able to contain extra
/// metadata like atime/ctime.
pub fn new_old() -> Header {
let mut header = Header { bytes: [0; 512] };
let mut header = Header {
bytes: [0; BLOCK_SIZE as usize],
};
header.set_mtime(0);
header
}
Expand Down Expand Up @@ -273,12 +281,12 @@ impl Header {
}

/// Returns a view into this header as a byte array.
pub fn as_bytes(&self) -> &[u8; 512] {
pub fn as_bytes(&self) -> &[u8; BLOCK_SIZE as usize] {
&self.bytes
}

/// Returns a view into this header as a byte array.
pub fn as_mut_bytes(&mut self) -> &mut [u8; 512] {
pub fn as_mut_bytes(&mut self) -> &mut [u8; BLOCK_SIZE as usize] {
&mut self.bytes
}

Expand Down Expand Up @@ -1367,15 +1375,15 @@ impl GnuExtSparseHeader {
}

/// Returns a view into this header as a byte array.
pub fn as_bytes(&self) -> &[u8; 512] {
debug_assert_eq!(mem::size_of_val(self), 512);
unsafe { &*(self as *const GnuExtSparseHeader as *const [u8; 512]) }
pub fn as_bytes(&self) -> &[u8; BLOCK_SIZE as usize] {
debug_assert_eq!(mem::size_of_val(self), BLOCK_SIZE as usize);
unsafe { &*(self as *const GnuExtSparseHeader as *const [u8; BLOCK_SIZE as usize]) }
}

/// Returns a view into this header as a byte array.
pub fn as_mut_bytes(&mut self) -> &mut [u8; 512] {
debug_assert_eq!(mem::size_of_val(self), 512);
unsafe { &mut *(self as *mut GnuExtSparseHeader as *mut [u8; 512]) }
pub fn as_mut_bytes(&mut self) -> &mut [u8; BLOCK_SIZE as usize] {
debug_assert_eq!(mem::size_of_val(self), BLOCK_SIZE as usize);
unsafe { &mut *(self as *mut GnuExtSparseHeader as *mut [u8; BLOCK_SIZE as usize]) }
}

/// Returns a slice of the underlying sparse headers.
Expand Down