use std::io::{ErrorKind, Read, Seek};
use crate::detect::{DetectError, DetectOptions, Filesystem};
use crate::io::{offset_of, read_exact_at};
use super::ondisk::{self, 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;
};
let Some(digest_len) = superblock.csum_type.digest_len() else {
return true;
};
if superblock.csum_type != ondisk::ChecksumType::CRC32C {
return true;
}
ondisk::stored_crc32c(bytes) == ondisk::checksum(bytes)
&& ondisk::padding_is_clear(bytes, digest_len)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::btrfs::forge::Forge;
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_this_crate_does_not_compute_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::BLAKE2B;
sb.write_to(&mut bytes);
assert!(
classify(&bytes),
"the checksum no longer covers it, and it is still btrfs"
);
}
#[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
);
}
}