Refine cpio and dtb subcommands

This commit is contained in:
LoveSy 2025-02-09 18:11:07 +08:00 committed by John Wu
parent f4204c3509
commit 3c068a017e
3 changed files with 90 additions and 110 deletions

View File

@ -1,5 +1,5 @@
use crate::cpio::{cpio_commands, print_cpio_usage}; use crate::cpio::{cpio_commands, print_cpio_usage};
use crate::dtb::{dtb_commands, print_dtb_usage}; use crate::dtb::{dtb_commands, print_dtb_usage, DtbAction};
use crate::ffi::{ use crate::ffi::{
cleanup, compress, decompress_raw, formats, repack, sign, split_image_dtb, unpack, verify cleanup, compress, decompress_raw, formats, repack, sign, split_image_dtb, unpack, verify
}; };
@ -100,6 +100,8 @@ struct HexPatch {
#[derive(FromArgs)] #[derive(FromArgs)]
#[argh(subcommand, name = "cpio")] #[argh(subcommand, name = "cpio")]
struct Cpio { struct Cpio {
#[argh(positional)]
file: String,
#[argh(positional)] #[argh(positional)]
cmds: Vec<String>, cmds: Vec<String>,
} }
@ -108,7 +110,9 @@ struct Cpio {
#[argh(subcommand, name = "dtb")] #[argh(subcommand, name = "dtb")]
struct Dtb { struct Dtb {
#[argh(positional)] #[argh(positional)]
cmds: Vec<String>, file: String,
#[argh(subcommand)]
action: DtbAction,
} }
#[derive(FromArgs)] #[derive(FromArgs)]
@ -353,15 +357,27 @@ pub unsafe extern "C" fn main(
Err(log_err!("Failed to patch"))?; Err(log_err!("Failed to patch"))?;
} }
} }
Action::Cpio(Cpio { ref cmds }) => { Action::Cpio(Cpio {
return if cpio_commands(&cmds.iter().map(|x| x.as_str()).collect::<Vec<_>>()) { ref mut file,
ref mut cmds,
}) => {
return if cpio_commands(file, cmds)
.log_with_msg(|w| w.write_str("Failed to process cpio"))
.is_ok()
{
0 0
} else { } else {
1 1
} }
} }
Action::Dtb(Dtb { ref cmds }) => { Action::Dtb(Dtb {
return if dtb_commands(&cmds.iter().map(|x| x.as_str()).collect::<Vec<_>>()) { ref mut file,
ref action,
}) => {
return if dtb_commands(file, action)
.log_with_msg(|w| w.write_str("Failed to process dtb"))
.unwrap_or(false)
{
0 0
} else { } else {
1 1

View File

@ -28,14 +28,6 @@ use crate::check_env;
use crate::ffi::{unxz, xz}; use crate::ffi::{unxz, xz};
use crate::patch::{patch_encryption, patch_verity}; use crate::patch::{patch_encryption, patch_verity};
#[derive(FromArgs)]
struct CpioCli {
#[argh(positional)]
file: String,
#[argh(positional)]
commands: Vec<String>,
}
#[derive(FromArgs)] #[derive(FromArgs)]
struct CpioCommand { struct CpioCommand {
#[argh(subcommand)] #[argh(subcommand)]
@ -753,68 +745,58 @@ impl Display for CpioEntry {
} }
} }
pub fn cpio_commands(cmds: &Vec<&str>) -> bool { pub(crate) fn cpio_commands(file: &mut String, cmds: &mut Vec<String>) -> LoggedResult<bool> {
let res: LoggedResult<()> = try { let file = Utf8CStr::from_string(file);
let mut cli = let mut cpio = if FsPath::from(file).exists() {
CpioCli::from_args(&["magiskboot", "cpio"], cmds).on_early_exit(print_cpio_usage); Cpio::load_from_file(file)?
} else {
let file = Utf8CStr::from_string(&mut cli.file); Cpio::new()
let mut cpio = if FsPath::from(file).exists() {
Cpio::load_from_file(file)?
} else {
Cpio::new()
};
for cmd in cli.commands {
if cmd.starts_with('#') {
continue;
}
let mut cli = CpioCommand::from_args(
&["magiskboot", "cpio", file],
cmd.split(' ')
.filter(|x| !x.is_empty())
.collect::<Vec<_>>()
.as_slice(),
)
.on_early_exit(print_cpio_usage);
match &mut cli.action {
CpioAction::Test(_) => exit(cpio.test()),
CpioAction::Restore(_) => cpio.restore()?,
CpioAction::Patch(_) => cpio.patch(),
CpioAction::Exists(Exists { path }) => {
if cpio.exists(path) {
exit(0);
} else {
exit(1);
}
}
CpioAction::Backup(Backup {
origin,
skip_compress,
}) => cpio.backup(origin, *skip_compress)?,
CpioAction::Remove(Remove { path, recursive }) => cpio.rm(path, *recursive),
CpioAction::Move(Move { from, to }) => cpio.mv(from, to)?,
CpioAction::MakeDir(MakeDir { mode, dir }) => cpio.mkdir(*mode, dir),
CpioAction::Link(Link { src, dst }) => cpio.ln(src, dst),
CpioAction::Add(Add { mode, path, file }) => cpio.add(*mode, path, file)?,
CpioAction::Extract(Extract { paths }) => {
if !paths.is_empty() && paths.len() != 2 {
Err(log_err!("invalid arguments"))?;
}
let mut it = paths.iter_mut();
cpio.extract(it.next(), it.next())?;
}
CpioAction::List(List { path, recursive }) => {
cpio.ls(path.as_str(), *recursive);
exit(0);
}
};
}
cpio.dump(file)?;
}; };
res.log_with_msg(|w| w.write_str("Failed to process cpio"))
.is_ok() for cmd in cmds {
if cmd.starts_with('#') {
continue;
}
let mut cmd = CpioCommand::from_args(
&["magiskboot", "cpio", file],
cmd.split(' ')
.filter(|x| !x.is_empty())
.collect::<Vec<_>>()
.as_slice(),
)
.on_early_exit(print_cpio_usage);
match &mut cmd.action {
CpioAction::Test(_) => exit(cpio.test()),
CpioAction::Restore(_) => cpio.restore()?,
CpioAction::Patch(_) => cpio.patch(),
CpioAction::Exists(Exists { path }) => {
return Ok(cpio.exists(path));
}
CpioAction::Backup(Backup {
origin,
skip_compress,
}) => cpio.backup(origin, *skip_compress)?,
CpioAction::Remove(Remove { path, recursive }) => cpio.rm(path, *recursive),
CpioAction::Move(Move { from, to }) => cpio.mv(from, to)?,
CpioAction::MakeDir(MakeDir { mode, dir }) => cpio.mkdir(*mode, dir),
CpioAction::Link(Link { src, dst }) => cpio.ln(src, dst),
CpioAction::Add(Add { mode, path, file }) => cpio.add(*mode, path, file)?,
CpioAction::Extract(Extract { paths }) => {
if !paths.is_empty() && paths.len() != 2 {
Err(log_err!("invalid arguments"))?;
}
let mut it = paths.iter_mut();
cpio.extract(it.next(), it.next())?;
}
CpioAction::List(List { path, recursive }) => {
cpio.ls(path.as_str(), *recursive);
return Ok(true);
}
};
}
cpio.dump(file)?;
Ok(true)
} }
fn x8u(x: &[u8; 8]) -> LoggedResult<u32> { fn x8u(x: &[u8; 8]) -> LoggedResult<u32> {

View File

@ -1,4 +1,4 @@
use std::{cell::UnsafeCell, process::exit}; use std::cell::UnsafeCell;
use argh::FromArgs; use argh::FromArgs;
use fdt::{ use fdt::{
@ -6,21 +6,14 @@ use fdt::{
Fdt, FdtError, Fdt, FdtError,
}; };
use base::{EarlyExitExt, LoggedResult, MappedFile, ResultExt, Utf8CStr}; use base::{LoggedResult, MappedFile, Utf8CStr};
use crate::{check_env, patch::patch_verity}; use crate::{check_env, patch::patch_verity};
#[derive(FromArgs)]
struct DtbCli {
#[argh(positional)]
file: String,
#[argh(subcommand)]
action: DtbAction,
}
#[derive(FromArgs)] #[derive(FromArgs)]
#[argh(subcommand)] #[argh(subcommand)]
enum DtbAction { #[allow(private_interfaces)]
pub(crate) enum DtbAction {
Print(Print), Print(Print),
Patch(Patch), Patch(Patch),
Test(Test), Test(Test),
@ -272,29 +265,18 @@ fn dtb_patch(file: &Utf8CStr) -> LoggedResult<bool> {
Ok(patched) Ok(patched)
} }
pub fn dtb_commands(cmds: &Vec<&str>) -> bool { pub(crate) fn dtb_commands(file: &mut String, action: &DtbAction) -> LoggedResult<bool> {
let res: LoggedResult<()> = try { let file = Utf8CStr::from_string(file);
let mut cli = match action {
DtbCli::from_args(&["magiskboot", "dtb"], &cmds).on_early_exit(print_dtb_usage); DtbAction::Print(Print { fstab }) => {
dtb_print(file, *fstab)?;
let file = Utf8CStr::from_string(&mut cli.file); Ok(true)
match cli.action {
DtbAction::Print(Print { fstab }) => {
dtb_print(file, fstab)?;
}
DtbAction::Test(_) => {
if !dtb_test(file)? {
exit(1);
}
}
DtbAction::Patch(_) => {
if !dtb_patch(file)? {
exit(1);
}
}
} }
}; DtbAction::Test(_) => {
res.log_with_msg(|w| w.write_str("Failed to process dtb")) Ok(dtb_test(file)?)
.is_ok() }
DtbAction::Patch(_) => {
Ok(dtb_patch(file)?)
}
}
} }