From eb5a3a6635499446454ff07ce91fd08126812264 Mon Sep 17 00:00:00 2001 From: Julien CLEMENT Date: Sun, 25 Dec 2022 16:36:44 +0100 Subject: [PATCH] add close and better fd assign Signed-off-by: Julien CLEMENT --- src/fd/mod.rs | 17 +++++++++++++---- src/fs/iso/fd.rs | 16 +++++++++------- src/fs/iso/iso9660.rs | 5 ++++- src/fs/iso/mod.rs | 21 +++++++++++++++------ src/fs/mod.rs | 5 +++++ src/lib.rs | 6 ++++-- 6 files changed, 50 insertions(+), 20 deletions(-) diff --git a/src/fd/mod.rs b/src/fd/mod.rs index c79edca..23aee57 100644 --- a/src/fd/mod.rs +++ b/src/fd/mod.rs @@ -4,6 +4,7 @@ use crate::utils::AsyncMutex; use alloc::{boxed::Box, collections::BTreeMap, sync::Arc}; use async_trait::async_trait; use core::cell::RefCell; +use core::sync::atomic::{AtomicU32, Ordering}; use lazy_static::lazy_static; pub type FDt = Arc>; @@ -13,12 +14,13 @@ lazy_static! { } #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] -pub struct FDId(u64); +pub struct FDId(u32); impl FDId { pub fn new() -> Self { // TODO: search for first available fd - FDId(1) + static NEXT_ID: AtomicU32 = AtomicU32::new(0); + FDId(NEXT_ID.fetch_add(1, Ordering::Relaxed)) } } @@ -33,12 +35,19 @@ impl FDTable { } } + pub fn unregister_fd(&mut self, fd: FDt) { + self.table.remove(&fd.borrow().get_fd()); + println!( + "Unregistered fd: {:?}", + fd.borrow().get_fd() + ); + } + pub fn register_fd(&mut self, fd: FDt) { - // TODO self.table.insert(fd.borrow().get_fd(), fd.clone()); println!( "Registered fd: {:?}", - self.table.get(&FDId(1)).unwrap().borrow().get_fd() + self.table.get(&FDId(0)).unwrap().borrow().get_fd() ); } } diff --git a/src/fs/iso/fd.rs b/src/fs/iso/fd.rs index 0b0a2a4..5d57cda 100644 --- a/src/fs/iso/fd.rs +++ b/src/fs/iso/fd.rs @@ -1,6 +1,5 @@ use crate::drivers::atapi::read_block; -use crate::fd::{FDId, FDt, FileDescriptor}; -use crate::println; +use crate::fd::{FDId, FDt, FileDescriptor, FD_TABLE}; use super::iso9660::{IsoDir, ISO_BLOCK_SIZE}; @@ -16,13 +15,16 @@ pub struct IsoFD { } impl IsoFD { - pub fn new(entry: &IsoDir) -> FDt { - Arc::new(RefCell::new(IsoFD { + pub async fn new(entry: &IsoDir) -> FDt { + let fd = Arc::new(RefCell::new(IsoFD { fd: FDId::new(), offset: 0, lba: entry.data_blk.le, size: entry.file_size.le, - })) + })); + + FD_TABLE.lock().await.register_fd(fd.clone()); + fd } } @@ -32,8 +34,8 @@ impl FileDescriptor for IsoFD { self.fd } - async fn write(&mut self, buf: &[u8], count: usize) -> isize { - 0 + async fn write(&mut self, _buf: &[u8], _count: usize) -> isize { + -1 } #[allow(unaligned_references)] diff --git a/src/fs/iso/iso9660.rs b/src/fs/iso/iso9660.rs index 793118d..b48b3f6 100644 --- a/src/fs/iso/iso9660.rs +++ b/src/fs/iso/iso9660.rs @@ -28,6 +28,7 @@ struct IsoPathTable { idf: [u8; 0], // Directory name, of size Self::idf_len } +#[allow(dead_code)] impl IsoPathTable { #[allow(unaligned_references)] pub fn get_idf(&self) -> &[u8] { @@ -37,11 +38,13 @@ impl IsoPathTable { // Directory structure +#[allow(dead_code)] const ISO_MAX_DIR_DEPTH: usize = 8; const ISO_DATE_LEN: usize = 7; #[repr(u8)] -#[derive(Copy, Clone)] +#[derive(Copy, Clone, PartialEq, Eq)] +#[allow(dead_code)] pub enum IsoFileType { HIDDEN = 0x1, // Hidden file ISDIR = 0x2, // Directory diff --git a/src/fs/iso/mod.rs b/src/fs/iso/mod.rs index ef7b7fe..6e4e8ce 100644 --- a/src/fs/iso/mod.rs +++ b/src/fs/iso/mod.rs @@ -3,15 +3,13 @@ pub mod iso9660; use crate::drivers::atapi::read_block; use crate::fd::{FDt, FD_TABLE}; -use crate::println; -use crate::serial_println; use crate::utils::unserialize; use super::FileSystem; use fd::IsoFD; use iso9660::{IsoDir, IsoPrimVolDesc}; -use alloc::{boxed::Box, sync::Arc, string::String, vec::Vec}; +use alloc::{boxed::Box, string::String, vec::Vec}; use async_trait::async_trait; pub struct IsoFS {} @@ -50,7 +48,14 @@ impl FileSystem for IsoFS { // Found entry if curr_entry.matches(path_component) { found = true; + + // Not the last component, go 1 directory deeper if path_component != path_split[path_split.len() - 1] { + // Not a directory + if curr_entry.file_type != iso9660::IsoFileType::ISDIR { + return None; + } + // Deeper entries curr_entry_block = read_block(curr_entry.data_blk.le).await; curr_entry = unserialize(curr_entry_block.as_ptr()); } @@ -60,14 +65,18 @@ impl FileSystem for IsoFS { // Next entry curr_entry = curr_entry.next_entry(); } + + // File not found if !found { return None; } } - let fd = IsoFD::new(curr_entry); - FD_TABLE.lock().await.register_fd(fd.clone()); - Some(fd) + Some(IsoFD::new(curr_entry).await) + } + + async fn close(&mut self, fd: FDt) { + FD_TABLE.lock().await.unregister_fd(fd.clone()); } } diff --git a/src/fs/mod.rs b/src/fs/mod.rs index e9f9d44..7d6ebde 100644 --- a/src/fs/mod.rs +++ b/src/fs/mod.rs @@ -17,6 +17,7 @@ lazy_static! { #[async_trait(?Send)] pub trait FileSystem { async fn open(&mut self, path: &str, flags: u32) -> Option; + async fn close(&mut self, fd: FDt); } pub struct VirtualFS { @@ -36,4 +37,8 @@ impl FileSystem for VirtualFS { async fn open(&mut self, path: &str, flags: u32) -> Option { self.fs.borrow_mut().open(path, flags).await } + + async fn close(&mut self, fd: FDt) { + self.fs.borrow_mut().close(fd).await + } } diff --git a/src/lib.rs b/src/lib.rs index 4aa5b95..6493d25 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -74,6 +74,8 @@ async fn get_file() { let mut buf: [u8; 100] = [0; 100]; let read = fd.borrow_mut().read(&mut buf, 100).await; - println!("{:?}", read); - println!("{:?}", alloc::str::from_utf8(&buf).unwrap()); + serial_println!("{:?}", read); + serial_println!("{}", alloc::str::from_utf8(&buf).unwrap()); + + fs::VIRTUAL_FS.lock().await.close(fd).await; }