From 9a74e19117398b9142e04e43e91aada77c556af9 Mon Sep 17 00:00:00 2001 From: LoveSy Date: Tue, 4 Feb 2025 21:15:16 +0800 Subject: [PATCH] Add `log_ok()` for `log().ok()` --- native/src/base/result.rs | 12 ++++++++++++ native/src/core/daemon.rs | 8 ++++---- native/src/core/db.rs | 2 +- native/src/core/package.rs | 4 ++-- native/src/init/init.rs | 4 ++-- native/src/init/mount.rs | 32 +++++++++++++------------------- native/src/init/twostage.rs | 4 ++-- 7 files changed, 36 insertions(+), 30 deletions(-) diff --git a/native/src/base/result.rs b/native/src/base/result.rs index 0d32d4f63..18482e039 100644 --- a/native/src/base/result.rs +++ b/native/src/base/result.rs @@ -55,6 +55,7 @@ impl SilentResultExt for Option { pub trait ResultExt { fn log(self) -> LoggedResult; fn log_with_msg fmt::Result>(self, f: F) -> LoggedResult; + fn log_ok(self); } // Internal C++ bridging logging routines @@ -95,6 +96,17 @@ impl> ResultExt for R { self.do_log(LogLevel::Error, Some(Location::caller())) } + #[track_caller] + #[cfg(debug_assertions)] + fn log_ok(self) { + self.log().ok(); + } + + #[cfg(not(debug_assertions))] + fn log_ok(self) { + self.log().ok(); + } + #[cfg(not(debug_assertions))] fn log_with_msg fmt::Result>(self, f: F) -> LoggedResult { self.do_log_msg(LogLevel::Error, None, f) diff --git a/native/src/core/daemon.rs b/native/src/core/daemon.rs index 38d5969cc..0cf92f8a3 100644 --- a/native/src/core/daemon.rs +++ b/native/src/core/daemon.rs @@ -109,7 +109,7 @@ impl MagiskD { let secure_dir = FsPath::from(cstr!(SECURE_DIR)); if !secure_dir.exists() { if self.sdk_int < 24 { - secure_dir.mkdir(0o700).log().ok(); + secure_dir.mkdir(0o700).log_ok(); } else { error!("* {} is not present, abort", SECURE_DIR); return true; @@ -137,7 +137,7 @@ impl MagiskD { info!("* Safe mode triggered"); // Disable all modules and zygisk so next boot will be clean disable_modules(); - self.set_db_setting(DbEntryKey::ZygiskConfig, 0).log().ok(); + self.set_db_setting(DbEntryKey::ZygiskConfig, 0).log_ok(); return true; } @@ -170,12 +170,12 @@ impl MagiskD { info!("** boot-complete triggered"); // Reset the bootloop counter once we have boot-complete - self.set_db_setting(DbEntryKey::BootloopCount, 0).log().ok(); + self.set_db_setting(DbEntryKey::BootloopCount, 0).log_ok(); // At this point it's safe to create the folder let secure_dir = FsPath::from(cstr!(SECURE_DIR)); if !secure_dir.exists() { - secure_dir.mkdir(0o700).log().ok(); + secure_dir.mkdir(0o700).log_ok(); } self.ensure_manager(); diff --git a/native/src/core/db.rs b/native/src/core/db.rs index 3160d3e53..f29f91c9e 100644 --- a/native/src/core/db.rs +++ b/native/src/core/db.rs @@ -314,7 +314,7 @@ impl MagiskD { out.push('='); out.push_str(values.get_text(i as i32)); } - writer.write_encodable(&out).log().ok(); + writer.write_encodable(&out).log_ok(); }; self.db_exec_with_rows(&sql, &[], &mut output_fn); writer.write_encodable("").log() diff --git a/native/src/core/package.rs b/native/src/core/package.rs index c1b5c9f36..c8ea1e69e 100644 --- a/native/src/core/package.rs +++ b/native/src/core/package.rs @@ -465,11 +465,11 @@ impl MagiskD { if let Ok(mut fd) = apk.open(O_RDONLY | O_CLOEXEC) { info.trusted_cert = read_certificate(&mut fd, MAGISK_VER_CODE); // Seek the fd back to start - fd.seek(SeekFrom::Start(0)).log().ok(); + fd.seek(SeekFrom::Start(0)).log_ok(); info.stub_apk_fd = Some(fd); } - apk.remove().log().ok(); + apk.remove().log_ok(); } pub fn get_manager_uid(&self, user: i32) -> i32 { diff --git a/native/src/init/init.rs b/native/src/init/init.rs index 91ce43e68..aba4beee7 100644 --- a/native/src/init/init.rs +++ b/native/src/init/init.rs @@ -66,7 +66,7 @@ impl MagiskInit { let orig_init = FsPath::from(unsafe { Utf8CStr::from_ptr_unchecked(self.backup_init()) }); if orig_init.exists() { - orig_init.rename_to(FsPath::from(cstr!("/init"))).log().ok(); + orig_init.rename_to(FsPath::from(cstr!("/init"))).log_ok(); } else { // If the backup init is missing, this means that the boot ramdisk // was created from scratch, and the real init is in a separate CPIO, @@ -151,7 +151,7 @@ pub unsafe extern "C" fn main( } if getpid() == 1 { - MagiskInit::new(argv).start().log().ok(); + MagiskInit::new(argv).start().log_ok(); } 1 diff --git a/native/src/init/mount.rs b/native/src/init/mount.rs index 128328cdf..8ab56f159 100644 --- a/native/src/init/mount.rs +++ b/native/src/init/mount.rs @@ -76,26 +76,20 @@ pub fn is_device_mounted(dev: u64, target: Pin<&mut CxxString>) -> bool { impl MagiskInit { pub(crate) fn prepare_data(&self) { debug!("Setup data tmp"); - fn inner() -> LoggedResult<()> { - FsPath::from(cstr!("/data")).mkdir(0o755)?; - unsafe { - mount( - raw_cstr!("magisk"), - raw_cstr!("/data"), - raw_cstr!("tmpfs"), - 0, - raw_cstr!("mode=755").cast(), - ) - } - .as_os_err()?; + FsPath::from(cstr!("/data")).mkdir(0o755).log_ok(); + unsafe { + mount( + raw_cstr!("magisk"), + raw_cstr!("/data"), + raw_cstr!("tmpfs"), + 0, + raw_cstr!("mode=755").cast(), + ) + }.as_os_err().log_ok(); - FsPath::from(cstr!("/init")).copy_to(FsPath::from(cstr!("/data/magiskinit")))?; - FsPath::from(cstr!("/.backup")).copy_to(FsPath::from(cstr!("/data/.backup")))?; - FsPath::from(cstr!("/overlay.d")).copy_to(FsPath::from(cstr!("/data/overlay.d")))?; - - Ok(()) - } - inner().ok(); + FsPath::from(cstr!("/init")).copy_to(FsPath::from(cstr!("/data/magiskinit"))).log_ok(); + FsPath::from(cstr!("/.backup")).copy_to(FsPath::from(cstr!("/data/.backup"))).log_ok(); + FsPath::from(cstr!("/overlay.d")).copy_to(FsPath::from(cstr!("/data/overlay.d"))).log_ok(); } pub(crate) fn exec_init(&self) { diff --git a/native/src/init/twostage.rs b/native/src/init/twostage.rs index 8958db4f1..7643db361 100644 --- a/native/src/init/twostage.rs +++ b/native/src/init/twostage.rs @@ -116,14 +116,14 @@ impl MagiskInit { debug!("Patch @ {:#010X} [{}] -> [{}]", off, from, to); } if let Ok(mut dest) = dest.create(O_CREAT | O_WRONLY, 0) { - dest.write_all(map.as_ref()).log().ok(); + dest.write_all(map.as_ref()).log_ok(); } else { error!("Failed to create {}", dest); } } else { error!("Failed to open {} for hexpatch", src); } - clone_attr(src, dest).log().ok(); + clone_attr(src, dest).log_ok(); unsafe { mount(dest.as_ptr(), src.as_ptr(), null(), MS_BIND, null()) .as_os_err()