diff --git a/src/libos/src/fs/mod.rs b/src/libos/src/fs/mod.rs index 8fe0777b..823149c5 100644 --- a/src/libos/src/fs/mod.rs +++ b/src/libos/src/fs/mod.rs @@ -248,9 +248,7 @@ pub fn do_dup3(old_fd: FileDesc, new_fd: FileDesc, flags: u32) -> Result Result<(), Error> { - unsafe { - ocall_sync(); - } + ROOT_INODE.fs().sync()?; Ok(()) } @@ -299,6 +297,21 @@ pub fn do_mkdir(path: &str, mode: usize) -> Result<(), Error> { Ok(()) } +pub fn do_rmdir(path: &str) -> Result<(), Error> { + let current_ref = process::get_current(); + let current_process = current_ref.lock().unwrap(); + info!("rmdir: path: {:?}", path); + + let (dir_path, file_name) = split_path(&path); + let dir_inode = current_process.lookup_inode(dir_path)?; + let file_inode = dir_inode.find(file_name)?; + if file_inode.metadata()?.type_ != FileType::Dir { + return Err(Error::new(ENOTDIR, "rmdir on not directory")); + } + dir_inode.unlink(file_name)?; + Ok(()) +} + pub fn do_link(oldpath: &str, newpath: &str) -> Result<(), Error> { let current_ref = process::get_current(); let current_process = current_ref.lock().unwrap(); @@ -318,6 +331,10 @@ pub fn do_unlink(path: &str) -> Result<(), Error> { let (dir_path, file_name) = split_path(&path); let dir_inode = current_process.lookup_inode(dir_path)?; + let file_inode = dir_inode.find(file_name)?; + if file_inode.metadata()?.type_ == FileType::Dir { + return Err(Error::new(EISDIR, "unlink on directory")); + } dir_inode.unlink(file_name)?; Ok(()) } diff --git a/src/libos/src/syscall/mod.rs b/src/libos/src/syscall/mod.rs index d0ae0340..4757baa1 100644 --- a/src/libos/src/syscall/mod.rs +++ b/src/libos/src/syscall/mod.rs @@ -52,6 +52,7 @@ pub extern "C" fn dispatch_syscall( SYS_CHDIR => do_chdir(arg0 as *mut i8), SYS_RENAME => do_rename(arg0 as *const i8, arg1 as *const i8), SYS_MKDIR => do_mkdir(arg0 as *const i8, arg1 as usize), + SYS_RMDIR => do_rmdir(arg0 as *const i8), SYS_LINK => do_link(arg0 as *const i8, arg1 as *const i8), SYS_UNLINK => do_unlink(arg0 as *const i8), @@ -508,7 +509,7 @@ fn do_getcwd(buf: *mut u8, size: usize) -> Result { } safe_buf[..cwd.len()].copy_from_slice(cwd.as_bytes()); safe_buf[cwd.len()] = 0; - Ok(0) + Ok(buf as isize) } fn do_chdir(path: *const i8) -> Result { @@ -530,6 +531,12 @@ fn do_mkdir(path: *const i8, mode: usize) -> Result { Ok(0) } +fn do_rmdir(path: *const i8) -> Result { + let path = clone_cstring_safely(path)?.to_string_lossy().into_owned(); + fs::do_rmdir(&path)?; + Ok(0) +} + fn do_link(oldpath: *const i8, newpath: *const i8) -> Result { let oldpath = clone_cstring_safely(oldpath)?.to_string_lossy().into_owned(); let newpath = clone_cstring_safely(newpath)?.to_string_lossy().into_owned();