浏览代码

Remove stat and lstat, which can be replaced with open and fstat

Jeremy Soller 6 年之前
父节点
当前提交
ff32c8cbbd

+ 1 - 0
Cargo.lock

@@ -586,6 +586,7 @@ name = "sys_stat"
 version = "0.1.0"
 dependencies = [
  "cbindgen 0.5.2",
+ "fcntl 0.1.0",
  "platform 0.1.0",
 ]
 

+ 7 - 5
src/fcntl/src/linux.rs

@@ -4,10 +4,12 @@ pub const O_RDONLY: c_int = 0x0000;
 pub const O_WRONLY: c_int = 0x0001;
 pub const O_RDWR: c_int = 0x0002;
 pub const O_CREAT: c_int = 0x0040;
+pub const O_EXCL: c_int = 0x0080;
 pub const O_TRUNC: c_int = 0x0200;
+pub const O_APPEND: c_int = 0x0400;
+pub const O_NONBLOCK: c_int = 0x0800;
+pub const O_DIRECTORY: c_int = 0x1_0000;
+pub const O_NOFOLLOW: c_int = 0x2_0000;
+pub const O_CLOEXEC: c_int = 0x8_0000;
+pub const O_PATH: c_int = 0x20_0000;
 pub const O_ACCMODE: c_int = O_RDONLY | O_WRONLY | O_RDWR;
-pub const O_APPEND: c_int = 0o2000;
-pub const O_CLOEXEC: c_int = 0o2_000_000;
-pub const O_DIRECTORY: c_int = 0o200_000;
-pub const O_EXCL: c_int = 0o200;
-pub const O_NONBLOCK: c_int = 0o4000;

+ 1 - 1
src/fcntl/src/redox.rs

@@ -14,7 +14,7 @@ pub const O_CREAT: c_int = 0x0200_0000;
 pub const O_TRUNC: c_int = 0x0400_0000;
 pub const O_EXCL: c_int = 0x0800_0000;
 pub const O_DIRECTORY: c_int = 0x1000_0000;
-pub const O_STAT: c_int = 0x2000_0000;
+pub const O_PATH: c_int = 0x2000_0000;
 pub const O_SYMLINK: c_int = 0x4000_0000;
 pub const O_NOFOLLOW: c_int = 0x8000_0000;
 pub const O_ACCMODE: c_int = O_RDONLY | O_WRONLY | O_RDWR;

+ 0 - 8
src/platform/src/linux/mod.rs

@@ -233,10 +233,6 @@ impl Pal for Sys {
         e(unsafe { syscall!(LSEEK, fildes, offset, whence) }) as off_t
     }
 
-    fn lstat(file: *const c_char, buf: *mut stat) -> c_int {
-        e(unsafe { syscall!(NEWFSTATAT, AT_FDCWD, file, buf, AT_SYMLINK_NOFOLLOW) }) as c_int
-    }
-
     fn mkdir(path: *const c_char, mode: mode_t) -> c_int {
         e(unsafe { syscall!(MKDIRAT, AT_FDCWD, path, mode) }) as c_int
     }
@@ -310,10 +306,6 @@ impl Pal for Sys {
         e(unsafe { syscall!(SETREUID, ruid, euid) }) as c_int
     }
 
-    fn stat(file: *const c_char, buf: *mut stat) -> c_int {
-        e(unsafe { syscall!(NEWFSTATAT, AT_FDCWD, file, buf, 0) }) as c_int
-    }
-
     fn tcgetattr(fd: c_int, out: *mut termios) -> c_int {
         Self::ioctl(fd, TCGETS, out as *mut c_void)
     }

+ 0 - 8
src/platform/src/pal/mod.rs

@@ -164,10 +164,6 @@ pub trait Pal {
         Self::no_pal("lseek") as off_t
     }
 
-    fn lstat(file: *const c_char, buf: *mut stat) -> c_int {
-        Self::no_pal("lstat")
-    }
-
     fn mkdir(path: *const c_char, mode: mode_t) -> c_int {
         Self::no_pal("mkdir")
     }
@@ -241,10 +237,6 @@ pub trait Pal {
         Self::no_pal("setreuid")
     }
 
-    fn stat(file: *const c_char, buf: *mut stat) -> c_int {
-        Self::no_pal("stat")
-    }
-
     fn tcgetattr(fd: c_int, out: *mut termios) -> c_int {
         Self::no_pal("tcgetattr")
     }

+ 0 - 24
src/platform/src/redox/mod.rs

@@ -454,18 +454,6 @@ impl Pal for Sys {
         )) as off_t
     }
 
-    fn lstat(path: *const c_char, buf: *mut stat) -> c_int {
-        let path = unsafe { c_str(path) };
-        match syscall::open(path, O_STAT | O_NOFOLLOW) {
-            Err(err) => e(Err(err)) as c_int,
-            Ok(fd) => {
-                let res = Self::fstat(fd as i32, buf);
-                let _ = syscall::close(fd);
-                res
-            }
-        }
-    }
-
     fn mkdir(path: *const c_char, mode: mode_t) -> c_int {
         let flags = O_CREAT | O_EXCL | O_CLOEXEC | O_DIRECTORY | mode as usize & 0o777;
         let path = unsafe { c_str(path) };
@@ -700,18 +688,6 @@ impl Pal for Sys {
         e(syscall::setreuid(ruid as usize, euid as usize)) as c_int
     }
 
-    fn stat(path: *const c_char, buf: *mut stat) -> c_int {
-        let path = unsafe { c_str(path) };
-        match syscall::open(path, O_STAT) {
-            Err(err) => e(Err(err)) as c_int,
-            Ok(fd) => {
-                let res = Self::fstat(fd as i32, buf);
-                let _ = syscall::close(fd);
-                res
-            }
-        }
-    }
-
     fn tcgetattr(fd: c_int, out: *mut termios) -> c_int {
         let dup = e(syscall::dup(fd as usize, b"termios"));
         if dup == !0 {

+ 1 - 1
src/stdlib/src/lib.rs

@@ -447,7 +447,7 @@ where
 pub extern "C" fn mktemp(name: *mut c_char) -> *mut c_char {
     if inner_mktemp(name, 0, || unsafe {
         let mut st: stat = mem::uninitialized();
-        let ret = if Sys::stat(name, &mut st) != 0 && platform::errno == ENOENT {
+        let ret = if Sys::access(name, 0) != 0 && platform::errno == ENOENT {
             Some(())
         } else {
             None

+ 1 - 1
src/sys_resource/src/lib.rs

@@ -54,7 +54,7 @@ pub unsafe extern "C" fn getrlimit(resource: c_int, rlp: *mut rlimit) -> c_int {
     unimplemented!();
 }
 
-//#[no_mangle]
+#[no_mangle]
 pub unsafe extern "C" fn getrusage(who: c_int, r_usage: *mut rusage) -> c_int {
     Sys::getrusage(who, r_usage as *mut platform::types::rusage)
 }

+ 1 - 0
src/sys_stat/Cargo.toml

@@ -8,4 +8,5 @@ build = "build.rs"
 cbindgen = { path = "../../cbindgen" }
 
 [dependencies]
+fcntl = { path = "../fcntl" }
 platform = { path = "../platform" }

+ 22 - 2
src/sys_stat/src/lib.rs

@@ -2,8 +2,10 @@
 
 #![no_std]
 
+extern crate fcntl;
 extern crate platform;
 
+use fcntl::{O_PATH, O_NOFOLLOW};
 use platform::{Pal, Sys};
 use platform::types::*;
 
@@ -85,7 +87,16 @@ pub extern "C" fn futimens(fd: c_int, times: *const timespec) -> c_int {
 
 #[no_mangle]
 pub extern "C" fn lstat(path: *const c_char, buf: *mut platform::types::stat) -> c_int {
-    Sys::lstat(path, buf)
+    let fd = Sys::open(path, O_PATH | O_NOFOLLOW, 0);
+    if fd < 0 {
+        return -1;
+    }
+
+    let res = Sys::fstat(fd, buf);
+
+    Sys::close(fd);
+
+    res
 }
 
 #[no_mangle]
@@ -105,7 +116,16 @@ pub extern "C" fn mknod(path: *const c_char, mode: mode_t, dev: dev_t) -> c_int
 
 #[no_mangle]
 pub extern "C" fn stat(file: *const c_char, buf: *mut platform::types::stat) -> c_int {
-    Sys::stat(file, buf)
+    let fd = Sys::open(file, O_PATH, 0);
+    if fd < 0 {
+        return -1;
+    }
+
+    let res = Sys::fstat(fd, buf);
+
+    Sys::close(fd);
+
+    res
 }
 
 #[no_mangle]

+ 1 - 1
src/sys_times/src/lib.rs

@@ -15,7 +15,7 @@ pub struct tms {
     tms_cstime: clock_t,
 }
 
-//#[no_mangle]
+#[no_mangle]
 pub extern "C" fn times(out: *mut tms) -> clock_t {
     Sys::times(out as *mut platform::types::tms)
 }