From ff32c8cbbd2074d73493a92c945c2454643cd6fa Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Sat, 25 Aug 2018 10:13:58 -0600 Subject: [PATCH] Remove stat and lstat, which can be replaced with open and fstat --- Cargo.lock | 1 + src/fcntl/src/linux.rs | 12 +++++++----- src/fcntl/src/redox.rs | 2 +- src/platform/src/linux/mod.rs | 8 -------- src/platform/src/pal/mod.rs | 8 -------- src/platform/src/redox/mod.rs | 24 ------------------------ src/stdlib/src/lib.rs | 2 +- src/sys_resource/src/lib.rs | 2 +- src/sys_stat/Cargo.toml | 1 + src/sys_stat/src/lib.rs | 24 ++++++++++++++++++++++-- src/sys_times/src/lib.rs | 2 +- 11 files changed, 35 insertions(+), 51 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2ba1307728..d329b0c9ef 100644 --- a/Cargo.lock +++ b/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", ] diff --git a/src/fcntl/src/linux.rs b/src/fcntl/src/linux.rs index 05948a6da5..7b0af1982f 100644 --- a/src/fcntl/src/linux.rs +++ b/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; diff --git a/src/fcntl/src/redox.rs b/src/fcntl/src/redox.rs index 1ea3ca45de..95f2da5570 100644 --- a/src/fcntl/src/redox.rs +++ b/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; diff --git a/src/platform/src/linux/mod.rs b/src/platform/src/linux/mod.rs index ccdd0f9b49..a11f13da22 100644 --- a/src/platform/src/linux/mod.rs +++ b/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) } diff --git a/src/platform/src/pal/mod.rs b/src/platform/src/pal/mod.rs index 92410c3882..45f13c053a 100644 --- a/src/platform/src/pal/mod.rs +++ b/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") } diff --git a/src/platform/src/redox/mod.rs b/src/platform/src/redox/mod.rs index 1129b3b4c5..bbe4920bcb 100644 --- a/src/platform/src/redox/mod.rs +++ b/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 { diff --git a/src/stdlib/src/lib.rs b/src/stdlib/src/lib.rs index 485e991244..e166f4ce8d 100644 --- a/src/stdlib/src/lib.rs +++ b/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 diff --git a/src/sys_resource/src/lib.rs b/src/sys_resource/src/lib.rs index 29612c7298..584dfaa7a8 100644 --- a/src/sys_resource/src/lib.rs +++ b/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) } diff --git a/src/sys_stat/Cargo.toml b/src/sys_stat/Cargo.toml index 4d04bee7d8..23e1253120 100644 --- a/src/sys_stat/Cargo.toml +++ b/src/sys_stat/Cargo.toml @@ -8,4 +8,5 @@ build = "build.rs" cbindgen = { path = "../../cbindgen" } [dependencies] +fcntl = { path = "../fcntl" } platform = { path = "../platform" } diff --git a/src/sys_stat/src/lib.rs b/src/sys_stat/src/lib.rs index 459ea5406b..81fb68623f 100644 --- a/src/sys_stat/src/lib.rs +++ b/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] diff --git a/src/sys_times/src/lib.rs b/src/sys_times/src/lib.rs index ebceafbd12..713fa6f18c 100644 --- a/src/sys_times/src/lib.rs +++ b/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) }