1
0
Fork 0
forked from koniifer/ableos
ableos-framebuffer/ableos/src/filesystem/mod.rs

119 lines
3.3 KiB
Rust
Raw Normal View History

2022-08-03 04:53:02 -05:00
/*
* Copyright (c) 2022, Umut İnan Erdoğan <umutinanerdogan@pm.me>
*
* SPDX-License-Identifier: MPL-2.0
*/
pub mod errors;
pub mod ext2;
2022-08-03 04:53:02 -05:00
use alloc::rc::Weak;
use bitflags::bitflags;
use crate::{handle::Handle, KERNEL_STATE};
2022-08-03 04:53:02 -05:00
use self::errors::FsError;
use FsResult as Result;
2022-08-03 04:53:02 -05:00
pub type FsResult<T> = core::result::Result<T, FsError>;
pub trait StorageDevice
where
Self: Send,
{
fn open(&self, node: &FsNode /* TODO: flags */) -> Result<Handle>;
fn close(&self, node: &FsNode) -> Result<()>;
fn read(&self, node: &FsNode, offset: u32, size: u32) -> Result<Box<[u8]>>;
fn write(&self, node: &FsNode, offset: u32, buffer: Box<[u8]>) -> Result<()>;
fn read_dir(&self, node: &FsNode, index: u32) -> Result<DirectoryEntry>;
fn find_dir(&self, node: &FsNode, name: &str) -> Result<FsNode>;
}
2022-08-03 04:53:02 -05:00
/// A VFS node, that can either be a file or a directory.
pub struct FsNode {
flags: FsNodeFlags,
length: u32, // in bytes
inode: u32, // implementation specific identifier for the node
device_handle: Handle, // uniquely assigned device handle
ptr: Weak<FsNode>, // used by mountpoints and symlinks
// todo: permissions mask
// todo: owning user/group
}
impl FsNode {
// TODO: make this take flags
fn open(&self) -> Result<Handle> {
let state = KERNEL_STATE.lock();
let device = state
.get_storage_device(self.device_handle)
.ok_or_else(|| FsError::InvalidDevice)?;
device.open(self)
2022-08-03 04:53:02 -05:00
}
fn close(&self) -> Result<()> {
let state = KERNEL_STATE.lock();
let device = state
.get_storage_device(self.device_handle)
.ok_or_else(|| FsError::InvalidDevice)?;
device.close(self)
2022-08-03 04:53:02 -05:00
}
fn read(&self, offset: u32, size: u32) -> Result<Box<[u8]>> {
let state = KERNEL_STATE.lock();
let device = state
.get_storage_device(self.device_handle)
.ok_or_else(|| FsError::InvalidDevice)?;
device.read(self, offset, size)
2022-08-03 04:53:02 -05:00
}
fn write(&self, offset: u32, buffer: Box<[u8]>) -> Result<()> {
let state = KERNEL_STATE.lock();
let device = state
.get_storage_device(self.device_handle)
.ok_or_else(|| FsError::InvalidDevice)?;
device.write(self, offset, buffer)
}
fn read_dir(&self, index: u32) -> Result<DirectoryEntry> {
let state = KERNEL_STATE.lock();
let device = state
.get_storage_device(self.device_handle)
.ok_or_else(|| FsError::InvalidDevice)?;
device.read_dir(self, index)
}
fn find_dir(&self, name: &str) -> Result<FsNode> {
let state = KERNEL_STATE.lock();
let device = state
.get_storage_device(self.device_handle)
.ok_or_else(|| FsError::InvalidDevice)?;
device.find_dir(self, name)
2022-08-03 04:53:02 -05:00
}
}
bitflags! {
/// Flags associated with VFS nodes.
///
/// 0x00000MST
/// T is set to 0 for files, 1 for directories
/// S is set when the node is a symbolic link
/// M is set if the node is an active mount point
pub struct FsNodeFlags: u8 {
const FILE = 0b00000000;
const DIRECTORY = 0b00000001;
const SYMBOLIC_LINK = 0b00000010;
const MOUNT_POINT = 0b00000100;
}
}
pub struct DirectoryEntry {
name: String,
inode: u32,
2022-08-03 04:53:02 -05:00
}