diff --git a/rcore-fs-sefs/Cargo.toml b/rcore-fs-sefs/Cargo.toml index e5d74c8..64da5ed 100644 --- a/rcore-fs-sefs/Cargo.toml +++ b/rcore-fs-sefs/Cargo.toml @@ -9,6 +9,7 @@ rcore-fs = { path = "../rcore-fs" } static_assertions = "0.3" spin = "0.4" log = "0.4" +sgx_tstd = { version = "1.0.6", optional = true } [dependencies.bitvec] version = "0.9" @@ -17,3 +18,4 @@ features = ["alloc"] [features] std = ["rcore-fs/std"] +sgx = ["sgx_tstd"] diff --git a/rcore-fs-sefs/src/dev/mod.rs b/rcore-fs-sefs/src/dev/mod.rs index 8cd51e7..38f87a7 100644 --- a/rcore-fs-sefs/src/dev/mod.rs +++ b/rcore-fs-sefs/src/dev/mod.rs @@ -2,9 +2,11 @@ use alloc::boxed::Box; use rcore_fs::vfs::FsError; +#[cfg(any(test, feature = "std"))] pub use self::std_impl::*; mod std_impl; +mod sgx_impl; /// A file stores a normal file or directory. /// diff --git a/rcore-fs-sefs/src/dev/sgx_impl.rs b/rcore-fs-sefs/src/dev/sgx_impl.rs new file mode 100644 index 0000000..03a846c --- /dev/null +++ b/rcore-fs-sefs/src/dev/sgx_impl.rs @@ -0,0 +1,90 @@ +#![cfg(feature = "sgx")] + +use std::boxed::Box; +use std::path::{Path, PathBuf}; +use std::sgxfs::{SgxFile as File, OpenOptions, remove}; +use std::io::{Read, Write, Seek, SeekFrom}; +use std::sync::SgxMutex as Mutex; +use super::{DeviceError, DevResult}; + +pub struct StdStorage { + path: PathBuf, +} + +impl StdStorage { + pub fn new(path: impl AsRef) -> Self { +// assert!(path.as_ref().is_dir()); + StdStorage { path: path.as_ref().to_path_buf() } + } +} + +impl super::Storage for StdStorage { + fn open(&self, file_id: usize) -> DevResult> { + let mut path = self.path.to_path_buf(); + path.push(format!("{}", file_id)); + let file = OpenOptions::new().read(true).write(true).open(path)?; + Ok(Box::new(LockedFile(Mutex::new(file)))) + } + + fn create(&self, file_id: usize) -> DevResult> { + let mut path = self.path.to_path_buf(); + path.push(format!("{}", file_id)); + let file = OpenOptions::new().read(true).write(true).open(path)?; + Ok(Box::new(LockedFile(Mutex::new(file)))) + } + + fn remove(&self, file_id: usize) -> DevResult<()> { + let mut path = self.path.to_path_buf(); + path.push(format!("{}", file_id)); + remove(path)?; + Ok(()) + } +} + +impl From for DeviceError { + fn from(e: std::io::Error) -> Self { + panic!("{:?}", e); + DeviceError + } +} + +pub struct LockedFile(Mutex); + +// `sgx_tstd::sgxfs::SgxFile` not impl Send ... +unsafe impl Send for LockedFile {} +unsafe impl Sync for LockedFile {} + +impl super::File for LockedFile { + fn read_at(&self, buf: &mut [u8], offset: usize) -> DevResult { + let mut file = self.0.lock().unwrap(); + let offset = offset as u64; + let real_offset = file.seek(SeekFrom::Start(offset))?; + if real_offset != offset { + return Err(DeviceError); + } + let len = file.read(buf)?; + Ok(len) + } + + fn write_at(&self, buf: &[u8], offset: usize) -> DevResult { + let mut file = self.0.lock().unwrap(); + let offset = offset as u64; + let real_offset = file.seek(SeekFrom::Start(offset))?; + if real_offset != offset { + return Err(DeviceError); + } + let len = file.write(buf)?; + Ok(len) + } + + fn set_len(&self, len: usize) -> DevResult<()> { + // NOTE: do nothing ?? + Ok(()) + } + + fn flush(&self) -> DevResult<()> { + let mut file = self.0.lock().unwrap(); + file.flush()?; + Ok(()) + } +} diff --git a/rcore-fs-sefs/src/lib.rs b/rcore-fs-sefs/src/lib.rs index 74f4f0c..a2f5075 100644 --- a/rcore-fs-sefs/src/lib.rs +++ b/rcore-fs-sefs/src/lib.rs @@ -3,6 +3,10 @@ extern crate alloc; +#[cfg(feature = "sgx")] +#[macro_use] +extern crate sgx_tstd as std; + use alloc::{boxed::Box, collections::BTreeMap, string::String, sync::{Arc, Weak}, vec::Vec}; use core::any::Any; use core::fmt::{Debug, Error, Formatter};