From ab2e5d1e7e4ab43f1aa5adda8d9baeaefa51beda Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Mon, 21 Apr 2025 21:11:13 -0700 Subject: [PATCH] Make FsPathBuf a trait and rename to FsPathBuilder --- native/src/base/cstr.rs | 66 ---------------------------- native/src/base/dir.rs | 20 ++++----- native/src/base/files.rs | 66 +++++++++++++++++++++++++--- native/src/core/daemon.rs | 8 ++-- native/src/core/logging.rs | 10 +++-- native/src/core/mount.rs | 35 +++++++++------ native/src/core/package.rs | 28 ++++++------ native/src/core/resetprop/persist.rs | 24 +++++----- native/src/core/zygisk/daemon.rs | 22 +++++----- native/src/init/mount.rs | 10 ++--- native/src/init/rootdir.rs | 10 ++--- 11 files changed, 152 insertions(+), 147 deletions(-) diff --git a/native/src/base/cstr.rs b/native/src/base/cstr.rs index c30295b17..a4bba5cee 100644 --- a/native/src/base/cstr.rs +++ b/native/src/base/cstr.rs @@ -358,11 +358,6 @@ impl Utf8CStr { self.0.as_ptr().cast() } - #[inline(always)] - pub fn as_mut_ptr(&mut self) -> *mut c_char { - self.0.as_mut_ptr().cast() - } - #[inline(always)] pub fn as_cstr(&self) -> &CStr { // SAFETY: Already validated as null terminated during construction @@ -424,64 +419,6 @@ impl AsUtf8CStr for FsPathFollow { } } -pub struct FsPathBuf<'a, const N: usize>(pub Utf8CStrBuffer<'a, N>); - -impl From for FsPathBuf<'static, 0> { - fn from(value: Utf8CString) -> Self { - FsPathBuf(From::from(value)) - } -} - -impl From> for FsPathBuf<'static, N> { - fn from(value: Utf8CStrBufArr) -> Self { - FsPathBuf(From::from(value)) - } -} - -impl<'a> From<&'a mut dyn Utf8CStrBuf> for FsPathBuf<'a, 0> { - fn from(value: &'a mut dyn Utf8CStrBuf) -> Self { - FsPathBuf(From::from(value)) - } -} - -impl Default for FsPathBuf<'static, 4096> { - fn default() -> Self { - FsPathBuf(Default::default()) - } -} - -impl FsPathBuf<'_, N> { - pub fn clear(&mut self) { - self.0.clear(); - } - - pub fn join>(mut self, path: T) -> Self { - fn inner(buf: &mut dyn Utf8CStrBuf, path: &str) { - if path.starts_with('/') { - buf.clear(); - } - if !buf.is_empty() && !buf.ends_with('/') { - buf.push_str("/"); - } - buf.push_str(path); - } - inner(self.0.deref_mut(), path.as_ref()); - self - } - - pub fn join_fmt(mut self, name: T) -> Self { - self.0.write_fmt(format_args!("/{}", name)).ok(); - self - } -} - -impl AsUtf8CStr for FsPathBuf<'_, N> { - #[inline(always)] - fn as_utf8_cstr(&self) -> &Utf8CStr { - &self.0 - } -} - // impl Deref for T { ... } macro_rules! impl_cstr_deref { ($( ($t:ty, $($g:tt)*) )*) => {$( @@ -501,7 +438,6 @@ impl_cstr_deref!( (Utf8CStrBufArr, const N: usize) (Utf8CString,) (FsPathFollow,) - (FsPathBuf<'_, N>, const N: usize) ); // impl> BoilerPlate for T { ... } @@ -588,7 +524,6 @@ impl_cstr_misc!( (Utf8CStrBufArr, const N: usize) (Utf8CString,) (FsPathFollow,) - (FsPathBuf<'_, N>, const N: usize) ); fn copy_cstr_truncate(dest: &mut [u8], src: &[u8]) -> usize { @@ -688,7 +623,6 @@ impl_fs_path!( (Utf8CStrBufRef<'_>,) (Utf8CStrBufArr, const N: usize) (Utf8CString,) - (FsPathBuf<'_, N>, const N: usize) ); #[macro_export] diff --git a/native/src/base/dir.rs b/native/src/base/dir.rs index 7de8a36b0..0ca0381e1 100644 --- a/native/src/base/dir.rs +++ b/native/src/base/dir.rs @@ -1,6 +1,6 @@ use crate::cxx_extern::readlinkat; use crate::{ - FileAttr, FsPath, FsPathBuf, LibcReturn, OsError, OsResult, OsResultStatic, Utf8CStr, + FileAttr, FsPath, FsPathBuilder, LibcReturn, OsError, OsResult, OsResultStatic, Utf8CStr, Utf8CStrBuf, cstr_buf, errno, fd_path, fd_set_attr, }; use libc::{EEXIST, O_CLOEXEC, O_CREAT, O_RDONLY, O_TRUNC, O_WRONLY, dirent, mode_t}; @@ -184,7 +184,7 @@ impl Directory { fn path_at(&self, name: &Utf8CStr, buf: &mut dyn Utf8CStrBuf) -> OsResult<'static, ()> { self.path(buf)?; - FsPathBuf::from(buf).join(name); + buf.append_path(name); Ok(()) } } @@ -247,14 +247,14 @@ impl Directory { } pub fn get_attr_at<'a>(&self, name: &'a Utf8CStr) -> OsResult<'a, FileAttr> { - let mut path = FsPathBuf::default(); - self.path_at(name, path.0.deref_mut())?; + let mut path = cstr_buf::default(); + self.path_at(name, &mut path)?; path.get_attr().map_err(|e| e.set_args(Some(name), None)) } pub fn set_attr_at<'a>(&self, name: &'a Utf8CStr, attr: &FileAttr) -> OsResult<'a, ()> { - let mut path = FsPathBuf::default(); - self.path_at(name, path.0.deref_mut())?; + let mut path = cstr_buf::default(); + self.path_at(name, &mut path)?; path.set_attr(attr) .map_err(|e| e.set_args(Some(name), None)) } @@ -264,15 +264,15 @@ impl Directory { name: &'a Utf8CStr, con: &mut dyn Utf8CStrBuf, ) -> OsResult<'a, ()> { - let mut path = FsPathBuf::default(); - self.path_at(name, path.0.deref_mut())?; + let mut path = cstr_buf::default(); + self.path_at(name, &mut path)?; path.get_secontext(con) .map_err(|e| e.set_args(Some(name), None)) } pub fn set_secontext_at<'a>(&self, name: &'a Utf8CStr, con: &'a Utf8CStr) -> OsResult<'a, ()> { - let mut path = FsPathBuf::default(); - self.path_at(name, path.0.deref_mut())?; + let mut path = cstr_buf::default(); + self.path_at(name, &mut path)?; path.set_secontext(con) .map_err(|e| e.set_args(Some(name), Some(con))) } diff --git a/native/src/base/files.rs b/native/src/base/files.rs index 0e033d264..9b6887386 100644 --- a/native/src/base/files.rs +++ b/native/src/base/files.rs @@ -1,6 +1,6 @@ use crate::{ - Directory, FsPathBuf, FsPathFollow, LibcReturn, OsError, OsResult, OsResultStatic, Utf8CStr, - Utf8CStrBuf, cstr_buf, errno, error, + Directory, FsPathFollow, LibcReturn, OsError, OsResult, OsResultStatic, Utf8CStr, Utf8CStrBuf, + cstr_buf, errno, error, }; use bytemuck::{Pod, bytes_of, bytes_of_mut}; use libc::{ @@ -11,6 +11,7 @@ use mem::MaybeUninit; use num_traits::AsPrimitive; use std::cmp::min; use std::ffi::CStr; +use std::fmt::Display; use std::fs::File; use std::io::{BufRead, BufReader, Read, Seek, SeekFrom, Write}; use std::ops::Deref; @@ -138,7 +139,9 @@ macro_rules! open_fd { } pub fn fd_path(fd: RawFd, buf: &mut dyn Utf8CStrBuf) -> OsResult<'static, ()> { - let path = FsPathBuf::default().join("/proc/self/fd").join_fmt(fd); + let path = cstr_buf::default() + .join_path("/proc/self/fd") + .join_path_fmt(fd); path.read_link(buf).map_err(|e| e.set_args(None, None)) } @@ -267,13 +270,13 @@ pub trait FsPath: Deref { return Ok(()); } - let mut path = FsPathBuf::default(); + let mut path = cstr_buf::default(); let mut components = self.split('/').filter(|s| !s.is_empty()); loop { let Some(s) = components.next() else { break; }; - path = path.join(s); + path.append_path(s); unsafe { if libc::mkdir(path.as_ptr(), mode) < 0 && *errno() != EEXIST { @@ -548,6 +551,59 @@ impl FsPath for FsPathFollow { } } +pub trait FsPathBuilder { + fn join_path>(mut self, path: T) -> Self + where + Self: Sized, + { + self.append_path(path); + self + } + fn join_path_fmt(mut self, name: T) -> Self + where + Self: Sized, + { + self.append_path_fmt(name); + self + } + fn append_path>(&mut self, path: T) -> &mut Self; + fn append_path_fmt(&mut self, name: T) -> &mut Self; +} + +fn append_path_impl(buf: &mut dyn Utf8CStrBuf, path: &str) { + if path.starts_with('/') { + buf.clear(); + } + if !buf.is_empty() && !buf.ends_with('/') { + buf.push_str("/"); + } + buf.push_str(path); +} + +impl FsPathBuilder for S { + fn append_path>(&mut self, path: T) -> &mut Self { + append_path_impl(self, path.as_ref()); + self + } + + fn append_path_fmt(&mut self, name: T) -> &mut Self { + self.write_fmt(format_args!("/{}", name)).ok(); + self + } +} + +impl FsPathBuilder for dyn Utf8CStrBuf + '_ { + fn append_path>(&mut self, path: T) -> &mut Self { + append_path_impl(self, path.as_ref()); + self + } + + fn append_path_fmt(&mut self, name: T) -> &mut Self { + self.write_fmt(format_args!("/{}", name)).ok(); + self + } +} + pub fn fd_get_attr(fd: RawFd) -> OsResult<'static, FileAttr> { let mut attr = FileAttr::new(); unsafe { diff --git a/native/src/core/daemon.rs b/native/src/core/daemon.rs index ace43bbae..d239ba1d0 100644 --- a/native/src/core/daemon.rs +++ b/native/src/core/daemon.rs @@ -11,7 +11,7 @@ use crate::package::ManagerInfo; use crate::su::SuInfo; use base::libc::{O_CLOEXEC, O_RDONLY}; use base::{ - AtomicArc, BufReadExt, FsPath, FsPathBuf, ResultExt, Utf8CStr, cstr, cstr_buf, error, info, + AtomicArc, BufReadExt, FsPath, FsPathBuilder, ResultExt, Utf8CStr, cstr, cstr_buf, error, info, libc, open_fd, }; use std::fs::File; @@ -229,9 +229,9 @@ pub fn daemon_entry() { || get_prop(cstr!("ro.product.device"), false).contains("vsoc"); // Load config status - let path = FsPathBuf::from(cstr_buf::new::<64>()) - .join(get_magisk_tmp()) - .join(MAIN_CONFIG); + let path = cstr_buf::new::<64>() + .join_path(get_magisk_tmp()) + .join_path(MAIN_CONFIG); let mut is_recovery = false; if let Ok(file) = path.open(O_RDONLY | O_CLOEXEC) { let mut file = BufReader::new(file); diff --git a/native/src/core/logging.rs b/native/src/core/logging.rs index 0178ad490..2f50c39fd 100644 --- a/native/src/core/logging.rs +++ b/native/src/core/logging.rs @@ -6,7 +6,7 @@ use base::libc::{ localtime_r, pthread_sigmask, sigaddset, sigset_t, sigtimedwait, time_t, timespec, tm, }; use base::{ - FsPathBuf, LOGGER, LogLevel, Logger, ReadExt, Utf8CStr, Utf8CStrBuf, WriteExt, + FsPathBuilder, LOGGER, LogLevel, Logger, ReadExt, Utf8CStr, Utf8CStrBuf, WriteExt, const_format::concatcp, cstr_buf, libc, raw_cstr, }; use bytemuck::{Pod, Zeroable, bytes_of, write_zeroes}; @@ -180,7 +180,9 @@ pub fn zygisk_get_logd() -> i32 { let mut fd = ZYGISK_LOGD.load(Ordering::Relaxed); if fd < 0 { android_logging(); - let path = FsPathBuf::default().join(get_magisk_tmp()).join(LOG_PIPE); + let path = cstr_buf::default() + .join_path(get_magisk_tmp()) + .join_path(LOG_PIPE); // Open as RW as sometimes it may block fd = unsafe { libc::open(path.as_ptr(), O_RDWR | O_CLOEXEC) }; if fd >= 0 { @@ -355,7 +357,9 @@ pub fn setup_logfile() { } pub fn start_log_daemon() { - let path = FsPathBuf::default().join(get_magisk_tmp()).join(LOG_PIPE); + let path = cstr_buf::default() + .join_path(get_magisk_tmp()) + .join_path(LOG_PIPE); unsafe { libc::mkfifo(path.as_ptr(), 0o666); diff --git a/native/src/core/mount.rs b/native/src/core/mount.rs index 389eacd4b..454b91049 100644 --- a/native/src/core/mount.rs +++ b/native/src/core/mount.rs @@ -7,8 +7,8 @@ use num_traits::AsPrimitive; use base::libc::{c_uint, dev_t}; use base::{ - FsPath, FsPathBuf, FsPathMnt, LibcReturn, LoggedResult, MountInfo, ResultExt, Utf8CStr, cstr, - cstr_buf, debug, info, libc, parse_mount_info, warn, + FsPath, FsPathBuilder, FsPathMnt, LibcReturn, LoggedResult, MountInfo, ResultExt, Utf8CStr, + Utf8CStrBuf, cstr, cstr_buf, debug, info, libc, parse_mount_info, warn, }; use crate::consts::{MODULEMNT, MODULEROOT, PREINITDEV, PREINITMIRR, WORKERDIR}; @@ -21,9 +21,9 @@ pub fn setup_mounts() { let magisk_tmp = get_magisk_tmp(); // Mount preinit directory - let dev_path = FsPathBuf::from(cstr_buf::new::<64>()) - .join(magisk_tmp) - .join(PREINITDEV); + let dev_path = cstr_buf::new::<64>() + .join_path(magisk_tmp) + .join_path(PREINITDEV); let mut linked = false; if let Ok(attr) = dev_path.get_attr() { if attr.st.st_mode & libc::S_IFMT as c_uint == libc::S_IFBLK.as_() { @@ -33,7 +33,9 @@ pub fn setup_mounts() { // What we do instead is to scan through the current mountinfo and find a pre-existing // mount point mounting our desired partition, and then bind mount the target folder. let preinit_dev = attr.st.st_rdev; - let mnt_path = FsPathBuf::default().join(magisk_tmp).join(PREINITMIRR); + let mnt_path = cstr_buf::default() + .join_path(magisk_tmp) + .join_path(PREINITMIRR); for info in parse_mount_info("self") { if info.root == "/" && info.device == preinit_dev { if !info.fs_option.split(',').any(|s| s == "rw") { @@ -66,7 +68,9 @@ pub fn setup_mounts() { } // Bind remount module root to clear nosuid - let module_mnt = FsPathBuf::default().join(magisk_tmp).join(MODULEMNT); + let module_mnt = cstr_buf::default() + .join_path(magisk_tmp) + .join_path(MODULEMNT); let _: LoggedResult<()> = try { module_mnt.mkdir(0o755)?; cstr!(MODULEROOT).bind_mount_to(&module_mnt)?; @@ -77,11 +81,13 @@ pub fn setup_mounts() { pub fn clean_mounts() { let magisk_tmp = get_magisk_tmp(); - let mut module_mnt = FsPathBuf::default().join(magisk_tmp).join(MODULEMNT); - module_mnt.unmount().log_ok(); + let mut buf = cstr_buf::default(); - module_mnt.clear(); - let worker_dir = module_mnt.join(magisk_tmp).join(WORKERDIR); + let module_mnt = buf.append_path(magisk_tmp).append_path(MODULEMNT); + module_mnt.unmount().log_ok(); + buf.clear(); + + let worker_dir = buf.append_path(magisk_tmp).append_path(WORKERDIR); let _: LoggedResult<()> = try { worker_dir.set_mount_private(true)?; worker_dir.unmount()?; @@ -180,7 +186,8 @@ pub fn find_preinit_device() -> String { && let Ok(tmp) = std::env::var("MAGISKTMP") && !tmp.is_empty() { - let mut mirror_dir = FsPathBuf::default().join(&tmp).join(PREINITMIRR); + let mut buf = cstr_buf::default(); + let mirror_dir = buf.append_path(&tmp).append_path(PREINITMIRR); let preinit_dir = Utf8CStr::from_string(&mut preinit_dir); let _: LoggedResult<()> = try { preinit_dir.mkdirs(0o700)?; @@ -190,8 +197,8 @@ pub fn find_preinit_device() -> String { mirror_dir.create_symlink_to(preinit_dir)?; }; if std::env::var_os("MAKEDEV").is_some() { - mirror_dir.clear(); - let dev_path = mirror_dir.join(&tmp).join(PREINITDEV); + buf.clear(); + let dev_path = buf.append_path(&tmp).append_path(PREINITDEV); unsafe { libc::mknod( dev_path.as_ptr(), diff --git a/native/src/core/package.rs b/native/src/core/package.rs index 0a330c73f..a76f2afda 100644 --- a/native/src/core/package.rs +++ b/native/src/core/package.rs @@ -4,8 +4,8 @@ use crate::ffi::{DbEntryKey, get_magisk_tmp, install_apk, uninstall_pkg}; use base::WalkResult::{Abort, Continue, Skip}; use base::libc::{O_CLOEXEC, O_CREAT, O_RDONLY, O_TRUNC, O_WRONLY}; use base::{ - BufReadExt, Directory, FsPath, FsPathBuf, LoggedResult, ReadExt, ResultExt, Utf8CStrBuf, cstr, - cstr_buf, error, fd_get_attr, open_fd, warn, + BufReadExt, Directory, FsPath, FsPathBuilder, LoggedResult, ReadExt, ResultExt, Utf8CStrBuf, + cstr, cstr_buf, error, fd_get_attr, open_fd, warn, }; use bit_set::BitSet; use cxx::CxxString; @@ -239,12 +239,12 @@ impl TrackedFile { impl ManagerInfo { fn check_dyn(&mut self, daemon: &MagiskD, user: i32, pkg: &str) -> Status { - let apk = FsPathBuf::default() - .join(daemon.app_data_dir()) - .join_fmt(user) - .join(pkg) - .join("dyn") - .join("current.apk"); + let apk = cstr_buf::default() + .join_path(daemon.app_data_dir()) + .join_path_fmt(user) + .join_path(pkg) + .join_path("dyn") + .join_path("current.apk"); let uid: i32; let cert = match apk.open(O_RDONLY | O_CLOEXEC) { Ok(mut fd) => { @@ -441,10 +441,10 @@ impl ManagerInfo { impl MagiskD { fn get_package_uid(&self, user: i32, pkg: &str) -> i32 { - let path = FsPathBuf::default() - .join(self.app_data_dir()) - .join_fmt(user) - .join(pkg); + let path = cstr_buf::default() + .join_path(self.app_data_dir()) + .join_path_fmt(user) + .join_path(pkg); path.get_attr() .map(|attr| attr.st.st_uid as i32) .unwrap_or(-1) @@ -453,7 +453,9 @@ impl MagiskD { pub fn preserve_stub_apk(&self) { let mut info = self.manager_info.lock().unwrap(); - let apk = FsPathBuf::default().join(get_magisk_tmp()).join("stub.apk"); + let apk = cstr_buf::default() + .join_path(get_magisk_tmp()) + .join_path("stub.apk"); if let Ok(mut fd) = apk.open(O_RDONLY | O_CLOEXEC) { info.trusted_cert = read_certificate(&mut fd, MAGISK_VER_CODE); diff --git a/native/src/core/resetprop/persist.rs b/native/src/core/resetprop/persist.rs index ea348e25a..7e18d468b 100644 --- a/native/src/core/resetprop/persist.rs +++ b/native/src/core/resetprop/persist.rs @@ -16,8 +16,8 @@ use crate::resetprop::proto::persistent_properties::{ use base::const_format::concatcp; use base::libc::{O_CLOEXEC, O_RDONLY}; use base::{ - Directory, FsPath, FsPathBuf, LibcReturn, LoggedResult, MappedFile, SilentResultExt, Utf8CStr, - WalkResult, clone_attr, cstr, debug, libc::mkstemp, + Directory, FsPath, FsPathBuilder, LibcReturn, LoggedResult, MappedFile, SilentResultExt, + Utf8CStr, Utf8CStrBuf, WalkResult, clone_attr, cstr, cstr_buf, debug, libc::mkstemp, }; const PERSIST_PROP_DIR: &str = "/data/property"; @@ -68,7 +68,9 @@ fn check_proto() -> bool { } fn file_get_prop(name: &Utf8CStr) -> LoggedResult { - let path = FsPathBuf::default().join(PERSIST_PROP_DIR).join(name); + let path = cstr_buf::default() + .join_path(PERSIST_PROP_DIR) + .join_path(name); let mut file = path.open(O_RDONLY | O_CLOEXEC).silent()?; debug!("resetprop: read prop from [{}]", path); let mut s = String::new(); @@ -77,14 +79,16 @@ fn file_get_prop(name: &Utf8CStr) -> LoggedResult { } fn file_set_prop(name: &Utf8CStr, value: Option<&Utf8CStr>) -> LoggedResult<()> { - let path = FsPathBuf::default().join(PERSIST_PROP_DIR).join(name); + let path = cstr_buf::default() + .join_path(PERSIST_PROP_DIR) + .join_path(name); if let Some(value) = value { - let mut tmp = FsPathBuf::default() - .join(PERSIST_PROP_DIR) - .join("prop.XXXXXX"); + let mut tmp = cstr_buf::default() + .join_path(PERSIST_PROP_DIR) + .join_path("prop.XXXXXX"); { let mut f = unsafe { - mkstemp(tmp.0.as_mut_ptr()) + mkstemp(tmp.as_mut_ptr()) .as_os_result("mkstemp", None, None) .map(|fd| File::from_raw_fd(fd))? }; @@ -111,10 +115,10 @@ fn proto_read_props() -> LoggedResult { } fn proto_write_props(props: &PersistentProperties) -> LoggedResult<()> { - let mut tmp = FsPathBuf::default().join(concatcp!(PERSIST_PROP, ".XXXXXX")); + let mut tmp = cstr_buf::default().join_path(concatcp!(PERSIST_PROP, ".XXXXXX")); { let f = unsafe { - mkstemp(tmp.0.as_mut_ptr()) + mkstemp(tmp.as_mut_ptr()) .as_os_result("mkstemp", None, None) .map(|fd| File::from_raw_fd(fd))? }; diff --git a/native/src/core/zygisk/daemon.rs b/native/src/core/zygisk/daemon.rs index 43a730a94..2c9c0f566 100644 --- a/native/src/core/zygisk/daemon.rs +++ b/native/src/core/zygisk/daemon.rs @@ -6,8 +6,8 @@ use crate::ffi::{ use crate::socket::{IpcRead, UnixSocketExt}; use base::libc::{O_CLOEXEC, O_CREAT, O_RDONLY, STDOUT_FILENO}; use base::{ - Directory, FsPathBuf, LoggedError, LoggedResult, ResultExt, WriteExt, cstr, cstr_buf, error, - fork_dont_care, libc, open_fd, raw_cstr, warn, + Directory, FsPathBuilder, LoggedError, LoggedResult, ResultExt, WriteExt, cstr, cstr_buf, + error, fork_dont_care, libc, open_fd, raw_cstr, warn, }; use std::fmt::Write; use std::os::fd::{AsRawFd, FromRawFd, RawFd}; @@ -37,9 +37,9 @@ fn exec_zygiskd(is_64_bit: bool, remote: UnixStream) { #[cfg(target_pointer_width = "32")] let magisk = "magisk"; - let exe = FsPathBuf::from(cstr_buf::new::<64>()) - .join(get_magisk_tmp()) - .join(magisk); + let exe = cstr_buf::new::<64>() + .join_path(get_magisk_tmp()) + .join_path(magisk); let mut fd_str = cstr_buf::new::<16>(); write!(fd_str, "{}", remote.as_raw_fd()).ok(); @@ -185,10 +185,10 @@ impl MagiskD { let failed_ids: Vec = client.read_decodable()?; if let Some(module_list) = self.module_list.get() { for id in failed_ids { - let path = FsPathBuf::default() - .join(MODULEROOT) - .join(&module_list[id as usize].name) - .join("zygisk"); + let path = cstr_buf::default() + .join_path(MODULEROOT) + .join_path(&module_list[id as usize].name) + .join_path("zygisk"); // Create the unloaded marker file if let Ok(dir) = Directory::open(&path) { dir.openat_as_file(cstr!("unloaded"), O_CREAT | O_RDONLY, 0o644) @@ -204,7 +204,9 @@ impl MagiskD { fn get_mod_dir(&self, mut client: UnixStream) -> LoggedResult<()> { let id: i32 = client.read_decodable()?; let module = &self.module_list.get().unwrap()[id as usize]; - let dir = FsPathBuf::default().join(MODULEROOT).join(&module.name); + let dir = cstr_buf::default() + .join_path(MODULEROOT) + .join_path(&module.name); let fd = open_fd!(&dir, O_RDONLY | O_CLOEXEC)?; client.send_fds(&[fd.as_raw_fd()])?; Ok(()) diff --git a/native/src/init/mount.rs b/native/src/init/mount.rs index e6625425f..1b90b4269 100644 --- a/native/src/init/mount.rs +++ b/native/src/init/mount.rs @@ -1,8 +1,8 @@ use crate::ffi::MagiskInit; use base::libc::{TMPFS_MAGIC, statfs}; use base::{ - Directory, FsPath, FsPathBuf, FsPathMnt, LibcReturn, LoggedResult, ResultExt, Utf8CStr, cstr, - debug, libc, + Directory, FsPath, FsPathBuilder, FsPathMnt, LibcReturn, LoggedResult, ResultExt, Utf8CStr, + cstr, cstr_buf, debug, libc, libc::{chdir, chroot, execve, exit, mount}, parse_mount_info, raw_cstr, }; @@ -38,9 +38,9 @@ pub(crate) fn switch_root(path: &Utf8CStr) { let mut target = info.target.clone(); let target = Utf8CStr::from_string(&mut target); - let new_path = FsPathBuf::default() - .join(path) - .join(info.target.trim_start_matches('/')); + let new_path = cstr_buf::default() + .join_path(path) + .join_path(info.target.trim_start_matches('/')); new_path.mkdirs(0o755).ok(); target.move_mount_to(&new_path)?; mounts.insert(info.target); diff --git a/native/src/init/rootdir.rs b/native/src/init/rootdir.rs index d06e2966c..02b102550 100644 --- a/native/src/init/rootdir.rs +++ b/native/src/init/rootdir.rs @@ -2,7 +2,7 @@ use crate::consts::{ROOTMNT, ROOTOVL}; use crate::ffi::MagiskInit; use base::libc::{O_CREAT, O_RDONLY, O_WRONLY}; use base::{ - BufReadExt, Directory, FsPath, FsPathBuf, FsPathMnt, LoggedResult, ResultExt, Utf8CStr, + BufReadExt, Directory, FsPath, FsPathBuilder, FsPathMnt, LoggedResult, ResultExt, Utf8CStr, Utf8CString, clone_attr, cstr, cstr_buf, debug, }; use std::io::BufReader; @@ -72,12 +72,8 @@ impl MagiskInit { None => return Ok(()), Some(e) => { let name = e.name(); - let src = FsPathBuf::from(cstr_buf::dynamic(256)) - .join(src_dir) - .join(name); - let dest = FsPathBuf::from(cstr_buf::dynamic(256)) - .join(dest_dir) - .join(name); + let src = cstr_buf::dynamic(256).join_path(src_dir).join_path(name); + let dest = cstr_buf::dynamic(256).join_path(dest_dir).join_path(name); if dest.exists() { if e.is_dir() { // Recursive