Handle cpio commands properly

This commit is contained in:
topjohnwu
2023-07-03 21:57:28 -07:00
parent 43b9a09c9b
commit 5ee6daf126
5 changed files with 51 additions and 27 deletions

View File

@@ -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)
}
},
}
}
}