diff --git a/src/archive.rs b/src/archive.rs index a3830ff..491f1eb 100644 --- a/src/archive.rs +++ b/src/archive.rs @@ -15,6 +15,7 @@ use tokio::{ }; use tokio_stream::*; +use crate::header::BLOCK_SIZE; use crate::{ entry::{EntryFields, EntryIo}, error::TarError, @@ -435,7 +436,7 @@ fn poll_next_raw( // 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; } @@ -443,7 +444,7 @@ fn poll_next_raw( return Poll::Ready(None); } - *next += 512; + *next += BLOCK_SIZE; header_pos = *next; } @@ -486,10 +487,10 @@ fn poll_next_raw( // 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()))) @@ -546,7 +547,7 @@ fn poll_parse_sparse_header( 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", @@ -597,7 +598,7 @@ fn poll_parse_sparse_header( Err(err) => return Poll::Ready(Err(err)), } - *next += 512; + *next += BLOCK_SIZE; for block in ext.sparse.iter() { add_block(block)?; } diff --git a/src/header.rs b/src/header.rs index 545bb73..a506d2b 100644 --- a/src/header.rs +++ b/src/header.rs @@ -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 @@ -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 "; @@ -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"; @@ -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 } @@ -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 } @@ -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.