mirror of
https://github.com/topjohnwu/Magisk.git
synced 2024-12-22 07:57: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)]
|
#[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) }
|
unsafe { slice::from_raw_parts(argv, argc as usize) }
|
||||||
.iter()
|
.iter()
|
||||||
.map(|s| unsafe { Utf8CStr::from_ptr(*s) })
|
.map(|s| unsafe { Utf8CStr::from_ptr(*s) }.map(|s| s.deref()))
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,13 +24,21 @@ use crate::ramdisk::MagiskCpio;
|
|||||||
|
|
||||||
#[derive(FromArgs)]
|
#[derive(FromArgs)]
|
||||||
struct CpioCli {
|
struct CpioCli {
|
||||||
|
#[argh(positional)]
|
||||||
|
file: String,
|
||||||
|
#[argh(positional)]
|
||||||
|
commands: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(FromArgs)]
|
||||||
|
struct CpioCommand {
|
||||||
#[argh(subcommand)]
|
#[argh(subcommand)]
|
||||||
command: CpioCommands,
|
command: CpioSubCommand,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(FromArgs)]
|
#[derive(FromArgs)]
|
||||||
#[argh(subcommand)]
|
#[argh(subcommand)]
|
||||||
enum CpioCommands {
|
enum CpioSubCommand {
|
||||||
Test(Test),
|
Test(Test),
|
||||||
Restore(Restore),
|
Restore(Restore),
|
||||||
Patch(Patch),
|
Patch(Patch),
|
||||||
@ -525,27 +533,21 @@ pub fn cpio_commands(argc: i32, argv: *const *const c_char) -> bool {
|
|||||||
|
|
||||||
let cmds = map_args(argc, argv)?;
|
let cmds = map_args(argc, argv)?;
|
||||||
|
|
||||||
if cmds[0] == "--help" {
|
let mut cli =
|
||||||
print_cpio_usage();
|
CpioCli::from_args(&["magiskboot", "cpio"], &cmds).on_early_exit(print_cpio_usage);
|
||||||
exit(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
if argc < 2 {
|
let file = Utf8CStr::from_string(&mut cli.file);
|
||||||
return Err(log_err!("No commands"));
|
|
||||||
}
|
|
||||||
|
|
||||||
let file = cmds[0];
|
|
||||||
let mut cpio = if Path::new(file).exists() {
|
let mut cpio = if Path::new(file).exists() {
|
||||||
Cpio::load_from_file(file)?
|
Cpio::load_from_file(file)?
|
||||||
} else {
|
} else {
|
||||||
Cpio::new()
|
Cpio::new()
|
||||||
};
|
};
|
||||||
|
|
||||||
for cmd in &cmds[1..] {
|
for cmd in cli.commands {
|
||||||
if cmd.starts_with('#') {
|
if cmd.starts_with('#') {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
let mut cli = CpioCli::from_args(
|
let mut cli = CpioCommand::from_args(
|
||||||
&["magiskboot", "cpio", file],
|
&["magiskboot", "cpio", file],
|
||||||
cmd.split(' ')
|
cmd.split(' ')
|
||||||
.filter(|x| !x.is_empty())
|
.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);
|
.on_early_exit(print_cpio_usage);
|
||||||
|
|
||||||
match &mut cli.command {
|
match &mut cli.command {
|
||||||
CpioCommands::Test(_) => exit(cpio.test()),
|
CpioSubCommand::Test(_) => exit(cpio.test()),
|
||||||
CpioCommands::Restore(_) => cpio.restore()?,
|
CpioSubCommand::Restore(_) => cpio.restore()?,
|
||||||
CpioCommands::Patch(_) => cpio.patch(),
|
CpioSubCommand::Patch(_) => cpio.patch(),
|
||||||
CpioCommands::Exists(Exists { path }) => {
|
CpioSubCommand::Exists(Exists { path }) => {
|
||||||
if cpio.exists(path) {
|
if cpio.exists(path) {
|
||||||
exit(0);
|
exit(0);
|
||||||
} else {
|
} else {
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
CpioCommands::Backup(Backup { origin }) => {
|
CpioSubCommand::Backup(Backup { origin }) => {
|
||||||
cpio.backup(Utf8CStr::from_string(origin))?
|
cpio.backup(Utf8CStr::from_string(origin))?
|
||||||
}
|
}
|
||||||
CpioCommands::Remove(Remove { path, recursive }) => cpio.rm(path, *recursive),
|
CpioSubCommand::Remove(Remove { path, recursive }) => cpio.rm(path, *recursive),
|
||||||
CpioCommands::Move(Move { from, to }) => cpio.mv(from, to)?,
|
CpioSubCommand::Move(Move { from, to }) => cpio.mv(from, to)?,
|
||||||
CpioCommands::MakeDir(MakeDir { mode, dir }) => cpio.mkdir(mode, dir),
|
CpioSubCommand::MakeDir(MakeDir { mode, dir }) => cpio.mkdir(mode, dir),
|
||||||
CpioCommands::Link(Link { src, dst }) => cpio.ln(src, dst),
|
CpioSubCommand::Link(Link { src, dst }) => cpio.ln(src, dst),
|
||||||
CpioCommands::Add(Add { mode, path, file }) => cpio.add(mode, path, file)?,
|
CpioSubCommand::Add(Add { mode, path, file }) => cpio.add(mode, path, file)?,
|
||||||
CpioCommands::Extract(Extract { paths }) => {
|
CpioSubCommand::Extract(Extract { paths }) => {
|
||||||
if !paths.is_empty() && paths.len() != 2 {
|
if !paths.is_empty() && paths.len() != 2 {
|
||||||
return Err(log_err!("invalid arguments"));
|
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()),
|
paths.get(1).map(|x| x.as_str()),
|
||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
CpioCommands::List(List { path, recursive }) => {
|
CpioSubCommand::List(List { path, recursive }) => {
|
||||||
cpio.ls(path.as_str(), *recursive);
|
cpio.ls(path.as_str(), *recursive);
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user