From 515f45bb91fa5f133d5fac26737d70afcdc57389 Mon Sep 17 00:00:00 2001 From: WangRunji Date: Mon, 7 May 2018 13:43:17 +0800 Subject: [PATCH] No panic when lookup failed --- Cargo.toml | 1 + src/c_interface.rs | 28 +++++++++++++++++++++++----- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a882a5b..fa77d7b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,4 +18,5 @@ static_assertions = "0.2.5" bitflags = "1.0" [features] +debug_print = [] ucore = [] \ No newline at end of file diff --git a/src/c_interface.rs b/src/c_interface.rs index 7f15e4d..d69114d 100644 --- a/src/c_interface.rs +++ b/src/c_interface.rs @@ -41,10 +41,13 @@ mod ucore { extern { pub fn kmalloc(size: usize) -> *mut u8; pub fn kfree(ptr: *mut u8); + pub fn inode_kill(inode: &mut INode); pub fn inode_get_fs(inode: *mut INode) -> *mut Fs; + pub fn inode_ref_inc(inode: *mut INode) -> i32; pub fn create_inode_for_sfs(ops: &INodeOps, fs: *mut Fs) -> *mut INode; pub fn create_fs_for_sfs(ops: &FsOps) -> *mut Fs; + pub fn __panic(file: *const u8, line: i32, fmt: *const u8, ...); pub fn cprintf(fmt: *const u8, ...); } @@ -61,11 +64,19 @@ mod macros { ($($arg:tt)*) => (unsafe{ ::c_interface::ucore::cprintf(format!($($arg)*).as_ptr())}); } + #[cfg(feature = "debug_print")] macro_rules! println { () => (print!("\n")); ($fmt:expr) => (print!(concat!($fmt, "\n"))); ($fmt:expr, $($arg:tt)*) => (print!(concat!($fmt, "\n"), $($arg)*)); } + + #[cfg(not(feature = "debug_print"))] + macro_rules! println { + () => (); + ($fmt:expr) => (); + ($fmt:expr, $($arg:tt)*) => (); + } } mod libc { @@ -262,7 +273,7 @@ pub enum ErrorCode { /// No Such File or Directory NoEntry = -16, /// Is a Directory - ISDIR = -17, + IsDir = -17, /// Not a Directory NotDir = -18, /// Cross Device-Link @@ -499,10 +510,17 @@ static INODE_OPS: INodeOps = { extern fn lookup(inode: &mut INode, path: *mut u8, inode_store: &mut *mut INode) -> ErrorCode { let path = unsafe{ libc::from_cstr(path) }; println!("inode.lookup({:?}) at {:?}", path, inode.borrow()); - let target = inode.borrow().lookup(path).unwrap(); - let fs = unsafe{ ucore::inode_get_fs(inode) }; - *inode_store = INode::get_or_create(target, fs); - ErrorCode::Ok + let target = inode.borrow().lookup(path); + match target { + Ok(target) => { + let fs = unsafe{ ucore::inode_get_fs(inode) }; + let inode = INode::get_or_create(target, fs); + unsafe { ucore::inode_ref_inc(inode) }; + *inode_store = inode; + ErrorCode::Ok + }, + Err(_) => ErrorCode::NoEntry, + } } extern fn ioctl(inode: &mut INode, op: i32, data: *mut u8) -> ErrorCode { unimplemented!();