Make FsPathBuf a trait and rename to FsPathBuilder

This commit is contained in:
topjohnwu
2025-04-21 21:11:13 -07:00
committed by John Wu
parent f3fef7bfe4
commit ab2e5d1e7e
11 changed files with 152 additions and 147 deletions

View File

@@ -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);

View File

@@ -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);

View File

@@ -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(),

View File

@@ -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);

View File

@@ -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<String> {
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<String> {
}
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<PersistentProperties> {
}
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))?
};

View File

@@ -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<i32> = 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(())