From 557eb7dc60f3b014ad172d880a51766eb4bc9687 Mon Sep 17 00:00:00 2001 From: WangRunji Date: Tue, 5 Mar 2019 18:46:20 +0800 Subject: [PATCH] fix sys_open. import crate log. --- deps/sefs | 2 +- src/libos/Cargo.lock | 1 + src/libos/Cargo.toml | 1 + src/libos/src/fs/inode_file.rs | 4 +-- src/libos/src/fs/mod.rs | 57 ++++++++++++++++++++-------------- src/libos/src/lib.rs | 2 ++ 6 files changed, 41 insertions(+), 26 deletions(-) diff --git a/deps/sefs b/deps/sefs index bffa92bb..def86151 160000 --- a/deps/sefs +++ b/deps/sefs @@ -1 +1 @@ -Subproject commit bffa92bb8d2571a15ab9e7d61e7330649fb74a7c +Subproject commit def861510944e13b99570d0d4cae5342ee729d2f diff --git a/src/libos/Cargo.lock b/src/libos/Cargo.lock index 18531d0c..5544f46d 100644 --- a/src/libos/Cargo.lock +++ b/src/libos/Cargo.lock @@ -5,6 +5,7 @@ name = "Occlum" version = "0.0.1" dependencies = [ "lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "rcore-fs 0.1.0", "rcore-fs-sefs 0.1.0", "sgx_trts 1.0.6", diff --git a/src/libos/Cargo.toml b/src/libos/Cargo.toml index 80551b82..c11360b8 100644 --- a/src/libos/Cargo.toml +++ b/src/libos/Cargo.toml @@ -7,6 +7,7 @@ name = "occlum_rs" crate-type = ["staticlib"] [dependencies] +log = "0.4" lazy_static = { version = "1.1.0", features = ["spin_no_std"] } # Implies nightly rcore-fs = { path = "../../deps/sefs/rcore-fs" } rcore-fs-sefs = { path = "../../deps/sefs/rcore-fs-sefs", features = ["sgx"] } diff --git a/src/libos/src/fs/inode_file.rs b/src/libos/src/fs/inode_file.rs index 289b3f8c..cd870721 100644 --- a/src/libos/src/fs/inode_file.rs +++ b/src/libos/src/fs/inode_file.rs @@ -67,9 +67,9 @@ impl File for INodeFile { } impl INodeFile { - pub fn open(path: &str, is_readable: bool, is_writable: bool, is_append: bool) -> Result { + pub fn open(inode: Arc, is_readable: bool, is_writable: bool, is_append: bool) -> Result { Ok(INodeFile { - inode: ROOT_INODE.lookup(path)?, + inode, offset: SgxMutex::new(0), is_readable, is_writable, diff --git a/src/libos/src/fs/mod.rs b/src/libos/src/fs/mod.rs index fff278e1..67322d7e 100644 --- a/src/libos/src/fs/mod.rs +++ b/src/libos/src/fs/mod.rs @@ -11,7 +11,8 @@ mod inode_file; pub use self::file::{File, FileRef, SgxFile, StdinFile, StdoutFile}; pub use self::file_table::{FileDesc, FileTable}; pub use self::pipe::Pipe; -pub use self::inode_file::INodeFile; +pub use self::inode_file::{INodeFile, ROOT_INODE}; +use rcore_fs::vfs::{FsError, FileType, INode}; pub const O_RDONLY: u32 = 0x00000000; pub const O_WRONLY: u32 = 0x00000001; @@ -29,33 +30,35 @@ pub const O_CLOEXEC: u32 = 0x00080000; pub type off_t = i64; pub fn do_open(path: &str, flags: u32, mode: u32) -> Result { -// let open_options = { -// let mut open_options = fs_impl::OpenOptions::new(); -// -// if ((flags & O_TRUNC) != 0 || (flags & O_CREAT) != 0) { -// open_options.write(true); -// } else { -// open_options.read(true); -// } -// open_options.update(true).binary(true); -// -// open_options -// }; -// -// let mut sgx_file = { -// let key: sgx_key_128bit_t = [0 as uint8_t; 16]; -// // TODO: what if two processes open the same underlying SGX file? -// let sgx_file = open_options -// .open_ex(path, &key) -// .map_err(|e| (Errno::ENOENT, "Failed to open the SGX-protected file"))?; -// Arc::new(SgxMutex::new(sgx_file)) -// }; + info!("open: path: {:?}, flags: {:?}, mode: {:#o}", path, flags, mode); let is_readable = (flags & O_WRONLY) == 0; let is_writable = (flags & O_WRONLY != 0) || (flags & O_RDWR != 0); let is_append = (flags & O_APPEND != 0); + let is_create = (flags & O_CREAT != 0); + + let inode = + if is_create { + let (dir_path, file_name) = split_path(&path); + let dir_inode = ROOT_INODE.lookup(dir_path)?; + match dir_inode.find(file_name) { + Ok(file_inode) => { +// if flags.contains(OpenFlags::EXCLUSIVE) { +// return Err(SysError::EEXIST); +// } + file_inode + }, + Err(FsError::EntryNotFound) => { + dir_inode.create(file_name, FileType::File, mode)? + } + Err(e) => return Err(Error::from(e)), + } + } else { + ROOT_INODE.lookup(&path)? + }; + let file_ref: Arc> = Arc::new(Box::new(INodeFile::open( - path, + inode, is_readable, is_writable, is_append, @@ -168,3 +171,11 @@ pub fn do_sync() -> Result<(), Error> { extern "C" { fn ocall_sync() -> sgx_status_t; } + +/// Split a `path` str to `(base_path, file_name)` +fn split_path(path: &str) -> (&str, &str) { + let mut split = path.trim_end_matches('/').rsplitn(2, '/'); + let file_name = split.next().unwrap(); + let dir_path = split.next().unwrap_or("."); + (dir_path, file_name) +} diff --git a/src/libos/src/lib.rs b/src/libos/src/lib.rs index 98a793c4..b315f22b 100644 --- a/src/libos/src/lib.rs +++ b/src/libos/src/lib.rs @@ -17,6 +17,8 @@ extern crate sgx_trts; extern crate xmas_elf; #[macro_use] extern crate lazy_static; +#[macro_use] +extern crate log; extern crate rcore_fs; extern crate rcore_fs_sefs;