mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-12-11 22:22:37 +00:00
Handle cpio commands properly
This commit is contained in:
@@ -3,9 +3,11 @@ use std::ffi::{CStr, FromBytesWithNulError, OsStr};
|
||||
use std::fmt::{Arguments, Debug, Display, Formatter};
|
||||
use std::ops::Deref;
|
||||
use std::path::Path;
|
||||
use std::process::exit;
|
||||
use std::str::Utf8Error;
|
||||
use std::{fmt, io, mem, slice, str};
|
||||
|
||||
use argh::EarlyExit;
|
||||
use libc::c_char;
|
||||
use thiserror::Error;
|
||||
|
||||
@@ -297,18 +299,18 @@ where
|
||||
fn as_raw_bytes(&self) -> &[u8] {
|
||||
unsafe {
|
||||
let self_ptr = self as *const Self as *const u8;
|
||||
slice::from_raw_parts(self_ptr, std::mem::size_of::<Self>())
|
||||
slice::from_raw_parts(self_ptr, mem::size_of::<Self>())
|
||||
}
|
||||
}
|
||||
fn as_raw_bytes_mut(&mut self) -> &mut [u8] {
|
||||
unsafe {
|
||||
let self_ptr = self as *mut Self as *mut u8;
|
||||
slice::from_raw_parts_mut(self_ptr, std::mem::size_of::<Self>())
|
||||
slice::from_raw_parts_mut(self_ptr, mem::size_of::<Self>())
|
||||
}
|
||||
}
|
||||
|
||||
fn bytes_size(&self) -> usize {
|
||||
std::mem::size_of::<Self>()
|
||||
mem::size_of::<Self>()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -369,3 +371,34 @@ impl<T: AsMut<[u8]>> MutBytesExt for T {
|
||||
ffi::mut_u8_patch(self.as_mut(), from, to)
|
||||
}
|
||||
}
|
||||
|
||||
// SAFETY: libc guarantees argc and argv is properly setup
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
pub fn map_args<'a>(argc: i32, argv: *const *const c_char) -> Result<Vec<&'a Utf8CStr>, StrErr> {
|
||||
unsafe { slice::from_raw_parts(argv, argc as usize) }
|
||||
.iter()
|
||||
.map(|s| unsafe { Utf8CStr::from_ptr(*s) })
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub trait EarlyExitExt<T> {
|
||||
fn early_exit(self) -> T;
|
||||
}
|
||||
|
||||
impl<T> EarlyExitExt<T> for Result<T, EarlyExit> {
|
||||
fn early_exit(self) -> T {
|
||||
match self {
|
||||
Ok(t) => t,
|
||||
Err(EarlyExit { output, status }) => match status {
|
||||
Ok(_) => {
|
||||
eprintln!("{}", output);
|
||||
exit(0)
|
||||
}
|
||||
Err(_) => {
|
||||
eprintln!("{}", output);
|
||||
exit(1)
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user