Rename cstr_buf to cstr::buf

This commit is contained in:
topjohnwu 2025-04-22 02:55:53 -07:00 committed by John Wu
parent ab2e5d1e7e
commit e02b5f7868
16 changed files with 65 additions and 63 deletions

View File

@ -21,7 +21,7 @@ use crate::{FsPath, FsPathMnt, slice_from_ptr_mut};
// Utf8CStrBufRef: reference to a fixed sized buffer
// Utf8CStrBufArr<N>: fixed sized buffer allocated on the stack
//
// For easier usage, please use the helper functions in cstr_buf.
// For easier usage, please use the helper functions in cstr::buf.
//
// In most cases, these are the types being used
//
@ -36,7 +36,7 @@ use crate::{FsPath, FsPathMnt, slice_from_ptr_mut};
// Public helper functions
pub mod cstr_buf {
pub mod buf {
use super::{Utf8CStrBufArr, Utf8CStrBufRef, Utf8CString};
#[inline(always)]
@ -118,7 +118,7 @@ impl<'a> From<&'a mut dyn Utf8CStrBuf> for Utf8CStrBuffer<'a, 0> {
impl Default for Utf8CStrBuffer<'static, 4096> {
fn default() -> Self {
Utf8CStrBuffer::Array(cstr_buf::default())
Utf8CStrBuffer::Array(buf::default())
}
}

View File

@ -8,12 +8,12 @@ use libc::{c_char, mode_t};
use crate::files::map_file_at;
pub(crate) use crate::xwrap::*;
use crate::{
CxxResultExt, Directory, FsPath, OsResultStatic, Utf8CStr, clone_attr, cstr, cstr_buf,
fclone_attr, fd_path, map_fd, map_file, slice_from_ptr,
CxxResultExt, Directory, FsPath, OsResultStatic, Utf8CStr, clone_attr, cstr, fclone_attr,
fd_path, map_fd, map_file, slice_from_ptr,
};
pub(crate) fn fd_path_for_cxx(fd: RawFd, buf: &mut [u8]) -> isize {
let mut buf = cstr_buf::wrap(buf);
let mut buf = cstr::buf::wrap(buf);
fd_path(fd, &mut buf)
.log_cxx()
.map_or(-1_isize, |_| buf.len() as isize)
@ -24,7 +24,7 @@ unsafe extern "C" fn canonical_path(path: *const c_char, buf: *mut u8, bufsz: us
unsafe {
match Utf8CStr::from_ptr(path) {
Ok(path) => {
let mut buf = cstr_buf::wrap_ptr(buf, bufsz);
let mut buf = cstr::buf::wrap_ptr(buf, bufsz);
path.realpath(&mut buf)
.log_cxx()
.map_or(-1_isize, |_| buf.len() as isize)

View File

@ -1,7 +1,7 @@
use crate::cxx_extern::readlinkat;
use crate::{
FileAttr, FsPath, FsPathBuilder, LibcReturn, OsError, OsResult, OsResultStatic, Utf8CStr,
Utf8CStrBuf, cstr_buf, errno, fd_path, fd_set_attr,
Utf8CStrBuf, cstr, errno, fd_path, fd_set_attr,
};
use libc::{EEXIST, O_CLOEXEC, O_CREAT, O_RDONLY, O_TRUNC, O_WRONLY, dirent, mode_t};
use std::fs::File;
@ -247,13 +247,13 @@ impl Directory {
}
pub fn get_attr_at<'a>(&self, name: &'a Utf8CStr) -> OsResult<'a, FileAttr> {
let mut path = cstr_buf::default();
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 = cstr_buf::default();
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,14 +264,14 @@ impl Directory {
name: &'a Utf8CStr,
con: &mut dyn Utf8CStrBuf,
) -> OsResult<'a, ()> {
let mut path = cstr_buf::default();
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 = cstr_buf::default();
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)))
@ -344,7 +344,7 @@ impl Directory {
std::io::copy(&mut src, &mut dest)?;
fd_set_attr(dest.as_raw_fd(), &attr)?;
} else if e.is_symlink() {
let mut target = cstr_buf::default();
let mut target = cstr::buf::default();
e.read_link(&mut target)?;
unsafe {
libc::symlinkat(target.as_ptr(), dir.as_raw_fd(), e.d_name.as_ptr())

View File

@ -1,6 +1,6 @@
use crate::{
Directory, FsPathFollow, LibcReturn, OsError, OsResult, OsResultStatic, Utf8CStr, Utf8CStrBuf,
cstr_buf, errno, error,
cstr, errno, error,
};
use bytemuck::{Pod, bytes_of, bytes_of_mut};
use libc::{
@ -139,7 +139,7 @@ macro_rules! open_fd {
}
pub fn fd_path(fd: RawFd, buf: &mut dyn Utf8CStrBuf) -> OsResult<'static, ()> {
let path = cstr_buf::default()
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))
@ -270,7 +270,7 @@ pub trait FsPath: Deref<Target = Utf8CStr> {
return Ok(());
}
let mut path = cstr_buf::default();
let mut path = cstr::buf::default();
let mut components = self.split('/').filter(|s| !s.is_empty());
loop {
let Some(s) = components.next() else {
@ -396,7 +396,7 @@ pub trait FsPath: Deref<Target = Utf8CStr> {
let mut dest = path.create(O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0o777)?;
std::io::copy(&mut src, &mut dest)?;
} else if attr.is_symlink() {
let mut buf = cstr_buf::default();
let mut buf = cstr::buf::default();
self.read_link(&mut buf)?;
unsafe {
libc::symlink(buf.as_ptr(), path.as_ptr()).check_os_err(

View File

@ -6,7 +6,9 @@ pub use const_format;
pub use libc;
use num_traits::FromPrimitive;
pub use cstr::*;
pub use cstr::{
FsPathFollow, StrErr, Utf8CStr, Utf8CStrBuf, Utf8CStrBufArr, Utf8CStrBufRef, Utf8CString,
};
use cxx_extern::*;
pub use dir::*;
pub use ffi::fork_dont_care;
@ -16,7 +18,7 @@ pub use misc::*;
pub use mount::*;
pub use result::*;
mod cstr;
pub mod cstr;
mod cxx_extern;
mod dir;
mod files;

View File

@ -7,7 +7,7 @@ use num_derive::{FromPrimitive, ToPrimitive};
use num_traits::FromPrimitive;
use crate::ffi::LogLevelCxx;
use crate::{Utf8CStr, cstr_buf};
use crate::{Utf8CStr, cstr};
// Ugly hack to avoid using enum
#[allow(non_snake_case, non_upper_case_globals)]
@ -96,7 +96,7 @@ pub fn log_from_cxx(level: LogLevelCxx, msg: &Utf8CStr) {
pub fn log_with_formatter<F: FnOnce(Formatter) -> fmt::Result>(level: LogLevel, f: F) {
log_with_writer(level, |write| {
let mut buf = cstr_buf::default();
let mut buf = cstr::buf::default();
f(&mut buf).ok();
write(level, &buf);
});

View File

@ -2,7 +2,7 @@
use crate::cxx_extern::readlinkat;
use crate::{
BorrowedDirectory, CxxResultExt, FsPath, LibcReturn, Utf8CStr, cstr_buf, slice_from_ptr,
BorrowedDirectory, CxxResultExt, FsPath, LibcReturn, Utf8CStr, cstr, slice_from_ptr,
slice_from_ptr_mut,
};
use libc::{
@ -30,7 +30,7 @@ unsafe extern "C" fn xrealpath(path: *const c_char, buf: *mut u8, bufsz: usize)
unsafe {
match Utf8CStr::from_ptr(path) {
Ok(path) => {
let mut buf = cstr_buf::wrap_ptr(buf, bufsz);
let mut buf = cstr::buf::wrap_ptr(buf, bufsz);
path.realpath(&mut buf)
.log_cxx()
.map_or(-1, |_| buf.len() as isize)
@ -45,7 +45,7 @@ unsafe extern "C" fn xreadlink(path: *const c_char, buf: *mut u8, bufsz: usize)
unsafe {
match Utf8CStr::from_ptr(path) {
Ok(path) => {
let mut buf = cstr_buf::wrap_ptr(buf, bufsz);
let mut buf = cstr::buf::wrap_ptr(buf, bufsz);
path.read_link(&mut buf)
.log_cxx()
.map_or(-1, |_| buf.len() as isize)

View File

@ -21,7 +21,7 @@ use base::libc::{
};
use base::{
BytesExt, EarlyExitExt, FsPath, LoggedResult, MappedFile, ResultExt, Utf8CStr, Utf8CStrBuf,
WriteExt, cstr_buf, log_err, map_args,
WriteExt, cstr, log_err, map_args,
};
use crate::check_env;
@ -343,7 +343,7 @@ impl Cpio {
let out = Utf8CStr::from_string(out);
let mut buf = cstr_buf::default();
let mut buf = cstr::buf::default();
// Make sure its parent directories exist
if out.parent(&mut buf) {

View File

@ -11,8 +11,8 @@ use crate::package::ManagerInfo;
use crate::su::SuInfo;
use base::libc::{O_CLOEXEC, O_RDONLY};
use base::{
AtomicArc, BufReadExt, FsPath, FsPathBuilder, ResultExt, Utf8CStr, cstr, cstr_buf, error, info,
libc, open_fd,
AtomicArc, BufReadExt, FsPath, FsPathBuilder, ResultExt, Utf8CStr, cstr, error, info, libc,
open_fd,
};
use std::fs::File;
use std::io::BufReader;
@ -229,7 +229,7 @@ pub fn daemon_entry() {
|| get_prop(cstr!("ro.product.device"), false).contains("vsoc");
// Load config status
let path = cstr_buf::new::<64>()
let path = cstr::buf::new::<64>()
.join_path(get_magisk_tmp())
.join_path(MAIN_CONFIG);
let mut is_recovery = false;

View File

@ -7,7 +7,7 @@ use base::libc::{
};
use base::{
FsPathBuilder, LOGGER, LogLevel, Logger, ReadExt, Utf8CStr, Utf8CStrBuf, WriteExt,
const_format::concatcp, cstr_buf, libc, raw_cstr,
const_format::concatcp, cstr, libc, raw_cstr,
};
use bytemuck::{Pod, Zeroable, bytes_of, write_zeroes};
use num_derive::{FromPrimitive, ToPrimitive};
@ -130,7 +130,7 @@ fn write_log_to_pipe(mut logd: &File, prio: i32, msg: &Utf8CStr) -> io::Result<u
let io2 = IoSlice::new(msg);
let result = logd.write_vectored(&[io1, io2]);
if let Err(ref e) = result {
let mut buf = cstr_buf::default();
let mut buf = cstr::buf::default();
buf.write_fmt(format_args!("Cannot write_log_to_pipe: {}", e))
.ok();
android_log_write(LogLevel::Error, &buf);
@ -180,7 +180,7 @@ pub fn zygisk_get_logd() -> i32 {
let mut fd = ZYGISK_LOGD.load(Ordering::Relaxed);
if fd < 0 {
android_logging();
let path = cstr_buf::default()
let path = cstr::buf::default()
.join_path(get_magisk_tmp())
.join_path(LOG_PIPE);
// Open as RW as sometimes it may block
@ -268,7 +268,7 @@ extern "C" fn logfile_writer(arg: *mut c_void) -> *mut c_void {
let mut meta = LogMeta::zeroed();
let mut msg_buf = [0u8; MAX_MSG_LEN];
let mut aux = cstr_buf::new::<64>();
let mut aux = cstr::buf::new::<64>();
loop {
// Read request
@ -357,7 +357,7 @@ pub fn setup_logfile() {
}
pub fn start_log_daemon() {
let path = cstr_buf::default()
let path = cstr::buf::default()
.join_path(get_magisk_tmp())
.join_path(LOG_PIPE);

View File

@ -8,7 +8,7 @@ use num_traits::AsPrimitive;
use base::libc::{c_uint, dev_t};
use base::{
FsPath, FsPathBuilder, FsPathMnt, LibcReturn, LoggedResult, MountInfo, ResultExt, Utf8CStr,
Utf8CStrBuf, cstr, cstr_buf, debug, info, libc, parse_mount_info, warn,
Utf8CStrBuf, cstr, debug, info, libc, parse_mount_info, warn,
};
use crate::consts::{MODULEMNT, MODULEROOT, PREINITDEV, PREINITMIRR, WORKERDIR};
@ -21,7 +21,7 @@ pub fn setup_mounts() {
let magisk_tmp = get_magisk_tmp();
// Mount preinit directory
let dev_path = cstr_buf::new::<64>()
let dev_path = cstr::buf::new::<64>()
.join_path(magisk_tmp)
.join_path(PREINITDEV);
let mut linked = false;
@ -33,7 +33,7 @@ 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 = cstr_buf::default()
let mnt_path = cstr::buf::default()
.join_path(magisk_tmp)
.join_path(PREINITMIRR);
for info in parse_mount_info("self") {
@ -68,7 +68,7 @@ pub fn setup_mounts() {
}
// Bind remount module root to clear nosuid
let module_mnt = cstr_buf::default()
let module_mnt = cstr::buf::default()
.join_path(magisk_tmp)
.join_path(MODULEMNT);
let _: LoggedResult<()> = try {
@ -81,7 +81,7 @@ pub fn setup_mounts() {
pub fn clean_mounts() {
let magisk_tmp = get_magisk_tmp();
let mut buf = cstr_buf::default();
let mut buf = cstr::buf::default();
let module_mnt = buf.append_path(magisk_tmp).append_path(MODULEMNT);
module_mnt.unmount().log_ok();
@ -186,7 +186,7 @@ pub fn find_preinit_device() -> String {
&& let Ok(tmp) = std::env::var("MAGISKTMP")
&& !tmp.is_empty()
{
let mut buf = cstr_buf::default();
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 {

View File

@ -5,7 +5,7 @@ use base::WalkResult::{Abort, Continue, Skip};
use base::libc::{O_CLOEXEC, O_CREAT, O_RDONLY, O_TRUNC, O_WRONLY};
use base::{
BufReadExt, Directory, FsPath, FsPathBuilder, LoggedResult, ReadExt, ResultExt, Utf8CStrBuf,
cstr, cstr_buf, error, fd_get_attr, open_fd, warn,
cstr, error, fd_get_attr, open_fd, warn,
};
use bit_set::BitSet;
use cxx::CxxString;
@ -239,7 +239,7 @@ impl TrackedFile {
impl ManagerInfo {
fn check_dyn(&mut self, daemon: &MagiskD, user: i32, pkg: &str) -> Status {
let apk = cstr_buf::default()
let apk = cstr::buf::default()
.join_path(daemon.app_data_dir())
.join_path_fmt(user)
.join_path(pkg)
@ -273,7 +273,7 @@ impl ManagerInfo {
}
fn check_stub(&mut self, user: i32, pkg: &str) -> Status {
let mut apk = cstr_buf::default();
let mut apk = cstr::buf::default();
if find_apk_path(pkg, &mut apk).is_err() {
return Status::NotInstalled;
}
@ -297,7 +297,7 @@ impl ManagerInfo {
}
fn check_orig(&mut self, user: i32) -> Status {
let mut apk = cstr_buf::default();
let mut apk = cstr::buf::default();
if find_apk_path(APP_PACKAGE_NAME, &mut apk).is_err() {
return Status::NotInstalled;
}
@ -441,7 +441,7 @@ impl ManagerInfo {
impl MagiskD {
fn get_package_uid(&self, user: i32, pkg: &str) -> i32 {
let path = cstr_buf::default()
let path = cstr::buf::default()
.join_path(self.app_data_dir())
.join_path_fmt(user)
.join_path(pkg);
@ -453,7 +453,7 @@ impl MagiskD {
pub fn preserve_stub_apk(&self) {
let mut info = self.manager_info.lock().unwrap();
let apk = cstr_buf::default()
let apk = cstr::buf::default()
.join_path(get_magisk_tmp())
.join_path("stub.apk");

View File

@ -17,7 +17,7 @@ use base::const_format::concatcp;
use base::libc::{O_CLOEXEC, O_RDONLY};
use base::{
Directory, FsPath, FsPathBuilder, LibcReturn, LoggedResult, MappedFile, SilentResultExt,
Utf8CStr, Utf8CStrBuf, WalkResult, clone_attr, cstr, cstr_buf, debug, libc::mkstemp,
Utf8CStr, Utf8CStrBuf, WalkResult, clone_attr, cstr, debug, libc::mkstemp,
};
const PERSIST_PROP_DIR: &str = "/data/property";
@ -68,7 +68,7 @@ fn check_proto() -> bool {
}
fn file_get_prop(name: &Utf8CStr) -> LoggedResult<String> {
let path = cstr_buf::default()
let path = cstr::buf::default()
.join_path(PERSIST_PROP_DIR)
.join_path(name);
let mut file = path.open(O_RDONLY | O_CLOEXEC).silent()?;
@ -79,11 +79,11 @@ fn file_get_prop(name: &Utf8CStr) -> LoggedResult<String> {
}
fn file_set_prop(name: &Utf8CStr, value: Option<&Utf8CStr>) -> LoggedResult<()> {
let path = cstr_buf::default()
let path = cstr::buf::default()
.join_path(PERSIST_PROP_DIR)
.join_path(name);
if let Some(value) = value {
let mut tmp = cstr_buf::default()
let mut tmp = cstr::buf::default()
.join_path(PERSIST_PROP_DIR)
.join_path("prop.XXXXXX");
{
@ -115,7 +115,7 @@ fn proto_read_props() -> LoggedResult<PersistentProperties> {
}
fn proto_write_props(props: &PersistentProperties) -> LoggedResult<()> {
let mut tmp = cstr_buf::default().join_path(concatcp!(PERSIST_PROP, ".XXXXXX"));
let mut tmp = cstr::buf::default().join_path(concatcp!(PERSIST_PROP, ".XXXXXX"));
{
let f = unsafe {
mkstemp(tmp.as_mut_ptr())

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, FsPathBuilder, LoggedError, LoggedResult, ResultExt, WriteExt, cstr, cstr_buf,
error, fork_dont_care, libc, open_fd, raw_cstr, warn,
Directory, FsPathBuilder, LoggedError, LoggedResult, ResultExt, WriteExt, cstr, error,
fork_dont_care, libc, open_fd, raw_cstr, warn,
};
use std::fmt::Write;
use std::os::fd::{AsRawFd, FromRawFd, RawFd};
@ -37,11 +37,11 @@ fn exec_zygiskd(is_64_bit: bool, remote: UnixStream) {
#[cfg(target_pointer_width = "32")]
let magisk = "magisk";
let exe = cstr_buf::new::<64>()
let exe = cstr::buf::new::<64>()
.join_path(get_magisk_tmp())
.join_path(magisk);
let mut fd_str = cstr_buf::new::<16>();
let mut fd_str = cstr::buf::new::<16>();
write!(fd_str, "{}", remote.as_raw_fd()).ok();
unsafe {
libc::execl(
@ -185,7 +185,7 @@ 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 = cstr_buf::default()
let path = cstr::buf::default()
.join_path(MODULEROOT)
.join_path(&module_list[id as usize].name)
.join_path("zygisk");
@ -204,7 +204,7 @@ 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 = cstr_buf::default()
let dir = cstr::buf::default()
.join_path(MODULEROOT)
.join_path(&module.name);
let fd = open_fd!(&dir, O_RDONLY | O_CLOEXEC)?;

View File

@ -2,7 +2,7 @@ use crate::ffi::MagiskInit;
use base::libc::{TMPFS_MAGIC, statfs};
use base::{
Directory, FsPath, FsPathBuilder, FsPathMnt, LibcReturn, LoggedResult, ResultExt, Utf8CStr,
cstr, cstr_buf, debug, libc,
cstr, debug, libc,
libc::{chdir, chroot, execve, exit, mount},
parse_mount_info, raw_cstr,
};
@ -38,7 +38,7 @@ pub(crate) fn switch_root(path: &Utf8CStr) {
let mut target = info.target.clone();
let target = Utf8CStr::from_string(&mut target);
let new_path = cstr_buf::default()
let new_path = cstr::buf::default()
.join_path(path)
.join_path(info.target.trim_start_matches('/'));
new_path.mkdirs(0o755).ok();

View File

@ -3,7 +3,7 @@ use crate::ffi::MagiskInit;
use base::libc::{O_CREAT, O_RDONLY, O_WRONLY};
use base::{
BufReadExt, Directory, FsPath, FsPathBuilder, FsPathMnt, LoggedResult, ResultExt, Utf8CStr,
Utf8CString, clone_attr, cstr, cstr_buf, debug,
Utf8CString, clone_attr, cstr, debug,
};
use std::io::BufReader;
use std::{
@ -66,14 +66,14 @@ impl MagiskInit {
mount_list: &mut String,
) -> LoggedResult<()> {
let mut dir = Directory::open(src_dir)?;
let mut con = cstr_buf::default();
let mut con = cstr::buf::default();
loop {
match &dir.read()? {
None => return Ok(()),
Some(e) => {
let name = e.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);
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