瀏覽代碼

chmod and fchmod

Paul Sajna 7 年之前
父節點
當前提交
29a6d24309
共有 3 個文件被更改,包括 36 次插入12 次删除
  1. 12 4
      src/platform/src/linux/mod.rs
  2. 22 6
      src/platform/src/redox/mod.rs
  3. 2 2
      src/stat/src/lib.rs

+ 12 - 4
src/platform/src/linux/mod.rs

@@ -32,6 +32,10 @@ pub fn chdir(path: *const c_char) -> c_int {
     e(unsafe { syscall!(CHDIR, path) }) as c_int
 }
 
+pub fn chmod(path: *const c_char, mode: mode_t) -> c_int {
+    e(unsafe { syscall!(FCHMODAT, AT_FDCWD, path, mode, 0) }) as c_int
+}
+
 pub fn chown(path: *const c_char, owner: uid_t, group: gid_t) -> c_int {
     e(unsafe { syscall!(FCHOWNAT, AT_FDCWD, path, owner as u32, group as u32) }) as c_int
 }
@@ -55,14 +59,18 @@ pub fn exit(status: c_int) -> ! {
     loop {}
 }
 
-pub fn fchown(fildes: c_int, owner: uid_t, group: gid_t) -> c_int {
-    e(unsafe { syscall!(FCHOWN, fildes, owner, group) }) as c_int
-}
-
 pub fn fchdir(fildes: c_int) -> c_int {
     e(unsafe { syscall!(FCHDIR, fildes) }) as c_int
 }
 
+pub fn fchmod(fildes: c_int, mode: mode_t) -> c_int {
+    e(unsafe { syscall!(FCHMOD, fildes, mode) }) as c_int
+}
+
+pub fn fchown(fildes: c_int, owner: uid_t, group: gid_t) -> c_int {
+    e(unsafe { syscall!(FCHOWN, fildes, owner, group) }) as c_int
+}
+
 pub fn fcntl(fildes: c_int, cmd: c_int, arg: c_int) -> c_int {
     e(unsafe { syscall!(FCNTL, fildes, cmd, arg) }) as c_int
 }

+ 22 - 6
src/platform/src/redox/mod.rs

@@ -30,10 +30,22 @@ pub fn chdir(path: *const c_char) -> c_int {
     e(syscall::chdir(path)) as c_int
 }
 
+pub fn chmod(path: *const c_char, mode: mode_t) -> c_int {
+    let path = unsafe { c_str(path) };
+    let result = syscall::open(path, 0x0001);
+    match result {
+        Err(err) => e(Err(err)) as c_int,
+        Ok(fd) => e(syscall::fchmod(fd as usize, mode)) as c_int,
+    }
+}
+
 pub fn chown(path: *const c_char, owner: uid_t, group: gid_t) -> c_int {
     let path = unsafe { c_str(path) };
-    let fd = syscall::open(path, 0x0001).unwrap();
-    e(syscall::fchown(fd as usize, owner as u32, group as u32)) as c_int
+    let result = syscall::open(path, 0x0001);
+    match result {
+        Err(err) => e(Err(err)) as c_int,
+        Ok(fd) => e(syscall::fchown(fd as usize, owner as u32, group as u32)) as c_int,
+    }
 }
 
 pub fn close(fd: c_int) -> c_int {
@@ -53,10 +65,6 @@ pub fn exit(status: c_int) -> ! {
     loop {}
 }
 
-pub fn fchown(fd: c_int, owner: uid_t, group: gid_t) -> c_int {
-    e(syscall::fchown(fd as usize, owner as u32, group as u32)) as c_int
-}
-
 pub fn fchdir(fd: c_int) -> c_int {
     let path: &mut [u8] = &mut [0; 4096];
     if e(syscall::fpath(fd as usize, path)) == !0 {
@@ -66,6 +74,14 @@ pub fn fchdir(fd: c_int) -> c_int {
     }
 }
 
+pub fn fchmod(fd: c_int, mode: mode_t) -> c_int {
+    e(syscall::fchmod(fd as usize, mode)) as c_int
+}
+
+pub fn fchown(fd: c_int, owner: uid_t, group: gid_t) -> c_int {
+    e(syscall::fchown(fd as usize, owner as u32, group as u32)) as c_int
+}
+
 pub fn fcntl(fd: c_int, cmd: c_int, args: c_int) -> c_int {
     e(syscall::fcntl(fd as usize, cmd as usize, args as usize)) as c_int
 }

+ 2 - 2
src/stat/src/lib.rs

@@ -24,12 +24,12 @@ pub struct stat {
 
 #[no_mangle]
 pub extern "C" fn chmod(path: *const c_char, mode: mode_t) -> c_int {
-    unimplemented!();
+    platform::chmod(path, mode)
 }
 
 #[no_mangle]
 pub extern "C" fn fchmod(fildes: c_int, mode: mode_t) -> c_int {
-    unimplemented!();
+    platform::fchmod(fildes, mode)
 }
 
 #[no_mangle]