move close in fd trait

Signed-off-by: Julien CLEMENT <julien.clement@epita.fr>
This commit is contained in:
Julien CLEMENT 2022-12-25 16:53:48 +01:00
parent eb5a3a6635
commit 23184f56a3
5 changed files with 10 additions and 14 deletions

@ -35,11 +35,11 @@ impl FDTable {
} }
} }
pub fn unregister_fd(&mut self, fd: FDt) { pub fn unregister_fd(&mut self, fd: &dyn FileDescriptor) {
self.table.remove(&fd.borrow().get_fd()); self.table.remove(&fd.get_fd());
println!( println!(
"Unregistered fd: {:?}", "Unregistered fd: {:?}",
fd.borrow().get_fd() fd.get_fd()
); );
} }
@ -57,4 +57,5 @@ pub trait FileDescriptor {
fn get_fd(&self) -> FDId; fn get_fd(&self) -> FDId;
async fn write(&mut self, buf: &[u8], count: usize) -> isize; async fn write(&mut self, buf: &[u8], count: usize) -> isize;
async fn read(&mut self, buf: &mut [u8], count: usize) -> isize; async fn read(&mut self, buf: &mut [u8], count: usize) -> isize;
async fn close(&mut self);
} }

@ -60,4 +60,8 @@ impl FileDescriptor for IsoFD {
read read
} }
async fn close(&mut self) {
FD_TABLE.lock().await.unregister_fd(self);
}
} }

@ -2,7 +2,7 @@ mod fd;
pub mod iso9660; pub mod iso9660;
use crate::drivers::atapi::read_block; use crate::drivers::atapi::read_block;
use crate::fd::{FDt, FD_TABLE}; use crate::fd::{FDt};
use crate::utils::unserialize; use crate::utils::unserialize;
use super::FileSystem; use super::FileSystem;
@ -74,10 +74,6 @@ impl FileSystem for IsoFS {
Some(IsoFD::new(curr_entry).await) Some(IsoFD::new(curr_entry).await)
} }
async fn close(&mut self, fd: FDt) {
FD_TABLE.lock().await.unregister_fd(fd.clone());
}
} }

@ -17,7 +17,6 @@ lazy_static! {
#[async_trait(?Send)] #[async_trait(?Send)]
pub trait FileSystem { pub trait FileSystem {
async fn open(&mut self, path: &str, flags: u32) -> Option<FDt>; async fn open(&mut self, path: &str, flags: u32) -> Option<FDt>;
async fn close(&mut self, fd: FDt);
} }
pub struct VirtualFS { pub struct VirtualFS {
@ -37,8 +36,4 @@ impl FileSystem for VirtualFS {
async fn open(&mut self, path: &str, flags: u32) -> Option<FDt> { async fn open(&mut self, path: &str, flags: u32) -> Option<FDt> {
self.fs.borrow_mut().open(path, flags).await self.fs.borrow_mut().open(path, flags).await
} }
async fn close(&mut self, fd: FDt) {
self.fs.borrow_mut().close(fd).await
}
} }

@ -77,5 +77,5 @@ async fn get_file() {
serial_println!("{:?}", read); serial_println!("{:?}", read);
serial_println!("{}", alloc::str::from_utf8(&buf).unwrap()); serial_println!("{}", alloc::str::from_utf8(&buf).unwrap());
fs::VIRTUAL_FS.lock().await.close(fd).await; fd.borrow_mut().close().await;
} }