use crate::raw::{FileType, OpenMode, Qid, Stat};
use std::future::Future;
#[derive(Debug)]
pub struct FileError(pub u32, pub String);
impl From<std::io::Error> for FileError {
fn from(e: std::io::Error) -> Self {
match e.raw_os_error() {
Some(ose) => FileError(ose as u32, format!("{e:?}")),
None => FileError(0, "".to_owned()),
}
}
}
pub trait OpenFile {
fn iounit(&self) -> u32;
fn read_at(
&mut self,
buf: &mut [u8],
offset: u64,
) -> impl Future<Output = FileResult<u32>> + Send;
fn write_at(
&mut self,
buf: &mut [u8],
offset: u64,
) -> impl Future<Output = FileResult<u32>> + Send;
}
pub trait File
where
Self: Sized,
{
type OpenFile: OpenFile + Send;
fn stat(&self) -> impl Future<Output = FileResult<Stat>> + Send;
fn wstat(&mut self, s: &Stat) -> impl Future<Output = FileResult<()>> + Send;
fn walk(
&self,
path: &[&str],
) -> impl Future<Output = FileResult<(Option<Self>, Vec<Self>)>> + Send;
fn unlink(&mut self) -> impl Future<Output = FileResult<()>> + Send;
fn create(
&mut self,
name: &str,
perm: u16,
ty: FileType,
mode: OpenMode,
extension: &str,
) -> impl Future<Output = FileResult<Self>> + Send;
fn open(&mut self, mode: OpenMode) -> impl Future<Output = FileResult<Self::OpenFile>> + Send;
fn qid(&self) -> Qid;
}
pub type FileResult<RetT> = Result<RetT, FileError>;
pub type FilesystemResult<RetT> = Result<RetT, FileError>;
pub trait Filesystem {
type File: File + Send + 'static;
fn attach(
&self,
aname: &str,
uname: &str,
nuname: u32,
) -> impl Future<Output = FilesystemResult<Self::File>> + Send;
}