forked from koniifer/ableos
vfs+ext2: full implementation of FsNode::open()
This commit is contained in:
parent
aa3667714b
commit
cdb6632f98
|
@ -9,7 +9,7 @@ use ext2::sector::SectorSize;
|
||||||
use ext2::volume::Volume;
|
use ext2::volume::Volume;
|
||||||
|
|
||||||
use super::errors::FsError;
|
use super::errors::FsError;
|
||||||
use super::{FsResult as Result, StorageDevice};
|
use super::{FsResult as Result, StorageDevice, DirectoryEntry, FsNode, FileDescriptor};
|
||||||
|
|
||||||
pub struct Ext2StorageDevice<S, V>
|
pub struct Ext2StorageDevice<S, V>
|
||||||
where
|
where
|
||||||
|
@ -36,32 +36,32 @@ where
|
||||||
S: SectorSize + Send,
|
S: SectorSize + Send,
|
||||||
V: Volume<u8, S> + Send,
|
V: Volume<u8, S> + Send,
|
||||||
{
|
{
|
||||||
fn open(&self, node: &super::FsNode /* TODO: flags */) -> Result<crate::handle::Handle> {
|
fn open(&self, node: &super::FsNode /* TODO: flags */) -> Result<FileDescriptor> {
|
||||||
let inode = self
|
let inode = self
|
||||||
.fs
|
.fs
|
||||||
.inode_nth(node.inode as usize)
|
.inode_nth(node.inode as usize)
|
||||||
.ok_or_else(|| FsError::InodeNotFound)?;
|
.ok_or_else(|| FsError::InodeNotFound)?;
|
||||||
|
|
||||||
todo!()
|
Ok(FileDescriptor::new(node.flags, inode.size(), node.inode))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn close(&self, node: &super::FsNode) -> Result<()> {
|
fn close(&self, node: &super::FsNode) -> Result<()> {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read(&self, node: &super::FsNode, offset: u32, size: u32) -> Result<Box<[u8]>> {
|
fn read(&self, node: &super::FsNode, offset: usize, size: usize) -> Result<Box<[u8]>> {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write(&self, node: &super::FsNode, offset: u32, buffer: Box<[u8]>) -> Result<()> {
|
fn write(&self, node: &super::FsNode, offset: usize, buffer: Box<[u8]>) -> Result<()> {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_dir(&self, node: &super::FsNode, index: u32) -> Result<super::DirectoryEntry> {
|
fn read_dir(&self, node: &super::FsNode, index: usize) -> Result<DirectoryEntry> {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn find_dir(&self, node: &super::FsNode, name: &str) -> Result<super::FsNode> {
|
fn find_dir(&self, node: &super::FsNode, name: &str) -> Result<FsNode> {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,11 +21,11 @@ pub trait StorageDevice
|
||||||
where
|
where
|
||||||
Self: Send,
|
Self: Send,
|
||||||
{
|
{
|
||||||
fn open(&self, node: &FsNode /* TODO: flags */) -> Result<Handle>;
|
fn open(&self, node: &FsNode /* TODO: flags */) -> Result<FileDescriptor>;
|
||||||
fn close(&self, node: &FsNode) -> Result<()>;
|
fn close(&self, node: &FsNode) -> Result<()>;
|
||||||
fn read(&self, node: &FsNode, offset: u32, size: u32) -> Result<Box<[u8]>>;
|
fn read(&self, node: &FsNode, offset: usize, size: usize) -> Result<Box<[u8]>>;
|
||||||
fn write(&self, node: &FsNode, offset: u32, buffer: Box<[u8]>) -> Result<()>;
|
fn write(&self, node: &FsNode, offset: usize, buffer: Box<[u8]>) -> Result<()>;
|
||||||
fn read_dir(&self, node: &FsNode, index: u32) -> Result<DirectoryEntry>;
|
fn read_dir(&self, node: &FsNode, index: usize) -> Result<DirectoryEntry>;
|
||||||
fn find_dir(&self, node: &FsNode, name: &str) -> Result<FsNode>;
|
fn find_dir(&self, node: &FsNode, name: &str) -> Result<FsNode>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,21 +35,21 @@ where
|
||||||
/// opened. When there are no open file descriptors to a file, the associated
|
/// opened. When there are no open file descriptors to a file, the associated
|
||||||
/// VFS node is dropped.
|
/// VFS node is dropped.
|
||||||
pub struct FsNode {
|
pub struct FsNode {
|
||||||
flags: FsNodeFlags,
|
flags: FsFlags,
|
||||||
length: u32, // in bytes
|
length: usize, // in bytes
|
||||||
fd_count: u32, // count of open file descriptors
|
fd_count: usize, // count of open file descriptors
|
||||||
inode: u32, // implementation specific identifier for the node
|
inode: usize, // implementation specific identifier for the node
|
||||||
device_handle: Handle, // uniquely assigned device handle
|
device_handle: Handle, // uniquely assigned device handle
|
||||||
ptr: Weak<FsNode>, // used by mountpoints and symlinks
|
ptr: Weak<FsNode>, // used by mountpoints and symlinks
|
||||||
// todo: permissions mask
|
// TODO: permissions mask
|
||||||
// todo: owning user/group
|
// TODO: owning user/group
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FsNode {
|
impl FsNode {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
flags: FsNodeFlags,
|
flags: FsFlags,
|
||||||
length: u32,
|
length: usize,
|
||||||
inode: u32,
|
inode: usize,
|
||||||
device_handle: Handle,
|
device_handle: Handle,
|
||||||
ptr: Weak<FsNode>,
|
ptr: Weak<FsNode>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
|
@ -65,13 +65,16 @@ impl FsNode {
|
||||||
|
|
||||||
/// This method opens a new file descriptor for this VFS node.
|
/// This method opens a new file descriptor for this VFS node.
|
||||||
// TODO: make this take flags
|
// TODO: make this take flags
|
||||||
pub fn open(&self) -> Result<Handle> {
|
pub fn open(&mut self) -> Result<Handle> {
|
||||||
let state = KERNEL_STATE.lock();
|
let state = KERNEL_STATE.lock();
|
||||||
let device = state
|
let device = state
|
||||||
.get_storage_device(self.device_handle)
|
.get_storage_device(self.device_handle)
|
||||||
.ok_or_else(|| FsError::InvalidDevice)?;
|
.ok_or_else(|| FsError::InvalidDevice)?;
|
||||||
|
let fd = device.open(self)?;
|
||||||
|
let mut kernel_state = KERNEL_STATE.lock();
|
||||||
|
let handle = kernel_state.add_file_descriptor(fd);
|
||||||
|
|
||||||
device.open(self)
|
Ok(handle)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This method is for closing the VFS node, which is done when no open file
|
/// This method is for closing the VFS node, which is done when no open file
|
||||||
|
@ -88,7 +91,7 @@ impl FsNode {
|
||||||
/// This method reads from the VFS node without opening a new file
|
/// This method reads from the VFS node without opening a new file
|
||||||
/// descriptor. This is intended to be used internally, if you're trying to
|
/// descriptor. This is intended to be used internally, if you're trying to
|
||||||
/// read a file then you probably want to read from a file descriptor.
|
/// read a file then you probably want to read from a file descriptor.
|
||||||
pub fn read(&self, offset: u32, size: u32) -> Result<Box<[u8]>> {
|
pub fn read(&self, offset: usize, size: usize) -> Result<Box<[u8]>> {
|
||||||
let state = KERNEL_STATE.lock();
|
let state = KERNEL_STATE.lock();
|
||||||
let device = state
|
let device = state
|
||||||
.get_storage_device(self.device_handle)
|
.get_storage_device(self.device_handle)
|
||||||
|
@ -100,7 +103,7 @@ impl FsNode {
|
||||||
/// This method writes to the VFS node without opening a new file
|
/// This method writes to the VFS node without opening a new file
|
||||||
/// descriptor. This is intended to be used internally, if you're trying to
|
/// descriptor. This is intended to be used internally, if you're trying to
|
||||||
/// write to a file then you probably want to write to a file descriptor.
|
/// write to a file then you probably want to write to a file descriptor.
|
||||||
pub fn write(&self, offset: u32, buffer: Box<[u8]>) -> Result<()> {
|
pub fn write(&self, offset: usize, buffer: Box<[u8]>) -> Result<()> {
|
||||||
let state = KERNEL_STATE.lock();
|
let state = KERNEL_STATE.lock();
|
||||||
let device = state
|
let device = state
|
||||||
.get_storage_device(self.device_handle)
|
.get_storage_device(self.device_handle)
|
||||||
|
@ -109,7 +112,7 @@ impl FsNode {
|
||||||
device.write(self, offset, buffer)
|
device.write(self, offset, buffer)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_dir(&self, index: u32) -> Result<DirectoryEntry> {
|
pub fn read_dir(&self, index: usize) -> Result<DirectoryEntry> {
|
||||||
let state = KERNEL_STATE.lock();
|
let state = KERNEL_STATE.lock();
|
||||||
let device = state
|
let device = state
|
||||||
.get_storage_device(self.device_handle)
|
.get_storage_device(self.device_handle)
|
||||||
|
@ -129,13 +132,13 @@ impl FsNode {
|
||||||
}
|
}
|
||||||
|
|
||||||
bitflags! {
|
bitflags! {
|
||||||
/// Flags associated with VFS nodes.
|
/// Flags associated with VFS nodes and file descriptors
|
||||||
///
|
///
|
||||||
/// 0x00000MST
|
/// 0x00000MST
|
||||||
/// T is set to 0 for files, 1 for directories
|
/// T is set to 0 for files, 1 for directories
|
||||||
/// S is set when the node is a symbolic link
|
/// S is set when the file is a symbolic link
|
||||||
/// M is set if the node is an active mount point
|
/// M is set if the file is an active mount point
|
||||||
pub struct FsNodeFlags: u8 {
|
pub struct FsFlags: u8 {
|
||||||
const FILE = 0b00000000;
|
const FILE = 0b00000000;
|
||||||
const DIRECTORY = 0b00000001;
|
const DIRECTORY = 0b00000001;
|
||||||
const SYMBOLIC_LINK = 0b00000010;
|
const SYMBOLIC_LINK = 0b00000010;
|
||||||
|
@ -144,9 +147,28 @@ bitflags! {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A file descriptor.
|
/// A file descriptor.
|
||||||
pub struct FileDescriptor {}
|
pub struct FileDescriptor {
|
||||||
|
flags: FsFlags,
|
||||||
|
length: usize, // in bytes
|
||||||
|
inode: usize, // implementation specific identifier for the node
|
||||||
|
// TODO: permissions mask
|
||||||
|
// TODO: owning user/group
|
||||||
|
// TODO: read file position?
|
||||||
|
// FIXME: I'm not sure if this needs to have a ptr to the VFS node,
|
||||||
|
// figure that out.
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FileDescriptor {
|
||||||
|
fn new(flags: FsFlags, length: usize, inode: usize) -> Self {
|
||||||
|
Self {
|
||||||
|
flags,
|
||||||
|
length,
|
||||||
|
inode,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub struct DirectoryEntry {
|
pub struct DirectoryEntry {
|
||||||
name: String,
|
name: String,
|
||||||
inode: u32,
|
inode: usize,
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ use hashbrown::HashMap;
|
||||||
use spin::Lazy;
|
use spin::Lazy;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
filesystem::StorageDevice,
|
filesystem::{StorageDevice, FileDescriptor},
|
||||||
handle::{Handle, HandleResource},
|
handle::{Handle, HandleResource},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -12,6 +12,7 @@ pub static KERNEL_STATE: Lazy<spin::Mutex<KernelInternalState>> =
|
||||||
pub struct KernelInternalState {
|
pub struct KernelInternalState {
|
||||||
pub hostname: String,
|
pub hostname: String,
|
||||||
storage_devices: HashMap<Handle, Box<dyn StorageDevice>>,
|
storage_devices: HashMap<Handle, Box<dyn StorageDevice>>,
|
||||||
|
fd_table: HashMap<Handle, FileDescriptor>,
|
||||||
should_shutdown: bool,
|
should_shutdown: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,6 +21,7 @@ impl KernelInternalState {
|
||||||
Self {
|
Self {
|
||||||
should_shutdown: false,
|
should_shutdown: false,
|
||||||
storage_devices: HashMap::new(),
|
storage_devices: HashMap::new(),
|
||||||
|
fd_table: HashMap::new(),
|
||||||
hostname: "".to_string(),
|
hostname: "".to_string(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -28,15 +30,22 @@ impl KernelInternalState {
|
||||||
self.hostname = hostname;
|
self.hostname = hostname;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_storage_device(&mut self, device: impl StorageDevice + Send + 'static) {
|
pub fn add_storage_device(&mut self, device: impl StorageDevice + Send + 'static) -> Handle {
|
||||||
self.storage_devices
|
let handle = Handle::new(HandleResource::StorageDevice);
|
||||||
.insert(Handle::new(HandleResource::StorageDevice), Box::new(device));
|
self.storage_devices.insert(handle, Box::new(device));
|
||||||
|
handle
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_storage_device(&self, handle: Handle) -> Option<&dyn StorageDevice> {
|
pub fn get_storage_device(&self, handle: Handle) -> Option<&dyn StorageDevice> {
|
||||||
self.storage_devices.get(&handle).map(|d| &**d)
|
self.storage_devices.get(&handle).map(|d| &**d)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn add_file_descriptor(&mut self, fd: FileDescriptor) -> Handle {
|
||||||
|
let handle = Handle::new(HandleResource::FileDescriptor);
|
||||||
|
self.fd_table.insert(handle, fd);
|
||||||
|
handle
|
||||||
|
}
|
||||||
|
|
||||||
pub fn shutdown(&mut self) {
|
pub fn shutdown(&mut self) {
|
||||||
self.should_shutdown = true;
|
self.should_shutdown = true;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue