rewrite fs to init in new

This commit is contained in:
Szymon Walter 2018-03-19 18:38:33 +01:00
parent e6acaad9a9
commit f1b5d2b2a0

View file

@ -1,30 +1,61 @@
use error::Error; use error::Error;
use buffer::Buffer; use buffer::{Buffer, BufferSlice};
use sys::superblock::Superblock; use sys::superblock::Superblock;
struct Struct<T> {
pub inner: T,
pub offset: usize,
}
impl<T> From<(T, usize)> for Struct<T> {
#[inline]
fn from((inner, offset): (T, usize)) -> Struct<T> {
Struct { inner, offset }
}
}
/// Safe wrapper for raw sys structs /// Safe wrapper for raw sys structs
pub struct Ext2<B: Buffer<u8>> { pub struct Ext2<B: Buffer<u8>> {
buffer: B, buffer: B,
superblock: Option<(Superblock, usize)>, superblock: Struct<Superblock>,
} }
impl<B: Buffer<u8>> Ext2<B> impl<B: Buffer<u8>> Ext2<B>
where where
Error: From<B::Error>, Error: From<B::Error>,
{ {
pub fn new(buffer: B) -> Ext2<B> { pub fn new(buffer: B) -> Result<Ext2<B>, Error> {
Ext2 { let superblock = Superblock::find(&buffer)?.into();
buffer, Ok(Ext2 { buffer, superblock })
superblock: None,
}
} }
pub fn init(&mut self) -> Result<(), Error> { pub fn update(&mut self) -> Result<(), Error> {
let superblock = Superblock::find(&self.buffer); let slice = BufferSlice::from_cast(
match superblock { &self.superblock.inner,
Ok(sb) => Ok(self.superblock = Some(sb)), self.superblock.offset,
Err(err) => Err(err), );
} 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] #[test]
fn file() { fn file() {
let file = RefCell::new(File::open("ext2.bin").unwrap()); let file = RefCell::new(File::open("ext2.bin").unwrap());
let mut fs = Ext2::new(file); let fs = Ext2::new(file);
assert_eq!(Ok(()), fs.init()); assert_eq!(Ok(()), fs.map(|_| ()));
} }
} }