From 64c82e1f2cd2e4229a282e1b36ee959947994931 Mon Sep 17 00:00:00 2001 From: LoveSy Date: Fri, 7 Jul 2023 01:06:14 +0800 Subject: [PATCH] Refine cpio argh we can use argh to handle `--help` now --- native/src/base/misc.rs | 6 ++--- native/src/boot/cpio.rs | 52 +++++++++++++++++++++-------------------- 2 files changed, 30 insertions(+), 28 deletions(-) diff --git a/native/src/base/misc.rs b/native/src/base/misc.rs index 716fa0f89..a1034cab3 100644 --- a/native/src/base/misc.rs +++ b/native/src/base/misc.rs @@ -360,12 +360,12 @@ impl> MutBytesExt for T { } } -// SAFETY: libc guarantees argc and argv is properly setup +// SAFETY: libc guarantees argc and argv are properly setup and are static #[allow(clippy::not_unsafe_ptr_arg_deref)] -pub fn map_args<'a>(argc: i32, argv: *const *const c_char) -> Result, StrErr> { +pub fn map_args(argc: i32, argv: *const *const c_char) -> Result, StrErr> { unsafe { slice::from_raw_parts(argv, argc as usize) } .iter() - .map(|s| unsafe { Utf8CStr::from_ptr(*s) }) + .map(|s| unsafe { Utf8CStr::from_ptr(*s) }.map(|s| s.deref())) .collect() } diff --git a/native/src/boot/cpio.rs b/native/src/boot/cpio.rs index 268ee4c7f..d19ce29e9 100644 --- a/native/src/boot/cpio.rs +++ b/native/src/boot/cpio.rs @@ -24,13 +24,21 @@ use crate::ramdisk::MagiskCpio; #[derive(FromArgs)] struct CpioCli { + #[argh(positional)] + file: String, + #[argh(positional)] + commands: Vec, +} + +#[derive(FromArgs)] +struct CpioCommand { #[argh(subcommand)] - command: CpioCommands, + command: CpioSubCommand, } #[derive(FromArgs)] #[argh(subcommand)] -enum CpioCommands { +enum CpioSubCommand { Test(Test), Restore(Restore), Patch(Patch), @@ -525,27 +533,21 @@ pub fn cpio_commands(argc: i32, argv: *const *const c_char) -> bool { let cmds = map_args(argc, argv)?; - if cmds[0] == "--help" { - print_cpio_usage(); - exit(0); - } + let mut cli = + CpioCli::from_args(&["magiskboot", "cpio"], &cmds).on_early_exit(print_cpio_usage); - if argc < 2 { - return Err(log_err!("No commands")); - } - - let file = cmds[0]; + let file = Utf8CStr::from_string(&mut cli.file); let mut cpio = if Path::new(file).exists() { Cpio::load_from_file(file)? } else { Cpio::new() }; - for cmd in &cmds[1..] { + for cmd in cli.commands { if cmd.starts_with('#') { continue; } - let mut cli = CpioCli::from_args( + let mut cli = CpioCommand::from_args( &["magiskboot", "cpio", file], cmd.split(' ') .filter(|x| !x.is_empty()) @@ -555,25 +557,25 @@ pub fn cpio_commands(argc: i32, argv: *const *const c_char) -> bool { .on_early_exit(print_cpio_usage); match &mut cli.command { - CpioCommands::Test(_) => exit(cpio.test()), - CpioCommands::Restore(_) => cpio.restore()?, - CpioCommands::Patch(_) => cpio.patch(), - CpioCommands::Exists(Exists { path }) => { + CpioSubCommand::Test(_) => exit(cpio.test()), + CpioSubCommand::Restore(_) => cpio.restore()?, + CpioSubCommand::Patch(_) => cpio.patch(), + CpioSubCommand::Exists(Exists { path }) => { if cpio.exists(path) { exit(0); } else { exit(1); } } - CpioCommands::Backup(Backup { origin }) => { + CpioSubCommand::Backup(Backup { origin }) => { cpio.backup(Utf8CStr::from_string(origin))? } - CpioCommands::Remove(Remove { path, recursive }) => cpio.rm(path, *recursive), - CpioCommands::Move(Move { from, to }) => cpio.mv(from, to)?, - CpioCommands::MakeDir(MakeDir { mode, dir }) => cpio.mkdir(mode, dir), - CpioCommands::Link(Link { src, dst }) => cpio.ln(src, dst), - CpioCommands::Add(Add { mode, path, file }) => cpio.add(mode, path, file)?, - CpioCommands::Extract(Extract { paths }) => { + CpioSubCommand::Remove(Remove { path, recursive }) => cpio.rm(path, *recursive), + CpioSubCommand::Move(Move { from, to }) => cpio.mv(from, to)?, + CpioSubCommand::MakeDir(MakeDir { mode, dir }) => cpio.mkdir(mode, dir), + CpioSubCommand::Link(Link { src, dst }) => cpio.ln(src, dst), + CpioSubCommand::Add(Add { mode, path, file }) => cpio.add(mode, path, file)?, + CpioSubCommand::Extract(Extract { paths }) => { if !paths.is_empty() && paths.len() != 2 { return Err(log_err!("invalid arguments")); } @@ -582,7 +584,7 @@ pub fn cpio_commands(argc: i32, argv: *const *const c_char) -> bool { paths.get(1).map(|x| x.as_str()), )?; } - CpioCommands::List(List { path, recursive }) => { + CpioSubCommand::List(List { path, recursive }) => { cpio.ls(path.as_str(), *recursive); exit(0); }