From eef8f86e8b1b8c1530ac8e70bb3448ecd2727774 Mon Sep 17 00:00:00 2001 From: Szymon Walter Date: Mon, 19 Mar 2018 20:25:35 +0100 Subject: [PATCH] add `find_inode` function do `impl Inode` --- src/sys/inode.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/sys/inode.rs b/src/sys/inode.rs index d5cc47c..d391227 100644 --- a/src/sys/inode.rs +++ b/src/sys/inode.rs @@ -1,3 +1,8 @@ +use core::mem; + +use error::Error; +use buffer::Buffer; + /// An inode is a structure on the disk that represents a file, directory, /// symbolic link, etc. Inodes do not contain the data of the file / directory / /// etc. that they represent. Instead, they link to the blocks that actually @@ -6,6 +11,7 @@ /// array of inodes it is responsible for, and conversely every inode within a /// file system belongs to one of such tables (and one of such block groups). #[repr(C, packed)] +#[derive(Clone, Copy)] pub struct Inode { /// Type and Permissions (see below) pub type_perm: u16, @@ -60,6 +66,32 @@ pub struct Inode { pub _os_specific_2: [u8; 12], } +impl Inode { + pub unsafe fn find_inode<'a, E>( + haystack: &'a Buffer, + offset: usize, + size: usize, + ) -> Result<(Inode, usize), Error> + where + Error: From, + { + if size != mem::size_of::() { + unimplemented!("inodes with a size != 128"); + } + + let end = offset + size; + if haystack.len() < end { + return Err(Error::OutOfBounds(end)); + } + + let inode = haystack + .slice_unchecked(offset..end) + .dynamic_cast::(); + + Ok(inode) + } +} + bitflags! { pub struct TypePerm: u16 { /// FIFO