Newer
Older
//! Implementation of the Second Extended Filesystem (ext2fs) filesystem
use alloc::slice;
use alloc::sync::Arc;
use alloc::vec::Vec;
use core::{mem, ptr};
use anyhow::Result;
use spin::Mutex;
use self::block::{BlockGroupDescriptor, Superblock};
use crate::kernel::device::storage::Device;
use crate::println;
#[allow(clippy::same_name_method)]
mod inode;
/// Reads the device at the given offset **in bytes** and returns the expected structure
///
/// # Safety
///
/// Must ensure that the structure `T` is present on the device at the given offset
unsafe fn read<T>(device: &Arc<Mutex<dyn Device + Send>>, offset: usize) -> Result<T> {
let mut binding = device.lock();
let block_size = binding.block_size();
let mut buffer_size = 0_usize;
while buffer_size < mem::size_of::<T>() {
buffer_size += block_size;
}
let (raw_buffer, _, _) = Vec::<u8>::with_capacity(buffer_size).into_raw_parts();
let buffer = slice::from_raw_parts_mut(raw_buffer, buffer_size);
binding.read(buffer, offset / block_size, buffer_size / block_size)?;
Ok(ptr::read::<T>((buffer.as_ptr().add(mem::size_of::<T>() * (offset % block_size))) as *const _))
}
/// Searches for an ext2 filesystem, and returns the root if found
pub fn search(device: &Arc<Mutex<dyn Device + Send>>) -> Result<Arc<Mutex<dyn super::node::Directory + Send>>> {
let superblock = Superblock::new(device);
println!("{:?}", superblock);
println!("{:?}", BlockGroupDescriptor::new(device, 0));
println!("{:?}", BlockGroupDescriptor::new(device, 1));
todo!()
}