use diskformat::*;
#[ derive (Clone) ]
pub enum BtrfsNode <'a> {
Internal (BtrfsInternalNode <'a>),
Leaf (BtrfsLeafNode <'a>),
}
impl <'a> BtrfsNode <'a> {
pub fn from_bytes (
bytes: & 'a [u8],
) -> Result <BtrfsNode <'a>, String> {
let calculated_checksum =
BtrfsChecksum::for_bytes (
& bytes [0x20 .. bytes.len ()]);
let header = unsafe {
& * (bytes.as_ptr () as * const BtrfsNodeHeader)
};
if header.checksum () != calculated_checksum {
return Err (
"Checksum mismatch".to_owned ());
}
if header.level () == 0 {
Ok (
BtrfsNode::Leaf (
BtrfsLeafNode::new (
bytes,
)
)
)
} else {
Ok (
BtrfsNode::Internal (
BtrfsInternalNode::new (
bytes,
)
)
)
}
}
}