diff --git a/kernel/Cargo.lock b/kernel/Cargo.lock index 742cb60..bf16e11 100644 --- a/kernel/Cargo.lock +++ b/kernel/Cargo.lock @@ -337,12 +337,12 @@ dependencies = [ [[package]] name = "rcore-fs" version = "0.1.0" -source = "git+https://github.com/rcore-os/rcore-fs?branch=sefs#0eba40886d36311b28d42ff2a27a0f4292e2ad9f" +source = "git+https://github.com/rcore-os/rcore-fs?branch=sefs#38dab25178eb25abaab8e6d929af354a56158d5f" [[package]] name = "rcore-fs-sfs" version = "0.1.0" -source = "git+https://github.com/rcore-os/rcore-fs?branch=sefs#0eba40886d36311b28d42ff2a27a0f4292e2ad9f" +source = "git+https://github.com/rcore-os/rcore-fs?branch=sefs#38dab25178eb25abaab8e6d929af354a56158d5f" dependencies = [ "bitvec 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/kernel/src/fs/file.rs b/kernel/src/fs/file.rs index f6d3395..fa6d837 100644 --- a/kernel/src/fs/file.rs +++ b/kernel/src/fs/file.rs @@ -36,10 +36,7 @@ impl FileHandle { } pub fn read(&mut self, buf: &mut [u8]) -> Result { - if !self.options.read { - return Err(FsError::InvalidParam); // FIXME: => EBADF - } - let len = self.inode.read_at(self.offset as usize, buf)?; + let len = self.read_at(self.offset as usize, buf)?; self.offset += len as u64; Ok(len) } @@ -53,15 +50,12 @@ impl FileHandle { } pub fn write(&mut self, buf: &[u8]) -> Result { - if !self.options.write { - return Err(FsError::InvalidParam); // FIXME: => EBADF - } - if self.options.append { - let info = self.inode.metadata()?; - self.offset = info.size as u64; - } - let len = self.inode.write_at(self.offset as usize, buf)?; - self.offset += len as u64; + let offset = match self.options.append { + true => self.inode.metadata()?.size as u64, + false => self.offset, + } as usize; + let len = self.write_at(offset, buf)?; + self.offset = (offset + len) as u64; Ok(len) } @@ -91,12 +85,11 @@ impl FileHandle { } pub fn sync_all(&mut self) -> Result<()> { - self.inode.sync() + self.inode.sync_all() } pub fn sync_data(&mut self) -> Result<()> { - // TODO: add sync_data to VFS - self.inode.sync() + self.inode.sync_data() } pub fn metadata(&self) -> Result { diff --git a/kernel/src/fs/pipe.rs b/kernel/src/fs/pipe.rs index c905d03..9521904 100644 --- a/kernel/src/fs/pipe.rs +++ b/kernel/src/fs/pipe.rs @@ -58,12 +58,12 @@ impl Pipe { macro_rules! impl_inode { () => { fn metadata(&self) -> Result { Err(FsError::NotSupported) } - fn sync(&self) -> Result<()> { Ok(()) } + fn sync_all(&self) -> Result<()> { Ok(()) } + fn sync_data(&self) -> Result<()> { Ok(()) } fn resize(&self, _len: usize) -> Result<()> { Err(FsError::NotSupported) } fn create(&self, _name: &str, _type_: FileType, _mode: u32) -> Result> { Err(FsError::NotDir) } fn unlink(&self, _name: &str) -> Result<()> { Err(FsError::NotDir) } fn link(&self, _name: &str, _other: &Arc) -> Result<()> { Err(FsError::NotDir) } - fn rename(&self, _old_name: &str, _new_name: &str) -> Result<()> { Err(FsError::NotDir) } fn move_(&self, _old_name: &str, _target: &Arc, _new_name: &str) -> Result<()> { Err(FsError::NotDir) } fn find(&self, _name: &str) -> Result> { Err(FsError::NotDir) } fn get_entry(&self, _id: usize) -> Result { Err(FsError::NotDir) } diff --git a/kernel/src/fs/stdio.rs b/kernel/src/fs/stdio.rs index bbce87b..4987fe6 100644 --- a/kernel/src/fs/stdio.rs +++ b/kernel/src/fs/stdio.rs @@ -53,12 +53,12 @@ lazy_static! { macro_rules! impl_inode { () => { fn metadata(&self) -> Result { Err(FsError::NotSupported) } - fn sync(&self) -> Result<()> { Ok(()) } + fn sync_all(&self) -> Result<()> { Ok(()) } + fn sync_data(&self) -> Result<()> { Ok(()) } fn resize(&self, _len: usize) -> Result<()> { Err(FsError::NotSupported) } fn create(&self, _name: &str, _type_: FileType, _mode: u32) -> Result> { Err(FsError::NotDir) } fn unlink(&self, _name: &str) -> Result<()> { Err(FsError::NotDir) } fn link(&self, _name: &str, _other: &Arc) -> Result<()> { Err(FsError::NotDir) } - fn rename(&self, _old_name: &str, _new_name: &str) -> Result<()> { Err(FsError::NotDir) } fn move_(&self, _old_name: &str, _target: &Arc, _new_name: &str) -> Result<()> { Err(FsError::NotDir) } fn find(&self, _name: &str) -> Result> { Err(FsError::NotDir) } fn get_entry(&self, _id: usize) -> Result { Err(FsError::NotDir) } diff --git a/kernel/src/syscall/fs.rs b/kernel/src/syscall/fs.rs index ce3c753..378ffac 100644 --- a/kernel/src/syscall/fs.rs +++ b/kernel/src/syscall/fs.rs @@ -319,12 +319,7 @@ pub fn sys_open(path: *const u8, flags: usize, mode: usize) -> SysResult { Err(e) => return Err(SysError::from(e)), } } else { - // TODO: remove "stdin:" "stdout:" - match path.as_str() { - "stdin:" => crate::fs::STDIN.clone() as Arc, - "stdout:" => crate::fs::STDOUT.clone() as Arc, - _ => proc.lookup_inode(&path)?, - } + proc.lookup_inode(&path)? }; let fd = proc.get_free_fd(); @@ -502,12 +497,7 @@ pub fn sys_rename(oldpath: *const u8, newpath: *const u8) -> SysResult { let (new_dir_path, new_file_name) = split_path(&newpath); let old_dir_inode = proc.lookup_inode(old_dir_path)?; let new_dir_inode = proc.lookup_inode(new_dir_path)?; - // TODO: merge `rename` and `move` in VFS - if Arc::ptr_eq(&old_dir_inode, &new_dir_inode) { - old_dir_inode.rename(old_file_name, new_file_name)?; - } else { - old_dir_inode.move_(old_file_name, &new_dir_inode, new_file_name)?; - } + old_dir_inode.move_(old_file_name, &new_dir_inode, new_file_name)?; Ok(0) } @@ -526,6 +516,21 @@ pub fn sys_mkdir(path: *const u8, mode: usize) -> SysResult { Ok(0) } +pub fn sys_rmdir(path: *const u8) -> SysResult { + let proc = process(); + let path = unsafe { proc.memory_set.check_and_clone_cstr(path)? }; + info!("rmdir: path: {:?}", path); + + let (dir_path, file_name) = split_path(&path); + let dir_inode = proc.lookup_inode(dir_path)?; + let file_inode = dir_inode.find(file_name)?; + if file_inode.metadata()?.type_ != FileType::Dir { + return Err(SysError::ENOTDIR); + } + dir_inode.unlink(file_name)?; + Ok(0) +} + pub fn sys_link(oldpath: *const u8, newpath: *const u8) -> SysResult { let proc = process(); let oldpath = unsafe { proc.memory_set.check_and_clone_cstr(oldpath)? }; @@ -546,6 +551,10 @@ pub fn sys_unlink(path: *const u8) -> SysResult { let (dir_path, file_name) = split_path(&path); let dir_inode = proc.lookup_inode(dir_path)?; + let file_inode = dir_inode.find(file_name)?; + if file_inode.metadata()?.type_ == FileType::Dir { + return Err(SysError::EISDIR); + } dir_inode.unlink(file_name)?; Ok(0) } @@ -556,7 +565,6 @@ pub fn sys_pipe(fds: *mut u32) -> SysResult { let mut proc = process(); proc.memory_set.check_mut_array(fds, 2)?; let (read, write) = Pipe::create_pair(); - let read_fd = proc.get_free_fd(); let read_fd = proc.get_free_fd(); proc.files.insert(read_fd, FileLike::File(FileHandle::new(Arc::new(read), OpenOptions { read: true, write: false, append: false }))); @@ -574,6 +582,11 @@ pub fn sys_pipe(fds: *mut u32) -> SysResult { Ok(0) } +pub fn sys_sync() -> SysResult { + ROOT_INODE.fs().sync()?; + Ok(0) +} + impl Process { fn get_file(&mut self, fd: usize) -> Result<&mut FileHandle, SysError> { self.files.get_mut(&fd).ok_or(SysError::EBADF).and_then(|f| { @@ -856,7 +869,6 @@ impl StatMode { FileType::BlockDevice => StatMode::BLOCK, FileType::Socket => StatMode::SOCKET, FileType::NamedPipe => StatMode::FIFO, - _ => StatMode::NULL, }; let mode = StatMode::from_bits_truncate(mode as u32); type_ | mode diff --git a/kernel/src/syscall/mod.rs b/kernel/src/syscall/mod.rs index 10158ac..09e57ea 100644 --- a/kernel/src/syscall/mod.rs +++ b/kernel/src/syscall/mod.rs @@ -99,7 +99,7 @@ pub fn syscall(id: usize, args: [usize; 6], tf: &mut TrapFrame) -> isize { SYS_GETPPID => sys_getppid(), SYS_SETPRIORITY => sys_set_priority(args[0]), // SYS_SETRLIMIT => sys_setrlimit(), -// SYS_SYNC => sys_sync(), + SYS_SYNC => sys_sync(), SYS_REBOOT => sys_reboot(args[0] as u32, args[1] as u32, args[2] as u32, args[3] as *const u8), SYS_GETTID => sys_gettid(), SYS_FUTEX => sys_futex(args[0], args[1] as u32, args[2] as i32, args[3] as *const TimeSpec), @@ -183,10 +183,6 @@ pub fn syscall(id: usize, args: [usize; 6], tf: &mut TrapFrame) -> isize { warn!("sys_sigaltstack is unimplemented"); Ok(0) } - SYS_SYNC => { - warn!("sys_sync is unimplemented"); - Ok(0) - } SYS_SET_TID_ADDRESS => { warn!("sys_set_tid_address is unimplemented"); Ok(thread::current().id()) @@ -243,6 +239,7 @@ fn x86_64_syscall(id: usize, args: [usize; 6], tf: &mut TrapFrame) -> Option sys_fork(tf), SYS_RENAME => sys_rename(args[0] as *const u8, args[1] as *const u8), SYS_MKDIR => sys_mkdir(args[0] as *const u8, args[1]), + SYS_RMDIR => sys_rmdir(args[0] as *const u8), SYS_LINK => sys_link(args[0] as *const u8, args[1] as *const u8), SYS_UNLINK => sys_unlink(args[0] as *const u8), SYS_ARCH_PRCTL => sys_arch_prctl(args[0] as i32, args[1], tf),