use std::io::{ErrorKind, Read, Seek};
use crate::detect::{DetectError, DetectOptions, Filesystem};
use crate::io::{offset_of, read_exact_at};
use super::ondisk::{MIRRORS, SUPER_INFO_SIZE, SuperBlock};
pub(crate) fn claim<R: Read + Seek>(
mut src: R,
options: &DetectOptions,
) -> Result<Option<Filesystem>, DetectError> {
let Some(at) = offset_of(options.base, MIRRORS[0], 1) else {
return Ok(None);
};
let bytes = match read_exact_at(&mut src, at, SUPER_INFO_SIZE) {
Ok(bytes) => bytes,
Err(e) if e.kind() == ErrorKind::UnexpectedEof => return Ok(None),
Err(e) => return Err(e.into()),
};
Ok(classify(&bytes).then_some(Filesystem::Btrfs))
}
fn classify(bytes: &[u8]) -> bool {
let Ok(superblock) = SuperBlock::read_from(bytes) else {
return false;
};
if superblock.csum_type.digest_len().is_none() {
return true;
}
superblock.csum_type.holds(bytes)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::btrfs::forge::Forge;
use crate::btrfs::ondisk;
use std::io::Cursor;
fn a_superblock() -> Vec<u8> {
let forge = Forge::new();
let mut src = forge.source();
read_exact_at(&mut src, MIRRORS[0], SUPER_INFO_SIZE).expect("the forged superblock")
}
#[test]
fn a_filesystem_this_crate_can_open_is_one_this_classifier_claims() {
let forge = Forge::new();
assert_eq!(
claim(forge.source(), &DetectOptions::new()).expect("not a failure"),
Some(Filesystem::Btrfs)
);
let mut disk = vec![0u8; 1 << 20];
let mut src = forge.source();
std::io::Read::read_to_end(&mut src, &mut disk).expect("the forged device");
assert_eq!(
claim(Cursor::new(disk), &DetectOptions::new().base(1 << 20)).expect("not a failure"),
Some(Filesystem::Btrfs)
);
}
#[test]
fn eight_bytes_that_spell_the_magic_are_not_a_superblock() {
let mut bytes = vec![0u8; SUPER_INFO_SIZE];
bytes[64..72].copy_from_slice(&ondisk::MAGIC);
assert!(!classify(&bytes));
let mut real = a_superblock();
assert!(classify(&real));
real[200] ^= 0xff;
assert!(!classify(&real));
}
#[test]
fn a_filesystem_beyond_this_reader_is_still_classified_as_what_it_is() {
let mut forge = Forge::new();
forge.amend_superblock(0, |sb| {
sb.incompat_flags = crate::btrfs::ondisk::IncompatFlags::from_bits(1 << 40);
sb.num_devices = 4;
});
assert_eq!(
claim(forge.source(), &DetectOptions::new()).expect("not a failure"),
Some(Filesystem::Btrfs)
);
assert!(crate::btrfs::Volume::open(forge.source()).is_err());
}
#[test]
fn a_checksum_the_format_does_not_define_is_not_held_against_the_image() {
let mut bytes = a_superblock();
let mut sb = SuperBlock::read_from(&bytes).expect("a superblock");
sb.csum_type = ondisk::ChecksumType::from_value(0x7f);
sb.write_to(&mut bytes);
assert!(
classify(&bytes),
"the checksum no longer covers it, and it is still btrfs"
);
}
#[test]
fn a_superblock_is_held_to_whichever_algorithm_it_names() {
for csum_type in [
ondisk::ChecksumType::CRC32C,
ondisk::ChecksumType::XXHASH64,
ondisk::ChecksumType::SHA256,
ondisk::ChecksumType::BLAKE2B,
] {
let mut bytes = a_superblock();
let mut sb = SuperBlock::read_from(&bytes).expect("a superblock");
sb.csum_type = csum_type;
sb.write_to(&mut bytes);
let field = csum_type
.field(&bytes[ondisk::CHECKSUM_COVERED_FROM..])
.expect("a defined algorithm");
bytes[..ondisk::CSUM_FIELD_LEN].copy_from_slice(&field);
assert!(classify(&bytes), "{csum_type:?}");
bytes[ondisk::CSUM_FIELD_LEN] ^= 1;
assert!(
!classify(&bytes),
"{csum_type:?}, a byte past the field changed"
);
}
}
#[test]
fn a_short_source_is_not_ours_rather_than_a_failure() {
let options = DetectOptions::new();
for len in [0usize, 512, 1 << 16] {
assert_eq!(
claim(Cursor::new(vec![0u8; len]), &options).expect("not a failure"),
None,
"a {len}-byte source"
);
}
assert_eq!(
claim(
Cursor::new(vec![0u8; 1 << 20]),
&DetectOptions::new().base(u64::MAX - 1)
)
.expect("not a failure"),
None
);
}
}