Cleanup, add node and header commands

This commit is contained in:
Jeremy Soller
2016-02-24 17:30:37 -07:00
parent d116ce57da
commit 8ff4863bbb
7 changed files with 162 additions and 81 deletions
+34 -7
View File
@@ -1,5 +1,4 @@
use core::{mem, slice};
use core::ops::{Deref, DerefMut};
use core::{fmt, mem, ops, slice, str};
use super::Extent;
@@ -7,19 +6,42 @@ use super::Extent;
#[repr(packed)]
pub struct Node {
pub name: [u8; 256],
pub extents: [Extent; 16],
pub mode: u64,
pub next: u64,
pub extents: [Extent; 15],
}
impl Node {
pub fn new() -> Node {
pub fn default() -> Node {
Node {
name: [0; 256],
extents: [Extent::default(); 16]
mode: 0,
next: 0,
extents: [Extent::default(); 15]
}
}
pub fn name(&self) -> Result<&str, str::Utf8Error> {
let mut len = 0;
for &b in self.name.iter() {
if b == 0 {
break;
}
len += 1;
}
str::from_utf8(&self.name[..len])
}
}
impl Deref for Node {
impl fmt::Debug for Node {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Node {{ name: {:?}, mode: {:?}, next: {:?}, extents: {:?} }}", self.name(), self.mode, self.next, self.extents)
}
}
impl ops::Deref for Node {
type Target = [u8];
fn deref(&self) -> &[u8] {
unsafe {
@@ -28,10 +50,15 @@ impl Deref for Node {
}
}
impl DerefMut for Node {
impl ops::DerefMut for Node {
fn deref_mut(&mut self) -> &mut [u8] {
unsafe {
slice::from_raw_parts_mut(self as *mut Node as *mut u8, mem::size_of::<Node>()) as &mut [u8]
}
}
}
#[test]
fn node_size_test(){
assert!(mem::size_of::<Node>() <= 512);
}