add block group descriptor table loading to Ext2 struct

This commit is contained in:
Szymon Walter 2018-03-19 19:32:36 +01:00
parent f1b5d2b2a0
commit 35170f2aee

View file

@ -1,6 +1,9 @@
use alloc::Vec;
use error::Error; use error::Error;
use buffer::{Buffer, BufferSlice}; use buffer::{Buffer, BufferSlice};
use sys::superblock::Superblock; use sys::superblock::Superblock;
use sys::block_group::BlockGroupDescriptor;
struct Struct<T> { struct Struct<T> {
pub inner: T, pub inner: T,
@ -18,6 +21,7 @@ impl<T> From<(T, usize)> for Struct<T> {
pub struct Ext2<B: Buffer<u8>> { pub struct Ext2<B: Buffer<u8>> {
buffer: B, buffer: B,
superblock: Struct<Superblock>, superblock: Struct<Superblock>,
block_groups: Struct<Vec<BlockGroupDescriptor>>,
} }
impl<B: Buffer<u8>> Ext2<B> impl<B: Buffer<u8>> Ext2<B>
@ -25,8 +29,26 @@ where
Error: From<B::Error>, Error: From<B::Error>,
{ {
pub fn new(buffer: B) -> Result<Ext2<B>, Error> { pub fn new(buffer: B) -> Result<Ext2<B>, Error> {
let superblock = Superblock::find(&buffer)?.into(); let superblock = Struct::from(Superblock::find(&buffer)?);
Ok(Ext2 { buffer, superblock }) let block_size = superblock.inner.block_size();
let block_groups_offset =
(superblock.inner.first_data_block as usize + 1) * block_size;
let block_groups_count = superblock
.inner
.block_group_count()
.map(|count| count as usize)
.map_err(|(a, b)| Error::BadBlockGroupCount(a, b))?;
let block_groups = BlockGroupDescriptor::find_descriptor_table(
&buffer,
block_groups_offset,
block_groups_count,
)?;
let block_groups = Struct::from(block_groups);
Ok(Ext2 {
buffer,
superblock,
block_groups,
})
} }
pub fn update(&mut self) -> Result<(), Error> { pub fn update(&mut self) -> Result<(), Error> {
@ -46,6 +68,13 @@ where
&mut self.superblock.inner &mut self.superblock.inner
} }
pub fn block_group_count(&self) -> Result<usize, Error> {
self.superblock()
.block_group_count()
.map(|count| count as usize)
.map_err(|(a, b)| Error::BadBlockGroupCount(a, b))
}
pub fn total_block_count(&self) -> usize { pub fn total_block_count(&self) -> usize {
self.superblock().blocks_count as _ self.superblock().blocks_count as _
} }