Rustify some functions (following TODO)

This commit is contained in:
David DA SILVA
2025-06-25 14:39:01 +00:00
committed by Jeremy Soller
parent 63418095e5
commit 7fd3a8d698
4 changed files with 21 additions and 53 deletions
+1 -9
View File
@@ -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)
}
+10 -22
View File
@@ -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]
+5 -11
View File
@@ -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
})
}
+5 -11
View File
@@ -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 <https://pubs.opengroup.org/onlinepubs/9799919799/functions/ttyname.html>.