add close and better fd assign
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
Signed-off-by: Julien CLEMENT <julien.clement@epita.fr>
This commit is contained in:
parent
446aff49ef
commit
eb5a3a6635
@ -4,6 +4,7 @@ use crate::utils::AsyncMutex;
|
|||||||
use alloc::{boxed::Box, collections::BTreeMap, sync::Arc};
|
use alloc::{boxed::Box, collections::BTreeMap, sync::Arc};
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use core::cell::RefCell;
|
use core::cell::RefCell;
|
||||||
|
use core::sync::atomic::{AtomicU32, Ordering};
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
|
|
||||||
pub type FDt = Arc<RefCell<dyn FileDescriptor>>;
|
pub type FDt = Arc<RefCell<dyn FileDescriptor>>;
|
||||||
@ -13,12 +14,13 @@ lazy_static! {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
pub struct FDId(u64);
|
pub struct FDId(u32);
|
||||||
|
|
||||||
impl FDId {
|
impl FDId {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
// TODO: search for first available fd
|
// 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) {
|
pub fn register_fd(&mut self, fd: FDt) {
|
||||||
// TODO
|
|
||||||
self.table.insert(fd.borrow().get_fd(), fd.clone());
|
self.table.insert(fd.borrow().get_fd(), fd.clone());
|
||||||
println!(
|
println!(
|
||||||
"Registered fd: {:?}",
|
"Registered fd: {:?}",
|
||||||
self.table.get(&FDId(1)).unwrap().borrow().get_fd()
|
self.table.get(&FDId(0)).unwrap().borrow().get_fd()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
use crate::drivers::atapi::read_block;
|
use crate::drivers::atapi::read_block;
|
||||||
use crate::fd::{FDId, FDt, FileDescriptor};
|
use crate::fd::{FDId, FDt, FileDescriptor, FD_TABLE};
|
||||||
use crate::println;
|
|
||||||
|
|
||||||
use super::iso9660::{IsoDir, ISO_BLOCK_SIZE};
|
use super::iso9660::{IsoDir, ISO_BLOCK_SIZE};
|
||||||
|
|
||||||
@ -16,13 +15,16 @@ pub struct IsoFD {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl IsoFD {
|
impl IsoFD {
|
||||||
pub fn new(entry: &IsoDir) -> FDt {
|
pub async fn new(entry: &IsoDir) -> FDt {
|
||||||
Arc::new(RefCell::new(IsoFD {
|
let fd = Arc::new(RefCell::new(IsoFD {
|
||||||
fd: FDId::new(),
|
fd: FDId::new(),
|
||||||
offset: 0,
|
offset: 0,
|
||||||
lba: entry.data_blk.le,
|
lba: entry.data_blk.le,
|
||||||
size: entry.file_size.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
|
self.fd
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn write(&mut self, buf: &[u8], count: usize) -> isize {
|
async fn write(&mut self, _buf: &[u8], _count: usize) -> isize {
|
||||||
0
|
-1
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(unaligned_references)]
|
#[allow(unaligned_references)]
|
||||||
|
@ -28,6 +28,7 @@ struct IsoPathTable {
|
|||||||
idf: [u8; 0], // Directory name, of size Self::idf_len
|
idf: [u8; 0], // Directory name, of size Self::idf_len
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
impl IsoPathTable {
|
impl IsoPathTable {
|
||||||
#[allow(unaligned_references)]
|
#[allow(unaligned_references)]
|
||||||
pub fn get_idf(&self) -> &[u8] {
|
pub fn get_idf(&self) -> &[u8] {
|
||||||
@ -37,11 +38,13 @@ impl IsoPathTable {
|
|||||||
|
|
||||||
// Directory structure
|
// Directory structure
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
const ISO_MAX_DIR_DEPTH: usize = 8;
|
const ISO_MAX_DIR_DEPTH: usize = 8;
|
||||||
const ISO_DATE_LEN: usize = 7;
|
const ISO_DATE_LEN: usize = 7;
|
||||||
|
|
||||||
#[repr(u8)]
|
#[repr(u8)]
|
||||||
#[derive(Copy, Clone)]
|
#[derive(Copy, Clone, PartialEq, Eq)]
|
||||||
|
#[allow(dead_code)]
|
||||||
pub enum IsoFileType {
|
pub enum IsoFileType {
|
||||||
HIDDEN = 0x1, // Hidden file
|
HIDDEN = 0x1, // Hidden file
|
||||||
ISDIR = 0x2, // Directory
|
ISDIR = 0x2, // Directory
|
||||||
|
@ -3,15 +3,13 @@ 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, FD_TABLE};
|
||||||
use crate::println;
|
|
||||||
use crate::serial_println;
|
|
||||||
use crate::utils::unserialize;
|
use crate::utils::unserialize;
|
||||||
|
|
||||||
use super::FileSystem;
|
use super::FileSystem;
|
||||||
use fd::IsoFD;
|
use fd::IsoFD;
|
||||||
use iso9660::{IsoDir, IsoPrimVolDesc};
|
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;
|
use async_trait::async_trait;
|
||||||
|
|
||||||
pub struct IsoFS {}
|
pub struct IsoFS {}
|
||||||
@ -50,7 +48,14 @@ impl FileSystem for IsoFS {
|
|||||||
// Found entry
|
// Found entry
|
||||||
if curr_entry.matches(path_component) {
|
if curr_entry.matches(path_component) {
|
||||||
found = true;
|
found = true;
|
||||||
|
|
||||||
|
// Not the last component, go 1 directory deeper
|
||||||
if path_component != path_split[path_split.len() - 1] {
|
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_block = read_block(curr_entry.data_blk.le).await;
|
||||||
curr_entry = unserialize(curr_entry_block.as_ptr());
|
curr_entry = unserialize(curr_entry_block.as_ptr());
|
||||||
}
|
}
|
||||||
@ -60,14 +65,18 @@ impl FileSystem for IsoFS {
|
|||||||
// Next entry
|
// Next entry
|
||||||
curr_entry = curr_entry.next_entry();
|
curr_entry = curr_entry.next_entry();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// File not found
|
||||||
if !found {
|
if !found {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let fd = IsoFD::new(curr_entry);
|
Some(IsoFD::new(curr_entry).await)
|
||||||
FD_TABLE.lock().await.register_fd(fd.clone());
|
}
|
||||||
Some(fd)
|
|
||||||
|
async fn close(&mut self, fd: FDt) {
|
||||||
|
FD_TABLE.lock().await.unregister_fd(fd.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,6 +17,7 @@ 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 {
|
||||||
@ -36,4 +37,8 @@ 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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -74,6 +74,8 @@ async fn get_file() {
|
|||||||
let mut buf: [u8; 100] = [0; 100];
|
let mut buf: [u8; 100] = [0; 100];
|
||||||
let read = fd.borrow_mut().read(&mut buf, 100).await;
|
let read = fd.borrow_mut().read(&mut buf, 100).await;
|
||||||
|
|
||||||
println!("{:?}", read);
|
serial_println!("{:?}", read);
|
||||||
println!("{:?}", alloc::str::from_utf8(&buf).unwrap());
|
serial_println!("{}", alloc::str::from_utf8(&buf).unwrap());
|
||||||
|
|
||||||
|
fs::VIRTUAL_FS.lock().await.close(fd).await;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user