Add more to the Utf8CStr family

Better C strings with path operations
This commit is contained in:
topjohnwu
2023-09-12 17:35:01 -07:00
parent 4eaf701cb7
commit 89aee6ffa7
13 changed files with 783 additions and 437 deletions
+9 -8
View File
@@ -3,7 +3,7 @@ use std::fs::File;
use std::io;
use std::sync::{Mutex, OnceLock};
use base::{copy_str, cstr, Directory, ResultExt, Utf8CStr, WalkResult};
use base::{cstr, Directory, ResultExt, Utf8CStr, Utf8CStrBuf, Utf8CStrSlice, WalkResult};
use crate::logging::{magisk_logging, zygisk_logging};
@@ -36,8 +36,7 @@ impl MagiskD {}
pub fn find_apk_path(pkg: &[u8], data: &mut [u8]) -> usize {
use WalkResult::*;
fn inner(pkg: &[u8], data: &mut [u8]) -> io::Result<usize> {
let mut len = 0_usize;
fn inner(pkg: &[u8], buf: &mut dyn Utf8CStrBuf) -> io::Result<usize> {
let pkg = match Utf8CStr::from_bytes(pkg) {
Ok(pkg) => pkg,
Err(e) => return Err(io::Error::new(io::ErrorKind::Other, e)),
@@ -49,7 +48,7 @@ pub fn find_apk_path(pkg: &[u8], data: &mut [u8]) -> usize {
let d_name = e.d_name().to_bytes();
if d_name.starts_with(pkg.as_bytes()) && d_name[pkg.len()] == b'-' {
// Found the APK path, we can abort now
len = e.path(data)?;
e.path(buf)?;
return Ok(Abort);
}
if d_name.starts_with(b"~~") {
@@ -57,10 +56,12 @@ pub fn find_apk_path(pkg: &[u8], data: &mut [u8]) -> usize {
}
Ok(Skip)
})?;
if len > 0 {
len += copy_str(&mut data[len..], "/base.apk");
if !buf.is_empty() {
buf.append("/base.apk");
}
Ok(len)
Ok(buf.len())
}
inner(pkg, data).log().unwrap_or(0)
inner(pkg, &mut Utf8CStrSlice::from(data))
.log()
.unwrap_or(0)
}