Skip to content

Commit

Permalink
Implement fat16 read + file read
Browse files Browse the repository at this point in the history
  • Loading branch information
gcarvellas committed Oct 22, 2024
1 parent b9512ae commit 67281c6
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
18 changes: 14 additions & 4 deletions src/fs/fat/fat16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ impl Fat16 {
Ok(cluster_to_use)
}

fn read_internal_recursively<T : Sized>(
fn read_internal_recursively<T: Sized>(
&mut self,
cluster: u16,
offset: usize,
Expand Down Expand Up @@ -488,8 +488,13 @@ impl FileSystem for Fat16 {
unimplemented!()
}

fn fread(&mut self, out: &mut [u16], size: u32, nmemb: u32, fd: usize) -> Result<u32, ErrorCode> {

fn fread(
&mut self,
out: &mut [u16],
size: u32,
nmemb: u32,
fd: usize,
) -> Result<u32, ErrorCode> {
let fat_desc = self.fds.get(fd).ok_or(ErrorCode::InvArg)?;
let item_binding = Arc::clone(&fat_desc.item);
let item = match &*item_binding {
Expand All @@ -500,7 +505,12 @@ impl FileSystem for Fat16 {
let offset = fat_desc.pos;

for _ in 0..nmemb {
self.read_internal(item.first_cluster(), offset.try_into()?, size.try_into()?, out)?;
self.read_internal(
item.first_cluster(),
offset.try_into()?,
size.try_into()?,
out,
)?;
}
Ok(nmemb)
}
Expand Down
17 changes: 13 additions & 4 deletions src/fs/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,12 @@ pub struct FileDescriptor {
impl FileDescriptor {
pub fn new(disk: Arc<Mutex<Disk>>) -> Result<Arc<Self>, ErrorCode> {
let mut descriptors = FILE_DESCRIPTORS.write();

if let Some((i, slot)) = descriptors.iter_mut().enumerate().find(|(_, d)| d.is_none()) {

if let Some((i, slot)) = descriptors
.iter_mut()
.enumerate()
.find(|(_, d)| d.is_none())
{
let fd = Arc::new(Self {
index: i + 1,
disk: Arc::clone(&disk),
Expand Down Expand Up @@ -80,7 +84,12 @@ fn file_get_mode_by_string(mode_str: &str) -> FileMode {
}
}

pub fn fread(out: &mut [u16], size: u32, nmemb: u32, fd: FileDescriptorIndex) -> Result<(), ErrorCode> {
pub fn fread(
out: &mut [u16],
size: u32,
nmemb: u32,
fd: FileDescriptorIndex,
) -> Result<(), ErrorCode> {
if size == 0 || nmemb == 0 || fd < 1 {
return Err(ErrorCode::InvArg);
}
Expand All @@ -91,7 +100,7 @@ pub fn fread(out: &mut [u16], size: u32, nmemb: u32, fd: FileDescriptorIndex) ->
let mut disk = desc.disk.lock();
match &mut disk.fs {
None => return Err(ErrorCode::NoFs),
Some(fs) => fs.fread(out, size, nmemb, fd)?
Some(fs) => fs.fread(out, size, nmemb, fd)?,
};
}
Ok(())
Expand Down
8 changes: 7 additions & 1 deletion src/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ pub trait FileSystem: Send + Sync {
mode: FileMode,
) -> Result<(), ErrorCode>;
fn fseek(&self, fd: usize, offset: usize, whence: FileSeekMode) -> Result<(), ErrorCode>;
fn fread(&mut self, out: &mut [u16], size: u32, nmemb: u32, fd: usize) -> Result<u32, ErrorCode>;
fn fread(
&mut self,
out: &mut [u16],
size: u32,
nmemb: u32,
fd: usize,
) -> Result<u32, ErrorCode>;
fn fstat(&self, fd: usize, stat: FileStat) -> Result<(), ErrorCode>;
fn fclose(&self, fd: usize) -> Result<(), ErrorCode>;
fn fs_resolve(disk: &Disk) -> Result<Self, ErrorCode>
Expand Down
3 changes: 1 addition & 2 deletions src/kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ use crate::memory::paging::PageAddress;
use crate::memory::paging::PageDirectoryEntry;
use crate::memory::paging::Paging256TBChunk;
use alloc::boxed::Box;
use fs::file::fread;
use core::panic::PanicInfo;
use fs::file::fopen;
use fs::file::fread;
use idt::IDT;

#[panic_handler]
Expand Down Expand Up @@ -165,7 +165,6 @@ pub extern "C" fn kernel_main() -> ! {
let mut buf = [0; 8];
fread(&mut buf, 8, 1, fd).expect("Failed to read HELLO.TXT");
println!("We read 1:/HELLO.TXT: \"{:?}\"", buf);


println!("Testing a kernel panic using Rust's unimplemented! macro.");

Expand Down

0 comments on commit 67281c6

Please sign in to comment.