mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-04-21 14:31:27 +00:00
Create Utf8CStrBuffer type
This commit is contained in:
parent
55b036c071
commit
084d89fcce
@ -41,7 +41,7 @@ pub mod cstr_buf {
|
|||||||
use super::{Utf8CStrBufArr, Utf8CStrBufRef, Utf8CString};
|
use super::{Utf8CStrBufArr, Utf8CStrBufRef, Utf8CString};
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn with_capacity(capacity: usize) -> Utf8CString {
|
pub fn dynamic(capacity: usize) -> Utf8CString {
|
||||||
Utf8CString::with_capacity(capacity)
|
Utf8CString::with_capacity(capacity)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,6 +66,63 @@ pub mod cstr_buf {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Union type for storing a Utf8CStrBuf value
|
||||||
|
|
||||||
|
pub enum Utf8CStrBuffer<'a, const N: usize> {
|
||||||
|
Array(Utf8CStrBufArr<N>),
|
||||||
|
Reference(Utf8CStrBufRef<'a>),
|
||||||
|
Dynamic(Utf8CString),
|
||||||
|
Wrap(&'a mut dyn Utf8CStrBuf),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, const N: usize> Deref for Utf8CStrBuffer<'a, N> {
|
||||||
|
type Target = dyn Utf8CStrBuf + 'a;
|
||||||
|
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
match self {
|
||||||
|
Utf8CStrBuffer::Array(s) => s,
|
||||||
|
Utf8CStrBuffer::Reference(s) => s,
|
||||||
|
Utf8CStrBuffer::Dynamic(s) => s,
|
||||||
|
Utf8CStrBuffer::Wrap(s) => *s,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<const N: usize> DerefMut for Utf8CStrBuffer<'_, N> {
|
||||||
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||||
|
match self {
|
||||||
|
Utf8CStrBuffer::Array(s) => s,
|
||||||
|
Utf8CStrBuffer::Reference(s) => s,
|
||||||
|
Utf8CStrBuffer::Dynamic(s) => s,
|
||||||
|
Utf8CStrBuffer::Wrap(s) => *s,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Utf8CString> for Utf8CStrBuffer<'static, 0> {
|
||||||
|
fn from(value: Utf8CString) -> Self {
|
||||||
|
Utf8CStrBuffer::Dynamic(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<const N: usize> From<Utf8CStrBufArr<N>> for Utf8CStrBuffer<'static, N> {
|
||||||
|
fn from(value: Utf8CStrBufArr<N>) -> Self {
|
||||||
|
Utf8CStrBuffer::Array(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> From<&'a mut dyn Utf8CStrBuf> for Utf8CStrBuffer<'a, 0> {
|
||||||
|
fn from(value: &'a mut dyn Utf8CStrBuf) -> Self {
|
||||||
|
Utf8CStrBuffer::Wrap(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Utf8CStrBuffer<'static, 4096> {
|
||||||
|
fn default() -> Self {
|
||||||
|
Utf8CStrBuffer::Array(cstr_buf::default())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Trait definitions
|
// Trait definitions
|
||||||
|
|
||||||
pub trait Utf8CStrBuf:
|
pub trait Utf8CStrBuf:
|
||||||
@ -125,7 +182,7 @@ impl<T: Utf8CStrBufWithSlice> AsUtf8CStr for T {
|
|||||||
|
|
||||||
// Implementation for Utf8CString
|
// Implementation for Utf8CString
|
||||||
|
|
||||||
fn utf8_cstr_buf_append(buf: &mut dyn Utf8CStrBufWithSlice, s: &[u8]) -> usize {
|
fn utf8_cstr_append(buf: &mut dyn Utf8CStrBufWithSlice, s: &[u8]) -> usize {
|
||||||
let mut used = buf.len();
|
let mut used = buf.len();
|
||||||
if used >= buf.capacity() - 1 {
|
if used >= buf.capacity() - 1 {
|
||||||
// Truncate
|
// Truncate
|
||||||
@ -480,7 +537,7 @@ impl FsPath {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub const fn from_utfcstr(value: &Utf8CStr) -> &FsPath {
|
pub const fn __from_utfcstr(value: &Utf8CStr) -> &FsPath {
|
||||||
unsafe { mem::transmute(value) }
|
unsafe { mem::transmute(value) }
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -525,52 +582,33 @@ impl DerefMut for FsPathFollow {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
enum Utf8CStrBufOwned<const N: usize> {
|
pub struct FsPathBuf<'a, const N: usize>(pub Utf8CStrBuffer<'a, N>);
|
||||||
Dynamic(Utf8CString),
|
|
||||||
Fixed(Utf8CStrBufArr<N>),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<const N: usize> Deref for Utf8CStrBufOwned<N> {
|
impl From<Utf8CString> for FsPathBuf<'static, 0> {
|
||||||
type Target = dyn Utf8CStrBuf;
|
fn from(value: Utf8CString) -> Self {
|
||||||
|
FsPathBuf(From::from(value))
|
||||||
fn deref(&self) -> &Self::Target {
|
|
||||||
match self {
|
|
||||||
Utf8CStrBufOwned::Dynamic(s) => s,
|
|
||||||
Utf8CStrBufOwned::Fixed(arr) => arr,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<const N: usize> DerefMut for Utf8CStrBufOwned<N> {
|
impl<const N: usize> From<Utf8CStrBufArr<N>> for FsPathBuf<'static, N> {
|
||||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
fn from(value: Utf8CStrBufArr<N>) -> Self {
|
||||||
match self {
|
FsPathBuf(From::from(value))
|
||||||
Utf8CStrBufOwned::Dynamic(s) => s,
|
|
||||||
Utf8CStrBufOwned::Fixed(arr) => arr,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct FsPathBuf<const N: usize>(Utf8CStrBufOwned<N>);
|
|
||||||
|
|
||||||
impl FsPathBuf<0> {
|
|
||||||
pub fn new_dynamic(capacity: usize) -> Self {
|
|
||||||
FsPathBuf(Utf8CStrBufOwned::Dynamic(Utf8CString::with_capacity(
|
|
||||||
capacity,
|
|
||||||
)))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for FsPathBuf<4096> {
|
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 {
|
fn default() -> Self {
|
||||||
FsPathBuf(Utf8CStrBufOwned::Fixed(cstr_buf::default()))
|
FsPathBuf(Default::default())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<const N: usize> FsPathBuf<N> {
|
impl<const N: usize> FsPathBuf<'_, N> {
|
||||||
pub fn new() -> Self {
|
|
||||||
FsPathBuf(Utf8CStrBufOwned::Fixed(cstr_buf::new::<N>()))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn clear(&mut self) {
|
pub fn clear(&mut self) {
|
||||||
self.0.clear();
|
self.0.clear();
|
||||||
}
|
}
|
||||||
@ -595,7 +633,7 @@ impl<const N: usize> FsPathBuf<N> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<const N: usize> Deref for FsPathBuf<N> {
|
impl<const N: usize> Deref for FsPathBuf<'_, N> {
|
||||||
type Target = FsPath;
|
type Target = FsPath;
|
||||||
|
|
||||||
fn deref(&self) -> &FsPath {
|
fn deref(&self) -> &FsPath {
|
||||||
@ -603,7 +641,7 @@ impl<const N: usize> Deref for FsPathBuf<N> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<const N: usize> DerefMut for FsPathBuf<N> {
|
impl<const N: usize> DerefMut for FsPathBuf<'_, N> {
|
||||||
fn deref_mut(&mut self) -> &mut FsPath {
|
fn deref_mut(&mut self) -> &mut FsPath {
|
||||||
FsPath::from_mut(self.0.deref_mut())
|
FsPath::from_mut(self.0.deref_mut())
|
||||||
}
|
}
|
||||||
@ -692,7 +730,7 @@ impl_str!(
|
|||||||
(Utf8CStr,)
|
(Utf8CStr,)
|
||||||
(FsPath,)
|
(FsPath,)
|
||||||
(FsPathFollow,)
|
(FsPathFollow,)
|
||||||
(FsPathBuf<N>, const N: usize)
|
(FsPathBuf<'_, N>, const N: usize)
|
||||||
(Utf8CStrBufRef<'_>,)
|
(Utf8CStrBufRef<'_>,)
|
||||||
(Utf8CStrBufArr<N>, const N: usize)
|
(Utf8CStrBufArr<N>, const N: usize)
|
||||||
(Utf8CString,)
|
(Utf8CString,)
|
||||||
@ -749,7 +787,7 @@ macro_rules! impl_str_buf_with_slice {
|
|||||||
}
|
}
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn push_str(&mut self, s: &str) -> usize {
|
fn push_str(&mut self, s: &str) -> usize {
|
||||||
utf8_cstr_buf_append(self, s.as_bytes())
|
utf8_cstr_append(self, s.as_bytes())
|
||||||
}
|
}
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn push_lossy(&mut self, s: &[u8]) -> usize {
|
fn push_lossy(&mut self, s: &[u8]) -> usize {
|
||||||
@ -791,5 +829,5 @@ macro_rules! raw_cstr {
|
|||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! path {
|
macro_rules! path {
|
||||||
($str:expr) => {{ $crate::FsPath::from_utfcstr($crate::cstr!($str)) }};
|
($str:expr) => {{ $crate::FsPath::__from_utfcstr($crate::cstr!($str)) }};
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use crate::cxx_extern::readlinkat;
|
use crate::cxx_extern::readlinkat;
|
||||||
use crate::{
|
use crate::{
|
||||||
FileAttr, FsPath, LibcReturn, OsError, OsResult, OsResultStatic, Utf8CStr, Utf8CStrBuf, cstr,
|
FileAttr, FsPath, FsPathBuf, LibcReturn, OsError, OsResult, OsResultStatic, Utf8CStr,
|
||||||
cstr_buf, errno, fd_path, fd_set_attr,
|
Utf8CStrBuf, cstr, cstr_buf, errno, fd_path, fd_set_attr,
|
||||||
};
|
};
|
||||||
use libc::{EEXIST, O_CLOEXEC, O_CREAT, O_RDONLY, O_TRUNC, O_WRONLY, dirent, mode_t};
|
use libc::{EEXIST, O_CLOEXEC, O_CREAT, O_RDONLY, O_TRUNC, O_WRONLY, dirent, mode_t};
|
||||||
use std::ffi::CStr;
|
use std::ffi::CStr;
|
||||||
@ -39,8 +39,12 @@ impl DirEntry<'_> {
|
|||||||
|
|
||||||
pub fn path(&self, buf: &mut dyn Utf8CStrBuf) -> OsResult<'static, ()> {
|
pub fn path(&self, buf: &mut dyn Utf8CStrBuf) -> OsResult<'static, ()> {
|
||||||
self.dir.path(buf)?;
|
self.dir.path(buf)?;
|
||||||
|
if let Ok(s) = self.name().to_str() {
|
||||||
|
FsPathBuf::from(buf).join(s);
|
||||||
|
} else {
|
||||||
buf.push_str("/");
|
buf.push_str("/");
|
||||||
buf.push_lossy(self.name().to_bytes());
|
buf.push_lossy(self.name().to_bytes());
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,7 +11,8 @@ use crate::package::ManagerInfo;
|
|||||||
use crate::su::SuInfo;
|
use crate::su::SuInfo;
|
||||||
use base::libc::{O_CLOEXEC, O_RDONLY};
|
use base::libc::{O_CLOEXEC, O_RDONLY};
|
||||||
use base::{
|
use base::{
|
||||||
AtomicArc, BufReadExt, FsPathBuf, ResultExt, Utf8CStr, cstr, error, info, libc, open_fd, path,
|
AtomicArc, BufReadExt, FsPathBuf, ResultExt, Utf8CStr, cstr, cstr_buf, error, info, libc,
|
||||||
|
open_fd, path,
|
||||||
};
|
};
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::BufReader;
|
use std::io::BufReader;
|
||||||
@ -228,7 +229,7 @@ pub fn daemon_entry() {
|
|||||||
|| get_prop(cstr!("ro.product.device"), false).contains("vsoc");
|
|| get_prop(cstr!("ro.product.device"), false).contains("vsoc");
|
||||||
|
|
||||||
// Load config status
|
// Load config status
|
||||||
let path = FsPathBuf::<64>::new()
|
let path = FsPathBuf::from(cstr_buf::new::<64>())
|
||||||
.join(get_magisk_tmp())
|
.join(get_magisk_tmp())
|
||||||
.join(MAIN_CONFIG);
|
.join(MAIN_CONFIG);
|
||||||
let mut is_recovery = false;
|
let mut is_recovery = false;
|
||||||
|
@ -7,8 +7,8 @@ use num_traits::AsPrimitive;
|
|||||||
|
|
||||||
use base::libc::{c_uint, dev_t};
|
use base::libc::{c_uint, dev_t};
|
||||||
use base::{
|
use base::{
|
||||||
FsPath, FsPathBuf, LibcReturn, LoggedResult, MountInfo, ResultExt, Utf8CStr, cstr, debug, info,
|
FsPath, FsPathBuf, LibcReturn, LoggedResult, MountInfo, ResultExt, Utf8CStr, cstr, cstr_buf,
|
||||||
libc, parse_mount_info, path, warn,
|
debug, info, libc, parse_mount_info, path, warn,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::consts::{MODULEMNT, MODULEROOT, PREINITDEV, PREINITMIRR, WORKERDIR};
|
use crate::consts::{MODULEMNT, MODULEROOT, PREINITDEV, PREINITMIRR, WORKERDIR};
|
||||||
@ -21,7 +21,9 @@ pub fn setup_mounts() {
|
|||||||
let magisk_tmp = get_magisk_tmp();
|
let magisk_tmp = get_magisk_tmp();
|
||||||
|
|
||||||
// Mount preinit directory
|
// Mount preinit directory
|
||||||
let dev_path = FsPathBuf::<64>::new().join(magisk_tmp).join(PREINITDEV);
|
let dev_path = FsPathBuf::from(cstr_buf::new::<64>())
|
||||||
|
.join(magisk_tmp)
|
||||||
|
.join(PREINITDEV);
|
||||||
let mut linked = false;
|
let mut linked = false;
|
||||||
if let Ok(attr) = dev_path.get_attr() {
|
if let Ok(attr) = dev_path.get_attr() {
|
||||||
if attr.st.st_mode & libc::S_IFMT as c_uint == libc::S_IFBLK.as_() {
|
if attr.st.st_mode & libc::S_IFMT as c_uint == libc::S_IFBLK.as_() {
|
||||||
|
@ -37,7 +37,9 @@ fn exec_zygiskd(is_64_bit: bool, remote: UnixStream) {
|
|||||||
#[cfg(target_pointer_width = "32")]
|
#[cfg(target_pointer_width = "32")]
|
||||||
let magisk = "magisk";
|
let magisk = "magisk";
|
||||||
|
|
||||||
let exe = FsPathBuf::<64>::new().join(get_magisk_tmp()).join(magisk);
|
let exe = FsPathBuf::from(cstr_buf::new::<64>())
|
||||||
|
.join(get_magisk_tmp())
|
||||||
|
.join(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();
|
write!(fd_str, "{}", remote.as_raw_fd()).ok();
|
||||||
|
@ -2,8 +2,8 @@ use crate::consts::{ROOTMNT, ROOTOVL};
|
|||||||
use crate::ffi::MagiskInit;
|
use crate::ffi::MagiskInit;
|
||||||
use base::libc::{O_CREAT, O_RDONLY, O_WRONLY};
|
use base::libc::{O_CREAT, O_RDONLY, O_WRONLY};
|
||||||
use base::{
|
use base::{
|
||||||
BufReadExt, Directory, FsPath, FsPathBuf, LoggedResult, ResultExt, Utf8CStr, Utf8CString,
|
BufReadExt, Directory, FsPath, FsPathBuf, LoggedResult, ResultExt, Utf8CStr,
|
||||||
clone_attr, cstr, cstr_buf, debug, path,
|
Utf8CString, clone_attr, cstr, cstr_buf, debug, path,
|
||||||
};
|
};
|
||||||
use std::io::BufReader;
|
use std::io::BufReader;
|
||||||
use std::{
|
use std::{
|
||||||
@ -72,8 +72,12 @@ impl MagiskInit {
|
|||||||
None => return Ok(()),
|
None => return Ok(()),
|
||||||
Some(e) => {
|
Some(e) => {
|
||||||
let name = e.name().to_str()?;
|
let name = e.name().to_str()?;
|
||||||
let src = FsPathBuf::new_dynamic(256).join(src_dir).join(name);
|
let src = FsPathBuf::from(cstr_buf::dynamic(256))
|
||||||
let dest = FsPathBuf::new_dynamic(256).join(dest_dir).join(name);
|
.join(src_dir)
|
||||||
|
.join(name);
|
||||||
|
let dest = FsPathBuf::from(cstr_buf::dynamic(256))
|
||||||
|
.join(dest_dir)
|
||||||
|
.join(name);
|
||||||
if dest.exists() {
|
if dest.exists() {
|
||||||
if e.is_dir() {
|
if e.is_dir() {
|
||||||
// Recursive
|
// Recursive
|
||||||
|
Loading…
x
Reference in New Issue
Block a user