From 7fd3a8d6982f37e3255cb281880e50d8fbe17213 Mon Sep 17 00:00:00 2001 From: David DA SILVA Date: Wed, 25 Jun 2025 14:39:01 +0000 Subject: [PATCH] Rustify some functions (following TODO) --- src/header/stdlib/mod.rs | 10 +--------- src/header/sys_stat/mod.rs | 32 ++++++++++---------------------- src/header/sys_statvfs/mod.rs | 16 +++++----------- src/header/unistd/mod.rs | 16 +++++----------- 4 files changed, 21 insertions(+), 53 deletions(-) diff --git a/src/header/stdlib/mod.rs b/src/header/stdlib/mod.rs index 1da1a154fa..1b7b58df23 100644 --- a/src/header/stdlib/mod.rs +++ b/src/header/stdlib/mod.rs @@ -791,20 +791,12 @@ pub unsafe extern "C" fn mkostemps( suffix_len: c_int, mut flags: c_int, ) -> c_int { - // TODO: Rustify impl - flags &= !O_ACCMODE; flags |= O_RDWR | O_CREAT | O_EXCL; inner_mktemp(name, suffix_len, || { let name = CStr::from_ptr(name); - let fd = Sys::open(name, flags, 0o600).or_minus_one_errno(); - - if fd >= 0 { - Some(fd) - } else { - None - } + Sys::open(name, flags, 0o600).ok() }) .unwrap_or(-1) } diff --git a/src/header/sys_stat/mod.rs b/src/header/sys_stat/mod.rs index 5b75c31a30..ef5866919a 100644 --- a/src/header/sys_stat/mod.rs +++ b/src/header/sys_stat/mod.rs @@ -94,18 +94,12 @@ pub unsafe extern "C" fn futimens(fd: c_int, times: *const timespec) -> c_int { #[no_mangle] pub unsafe extern "C" fn lstat(path: *const c_char, buf: *mut stat) -> c_int { let path = CStr::from_ptr(path); - // TODO: Rustify - let fd = Sys::open(path, O_PATH | O_NOFOLLOW, 0).or_minus_one_errno(); - if fd < 0 { - return -1; - } - // TODO: Rustify - let res = Sys::fstat(fd, buf).map(|()| 0).or_minus_one_errno(); - - Sys::close(fd); - - res + Sys::open(path, O_PATH | O_NOFOLLOW, 0).map_or(-1, |fd| { + let res = Sys::fstat(fd, buf).map(|()| 0).or_minus_one_errno(); + Sys::close(fd); + res + }) } #[no_mangle] @@ -142,18 +136,12 @@ pub unsafe extern "C" fn mknodat( #[no_mangle] pub unsafe extern "C" fn stat(file: *const c_char, buf: *mut stat) -> c_int { let file = CStr::from_ptr(file); - // TODO: Rustify - let fd = Sys::open(file, O_PATH, 0).or_minus_one_errno(); - if fd < 0 { - return -1; - } - // TODO: Rustify - let res = Sys::fstat(fd, buf).map(|()| 0).or_minus_one_errno(); - - Sys::close(fd); - - res + Sys::open(file, O_PATH, 0).map_or(-1, |fd| { + let res = Sys::fstat(fd, buf).map(|()| 0).or_minus_one_errno(); + Sys::close(fd); + res + }) } #[no_mangle] diff --git a/src/header/sys_statvfs/mod.rs b/src/header/sys_statvfs/mod.rs index 306e0436bf..70a76e3ea8 100644 --- a/src/header/sys_statvfs/mod.rs +++ b/src/header/sys_statvfs/mod.rs @@ -34,15 +34,9 @@ pub unsafe extern "C" fn fstatvfs(fildes: c_int, buf: *mut statvfs) -> c_int { #[no_mangle] pub unsafe extern "C" fn statvfs(file: *const c_char, buf: *mut statvfs) -> c_int { let file = CStr::from_ptr(file); - // TODO: Rustify - let fd = Sys::open(file, O_PATH, 0).or_minus_one_errno(); - if fd < 0 { - return -1; - } - - let res = Sys::fstatvfs(fd, buf).map(|()| 0).or_minus_one_errno(); - - Sys::close(fd); - - res + Sys::open(file, O_PATH, 0).map_or(-1, |fd| { + let res = Sys::fstatvfs(fd, buf).map(|()| 0).or_minus_one_errno(); + Sys::close(fd); + res + }) } diff --git a/src/header/unistd/mod.rs b/src/header/unistd/mod.rs index 26468bccf7..998c84094b 100644 --- a/src/header/unistd/mod.rs +++ b/src/header/unistd/mod.rs @@ -982,17 +982,11 @@ pub extern "C" fn tcsetpgrp(fd: c_int, pgrp: pid_t) -> c_int { #[no_mangle] pub unsafe extern "C" fn truncate(path: *const c_char, length: off_t) -> c_int { let file = unsafe { CStr::from_ptr(path) }; - // TODO: Rustify - let fd = Sys::open(file, fcntl::O_WRONLY, 0).or_minus_one_errno(); - if fd < 0 { - return -1; - } - - let res = ftruncate(fd, length); - - Sys::close(fd); - - res + Sys::open(file, fcntl::O_WRONLY, 0).map_or(-1, |fd| { + let res = ftruncate(fd, length); + Sys::close(fd); + res + }) } /// See .