use std::path::Path;
use std::str::FromStr;
use crate::ArchiveError;
use crate::ConfigProblem;
use crate::ConfigSetting;
use crate::Result;
use crate::closed_enum::ClosedEnum;
use crate::closed_enum::closed_enum;
use crate::formats::bounded_read::read_up_to;
use crate::formats::common::NotARegularFile;
use crate::formats::common::open_archive;
use crate::formats::compression::CompressionCodec;
use crate::formats::sevenz::SEVENZ_MAGIC;
const MAGIC_READ_LEN: usize = 512;
const TAR_BLOCK_LEN: usize = 512;
const USTAR_MAGIC_OFFSET: usize = 257;
const TAR_CKSUM_FIELD: std::ops::Range<usize> = 148..156;
const MAGIC_SIGNATURES: &[(usize, &[u8], ArchiveType)] = &[
(0, b"\x1f\x8b", ArchiveType::TarGz),
(0, b"\x28\xb5\x2f\xfd", ArchiveType::TarZst),
(0, b"\x42\x5a\x68", ArchiveType::TarBz2),
(0, b"\x50\x4b\x03\x04", ArchiveType::Zip),
(0, b"\x50\x4b\x05\x06", ArchiveType::Zip),
(0, b"\x50\x4b\x07\x08", ArchiveType::Zip),
(0, &SEVENZ_MAGIC, ArchiveType::SevenZ),
(0, b"\xfd\x37\x7a\x58\x5a\x00", ArchiveType::TarXz),
(257, b"ustar", ArchiveType::Tar),
];
#[cfg_attr(
not(feature = "testing"),
allow(
unreachable_pub,
reason = "reachable outside the crate only with the testing feature"
)
)]
pub const ZIP_FAMILY_ALIASES: &[&str] = &[
"jar", "war", "ear", "nar", "nbm", "apk", "aab", "ipa", "appx", "msix", "whl", "vsix", "xpi",
"epub",
];
pub(crate) fn is_zip_family_alias(ext: &str) -> bool {
ZIP_FAMILY_ALIASES
.iter()
.any(|alias| ext.eq_ignore_ascii_case(alias))
}
closed_enum! {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum ArchiveType {
Tar => "Tar",
TarGz => "TarGz",
TarBz2 => "TarBz2",
TarXz => "TarXz",
TarZst => "TarZst",
Zip => "Zip",
SevenZ => "SevenZ",
}
}
closed_enum! {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum CreatableFormat {
Tar => "Tar",
TarGz => "TarGz",
TarBz2 => "TarBz2",
TarXz => "TarXz",
TarZst => "TarZst",
Zip => "Zip",
}
}
closed_enum! {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum ExtractOnlyFormat {
SevenZ => "SevenZ",
}
}
impl ExtractOnlyFormat {
#[must_use]
pub const fn extension(self) -> &'static str {
match self {
Self::SevenZ => "7z",
}
}
fn not_creatable(self) -> ArchiveError {
ArchiveError::InvalidConfiguration {
setting: ConfigSetting::CreationFormat,
problem: ConfigProblem::NotCreatable { format: self },
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum CreationFamily {
Tar(Option<CompressionCodec>),
Zip,
}
impl CreatableFormat {
pub(crate) const fn tar_with(codec: Option<CompressionCodec>) -> Self {
match codec {
None => Self::Tar,
Some(CompressionCodec::Gzip) => Self::TarGz,
Some(CompressionCodec::Bzip2) => Self::TarBz2,
Some(CompressionCodec::Xz) => Self::TarXz,
Some(CompressionCodec::Zstd) => Self::TarZst,
}
}
pub(crate) const fn family(self) -> CreationFamily {
match self {
Self::Tar => CreationFamily::Tar(None),
Self::TarGz => CreationFamily::Tar(Some(CompressionCodec::Gzip)),
Self::TarBz2 => CreationFamily::Tar(Some(CompressionCodec::Bzip2)),
Self::TarXz => CreationFamily::Tar(Some(CompressionCodec::Xz)),
Self::TarZst => CreationFamily::Tar(Some(CompressionCodec::Zstd)),
Self::Zip => CreationFamily::Zip,
}
}
}
impl From<CreatableFormat> for ArchiveType {
fn from(format: CreatableFormat) -> Self {
match format {
CreatableFormat::Tar => Self::Tar,
CreatableFormat::TarGz => Self::TarGz,
CreatableFormat::TarBz2 => Self::TarBz2,
CreatableFormat::TarXz => Self::TarXz,
CreatableFormat::TarZst => Self::TarZst,
CreatableFormat::Zip => Self::Zip,
}
}
}
impl TryFrom<ArchiveType> for CreatableFormat {
type Error = ArchiveError;
fn try_from(format: ArchiveType) -> Result<Self> {
match format {
ArchiveType::Tar => Ok(Self::Tar),
ArchiveType::TarGz => Ok(Self::TarGz),
ArchiveType::TarBz2 => Ok(Self::TarBz2),
ArchiveType::TarXz => Ok(Self::TarXz),
ArchiveType::TarZst => Ok(Self::TarZst),
ArchiveType::Zip => Ok(Self::Zip),
ArchiveType::SevenZ => Err(ExtractOnlyFormat::SevenZ.not_creatable()),
}
}
}
impl FromStr for CreatableFormat {
type Err = ArchiveError;
fn from_str(value: &str) -> Result<Self> {
if let Some(format) = ExtractOnlyFormat::ALL
.iter()
.find(|format| format.as_str() == value)
{
return Err(format.not_creatable());
}
<Self as ClosedEnum>::parse_for(ConfigSetting::CreationFormat, value)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum FormatFamily {
Tar(Option<CompressionCodec>),
Seekable(crate::SeekableFormat),
}
impl ArchiveType {
pub(crate) const fn tar_with(codec: Option<CompressionCodec>) -> Self {
match codec {
None => Self::Tar,
Some(CompressionCodec::Gzip) => Self::TarGz,
Some(CompressionCodec::Bzip2) => Self::TarBz2,
Some(CompressionCodec::Xz) => Self::TarXz,
Some(CompressionCodec::Zstd) => Self::TarZst,
}
}
#[must_use]
pub const fn extension(self) -> &'static str {
match self {
Self::Tar => "tar",
Self::TarGz => "tar.gz",
Self::TarBz2 => "tar.bz2",
Self::TarXz => "tar.xz",
Self::TarZst => "tar.zst",
Self::Zip => "zip",
Self::SevenZ => "7z",
}
}
#[must_use]
pub const fn is_tar(self) -> bool {
match self.family() {
FormatFamily::Tar(_) => true,
FormatFamily::Seekable(_) => false,
}
}
pub(crate) const fn family(self) -> FormatFamily {
match self {
Self::Tar => FormatFamily::Tar(None),
Self::TarGz => FormatFamily::Tar(Some(CompressionCodec::Gzip)),
Self::TarBz2 => FormatFamily::Tar(Some(CompressionCodec::Bzip2)),
Self::TarXz => FormatFamily::Tar(Some(CompressionCodec::Xz)),
Self::TarZst => FormatFamily::Tar(Some(CompressionCodec::Zstd)),
Self::Zip => FormatFamily::Seekable(crate::SeekableFormat::Zip),
Self::SevenZ => FormatFamily::Seekable(crate::SeekableFormat::SevenZ),
}
}
}
pub fn detect_format(path: &Path) -> Result<ArchiveType> {
let ext_result = detect_format_from_extension(path);
match ext_result {
Ok(ext_type) => {
if let Some(magic_type) = detect_format_from_magic(path)?
&& magic_type != ext_type
{
return Ok(magic_type);
}
Ok(ext_type)
}
Err(_) => {
detect_format_from_magic(path)?.ok_or_else(|| ArchiveError::UnknownFormat {
path: path.to_path_buf(),
})
}
}
}
pub(crate) fn detect_format_from_extension(path: &Path) -> Result<ArchiveType> {
let extension =
path.extension()
.and_then(|e| e.to_str())
.ok_or_else(|| ArchiveError::UnknownFormat {
path: path.to_path_buf(),
})?;
let ext_lower = extension.to_ascii_lowercase();
match ext_lower.as_str() {
"tar" => Ok(ArchiveType::Tar),
"tgz" => Ok(ArchiveType::TarGz),
"gz" => {
if let Some(stem) = path.file_stem()
&& stem
.as_encoded_bytes()
.last_chunk::<4>()
.is_some_and(|tail| tail.eq_ignore_ascii_case(b".tar"))
{
Ok(ArchiveType::TarGz)
} else {
Err(ArchiveError::UnknownFormat {
path: path.to_path_buf(),
})
}
}
"bz2" | "tbz" | "tbz2" => Ok(ArchiveType::TarBz2),
"xz" | "txz" => Ok(ArchiveType::TarXz),
"zst" | "tzst" => Ok(ArchiveType::TarZst),
"zip" => Ok(ArchiveType::Zip),
"7z" => Ok(ArchiveType::SevenZ),
ext if is_zip_family_alias(ext) => Ok(ArchiveType::Zip),
_ => Err(ArchiveError::UnknownFormat {
path: path.to_path_buf(),
}),
}
}
fn detect_format_from_magic(path: &Path) -> Result<Option<ArchiveType>> {
let mut file = match open_archive(path) {
Ok(file) => file,
Err(refusal) if NotARegularFile::is_cause_of(&refusal) => return Err(refusal),
Err(_) => return Ok(None),
};
Ok(peek_header(&mut file)
.ok()
.and_then(|header| match_signature(header.as_slice())))
}
pub(crate) struct Header {
buf: [u8; MAGIC_READ_LEN],
filled: usize,
}
impl Header {
#[expect(
clippy::indexing_slicing,
reason = "`filled` is set only by `read_up_to`, which never exceeds the buffer"
)]
pub(crate) fn as_slice(&self) -> &[u8] {
&self.buf[..self.filled]
}
}
pub(crate) fn peek_header<R: std::io::Read + ?Sized>(reader: &mut R) -> std::io::Result<Header> {
let mut header = Header {
buf: [0; MAGIC_READ_LEN],
filled: 0,
};
header.filled = read_up_to(reader, &mut header.buf)?;
Ok(header)
}
fn is_ustar_header(header: &[u8]) -> bool {
let Some(block) = header.first_chunk::<TAR_BLOCK_LEN>() else {
return false;
};
if !block[USTAR_MAGIC_OFFSET..].starts_with(b"ustar") {
return false;
}
let sum: u32 = block
.iter()
.enumerate()
.map(|(i, &b)| {
if TAR_CKSUM_FIELD.contains(&i) {
u32::from(b' ')
} else {
u32::from(b)
}
})
.sum();
tar::Header::from_byte_slice(block)
.cksum()
.is_ok_and(|stored| stored == sum)
}
fn is_bzip2_stream_start(header: &[u8]) -> bool {
const BLOCK_MAGIC: &[u8; 6] = b"1AY&SY";
const END_MAGIC: &[u8; 6] = &[0x17, 0x72, 0x45, 0x38, 0x50, 0x90];
header.starts_with(b"BZh")
&& header
.get(3)
.is_some_and(|level| (b'1'..=b'9').contains(level))
&& header
.get(4..10)
.is_some_and(|marker| marker == BLOCK_MAGIC || marker == END_MAGIC)
}
pub(crate) fn match_signature(header: &[u8]) -> Option<ArchiveType> {
let table_match = MAGIC_SIGNATURES
.iter()
.find(|&&(offset, sig, _)| {
offset
.checked_add(sig.len())
.and_then(|end| header.get(offset..end))
.is_some_and(|window| window == sig)
})
.map(|&(_, _, archive_type)| archive_type);
match table_match {
Some(ArchiveType::TarBz2) if !is_bzip2_stream_start(header) && is_ustar_header(header) => {
Some(ArchiveType::Tar)
}
_ => table_match,
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::assert_matches;
use std::path::PathBuf;
#[test]
fn a_creatable_format_parses_from_its_name() {
for &format in CreatableFormat::ALL {
assert_eq!(format.as_str().parse::<CreatableFormat>().unwrap(), format);
}
}
#[test]
fn an_unknown_creation_format_lists_the_creatable_formats_only() {
for text in ["zip", "ZIP", "tar.gz", "bogus", "", "7z"] {
let err = text.parse::<CreatableFormat>().unwrap_err();
assert_eq!(
err.to_string(),
"invalid configuration: creation format is not one of the allowed values: \
Tar, TarGz, TarBz2, TarXz, TarZst, Zip",
"{text:?}"
);
}
}
#[test]
fn an_extract_only_format_is_not_creatable() {
let from_name = "SevenZ".parse::<CreatableFormat>().unwrap_err();
let from_type = CreatableFormat::try_from(ArchiveType::SevenZ).unwrap_err();
for err in [from_name, from_type] {
assert_matches!(
err,
ArchiveError::InvalidConfiguration {
setting: ConfigSetting::CreationFormat,
problem: ConfigProblem::NotCreatable {
format: ExtractOnlyFormat::SevenZ
},
}
);
}
}
#[test]
fn creatable_and_extract_only_formats_partition_the_archive_types() {
let mut names: Vec<&str> = CreatableFormat::NAMES
.iter()
.chain(ExtractOnlyFormat::NAMES)
.copied()
.collect();
names.sort_unstable();
let mut all: Vec<&str> = ArchiveType::NAMES.to_vec();
all.sort_unstable();
assert_eq!(names, all);
}
#[test]
fn test_archive_type_as_str_matches_display_and_is_unique() {
let names: Vec<&str> = ArchiveType::ALL.iter().map(|t| t.as_str()).collect();
assert_eq!(
names,
["Tar", "TarGz", "TarBz2", "TarXz", "TarZst", "Zip", "SevenZ"]
);
for t in ArchiveType::ALL {
assert_eq!(t.to_string(), t.as_str());
}
}
#[test]
fn test_archive_type_extension_round_trips_through_detection() {
for &t in ArchiveType::ALL {
let name = format!("archive.{}", t.extension());
assert_eq!(detect_format_from_extension(Path::new(&name)).unwrap(), t);
}
}
#[test]
fn test_creatable_format_round_trips_through_archive_type() {
for &format in CreatableFormat::ALL {
let archive_type = ArchiveType::from(format);
assert_eq!(CreatableFormat::try_from(archive_type).unwrap(), format);
assert_eq!(archive_type.as_str(), format.as_str());
}
assert_eq!(CreatableFormat::ALL.len(), ArchiveType::ALL.len() - 1);
assert_matches!(
CreatableFormat::try_from(ArchiveType::SevenZ),
Err(ArchiveError::InvalidConfiguration { .. })
);
}
#[test]
fn test_family_maps_tar_codecs() {
assert_eq!(ArchiveType::Tar.family(), FormatFamily::Tar(None));
assert_eq!(
ArchiveType::TarGz.family(),
FormatFamily::Tar(Some(CompressionCodec::Gzip))
);
assert_eq!(
ArchiveType::TarBz2.family(),
FormatFamily::Tar(Some(CompressionCodec::Bzip2))
);
assert_eq!(
ArchiveType::TarXz.family(),
FormatFamily::Tar(Some(CompressionCodec::Xz))
);
assert_eq!(
ArchiveType::TarZst.family(),
FormatFamily::Tar(Some(CompressionCodec::Zstd))
);
assert_eq!(
ArchiveType::Zip.family(),
FormatFamily::Seekable(crate::SeekableFormat::Zip)
);
assert_eq!(
ArchiveType::SevenZ.family(),
FormatFamily::Seekable(crate::SeekableFormat::SevenZ)
);
}
#[test]
fn test_detect_tar() {
let path = PathBuf::from("archive.tar");
assert_eq!(detect_format(&path).unwrap(), ArchiveType::Tar);
}
#[test]
fn test_detect_tar_gz_still_works() {
let path = PathBuf::from("archive.tar.gz");
assert_eq!(detect_format(&path).unwrap(), ArchiveType::TarGz);
let path2 = PathBuf::from("archive.tgz");
assert_eq!(detect_format(&path2).unwrap(), ArchiveType::TarGz);
}
#[test]
fn test_detect_tar_gz_stem_is_case_insensitive() {
for name in ["a.TAR.gz", "a.Tar.GZ", "A.TAR.GZ"] {
assert_eq!(
detect_format(&PathBuf::from(name)).unwrap(),
ArchiveType::TarGz,
"{name}"
);
}
}
#[test]
fn test_detect_bare_dot_tar_gz_still_matches() {
assert_eq!(
detect_format_from_extension(Path::new(".tar.gz")).unwrap(),
ArchiveType::TarGz
);
}
#[test]
fn test_detect_bare_gz_returns_unknown_format() {
let path = PathBuf::from("archive.gz");
assert_matches!(
detect_format(&path),
Err(ArchiveError::UnknownFormat { .. })
);
}
#[test]
fn test_detect_bare_gz_error_carries_path() {
let path = PathBuf::from("archive.gz");
let err = detect_format(&path).unwrap_err();
assert_matches!(
err,
ArchiveError::UnknownFormat { path: ref p } if p == &PathBuf::from("archive.gz")
);
}
#[test]
fn test_detect_tar_bz2() {
let path = PathBuf::from("archive.tar.bz2");
assert_eq!(detect_format(&path).unwrap(), ArchiveType::TarBz2);
let path2 = PathBuf::from("archive.tbz");
assert_eq!(detect_format(&path2).unwrap(), ArchiveType::TarBz2);
let path3 = PathBuf::from("archive.tbz2");
assert_eq!(detect_format(&path3).unwrap(), ArchiveType::TarBz2);
}
#[test]
fn test_detect_tar_xz() {
let path = PathBuf::from("archive.tar.xz");
assert_eq!(detect_format(&path).unwrap(), ArchiveType::TarXz);
let path2 = PathBuf::from("archive.txz");
assert_eq!(detect_format(&path2).unwrap(), ArchiveType::TarXz);
}
#[test]
fn test_detect_tar_zst() {
let path = PathBuf::from("archive.tar.zst");
assert_eq!(detect_format(&path).unwrap(), ArchiveType::TarZst);
let path2 = PathBuf::from("archive.tzst");
assert_eq!(detect_format(&path2).unwrap(), ArchiveType::TarZst);
}
#[test]
fn test_detect_zip() {
let path = PathBuf::from("archive.zip");
assert_eq!(detect_format(&path).unwrap(), ArchiveType::Zip);
}
#[test]
fn test_detect_zip_family_extensions() {
for ext in ZIP_FAMILY_ALIASES {
let path = PathBuf::from(format!("archive.{ext}"));
assert_eq!(
detect_format(&path).unwrap(),
ArchiveType::Zip,
"{ext} should detect as ZIP",
);
let upper = PathBuf::from(format!("archive.{}", ext.to_ascii_uppercase()));
assert_eq!(
detect_format(&upper).unwrap(),
ArchiveType::Zip,
"{ext} uppercase should detect as ZIP",
);
}
}
#[test]
fn test_detect_7z() {
let path = PathBuf::from("archive.7z");
assert_eq!(detect_format(&path).unwrap(), ArchiveType::SevenZ);
}
#[test]
fn test_detect_7z_case_insensitive() {
let path = PathBuf::from("ARCHIVE.7Z");
assert_eq!(detect_format(&path).unwrap(), ArchiveType::SevenZ);
let path2 = PathBuf::from("Archive.7Z");
assert_eq!(detect_format(&path2).unwrap(), ArchiveType::SevenZ);
}
#[test]
fn test_detect_unknown_format() {
let path = PathBuf::from("archive.rar");
assert_matches!(
detect_format(&path),
Err(ArchiveError::UnknownFormat { .. })
);
}
#[test]
fn test_detect_unknown_format_error_carries_path() {
let path = PathBuf::from("archive.rar");
let err = detect_format(&path).unwrap_err();
assert_matches!(
err,
ArchiveError::UnknownFormat { path: ref p } if p == &PathBuf::from("archive.rar")
);
}
#[test]
fn test_archive_type_sevenz_equality() {
assert_eq!(ArchiveType::SevenZ, ArchiveType::SevenZ);
assert_ne!(ArchiveType::SevenZ, ArchiveType::Zip);
}
#[test]
fn test_archive_type_sevenz_debug() {
let format = ArchiveType::SevenZ;
let debug_str = format!("{format:?}");
assert_eq!(debug_str, "SevenZ");
}
fn write_magic_file(dir: &tempfile::TempDir, name: &str, header: &[u8]) -> PathBuf {
let path = dir.path().join(name);
let mut data = vec![0u8; MAGIC_READ_LEN];
let n = header.len().min(data.len());
data[..n].copy_from_slice(&header[..n]);
std::fs::write(&path, &data).unwrap();
path
}
fn write_magic_file_at_offset(
dir: &tempfile::TempDir,
name: &str,
offset: usize,
sig: &[u8],
) -> PathBuf {
let path = dir.path().join(name);
let mut data = vec![0u8; MAGIC_READ_LEN];
let end = offset + sig.len();
if end <= data.len() {
data[offset..end].copy_from_slice(sig);
}
std::fs::write(&path, &data).unwrap();
path
}
#[test]
fn test_magic_zip_no_extension() {
let dir = tempfile::tempdir().unwrap();
let path = write_magic_file(&dir, "data", b"\x50\x4b\x03\x04");
assert_eq!(detect_format(&path).unwrap(), ArchiveType::Zip);
}
#[test]
fn test_magic_gzip_no_extension() {
let dir = tempfile::tempdir().unwrap();
let path = write_magic_file(&dir, "data", b"\x1f\x8b\x00");
assert_eq!(detect_format(&path).unwrap(), ArchiveType::TarGz);
}
#[test]
fn test_magic_bz2_no_extension() {
let dir = tempfile::tempdir().unwrap();
let path = write_magic_file(&dir, "data", b"\x42\x5a\x68");
assert_eq!(detect_format(&path).unwrap(), ArchiveType::TarBz2);
}
#[test]
fn test_magic_xz_no_extension() {
let dir = tempfile::tempdir().unwrap();
let path = write_magic_file(&dir, "data", b"\xfd\x37\x7a\x58\x5a\x00");
assert_eq!(detect_format(&path).unwrap(), ArchiveType::TarXz);
}
#[test]
fn test_magic_zstd_no_extension() {
let dir = tempfile::tempdir().unwrap();
let path = write_magic_file(&dir, "data", b"\x28\xb5\x2f\xfd");
assert_eq!(detect_format(&path).unwrap(), ArchiveType::TarZst);
}
#[test]
fn test_magic_sevenz_no_extension() {
let dir = tempfile::tempdir().unwrap();
let path = write_magic_file(&dir, "data", b"\x37\x7a\xbc\xaf\x27\x1c");
assert_eq!(detect_format(&path).unwrap(), ArchiveType::SevenZ);
}
#[test]
fn test_magic_tar_ustar_no_extension() {
let dir = tempfile::tempdir().unwrap();
let path = write_magic_file_at_offset(&dir, "data", 257, b"ustar");
assert_eq!(detect_format(&path).unwrap(), ArchiveType::Tar);
}
#[test]
fn test_magic_wins_over_wrong_extension() {
let dir = tempfile::tempdir().unwrap();
let path = write_magic_file(&dir, "archive.zip", b"\x1f\x8b\x00");
assert_eq!(detect_format(&path).unwrap(), ArchiveType::TarGz);
}
#[test]
fn test_extension_wins_when_no_magic_mismatch() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("archive.zip");
std::fs::write(&path, b"\x00\x00\x00\x00").unwrap();
assert_eq!(detect_format(&path).unwrap(), ArchiveType::Zip);
}
#[test]
fn test_nonexistent_file_without_extension_returns_unknown() {
let path = PathBuf::from("/nonexistent/path/to/archive");
assert_matches!(
detect_format(&path),
Err(ArchiveError::UnknownFormat { .. })
);
}
#[test]
fn test_nonexistent_file_with_known_extension_uses_extension() {
let path = PathBuf::from("/nonexistent/archive.zip");
assert_eq!(detect_format(&path).unwrap(), ArchiveType::Zip);
}
#[test]
fn test_magic_empty_zip_eocd_no_extension() {
let dir = tempfile::tempdir().unwrap();
let path = write_magic_file(&dir, "empty", b"\x50\x4b\x05\x06");
assert_eq!(detect_format(&path).unwrap(), ArchiveType::Zip);
}
#[test]
fn test_magic_split_zip_no_extension() {
let dir = tempfile::tempdir().unwrap();
let path = write_magic_file(&dir, "split", b"\x50\x4b\x07\x08");
assert_eq!(detect_format(&path).unwrap(), ArchiveType::Zip);
}
#[test]
fn test_magic_tar_ustar_exact_boundary_file() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("minimal.tar");
let mut data = vec![0u8; MAGIC_READ_LEN];
data[257..262].copy_from_slice(b"ustar");
std::fs::write(&path, &data).unwrap();
assert_eq!(detect_format(&path).unwrap(), ArchiveType::Tar);
}
#[test]
fn test_magic_tar_ustar_minimal_263_byte_file() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("data");
let mut data = vec![0u8; 263];
data[257..262].copy_from_slice(b"ustar");
std::fs::write(&path, &data).unwrap();
assert_eq!(detect_format(&path).unwrap(), ArchiveType::Tar);
}
#[test]
fn match_signature_prefers_offset_zero_magic_over_ustar() {
let mut header = [0u8; MAGIC_READ_LEN];
header[257..262].copy_from_slice(b"ustar");
assert_eq!(match_signature(&header), Some(ArchiveType::Tar));
header[..4].copy_from_slice(b"PK\x03\x04");
assert_eq!(match_signature(&header), Some(ArchiveType::Zip));
}
fn sealed_block(name: &[u8]) -> [u8; TAR_BLOCK_LEN] {
let mut header = tar::Header::new_ustar();
header.as_old_mut().name[..name.len()].copy_from_slice(name);
header.set_size(0);
header.set_cksum();
*header.as_bytes()
}
#[test]
fn is_ustar_header_accepts_only_a_sealed_block() {
let block = sealed_block(b"a.txt");
assert!(is_ustar_header(&block));
let mut off_by_one = block;
off_by_one[0] ^= 1;
assert!(!is_ustar_header(&off_by_one));
let mut non_numeric = block;
non_numeric[TAR_CKSUM_FIELD].copy_from_slice(b"xxxxxxx\0");
assert!(!is_ustar_header(&non_numeric));
assert!(!is_ustar_header(&block[..TAR_BLOCK_LEN - 1]));
assert!(!is_ustar_header(&[]));
let mut v7 = sealed_block(b"a.txt");
v7[USTAR_MAGIC_OFFSET..USTAR_MAGIC_OFFSET + 5].fill(0);
assert!(!is_ustar_header(&v7));
}
#[test]
fn only_a_bzip2_looking_name_loses_to_a_sealed_ustar_block() {
let cases: [(&[u8], Option<ArchiveType>); 8] = [
(b"BZhfoo.txt", Some(ArchiveType::Tar)),
(b"BZh91AY&SYfoo", Some(ArchiveType::TarBz2)),
(b"\x1f\x8bfoo", Some(ArchiveType::TarGz)),
(b"\x28\xb5\x2f\xfdfoo", Some(ArchiveType::TarZst)),
(b"\xfd7zXZ\0foo", Some(ArchiveType::TarXz)),
(b"PK\x03\x04foo", Some(ArchiveType::Zip)),
(b"7z\xbc\xaf\x27\x1cfoo", Some(ArchiveType::SevenZ)),
(b"plain.txt", Some(ArchiveType::Tar)),
];
for (name, expected) in cases {
assert_eq!(match_signature(&sealed_block(name)), expected, "{name:?}");
}
}
#[test]
fn a_v7_tar_with_a_gzip_magic_name_stays_gzip() {
let mut block = sealed_block(b"\x1f\x8bfoo");
block[USTAR_MAGIC_OFFSET..USTAR_MAGIC_OFFSET + 5].fill(0);
assert_eq!(match_signature(&block), Some(ArchiveType::TarGz));
}
#[test]
fn match_signature_handles_short_and_empty_headers() {
assert_eq!(match_signature(&[]), None);
assert_eq!(match_signature(b"\x1f"), None);
assert_eq!(match_signature(b"\x1f\x8b"), Some(ArchiveType::TarGz));
assert_eq!(match_signature(&[0; 261]), None);
}
}