impl SEFS device for SgxFile

master
WangRunji 6 years ago
parent f3ed1acfe0
commit 7ecef38049

@ -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"]

@ -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.
///

@ -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<Path>) -> 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<Box<super::File>> {
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<Box<super::File>> {
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<std::io::Error> for DeviceError {
fn from(e: std::io::Error) -> Self {
panic!("{:?}", e);
DeviceError
}
}
pub struct LockedFile(Mutex<File>);
// `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<usize> {
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<usize> {
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(())
}
}

@ -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};

Loading…
Cancel
Save