1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
use std::mem;

use diskformat::*;

#[ derive (Clone) ]
pub struct BtrfsInternalNode <'a> {
	/*position: usize,*/
	bytes: & 'a [u8],
}

pub struct BtrfsInternalNodeItems <'a> {
	node: BtrfsInternalNode <'a>,
	index: u32,
}

impl <'a> BtrfsInternalNode <'a> {

	pub fn new (
		/*position: usize,*/
		bytes: & 'a [u8],
	) -> BtrfsInternalNode <'a> {

		BtrfsInternalNode {
			/*position: position,*/
			bytes: bytes,
		}

	}

	pub fn header (
		& 'a self,
	) -> & 'a BtrfsNodeHeader {

		unsafe {
			& * (self.bytes.as_ptr () as * const BtrfsNodeHeader)
		}

	}

	pub fn is_leaf (
		& self,
	) -> bool {
		false
	}

	pub fn items (
		self,
	) -> BtrfsInternalNodeItems <'a> {

		BtrfsInternalNodeItems {
			node: self,
			index: 0,
		}

	}

	pub fn checksum (& self) -> BtrfsChecksum {
		self.header ().checksum ()
	}

	pub fn fs_uuid (& self) -> BtrfsUuid {
		self.header ().fs_uuid ()
	}

	pub fn tree_id (& self) -> u64 {
		self.header ().tree_id ()
	}

	pub fn num_items (& self) -> u32 {
		self.header ().num_items ()
	}

	pub fn level (& self) -> u8 {
		self.header ().level ()
	}

}

impl <'a> Iterator for BtrfsInternalNodeItems <'a> {

	type Item = & 'a BtrfsInternalItem;

	fn next (
		& mut self,
	) -> Option <& 'a BtrfsInternalItem> {

		if self.index < self.node.num_items () {

			let item_start =
				mem::size_of::<BtrfsNodeHeader> ()
				+ self.index as usize
					* mem::size_of::<BtrfsInternalItem> ();

			let item = unsafe {
				& * (
					item_start
						as * const u8
						as * const BtrfsInternalItem
				)
			};

			self.index += 1;

			Some (item)

		} else {

			None

		}

	}

}

// ex: noet ts=4 filetype=rust