mirror of
https://github.com/topjohnwu/Magisk.git
synced 2024-12-21 23:47:39 +00:00
Refine cpio argh
we can use argh to handle `--help` now
This commit is contained in:
parent
e8e8afa6c2
commit
64c82e1f2c
@ -360,12 +360,12 @@ impl<T: AsMut<[u8]>> 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<Vec<&'a Utf8CStr>, StrErr> {
|
||||
pub fn map_args(argc: i32, argv: *const *const c_char) -> Result<Vec<&'static str>, 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()
|
||||
}
|
||||
|
||||
|
@ -24,13 +24,21 @@ use crate::ramdisk::MagiskCpio;
|
||||
|
||||
#[derive(FromArgs)]
|
||||
struct CpioCli {
|
||||
#[argh(positional)]
|
||||
file: String,
|
||||
#[argh(positional)]
|
||||
commands: Vec<String>,
|
||||
}
|
||||
|
||||
#[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);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user