diff --git a/native/src/base/dir.rs b/native/src/base/dir.rs index 395f41fcd..e6408670f 100644 --- a/native/src/base/dir.rs +++ b/native/src/base/dir.rs @@ -74,7 +74,7 @@ impl DirEntry<'_> { pub fn open_as_dir(&self) -> OsResult<'_, Directory> { if !self.is_dir() { - return Err(OsError::with_os_error( + return Err(OsError::new( libc::ENOTDIR, "fdopendir", Some(self.name()), @@ -86,7 +86,7 @@ impl DirEntry<'_> { pub fn open_as_file(&self, flags: i32) -> OsResult<'_, File> { if self.is_dir() { - return Err(OsError::with_os_error( + return Err(OsError::new( libc::EISDIR, "open_as_file", Some(self.name()), @@ -151,7 +151,7 @@ impl Directory { fn openat<'a>(&self, name: &'a Utf8CStr, flags: i32, mode: u32) -> OsResult<'a, OwnedFd> { unsafe { libc::openat(self.as_raw_fd(), name.as_ptr(), flags | O_CLOEXEC, mode) - .as_os_result("openat", Some(name), None) + .into_os_result("openat", Some(name), None) .map(|fd| OwnedFd::from_raw_fd(fd)) } } @@ -166,7 +166,7 @@ impl Directory { impl Directory { pub fn open(path: &Utf8CStr) -> OsResult<'_, Directory> { let dirp = unsafe { libc::opendir(path.as_ptr()) }; - let dirp = dirp.as_os_result("opendir", Some(path), None)?; + let dirp = dirp.into_os_result("opendir", Some(path), None)?; Ok(Directory { inner: dirp }) } @@ -233,7 +233,7 @@ impl Directory { buf.as_mut_ptr().cast(), buf.capacity(), ) - .as_os_result("readlinkat", Some(name), None)? as usize; + .into_os_result("readlinkat", Some(name), None)? as usize; buf.set_len(r); } Ok(()) @@ -469,7 +469,7 @@ impl TryFrom for Directory { fn try_from(fd: OwnedFd) -> OsResult<'static, Self> { let dirp = unsafe { libc::fdopendir(fd.into_raw_fd()) }; - let dirp = dirp.as_os_result("fdopendir", None, None)?; + let dirp = dirp.into_os_result("fdopendir", None, None)?; Ok(Directory { inner: dirp }) } } diff --git a/native/src/base/files.rs b/native/src/base/files.rs index d62155a6a..4cb0de09b 100644 --- a/native/src/base/files.rs +++ b/native/src/base/files.rs @@ -141,7 +141,7 @@ impl FileOrStd { fn open_fd(path: &Utf8CStr, flags: i32, mode: mode_t) -> OsResult<'_, OwnedFd> { unsafe { - let fd = libc::open(path.as_ptr(), flags, mode as c_uint).as_os_result( + let fd = libc::open(path.as_ptr(), flags, mode as c_uint).into_os_result( "open", Some(path), None, @@ -267,7 +267,7 @@ impl Utf8CStr { buf.clear(); unsafe { let r = libc::readlink(self.as_ptr(), buf.as_mut_ptr(), buf.capacity() - 1) - .as_os_result("readlink", Some(self), None)? as isize; + .into_os_result("readlink", Some(self), None)? as isize; *(buf.as_mut_ptr().offset(r) as *mut u8) = b'\0'; buf.set_len(r as usize); } @@ -763,7 +763,7 @@ pub(crate) fn map_file_at<'a>( let flag = if rw { O_RDWR } else { O_RDONLY }; let fd = unsafe { OwnedFd::from_raw_fd( - libc::openat(dirfd.as_raw_fd(), path.as_ptr(), flag | O_CLOEXEC).as_os_result( + libc::openat(dirfd.as_raw_fd(), path.as_ptr(), flag | O_CLOEXEC).into_os_result( "openat", Some(path), None, diff --git a/native/src/base/result.rs b/native/src/base/result.rs index 67762cf9e..b44c1889b 100644 --- a/native/src/base/result.rs +++ b/native/src/base/result.rs @@ -228,24 +228,20 @@ fn do_log_msg fmt::Result>( // Check libc return value and map to Result pub trait LibcReturn where - Self: Copy, + Self: Sized, { type Value; - fn is_error(&self) -> bool; - fn map_val(self) -> Self::Value; + fn into_result(self) -> Result; - fn as_os_result<'a>( + fn into_os_result<'a>( self, name: &'static str, arg1: Option<&'a str>, arg2: Option<&'a str>, ) -> OsResult<'a, Self::Value> { - if self.is_error() { - Err(OsError::last_os_error(name, arg1, arg2)) - } else { - Ok(self.map_val()) - } + self.into_result() + .map_err(|e| OsError::new(e, name, arg1, arg2)) } fn check_os_err<'a>( @@ -254,19 +250,15 @@ where arg1: Option<&'a str>, arg2: Option<&'a str>, ) -> OsResult<'a, ()> { - if self.is_error() { - Err(OsError::last_os_error(name, arg1, arg2)) - } else { - Ok(()) - } + self.into_result() + .map(|_| ()) + .map_err(|e| OsError::new(e, name, arg1, arg2)) } fn check_io_err(self) -> io::Result<()> { - if self.is_error() { - Err(io::Error::last_os_error()) - } else { - Ok(()) - } + self.into_result() + .map(|_| ()) + .map_err(io::Error::from_raw_os_error) } } @@ -276,13 +268,12 @@ macro_rules! impl_libc_return { type Value = Self; #[inline(always)] - fn is_error(&self) -> bool { - *self < 0 - } - - #[inline(always)] - fn map_val(self) -> Self::Value { - self + fn into_result(self) -> Result { + if self < 0 { + Err(*errno()) + } else { + Ok(self) + } } } )*) @@ -294,14 +285,8 @@ impl LibcReturn for *mut T { type Value = NonNull; #[inline(always)] - fn is_error(&self) -> bool { - self.is_null() - } - - #[inline(always)] - fn map_val(self) -> NonNull { - // SAFETY: pointer is null checked by is_error - unsafe { NonNull::new_unchecked(self.cast()) } + fn into_result(self) -> Result { + NonNull::new(self).ok_or_else(|| *errno()) } } @@ -345,7 +330,7 @@ pub struct OsError<'a> { } impl OsError<'_> { - pub fn with_os_error<'a>( + pub fn new<'a>( code: i32, name: &'static str, arg1: Option<&'a str>, @@ -364,16 +349,16 @@ impl OsError<'_> { arg1: Option<&'a str>, arg2: Option<&'a str>, ) -> OsError<'a> { - Self::with_os_error(*errno(), name, arg1, arg2) + Self::new(*errno(), name, arg1, arg2) } pub fn set_args<'a>(self, arg1: Option<&'a str>, arg2: Option<&'a str>) -> OsError<'a> { - Self::with_os_error(self.code, self.name, arg1, arg2) + Self::new(self.code, self.name, arg1, arg2) } pub fn into_owned(self) -> OsError<'static> { OsError { - code: *errno(), + code: self.code, name: self.name, arg1: self.arg1.into_owned(), arg2: self.arg2.into_owned(), diff --git a/native/src/base/xwrap.rs b/native/src/base/xwrap.rs index 89d7bbaa3..e4f33075d 100644 --- a/native/src/base/xwrap.rs +++ b/native/src/base/xwrap.rs @@ -61,7 +61,7 @@ unsafe extern "C" fn xreadlinkat( ) -> isize { unsafe { readlinkat(dirfd, path, buf, bufsz) - .as_os_result("readlinkat", ptr_to_str(path), None) + .into_os_result("readlinkat", ptr_to_str(path), None) .log_cxx() .unwrap_or(-1) } @@ -71,7 +71,7 @@ unsafe extern "C" fn xreadlinkat( unsafe extern "C" fn xfopen(path: *const c_char, mode: *const c_char) -> *mut libc::FILE { unsafe { libc::fopen(path, mode) - .as_os_result("fopen", ptr_to_str(path), None) + .into_os_result("fopen", ptr_to_str(path), None) .log_cxx() .map_or(ptr::null_mut(), NonNull::as_ptr) } @@ -81,7 +81,7 @@ unsafe extern "C" fn xfopen(path: *const c_char, mode: *const c_char) -> *mut li unsafe extern "C" fn xfdopen(fd: RawFd, mode: *const c_char) -> *mut libc::FILE { unsafe { libc::fdopen(fd, mode) - .as_os_result("fdopen", None, None) + .into_os_result("fdopen", None, None) .log_cxx() .map_or(ptr::null_mut(), NonNull::as_ptr) } @@ -91,7 +91,7 @@ unsafe extern "C" fn xfdopen(fd: RawFd, mode: *const c_char) -> *mut libc::FILE unsafe extern "C" fn xopen(path: *const c_char, flags: i32, mode: mode_t) -> RawFd { unsafe { libc::open(path, flags, mode as c_uint) - .as_os_result("open", ptr_to_str(path), None) + .into_os_result("open", ptr_to_str(path), None) .log_cxx() .unwrap_or(-1) } @@ -101,7 +101,7 @@ unsafe extern "C" fn xopen(path: *const c_char, flags: i32, mode: mode_t) -> Raw unsafe extern "C" fn xopenat(dirfd: RawFd, path: *const c_char, flags: i32, mode: mode_t) -> RawFd { unsafe { libc::openat(dirfd, path, flags, mode as c_uint) - .as_os_result("openat", ptr_to_str(path), None) + .into_os_result("openat", ptr_to_str(path), None) .log_cxx() .unwrap_or(-1) } @@ -120,7 +120,7 @@ unsafe extern "C" fn xwrite(fd: RawFd, buf: *const u8, bufsz: usize) -> isize { unsafe extern "C" fn xread(fd: RawFd, buf: *mut c_void, bufsz: usize) -> isize { unsafe { libc::read(fd, buf, bufsz) - .as_os_result("read", None, None) + .into_os_result("read", None, None) .log_cxx() .unwrap_or(-1) } @@ -138,7 +138,7 @@ unsafe extern "C" fn xxread(fd: RawFd, buf: *mut u8, bufsz: usize) -> isize { pub(crate) fn xpipe2(fds: &mut [i32; 2], flags: i32) -> i32 { unsafe { libc::pipe2(fds.as_mut_ptr(), flags) - .as_os_result("pipe2", None, None) + .into_os_result("pipe2", None, None) .log_cxx() .unwrap_or(-1) } @@ -148,7 +148,7 @@ pub(crate) fn xpipe2(fds: &mut [i32; 2], flags: i32) -> i32 { extern "C" fn xsetns(fd: RawFd, nstype: i32) -> i32 { unsafe { libc::setns(fd, nstype) - .as_os_result("setns", None, None) + .into_os_result("setns", None, None) .log_cxx() .unwrap_or(-1) } @@ -158,7 +158,7 @@ extern "C" fn xsetns(fd: RawFd, nstype: i32) -> i32 { extern "C" fn xunshare(flags: i32) -> i32 { unsafe { libc::unshare(flags) - .as_os_result("unshare", None, None) + .into_os_result("unshare", None, None) .log_cxx() .unwrap_or(-1) } @@ -168,7 +168,7 @@ extern "C" fn xunshare(flags: i32) -> i32 { unsafe extern "C" fn xopendir(path: *const c_char) -> *mut libc::DIR { unsafe { libc::opendir(path) - .as_os_result("opendir", ptr_to_str(path), None) + .into_os_result("opendir", ptr_to_str(path), None) .log_cxx() .map_or(ptr::null_mut(), NonNull::as_ptr) } @@ -178,7 +178,7 @@ unsafe extern "C" fn xopendir(path: *const c_char) -> *mut libc::DIR { extern "C" fn xfdopendir(fd: RawFd) -> *mut libc::DIR { unsafe { libc::fdopendir(fd) - .as_os_result("fdopendir", None, None) + .into_os_result("fdopendir", None, None) .log_cxx() .map_or(ptr::null_mut(), NonNull::as_ptr) } @@ -197,7 +197,7 @@ unsafe extern "C" fn xreaddir(mut dir: BorrowedDirectory) -> *mut libc::dirent { extern "C" fn xsetsid() -> i32 { unsafe { libc::setsid() - .as_os_result("setsid", None, None) + .into_os_result("setsid", None, None) .log_cxx() .unwrap_or(-1) } @@ -207,7 +207,7 @@ extern "C" fn xsetsid() -> i32 { extern "C" fn xsocket(domain: i32, ty: i32, protocol: i32) -> RawFd { unsafe { libc::socket(domain, ty, protocol) - .as_os_result("socket", None, None) + .into_os_result("socket", None, None) .log_cxx() .unwrap_or(-1) } @@ -217,7 +217,7 @@ extern "C" fn xsocket(domain: i32, ty: i32, protocol: i32) -> RawFd { unsafe extern "C" fn xbind(socket: i32, address: *const sockaddr, len: socklen_t) -> i32 { unsafe { libc::bind(socket, address, len) - .as_os_result("bind", None, None) + .into_os_result("bind", None, None) .log_cxx() .unwrap_or(-1) } @@ -227,7 +227,7 @@ unsafe extern "C" fn xbind(socket: i32, address: *const sockaddr, len: socklen_t extern "C" fn xlisten(socket: i32, backlog: i32) -> i32 { unsafe { libc::listen(socket, backlog) - .as_os_result("listen", None, None) + .into_os_result("listen", None, None) .log_cxx() .unwrap_or(-1) } @@ -242,7 +242,7 @@ unsafe extern "C" fn xaccept4( ) -> RawFd { unsafe { libc::accept4(sockfd, addr, len, flg) - .as_os_result("accept4", None, None) + .into_os_result("accept4", None, None) .log_cxx() .unwrap_or(-1) } @@ -252,7 +252,7 @@ unsafe extern "C" fn xaccept4( unsafe extern "C" fn xstat(path: *const c_char, buf: *mut libc::stat) -> i32 { unsafe { libc::stat(path, buf) - .as_os_result("stat", ptr_to_str(path), None) + .into_os_result("stat", ptr_to_str(path), None) .log_cxx() .unwrap_or(-1) } @@ -262,7 +262,7 @@ unsafe extern "C" fn xstat(path: *const c_char, buf: *mut libc::stat) -> i32 { unsafe extern "C" fn xfstat(fd: RawFd, buf: *mut libc::stat) -> i32 { unsafe { libc::fstat(fd, buf) - .as_os_result("fstat", None, None) + .into_os_result("fstat", None, None) .log_cxx() .unwrap_or(-1) } @@ -272,7 +272,7 @@ unsafe extern "C" fn xfstat(fd: RawFd, buf: *mut libc::stat) -> i32 { extern "C" fn xdup2(oldfd: RawFd, newfd: RawFd) -> RawFd { unsafe { libc::dup2(oldfd, newfd) - .as_os_result("dup2", None, None) + .into_os_result("dup2", None, None) .log_cxx() .unwrap_or(-1) } @@ -282,7 +282,7 @@ extern "C" fn xdup2(oldfd: RawFd, newfd: RawFd) -> RawFd { unsafe extern "C" fn xsymlink(target: *const c_char, linkpath: *const c_char) -> i32 { unsafe { libc::symlink(target, linkpath) - .as_os_result("symlink", ptr_to_str(target), ptr_to_str(linkpath)) + .into_os_result("symlink", ptr_to_str(target), ptr_to_str(linkpath)) .log_cxx() .unwrap_or(-1) } @@ -298,7 +298,7 @@ unsafe extern "C" fn xmount( ) -> i32 { unsafe { libc::mount(src, target, fstype, flags, data) - .as_os_result("mount", ptr_to_str(src), ptr_to_str(target)) + .into_os_result("mount", ptr_to_str(src), ptr_to_str(target)) .log_cxx() .unwrap_or(-1) } @@ -308,7 +308,7 @@ unsafe extern "C" fn xmount( unsafe extern "C" fn xumount2(target: *const c_char, flags: i32) -> i32 { unsafe { libc::umount2(target, flags) - .as_os_result("umount2", ptr_to_str(target), None) + .into_os_result("umount2", ptr_to_str(target), None) .log_cxx() .unwrap_or(-1) } @@ -318,7 +318,7 @@ unsafe extern "C" fn xumount2(target: *const c_char, flags: i32) -> i32 { unsafe extern "C" fn xrename(oldname: *const c_char, newname: *const c_char) -> i32 { unsafe { libc::rename(oldname, newname) - .as_os_result("rename", ptr_to_str(oldname), ptr_to_str(newname)) + .into_os_result("rename", ptr_to_str(oldname), ptr_to_str(newname)) .log_cxx() .unwrap_or(-1) } @@ -353,7 +353,7 @@ unsafe extern "C" fn xsendfile( ) -> isize { unsafe { libc::sendfile(out_fd, in_fd, offset, count) - .as_os_result("sendfile", None, None) + .into_os_result("sendfile", None, None) .log_cxx() .unwrap_or(-1) } @@ -363,7 +363,7 @@ unsafe extern "C" fn xsendfile( extern "C" fn xfork() -> i32 { unsafe { libc::fork() - .as_os_result("fork", None, None) + .into_os_result("fork", None, None) .log_cxx() .unwrap_or(-1) } @@ -373,7 +373,7 @@ extern "C" fn xfork() -> i32 { unsafe extern "C" fn xmknod(pathname: *const c_char, mode: mode_t, dev: dev_t) -> i32 { unsafe { libc::mknod(pathname, mode, dev) - .as_os_result("mknod", ptr_to_str(pathname), None) + .into_os_result("mknod", ptr_to_str(pathname), None) .log_cxx() .unwrap_or(-1) } diff --git a/native/src/core/resetprop/persist.rs b/native/src/core/resetprop/persist.rs index 9e1c6c8f6..0ef386795 100644 --- a/native/src/core/resetprop/persist.rs +++ b/native/src/core/resetprop/persist.rs @@ -64,7 +64,7 @@ fn file_set_prop(name: &Utf8CStr, value: Option<&Utf8CStr>) -> LoggedResult<()> { let mut f = unsafe { mkstemp(tmp.as_mut_ptr()) - .as_os_result("mkstemp", None, None) + .into_os_result("mkstemp", None, None) .map(|fd| File::from_raw_fd(fd))? }; f.write_all(value.as_bytes())?; @@ -96,7 +96,7 @@ fn proto_write_props(props: &PersistentProperties) -> LoggedResult<()> { { let f = unsafe { mkstemp(tmp.as_mut_ptr()) - .as_os_result("mkstemp", None, None) + .into_os_result("mkstemp", None, None) .map(|fd| File::from_raw_fd(fd))? }; debug!("resetprop: encode with protobuf [{}]", tmp); diff --git a/native/src/core/su/connect.rs b/native/src/core/su/connect.rs index 1ee3a5ad6..93a33fede 100644 --- a/native/src/core/su/connect.rs +++ b/native/src/core/su/connect.rs @@ -201,9 +201,10 @@ impl SuAppContext<'_> { events: libc::POLLIN, revents: 0, }; - if unsafe { libc::poll(&mut pfd, 1, 70 * 1000).as_os_result("poll", None, None)? } == 0 + if unsafe { libc::poll(&mut pfd, 1, 70 * 1000).into_os_result("poll", None, None)? } + == 0 { - Err(OsError::with_os_error(libc::ETIMEDOUT, "poll", None, None))?; + Err(OsError::new(libc::ETIMEDOUT, "poll", None, None))?; } fd diff --git a/native/src/core/su/pts.rs b/native/src/core/su/pts.rs index 5747f4c85..86d7a80c3 100644 --- a/native/src/core/su/pts.rs +++ b/native/src/core/su/pts.rs @@ -84,7 +84,7 @@ fn resize_pty(outfd: i32) { fn splice(fd_in: RawFd, fd_out: RawFd, len: usize, flags: u32) -> OsResult<'static, ssize_t> { unsafe { libc::splice(fd_in, null_mut(), fd_out, null_mut(), len, flags) } - .as_os_result("splice", None, None) + .into_os_result("splice", None, None) } fn pump_via_copy(infd: RawFd, outfd: RawFd) -> LoggedResult<()> {