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

214 lines
6.4 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 core::cmp;
use alloc::sync::{Weak, Arc};
2022-08-03 04:53:02 -05:00
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>;
/// The methods on this trait are to be used internally.
pub trait StorageDevice
where
Self: Send,
{
fn open(&self, node: Arc<FsNode> /* TODO: flags */) -> Result<FileDescriptor>;
fn release(&self, node: FsNode) -> Result<()>;
fn read(&self, node: &FsNode, offset: usize, size: usize, buffer: &mut Vec<u8>) -> Result<()>;
fn write(&self, node: &FsNode, offset: usize, buffer: &[u8]) -> Result<()>;
fn read_dir(&self, node: &FsNode, index: usize) -> 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.
///
/// VFS nodes are created whenever a file that doesn't have an open VFS node is
/// opened. When there are no open file descriptors to a file, the associated
/// VFS node is dropped.
#[derive(Debug)]
2022-08-03 04:53:02 -05:00
pub struct FsNode {
flags: FsFlags,
length: usize, // in bytes
inode: usize, // implementation specific identifier for the node
2022-08-03 04:53:02 -05:00
device_handle: Handle, // uniquely assigned device handle
ptr: Weak<FsNode>, // used by mountpoints and symlinks
// TODO: permissions mask
// TODO: owning user/group
2022-08-03 04:53:02 -05:00
}
impl FsNode {
2022-08-03 11:45:04 -05:00
pub fn new(
flags: FsFlags,
length: usize,
inode: usize,
2022-08-03 11:45:04 -05:00
device_handle: Handle,
ptr: Weak<FsNode>,
) -> Self {
Self {
flags,
length,
inode,
device_handle,
ptr,
}
}
/// This method opens a new file descriptor for this VFS node.
2022-08-03 04:53:02 -05:00
// TODO: make this take flags
pub fn open(self: Arc<Self>) -> Result<Handle> {
let mut state = KERNEL_STATE.lock();
let device = state
.get_storage_device(self.device_handle)
.ok_or_else(|| FsError::InvalidDevice)?;
let fd = device.open(self)?;
let handle = state.open_file_descriptor(fd);
Ok(handle)
2022-08-03 04:53:02 -05:00
}
/// This method is for closing the VFS node, which is usually done when no
/// open file descriptors for this file are left. File descriptors have
/// weak references to the VFS node, meaning it is okay to close the VFS
/// node before all file descriptors are closed.
pub fn release(self) -> Result<()> {
let state = KERNEL_STATE.lock();
let device = state
.get_storage_device(self.device_handle)
.ok_or_else(|| FsError::InvalidDevice)?;
device.release(self)?;
// self is moved into device.release, and at the end of it, self should
// be dropped.
Ok(())
2022-08-03 04:53:02 -05:00
}
/// 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
/// read a file then you probably want to read from a file descriptor.
pub fn read(&self, offset: usize, size: usize, buffer: &mut Vec<u8>) -> Result<usize> {
let state = KERNEL_STATE.lock();
let device = state
.get_storage_device(self.device_handle)
.ok_or_else(|| FsError::InvalidDevice)?;
if self.is_dir() {
Err(FsError::IsDirectory)?;
}
if offset > self.length {
Err(FsError::EndOfFile)?;
}
let readable_size = cmp::min(size, self.length - offset);
device.read(self, offset, readable_size, buffer)?;
Ok(readable_size)
2022-08-03 04:53:02 -05:00
}
/// 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
/// write to a file then you probably want to write to a file descriptor.
pub fn write(&self, offset: usize, buffer: &[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)
}
pub fn read_dir(&self, index: usize) -> 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)
}
2022-08-03 11:45:04 -05:00
pub 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
}
pub fn is_dir(&self) -> bool {
(self.flags & FsFlags::DIRECTORY) == FsFlags::DIRECTORY
}
2022-08-03 04:53:02 -05:00
}
impl Drop for FsNode {
fn drop(&mut self) {
trace!("dropping: {self:#?}")
}
}
2022-08-03 04:53:02 -05:00
bitflags! {
/// Flags associated with VFS nodes and file descriptors
///
2022-08-03 04:53:02 -05:00
/// 0x00000MST
/// T is set to 0 for files, 1 for directories
/// S is set when the file is a symbolic link
/// M is set if the file is an active mount point
pub struct FsFlags: u8 {
2022-08-03 04:53:02 -05:00
const FILE = 0b00000000;
const DIRECTORY = 0b00000001;
const SYMBOLIC_LINK = 0b00000010;
const MOUNT_POINT = 0b00000100;
}
}
/// A file descriptor.
#[derive(Debug)]
pub struct FileDescriptor {
vfs_node: Weak<FsNode>, // ptr to the associated VFS node
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?
}
impl FileDescriptor {
fn new(vfs_node: Weak<FsNode>, flags: FsFlags, length: usize, inode: usize) -> Self {
Self {
vfs_node,
flags,
length,
inode,
}
}
/// This method is for closing the file descriptor.
pub fn close(fd_handle: Handle) {
let mut state = KERNEL_STATE.lock();
state.close_file_descriptor(fd_handle);
}
}
impl Drop for FileDescriptor {
fn drop(&mut self) {
trace!("dropping: {self:#?}")
}
}
pub struct DirectoryEntry {
name: String,
inode: usize,
2022-08-03 04:53:02 -05:00
}