Stop differentiate Error vs ErrorCxx

This commit is contained in:
topjohnwu
2025-09-08 11:25:18 -07:00
parent ea5fe7525d
commit 1df5b34175
7 changed files with 50 additions and 66 deletions

View File

@@ -291,7 +291,7 @@ extern "C" int magisk_log_print(int prio, const char *tag, const char *fmt, ...)
level = LogLevel::Warn;
break;
case ANDROID_LOG_ERROR:
level = LogLevel::ErrorCxx;
level = LogLevel::Error;
break;
default:
return 0;
@@ -328,11 +328,11 @@ void LOGD(const char *fmt, ...) {}
#endif
void LOGI(const char *fmt, ...) { LOG_BODY(Info) }
void LOGW(const char *fmt, ...) { LOG_BODY(Warn) }
void LOGE(const char *fmt, ...) { LOG_BODY(ErrorCxx) }
void LOGE(const char *fmt, ...) { LOG_BODY(Error) }
// Export raw symbol to fortify compat
extern "C" void __vloge(const char* fmt, va_list ap) {
fmt_and_log_with_rs(LogLevel::ErrorCxx, fmt, ap);
fmt_and_log_with_rs(LogLevel::Error, fmt, ap);
}
string full_read(int fd) {

View File

@@ -13,7 +13,7 @@ use crate::ffi::{FnBoolStr, FnBoolStrStr};
use crate::files::map_file_at;
pub(crate) use crate::xwrap::*;
use crate::{
BufReadExt, CxxResultExt, Directory, OsResultStatic, Utf8CStr, clone_attr, cstr, fclone_attr,
BufReadExt, Directory, OsResultStatic, ResultExt, Utf8CStr, clone_attr, cstr, fclone_attr,
map_fd, map_file, slice_from_ptr,
};
@@ -24,7 +24,7 @@ unsafe extern "C" fn canonical_path(path: *const c_char, buf: *mut u8, bufsz: us
Ok(path) => {
let mut buf = cstr::buf::wrap_ptr(buf, bufsz);
path.realpath(&mut buf)
.log_cxx()
.log()
.map_or(-1_isize, |_| buf.len() as isize)
}
Err(_) => -1,
@@ -61,13 +61,13 @@ unsafe extern "C" fn frm_rf(fd: OwnedFd) -> bool {
}
pub(crate) fn map_file_for_cxx(path: &Utf8CStr, rw: bool) -> &'static mut [u8] {
map_file(path, rw).log_cxx().unwrap_or(&mut [])
map_file(path, rw).log().unwrap_or(&mut [])
}
pub(crate) fn map_file_at_for_cxx(fd: RawFd, path: &Utf8CStr, rw: bool) -> &'static mut [u8] {
unsafe {
map_file_at(BorrowedFd::borrow_raw(fd), path, rw)
.log_cxx()
.log()
.unwrap_or(&mut [])
}
}
@@ -75,7 +75,7 @@ pub(crate) fn map_file_at_for_cxx(fd: RawFd, path: &Utf8CStr, rw: bool) -> &'sta
pub(crate) fn map_fd_for_cxx(fd: RawFd, sz: usize, rw: bool) -> &'static mut [u8] {
unsafe {
map_fd(BorrowedFd::borrow_raw(fd), sz, rw)
.log_cxx()
.log()
.unwrap_or(&mut [])
}
}
@@ -113,7 +113,7 @@ unsafe extern "C" fn cp_afc_for_cxx(src: *const c_char, dest: *const c_char) ->
if let Ok(src) = Utf8CStr::from_ptr(src)
&& let Ok(dest) = Utf8CStr::from_ptr(dest)
{
return src.copy_to(dest).log_cxx().is_ok();
return src.copy_to(dest).log().is_ok();
}
false
}
@@ -125,7 +125,7 @@ unsafe extern "C" fn mv_path_for_cxx(src: *const c_char, dest: *const c_char) ->
if let Ok(src) = Utf8CStr::from_ptr(src)
&& let Ok(dest) = Utf8CStr::from_ptr(dest)
{
return src.move_to(dest).log_cxx().is_ok();
return src.move_to(dest).log().is_ok();
}
false
}
@@ -137,7 +137,7 @@ unsafe extern "C" fn link_path_for_cxx(src: *const c_char, dest: *const c_char)
if let Ok(src) = Utf8CStr::from_ptr(src)
&& let Ok(dest) = Utf8CStr::from_ptr(dest)
{
return src.link_to(dest).log_cxx().is_ok();
return src.link_to(dest).log().is_ok();
}
false
}
@@ -149,7 +149,7 @@ unsafe extern "C" fn clone_attr_for_cxx(src: *const c_char, dest: *const c_char)
if let Ok(src) = Utf8CStr::from_ptr(src)
&& let Ok(dest) = Utf8CStr::from_ptr(dest)
{
return clone_attr(src, dest).log_cxx().is_ok();
return clone_attr(src, dest).log().is_ok();
}
false
}
@@ -157,7 +157,7 @@ unsafe extern "C" fn clone_attr_for_cxx(src: *const c_char, dest: *const c_char)
#[unsafe(export_name = "fclone_attr")]
unsafe extern "C" fn fclone_attr_for_cxx(a: RawFd, b: RawFd) -> bool {
fclone_attr(a, b).log_cxx().is_ok()
fclone_attr(a, b).log().is_ok()
}
#[unsafe(export_name = "cxx$utf8str$new")]

View File

@@ -31,7 +31,6 @@ pub mod ffi {
#[repr(i32)]
#[cxx_name = "LogLevel"]
pub(crate) enum LogLevelCxx {
ErrorCxx,
Error,
Warn,
Info,

View File

@@ -21,7 +21,6 @@ mod LogFlag {
#[derive(Copy, Clone, FromPrimitive, ToPrimitive)]
#[repr(i32)]
pub enum LogLevel {
ErrorCxx = LogLevelCxx::ErrorCxx.repr,
Error = LogLevelCxx::Error.repr,
Warn = LogLevelCxx::Warn.repr,
Info = LogLevelCxx::Info.repr,
@@ -57,7 +56,7 @@ pub fn exit_on_error(b: bool) {
impl LogLevel {
fn as_disable_flag(&self) -> u32 {
match *self {
LogLevel::Error | LogLevel::ErrorCxx => LogFlag::DisableError,
LogLevel::Error => LogFlag::DisableError,
LogLevel::Warn => LogFlag::DisableWarn,
LogLevel::Info => LogFlag::DisableInfo,
LogLevel::Debug => LogFlag::DisableDebug,
@@ -82,7 +81,7 @@ fn log_with_writer<F: FnOnce(LogWriter)>(level: LogLevel, f: F) {
return;
}
f(logger.write);
if matches!(level, LogLevel::ErrorCxx) && (logger.flags & LogFlag::ExitOnError) != 0 {
if (logger.flags & LogFlag::ExitOnError) != 0 {
exit(-1);
}
}

View File

@@ -13,9 +13,6 @@ use thiserror::Error;
// log and convert to LoggedResult.
//
// To log an error with more information, use `ResultExt::log_with_msg()`.
//
// The "cxx" method variants in `CxxResultExt` are only used for C++ interop and
// should not be used directly in any Rust code.
#[derive(Default)]
pub struct LoggedError {}
@@ -56,11 +53,6 @@ pub trait ResultExt<T> {
fn log_ok(self);
}
// Internal C++ bridging logging routines
pub(crate) trait CxxResultExt<T> {
fn log_cxx(self) -> LoggedResult<T>;
}
// Public API for converting Option to LoggedResult
pub trait OptionExt<T> {
fn ok_or_log(self) -> LoggedResult<T>;
@@ -102,12 +94,6 @@ trait Loggable {
) -> LoggedError;
}
impl<T, E: Loggable> CxxResultExt<T> for Result<T, E> {
fn log_cxx(self) -> LoggedResult<T> {
self.map_err(|e| e.do_log(LogLevel::ErrorCxx, None))
}
}
impl<T, E: Loggable> ResultExt<T> for Result<T, E> {
#[cfg(not(debug_assertions))]
fn log(self) -> LoggedResult<T> {

View File

@@ -2,7 +2,7 @@
use crate::cxx_extern::readlinkat;
use crate::{
BorrowedDirectory, CxxResultExt, LibcReturn, Utf8CStr, cstr, slice_from_ptr, slice_from_ptr_mut,
BorrowedDirectory, LibcReturn, ResultExt, Utf8CStr, cstr, slice_from_ptr, slice_from_ptr_mut,
};
use libc::{c_char, c_uint, c_ulong, c_void, dev_t, mode_t, off_t, sockaddr, socklen_t};
use std::ffi::CStr;
@@ -29,7 +29,7 @@ unsafe extern "C" fn xrealpath(path: *const c_char, buf: *mut u8, bufsz: usize)
Ok(path) => {
let mut buf = cstr::buf::wrap_ptr(buf, bufsz);
path.realpath(&mut buf)
.log_cxx()
.log()
.map_or(-1, |_| buf.len() as isize)
}
Err(_) => -1,
@@ -44,7 +44,7 @@ unsafe extern "C" fn xreadlink(path: *const c_char, buf: *mut u8, bufsz: usize)
Ok(path) => {
let mut buf = cstr::buf::wrap_ptr(buf, bufsz);
path.read_link(&mut buf)
.log_cxx()
.log()
.map_or(-1, |_| buf.len() as isize)
}
Err(_) => -1,
@@ -62,7 +62,7 @@ unsafe extern "C" fn xreadlinkat(
unsafe {
readlinkat(dirfd, path, buf, bufsz)
.into_os_result("readlinkat", ptr_to_str(path), None)
.log_cxx()
.log()
.unwrap_or(-1)
}
}
@@ -72,7 +72,7 @@ unsafe extern "C" fn xfopen(path: *const c_char, mode: *const c_char) -> *mut li
unsafe {
libc::fopen(path, mode)
.into_os_result("fopen", ptr_to_str(path), None)
.log_cxx()
.log()
.map_or(ptr::null_mut(), NonNull::as_ptr)
}
}
@@ -82,7 +82,7 @@ unsafe extern "C" fn xfdopen(fd: RawFd, mode: *const c_char) -> *mut libc::FILE
unsafe {
libc::fdopen(fd, mode)
.into_os_result("fdopen", None, None)
.log_cxx()
.log()
.map_or(ptr::null_mut(), NonNull::as_ptr)
}
}
@@ -92,7 +92,7 @@ unsafe extern "C" fn xopen(path: *const c_char, flags: i32, mode: mode_t) -> Raw
unsafe {
libc::open(path, flags, mode as c_uint)
.into_os_result("open", ptr_to_str(path), None)
.log_cxx()
.log()
.unwrap_or(-1)
}
}
@@ -102,7 +102,7 @@ unsafe extern "C" fn xopenat(dirfd: RawFd, path: *const c_char, flags: i32, mode
unsafe {
libc::openat(dirfd, path, flags, mode as c_uint)
.into_os_result("openat", ptr_to_str(path), None)
.log_cxx()
.log()
.unwrap_or(-1)
}
}
@@ -112,7 +112,7 @@ unsafe extern "C" fn xwrite(fd: RawFd, buf: *const u8, bufsz: usize) -> isize {
let mut file = unsafe { ManuallyDrop::new(File::from_raw_fd(fd)) };
let data = unsafe { slice_from_ptr(buf, bufsz) };
file.write_all(data)
.log_cxx()
.log()
.map_or(-1, |_| data.len() as isize)
}
@@ -121,7 +121,7 @@ unsafe extern "C" fn xread(fd: RawFd, buf: *mut c_void, bufsz: usize) -> isize {
unsafe {
libc::read(fd, buf, bufsz)
.into_os_result("read", None, None)
.log_cxx()
.log()
.unwrap_or(-1)
}
}
@@ -131,7 +131,7 @@ unsafe extern "C" fn xxread(fd: RawFd, buf: *mut u8, bufsz: usize) -> isize {
let mut file = unsafe { ManuallyDrop::new(File::from_raw_fd(fd)) };
let data = unsafe { slice_from_ptr_mut(buf, bufsz) };
file.read_exact(data)
.log_cxx()
.log()
.map_or(-1, |_| data.len() as isize)
}
@@ -139,7 +139,7 @@ pub(crate) fn xpipe2(fds: &mut [i32; 2], flags: i32) -> i32 {
unsafe {
libc::pipe2(fds.as_mut_ptr(), flags)
.into_os_result("pipe2", None, None)
.log_cxx()
.log()
.unwrap_or(-1)
}
}
@@ -149,7 +149,7 @@ extern "C" fn xsetns(fd: RawFd, nstype: i32) -> i32 {
unsafe {
libc::setns(fd, nstype)
.into_os_result("setns", None, None)
.log_cxx()
.log()
.unwrap_or(-1)
}
}
@@ -159,7 +159,7 @@ extern "C" fn xunshare(flags: i32) -> i32 {
unsafe {
libc::unshare(flags)
.into_os_result("unshare", None, None)
.log_cxx()
.log()
.unwrap_or(-1)
}
}
@@ -169,7 +169,7 @@ unsafe extern "C" fn xopendir(path: *const c_char) -> *mut libc::DIR {
unsafe {
libc::opendir(path)
.into_os_result("opendir", ptr_to_str(path), None)
.log_cxx()
.log()
.map_or(ptr::null_mut(), NonNull::as_ptr)
}
}
@@ -179,7 +179,7 @@ extern "C" fn xfdopendir(fd: RawFd) -> *mut libc::DIR {
unsafe {
libc::fdopendir(fd)
.into_os_result("fdopendir", None, None)
.log_cxx()
.log()
.map_or(ptr::null_mut(), NonNull::as_ptr)
}
}
@@ -187,7 +187,7 @@ extern "C" fn xfdopendir(fd: RawFd) -> *mut libc::DIR {
#[unsafe(no_mangle)]
unsafe extern "C" fn xreaddir(mut dir: BorrowedDirectory) -> *mut libc::dirent {
dir.read()
.log_cxx()
.log()
.ok()
.flatten()
.map_or(ptr::null_mut(), |entry| entry.as_ptr())
@@ -198,7 +198,7 @@ extern "C" fn xsetsid() -> i32 {
unsafe {
libc::setsid()
.into_os_result("setsid", None, None)
.log_cxx()
.log()
.unwrap_or(-1)
}
}
@@ -208,7 +208,7 @@ extern "C" fn xsocket(domain: i32, ty: i32, protocol: i32) -> RawFd {
unsafe {
libc::socket(domain, ty, protocol)
.into_os_result("socket", None, None)
.log_cxx()
.log()
.unwrap_or(-1)
}
}
@@ -218,7 +218,7 @@ unsafe extern "C" fn xbind(socket: i32, address: *const sockaddr, len: socklen_t
unsafe {
libc::bind(socket, address, len)
.into_os_result("bind", None, None)
.log_cxx()
.log()
.unwrap_or(-1)
}
}
@@ -228,7 +228,7 @@ extern "C" fn xlisten(socket: i32, backlog: i32) -> i32 {
unsafe {
libc::listen(socket, backlog)
.into_os_result("listen", None, None)
.log_cxx()
.log()
.unwrap_or(-1)
}
}
@@ -243,7 +243,7 @@ unsafe extern "C" fn xaccept4(
unsafe {
libc::accept4(sockfd, addr, len, flg)
.into_os_result("accept4", None, None)
.log_cxx()
.log()
.unwrap_or(-1)
}
}
@@ -253,7 +253,7 @@ unsafe extern "C" fn xstat(path: *const c_char, buf: *mut libc::stat) -> i32 {
unsafe {
libc::stat(path, buf)
.into_os_result("stat", ptr_to_str(path), None)
.log_cxx()
.log()
.unwrap_or(-1)
}
}
@@ -263,7 +263,7 @@ unsafe extern "C" fn xfstat(fd: RawFd, buf: *mut libc::stat) -> i32 {
unsafe {
libc::fstat(fd, buf)
.into_os_result("fstat", None, None)
.log_cxx()
.log()
.unwrap_or(-1)
}
}
@@ -273,7 +273,7 @@ extern "C" fn xdup2(oldfd: RawFd, newfd: RawFd) -> RawFd {
unsafe {
libc::dup2(oldfd, newfd)
.into_os_result("dup2", None, None)
.log_cxx()
.log()
.unwrap_or(-1)
}
}
@@ -283,7 +283,7 @@ unsafe extern "C" fn xsymlink(target: *const c_char, linkpath: *const c_char) ->
unsafe {
libc::symlink(target, linkpath)
.into_os_result("symlink", ptr_to_str(target), ptr_to_str(linkpath))
.log_cxx()
.log()
.unwrap_or(-1)
}
}
@@ -299,7 +299,7 @@ unsafe extern "C" fn xmount(
unsafe {
libc::mount(src, target, fstype, flags, data)
.into_os_result("mount", ptr_to_str(src), ptr_to_str(target))
.log_cxx()
.log()
.unwrap_or(-1)
}
}
@@ -309,7 +309,7 @@ unsafe extern "C" fn xumount2(target: *const c_char, flags: i32) -> i32 {
unsafe {
libc::umount2(target, flags)
.into_os_result("umount2", ptr_to_str(target), None)
.log_cxx()
.log()
.unwrap_or(-1)
}
}
@@ -319,7 +319,7 @@ unsafe extern "C" fn xrename(oldname: *const c_char, newname: *const c_char) ->
unsafe {
libc::rename(oldname, newname)
.into_os_result("rename", ptr_to_str(oldname), ptr_to_str(newname))
.log_cxx()
.log()
.unwrap_or(-1)
}
}
@@ -328,7 +328,7 @@ unsafe extern "C" fn xrename(oldname: *const c_char, newname: *const c_char) ->
unsafe extern "C" fn xmkdir(path: *const c_char, mode: mode_t) -> i32 {
unsafe {
match Utf8CStr::from_ptr(path) {
Ok(path) => path.mkdir(mode).log_cxx().map_or(-1, |_| 0),
Ok(path) => path.mkdir(mode).log().map_or(-1, |_| 0),
Err(_) => -1,
}
}
@@ -338,7 +338,7 @@ unsafe extern "C" fn xmkdir(path: *const c_char, mode: mode_t) -> i32 {
unsafe extern "C" fn xmkdirs(path: *const c_char, mode: mode_t) -> i32 {
unsafe {
match Utf8CStr::from_ptr(path) {
Ok(path) => path.mkdirs(mode).log_cxx().map_or(-1, |_| 0),
Ok(path) => path.mkdirs(mode).log().map_or(-1, |_| 0),
Err(_) => -1,
}
}
@@ -354,7 +354,7 @@ unsafe extern "C" fn xsendfile(
unsafe {
libc::sendfile(out_fd, in_fd, offset, count)
.into_os_result("sendfile", None, None)
.log_cxx()
.log()
.unwrap_or(-1)
}
}
@@ -364,7 +364,7 @@ extern "C" fn xfork() -> i32 {
unsafe {
libc::fork()
.into_os_result("fork", None, None)
.log_cxx()
.log()
.unwrap_or(-1)
}
}
@@ -374,7 +374,7 @@ unsafe extern "C" fn xmknod(pathname: *const c_char, mode: mode_t, dev: dev_t) -
unsafe {
libc::mknod(pathname, mode, dev)
.into_os_result("mknod", ptr_to_str(pathname), None)
.log_cxx()
.log()
.unwrap_or(-1)
}
}

View File

@@ -50,7 +50,7 @@ unsafe extern "C" {
fn level_to_prio(level: LogLevel) -> i32 {
match level {
LogLevel::Error | LogLevel::ErrorCxx => ALogPriority::ANDROID_LOG_ERROR as i32,
LogLevel::Error => ALogPriority::ANDROID_LOG_ERROR as i32,
LogLevel::Warn => ALogPriority::ANDROID_LOG_WARN as i32,
LogLevel::Info => ALogPriority::ANDROID_LOG_INFO as i32,
LogLevel::Debug => ALogPriority::ANDROID_LOG_DEBUG as i32,