btrfs2/diskformat/node/
node.rs

1use diskformat::*;
2
3#[ derive (Clone) ]
4pub enum BtrfsNode <'a> {
5	Internal (BtrfsInternalNode <'a>),
6	Leaf (BtrfsLeafNode <'a>),
7}
8
9impl <'a> BtrfsNode <'a> {
10
11	pub fn from_bytes (
12		/*position: usize,*/
13		bytes: & 'a [u8],
14	) -> Result <BtrfsNode <'a>, String> {
15
16		// verify checksum
17
18		let calculated_checksum =
19			BtrfsChecksum::for_bytes (
20				& bytes [0x20 .. bytes.len ()]);
21
22		let header = unsafe {
23			& * (bytes.as_ptr () as * const BtrfsNodeHeader)
24		};
25
26		if header.checksum () != calculated_checksum {
27
28			return Err (
29				"Checksum mismatch".to_owned ());
30
31		}
32
33		// construct
34
35		if header.level () == 0 {
36
37			Ok (
38				BtrfsNode::Leaf (
39					BtrfsLeafNode::new (
40						/*position,*/
41						bytes,
42					)
43				)
44			)
45
46		} else {
47
48			Ok (
49				BtrfsNode::Internal (
50					BtrfsInternalNode::new (
51						/*position,*/
52						bytes,
53					)
54				)
55			)
56
57		}
58
59	}
60
61}
62
63// ex: noet ts=4 filetype=rust