From f1b5d2b2a085eb69b029f6a3dfe8974ec3466a35 Mon Sep 17 00:00:00 2001 From: Szymon Walter Date: Mon, 19 Mar 2018 18:38:33 +0100 Subject: [PATCH] rewrite fs to init in `new` --- src/fs.rs | 61 +++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 46 insertions(+), 15 deletions(-) diff --git a/src/fs.rs b/src/fs.rs index 1090b41..7d67234 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -1,30 +1,61 @@ use error::Error; -use buffer::Buffer; +use buffer::{Buffer, BufferSlice}; use sys::superblock::Superblock; +struct Struct { + pub inner: T, + pub offset: usize, +} + +impl From<(T, usize)> for Struct { + #[inline] + fn from((inner, offset): (T, usize)) -> Struct { + Struct { inner, offset } + } +} + /// Safe wrapper for raw sys structs pub struct Ext2> { buffer: B, - superblock: Option<(Superblock, usize)>, + superblock: Struct, } impl> Ext2 where Error: From, { - pub fn new(buffer: B) -> Ext2 { - Ext2 { - buffer, - superblock: None, - } + pub fn new(buffer: B) -> Result, Error> { + let superblock = Superblock::find(&buffer)?.into(); + Ok(Ext2 { buffer, superblock }) } - pub fn init(&mut self) -> Result<(), Error> { - let superblock = Superblock::find(&self.buffer); - match superblock { - Ok(sb) => Ok(self.superblock = Some(sb)), - Err(err) => Err(err), - } + pub fn update(&mut self) -> Result<(), Error> { + let slice = BufferSlice::from_cast( + &self.superblock.inner, + self.superblock.offset, + ); + let commit = slice.commit(); + self.buffer.commit(commit).map_err(|err| Error::from(err)) + } + + fn superblock(&self) -> &Superblock { + &self.superblock.inner + } + + fn superblock_mut(&mut self) -> &mut Superblock { + &mut self.superblock.inner + } + + pub fn total_block_count(&self) -> usize { + self.superblock().blocks_count as _ + } + + pub fn free_block_count(&self) -> usize { + self.superblock().free_blocks_count as _ + } + + pub fn block_size(&self) -> usize { + self.superblock().block_size() } } @@ -46,7 +77,7 @@ mod tests { #[test] fn file() { let file = RefCell::new(File::open("ext2.bin").unwrap()); - let mut fs = Ext2::new(file); - assert_eq!(Ok(()), fs.init()); + let fs = Ext2::new(file); + assert_eq!(Ok(()), fs.map(|_| ())); } }