Browse Source

feat(fs/syscall): 实现fchdir系统调用 (#1081)

Signed-off-by: longjin <[email protected]>
LoGin 2 months ago
parent
commit
a08191c719
2 changed files with 20 additions and 0 deletions
  1. 16 0
      kernel/src/filesystem/vfs/syscall.rs
  2. 4 0
      kernel/src/syscall/mod.rs

+ 16 - 0
kernel/src/filesystem/vfs/syscall.rs

@@ -740,6 +740,22 @@ impl Syscall {
         }
     }
 
+    pub fn fchdir(fd: i32) -> Result<usize, SystemError> {
+        let pcb = ProcessManager::current_pcb();
+        let file = pcb
+            .fd_table()
+            .read()
+            .get_file_by_fd(fd)
+            .ok_or(SystemError::EBADF)?;
+        let inode = file.inode();
+        if inode.metadata()?.file_type != FileType::Dir {
+            return Err(SystemError::ENOTDIR);
+        }
+        let path = inode.absolute_path()?;
+        pcb.basic_mut().set_cwd(path);
+        return Ok(0);
+    }
+
     /// @brief 获取当前进程的工作目录路径
     ///
     /// @param buf 指向缓冲区的指针

+ 4 - 0
kernel/src/syscall/mod.rs

@@ -238,6 +238,10 @@ impl Syscall {
                 let r = args[0] as *const u8;
                 Self::chdir(r)
             }
+            SYS_FCHDIR => {
+                let fd = args[0] as i32;
+                Self::fchdir(fd)
+            }
 
             #[allow(unreachable_patterns)]
             SYS_GETDENTS64 | SYS_GETDENTS => {