Convert more Pal methods to Result.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
use crate::{
|
||||
error::ResultExt,
|
||||
header::{fcntl, unistd},
|
||||
platform::types::*,
|
||||
Pal, Sys,
|
||||
@@ -10,7 +11,8 @@ pub(super) unsafe fn openpty(name: &mut [u8]) -> Result<(c_int, c_int), ()> {
|
||||
return Err(());
|
||||
}
|
||||
|
||||
let count = Sys::fpath(master, name);
|
||||
// TODO: better error handling
|
||||
let count = Sys::fpath(master, name).or_minus_one_errno();
|
||||
if count < 0 {
|
||||
unistd::close(master);
|
||||
return Err(());
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
//! sched.h implementation for Redox, following https://pubs.opengroup.org/onlinepubs/7908799/xsh/sched.h.html
|
||||
|
||||
use crate::{
|
||||
error::ResultExt,
|
||||
header::time::timespec,
|
||||
platform::{types::*, Pal, Sys},
|
||||
};
|
||||
@@ -44,5 +45,5 @@ pub extern "C" fn sched_setscheduler(
|
||||
}
|
||||
#[no_mangle]
|
||||
pub extern "C" fn sched_yield() -> c_int {
|
||||
Sys::sched_yield()
|
||||
Sys::sched_yield().map(|()| 0).or_minus_one_errno()
|
||||
}
|
||||
|
||||
@@ -682,7 +682,7 @@ pub unsafe fn fseek_locked(stream: &mut FILE, mut off: off_t, whence: c_int) ->
|
||||
return -1;
|
||||
}
|
||||
|
||||
let err = Sys::lseek(*stream.file, off, whence);
|
||||
let err = Sys::lseek(*stream.file, off, whence).or_minus_one_errno();
|
||||
if err < 0 {
|
||||
return err as c_int;
|
||||
}
|
||||
@@ -713,7 +713,7 @@ pub unsafe extern "C" fn ftello(stream: *mut FILE) -> off_t {
|
||||
ftell_locked(&mut *stream)
|
||||
}
|
||||
pub unsafe extern "C" fn ftell_locked(stream: &mut FILE) -> off_t {
|
||||
let pos = Sys::lseek(*stream.file, 0, SEEK_CUR);
|
||||
let pos = Sys::lseek(*stream.file, 0, SEEK_CUR).or_minus_one_errno();
|
||||
if pos < 0 {
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -985,7 +985,8 @@ pub unsafe extern "C" fn realpath(pathname: *const c_char, resolved: *mut c_char
|
||||
};
|
||||
|
||||
let len = out.len();
|
||||
let read = Sys::fpath(*file, &mut out[..len - 1]);
|
||||
// TODO: better error handling
|
||||
let read = Sys::fpath(*file, &mut out[..len - 1]).or_minus_one_errno();
|
||||
if read < 0 {
|
||||
return ptr::null_mut();
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
//! http://pubs.opengroup.org/onlinepubs/7908799/xsh/sysresource.h.html
|
||||
|
||||
use crate::{
|
||||
error::ResultExt,
|
||||
header::sys_time::timeval,
|
||||
platform::{types::*, Pal, Sys},
|
||||
};
|
||||
@@ -77,16 +78,22 @@ pub unsafe extern "C" fn getpriority(which: c_int, who: id_t) -> c_int {
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn setpriority(which: c_int, who: id_t, nice: c_int) -> c_int {
|
||||
Sys::setpriority(which, who, nice)
|
||||
.map(|()| 0)
|
||||
.or_minus_one_errno()
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn getrlimit(resource: c_int, rlp: *mut rlimit) -> c_int {
|
||||
Sys::getrlimit(resource, rlp)
|
||||
.map(|()| 0)
|
||||
.or_minus_one_errno()
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn setrlimit(resource: c_int, rlp: *const rlimit) -> c_int {
|
||||
Sys::setrlimit(resource, rlp)
|
||||
.map(|()| 0)
|
||||
.or_minus_one_errno()
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
|
||||
@@ -110,19 +110,19 @@ pub unsafe extern "C" fn lstat(path: *const c_char, buf: *mut stat) -> c_int {
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn mkdir(path: *const c_char, mode: mode_t) -> c_int {
|
||||
let path = CStr::from_ptr(path);
|
||||
Sys::mkdir(path, mode)
|
||||
Sys::mkdir(path, mode).map(|()| 0).or_minus_one_errno()
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn mkfifo(path: *const c_char, mode: mode_t) -> c_int {
|
||||
let path = CStr::from_ptr(path);
|
||||
Sys::mkfifo(path, mode)
|
||||
Sys::mkfifo(path, mode).map(|()| 0).or_minus_one_errno()
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn mknod(path: *const c_char, mode: mode_t, dev: dev_t) -> c_int {
|
||||
let path = CStr::from_ptr(path);
|
||||
Sys::mknod(path, mode, dev)
|
||||
Sys::mknod(path, mode, dev).map(|()| 0).or_minus_one_errno()
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
@@ -134,6 +134,8 @@ pub unsafe extern "C" fn mknodat(
|
||||
) -> c_int {
|
||||
let path = CStr::from_ptr(path);
|
||||
Sys::mknodat(dirfd, path, mode, dev)
|
||||
.map(|()| 0)
|
||||
.or_minus_one_errno()
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
//! sys/utsname implementation, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/sysutsname.h.html
|
||||
|
||||
use crate::platform::{types::*, Pal, Sys};
|
||||
use crate::{
|
||||
error::ResultExt,
|
||||
platform::{types::*, Pal, Sys},
|
||||
};
|
||||
|
||||
pub const UTSLENGTH: usize = 65;
|
||||
|
||||
@@ -16,5 +19,5 @@ pub struct utsname {
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn uname(uts: *mut utsname) -> c_int {
|
||||
Sys::uname(uts)
|
||||
Sys::uname(uts).map(|()| 0).or_minus_one_errno()
|
||||
}
|
||||
|
||||
@@ -453,7 +453,10 @@ pub extern "C" fn gethostid() -> c_long {
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn gethostname(mut name: *mut c_char, mut len: size_t) -> c_int {
|
||||
let mut uts = mem::MaybeUninit::<sys_utsname::utsname>::uninit();
|
||||
let err = Sys::uname(uts.as_mut_ptr());
|
||||
// TODO
|
||||
let err = Sys::uname(uts.as_mut_ptr())
|
||||
.map(|()| 0)
|
||||
.or_minus_one_errno();
|
||||
if err < 0 {
|
||||
mem::forget(uts);
|
||||
return err;
|
||||
@@ -558,7 +561,7 @@ pub unsafe extern "C" fn lchown(path: *const c_char, owner: uid_t, group: gid_t)
|
||||
pub unsafe extern "C" fn link(path1: *const c_char, path2: *const c_char) -> c_int {
|
||||
let path1 = CStr::from_ptr(path1);
|
||||
let path2 = CStr::from_ptr(path2);
|
||||
Sys::link(path1, path2)
|
||||
Sys::link(path1, path2).map(|()| 0).or_minus_one_errno()
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
@@ -602,7 +605,7 @@ pub unsafe extern "C" fn lockf(fildes: c_int, function: c_int, size: off_t) -> c
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn lseek(fildes: c_int, offset: off_t, whence: c_int) -> off_t {
|
||||
Sys::lseek(fildes, offset, whence)
|
||||
Sys::lseek(fildes, offset, whence).or_minus_one_errno()
|
||||
}
|
||||
|
||||
// #[no_mangle]
|
||||
@@ -623,6 +626,8 @@ pub unsafe extern "C" fn pipe(fildes: *mut c_int) -> c_int {
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn pipe2(fildes: *mut c_int, flags: c_int) -> c_int {
|
||||
Sys::pipe2(slice::from_raw_parts_mut(fildes, 2), flags)
|
||||
.map(|()| 0)
|
||||
.or_minus_one_errno()
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
@@ -694,7 +699,7 @@ pub unsafe extern "C" fn readlink(
|
||||
) -> ssize_t {
|
||||
let path = CStr::from_ptr(path);
|
||||
let buf = slice::from_raw_parts_mut(buf as *mut u8, bufsize as usize);
|
||||
Sys::readlink(path, buf)
|
||||
Sys::readlink(path, buf).or_minus_one_errno()
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
@@ -706,16 +711,18 @@ pub unsafe extern "C" fn rmdir(path: *const c_char) -> c_int {
|
||||
#[no_mangle]
|
||||
pub extern "C" fn setgid(gid: gid_t) -> c_int {
|
||||
Sys::setresgid(gid, gid, -1)
|
||||
.map(|()| 0)
|
||||
.or_minus_one_errno()
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn setgroups(size: size_t, list: *const gid_t) -> c_int {
|
||||
Sys::setgroups(size, list)
|
||||
Sys::setgroups(size, list).map(|()| 0).or_minus_one_errno()
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn setpgid(pid: pid_t, pgid: pid_t) -> c_int {
|
||||
Sys::setpgid(pid, pgid)
|
||||
Sys::setpgid(pid, pgid).map(|()| 0).or_minus_one_errno()
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
@@ -726,29 +733,39 @@ pub extern "C" fn setpgrp() -> pid_t {
|
||||
#[no_mangle]
|
||||
pub extern "C" fn setregid(rgid: gid_t, egid: gid_t) -> c_int {
|
||||
Sys::setresgid(rgid, egid, -1)
|
||||
.map(|()| 0)
|
||||
.or_minus_one_errno()
|
||||
}
|
||||
#[no_mangle]
|
||||
pub extern "C" fn setresgid(rgid: gid_t, egid: gid_t, sgid: gid_t) -> c_int {
|
||||
Sys::setresgid(rgid, egid, sgid)
|
||||
.map(|()| 0)
|
||||
.or_minus_one_errno()
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn setreuid(ruid: uid_t, euid: uid_t) -> c_int {
|
||||
Sys::setresuid(ruid, euid, -1)
|
||||
.map(|()| 0)
|
||||
.or_minus_one_errno()
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn setsid() -> pid_t {
|
||||
Sys::setsid()
|
||||
Sys::setsid().map(|()| 0).or_minus_one_errno()
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn setuid(uid: uid_t) -> c_int {
|
||||
Sys::setresuid(uid, uid, -1)
|
||||
.map(|()| 0)
|
||||
.or_minus_one_errno()
|
||||
}
|
||||
#[no_mangle]
|
||||
pub extern "C" fn setresuid(ruid: uid_t, euid: uid_t, suid: uid_t) -> c_int {
|
||||
Sys::setresuid(ruid, euid, suid)
|
||||
.map(|()| 0)
|
||||
.or_minus_one_errno()
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
@@ -782,7 +799,7 @@ pub extern "C" fn swab(src: *const c_void, dest: *mut c_void, nbytes: ssize_t) {
|
||||
pub unsafe extern "C" fn symlink(path1: *const c_char, path2: *const c_char) -> c_int {
|
||||
let path1 = CStr::from_ptr(path1);
|
||||
let path2 = CStr::from_ptr(path2);
|
||||
Sys::symlink(path1, path2)
|
||||
Sys::symlink(path1, path2).map(|()| 0).or_minus_one_errno()
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
@@ -840,7 +857,7 @@ pub extern "C" fn ttyname_r(fildes: c_int, name: *mut c_char, namesize: size_t)
|
||||
return errno::ERANGE;
|
||||
}
|
||||
|
||||
let len = Sys::fpath(fildes, &mut name[..namesize - 1]);
|
||||
let len = Sys::fpath(fildes, &mut name[..namesize - 1]).or_minus_one_errno();
|
||||
if len < 0 {
|
||||
return -platform::ERRNO.get();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user