diff --git a/Cargo.lock b/Cargo.lock index 2ba1307728bc1433d6d0130ebd647f3313e59000..d329b0c9ef2802ef322c473cc1e8a47fdfdc8e31 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 05948a6da50d482a5f6879dccffd6285c588b48c..7b0af1982fe495c8d766cf9724f2357fe01e204d 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 1ea3ca45de4191d7fda1467bff0fcc56f4084e61..95f2da5570ee2ba57af46e2b204c12c6d3e393cd 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 ccdd0f9b49b3c57479bdb971c3223556751ef5fc..a11f13da2235c406b11ce6f569f384130ec54a60 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 92410c38828d3414df8361727b1bcde6e6d78fe0..45f13c053ae379ada3328657be4677806c0e9f93 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 1129b3b4c5bb169409e1e6685c363c4ef9d7781f..bbe4920bcb7e0bebc342ece6d0ee3dbfaf48e3ca 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 485e991244ae6a228f7a88af8af899726419f114..e166f4ce8d8fda2a5b74d3db4905a02c7ab73cf3 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 29612c729854c6d2f02763366d7331c944e8cccf..584dfaa7a8c36d07fd8dd06fef813b3bc3410399 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 4d04bee7d8496f13ac4c91f7a143b90636809880..23e1253120c1bed45b15a0cbd82ef6f216c35ffd 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 459ea5406bbee2738bb8a2a10172f4977a84f230..81fb68623f2257bf01dd94a1912b5773069c5d37 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 ebceafbd126c04ea51ceb9c2d6a25c142daaa56e..713fa6f18cac162ac2b920dd515cbf5a22ea14e3 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) }