use std::collections::HashSet;
use std::fs::{self, File, Permissions};
use std::io::{self, BufReader, BufWriter, Read, Seek, SeekFrom, Write};
use std::os::unix::fs::lchown;
use std::os::unix::prelude::PermissionsExt;
use std::path::{Component, Path, PathBuf};
use std::process::ExitCode;
use std::sync::Mutex;
#[cfg(feature = "v3")]
use backhand::V3;
use backhand::kind::Kind;
use backhand::traits::filesystem::{BackhandInnerNode, BackhandNode, BackhandNodeHeader};
use backhand::{
DEFAULT_BLOCK_SIZE, FilesystemReaderTrait, SquashfsVersion, V4, create_squashfs_from_kind,
};
use backhand_cli::after_help;
use clap::builder::PossibleValuesParser;
use clap::{CommandFactory, Parser};
use clap_complete::{Shell, generate};
use console::Term;
use indicatif::{HumanDuration, ProgressBar, ProgressStyle};
use nix::fcntl::AT_FDCWD;
use nix::libc::geteuid;
use nix::sys::stat::{Mode, SFlag, UtimensatFlags, dev_t, mknod, mode_t, umask, utimensat, utimes};
use nix::sys::time::{TimeSpec, TimeVal};
use nix::unistd::mkfifo;
use rayon::prelude::*;
use std::time::{Duration, Instant};
#[cfg(all(target_env = "musl", target_pointer_width = "64"))]
#[global_allocator]
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
const AVAILABLE_KINDS: &[&str] = &[
"le_v4_0",
"be_v4_0",
#[cfg(feature = "v3")]
"le_v3_0",
#[cfg(feature = "v3")]
"be_v3_0",
#[cfg(feature = "v3_lzma")]
"le_v3_0_lzma",
#[cfg(feature = "v3_lzma")]
"be_v3_0_lzma",
#[cfg(feature = "v3_lzma")]
"netgear_be_v3_0_lzma_standard",
#[cfg(feature = "v3_lzma")]
"netgear_be_v3_0_lzma",
#[cfg(feature = "v3_lzma")]
"le_v3_1_lzma_swap",
#[cfg(feature = "v3_lzma")]
"be_v3_1_lzma_swap",
"avm_be_v4_0",
];
pub fn required_root(a: &str) -> Result<PathBuf, String> {
let p = PathBuf::from(a);
if p.has_root() { Ok(p) } else { Err("argument requires root \"/\"".to_string()) }
}
fn find_offset(file: &mut BufReader<File>, kind: &Kind) -> Option<u64> {
let mut magic = [0_u8; 4];
while file.read_exact(&mut magic).is_ok() {
if magic == kind.magic() {
let found = file.stream_position().ok()? - magic.len() as u64;
file.rewind().ok()?;
return Some(found);
}
}
file.rewind().ok()?;
None
}
pub fn extracted(pb: &ProgressBar, s: &str) {
let blue_bold: console::Style = console::Style::new().blue().bold();
let line = format!("{:>16} {}", blue_bold.apply_to("Extracted"), s,);
pb.println(line);
}
pub fn created(pb: &ProgressBar, s: &str) {
let blue_bold: console::Style = console::Style::new().blue().bold();
let line = format!("{:>16} {}", blue_bold.apply_to("Created"), s,);
pb.println(line);
}
pub fn exists(pb: &ProgressBar, s: &str) {
let red_bold: console::Style = console::Style::new().red().bold();
let line = format!("{:>16} {}", red_bold.apply_to("Exists"), s,);
pb.println(line);
}
pub fn failed(pb: &ProgressBar, s: &str) {
let red_bold: console::Style = console::Style::new().red().bold();
let line = format!("{:>16} {}", red_bold.apply_to("Failed"), s,);
pb.println(line);
}
pub fn error(pb: &ProgressBar, s: &str) {
let red_bold: console::Style = console::Style::new().red().bold();
let line = format!("{:>14} {}", red_bold.apply_to("Error"), s,);
pb.finish_with_message(line);
}
#[derive(Parser)]
#[command(author,
version,
name = "unsquashfs-backhand",
after_help = after_help(true),
max_term_width = 98,
styles = clap_cargo::style::CLAP_STYLING,
)]
struct Args {
#[arg(required_unless_present = "completions")]
filesystem: Option<PathBuf>,
#[arg(short, long, default_value_t = 0, name = "BYTES")]
offset: u64,
#[arg(short, long)]
auto_offset: bool,
#[arg(short, long)]
list: bool,
#[arg(short, long, default_value = "squashfs-root", name = "PATHNAME")]
dest: PathBuf,
#[arg(short, long)]
info: bool,
#[arg(long, default_value = "/", value_parser = required_root)]
path_filter: PathBuf,
#[arg(short, long)]
force: bool,
#[arg(short, long)]
stat: bool,
#[arg(short, long, value_parser = PossibleValuesParser::new(AVAILABLE_KINDS))]
kind: Option<String>,
#[arg(long)]
completions: Option<Shell>,
#[arg(long)]
quiet: bool,
}
fn try_all_kinds(
file: &mut BufReader<File>,
offset: u64,
pb: &ProgressBar,
quiet: bool,
) -> Result<(Kind, &'static str), String> {
let mut errors = Vec::new();
for &kind_name in AVAILABLE_KINDS {
if let Ok(kind) = Kind::from_target(kind_name) {
file.seek(SeekFrom::Start(0)).map_err(|e| format!("Failed to seek: {}", e))?;
let success = {
let result = create_squashfs_from_kind(&mut *file, offset, kind.clone());
match result {
Ok(_) => true,
Err(e) => {
errors.push((kind_name, e));
false
}
}
};
if success {
file.seek(SeekFrom::Start(0)).map_err(|e| format!("Failed to seek back: {}", e))?;
return Ok((kind, kind_name));
}
}
}
if !quiet {
pb.finish_and_clear();
let red_bold: console::Style = console::Style::new().red().bold();
for (kind_name, e) in errors {
eprintln!("{:>16} {}: {}", red_bold.apply_to("Failed"), kind_name, e);
}
}
Err("Could not detect kind".to_string())
}
fn handle_with_kind<'a>(
args: &mut Args,
file: &'a mut BufReader<File>,
kind_str: &str,
pb: &ProgressBar,
blue_bold: &console::Style,
) -> Result<(Box<dyn FilesystemReaderTrait + 'a>, Kind), ExitCode> {
let kind = match Kind::from_target(kind_str) {
Ok(k) => k,
Err(e) => {
error(pb, &format!("Invalid kind: {e}"));
return Err(ExitCode::FAILURE);
}
};
if args.auto_offset {
if !args.quiet {
pb.enable_steady_tick(Duration::from_millis(120));
let line = format!("{:>14}", blue_bold.apply_to("Searching for magic"));
pb.set_message(line);
}
if let Some(found_offset) = find_offset(file, &kind) {
if !args.quiet {
let line =
format!("{:>14} 0x{:08x}", blue_bold.apply_to("Found magic"), found_offset,);
pb.finish_with_message(line);
}
args.offset = found_offset;
} else {
if !args.quiet {
error(pb, "Magic not found");
}
return Err(ExitCode::FAILURE);
}
}
match create_squashfs_from_kind(file, args.offset, kind.clone()) {
Ok(filesystem) => Ok((filesystem, kind)),
Err(e) => {
error(pb, &format!("Could not read image: {e:?}"));
Err(ExitCode::FAILURE)
}
}
}
fn handle_auto_detect_kind<'a>(
args: &Args,
file: &'a mut BufReader<File>,
pb: &ProgressBar,
blue_bold: &console::Style,
) -> Result<(Box<dyn FilesystemReaderTrait + 'a>, Kind), ExitCode> {
if !args.quiet {
pb.enable_steady_tick(Duration::from_millis(120));
let line = format!("{:>14}", blue_bold.apply_to("Auto-detecting kind"));
pb.set_message(line);
}
if args.auto_offset {
if !args.quiet {
error(pb, "--auto-offset requires --kind to be specified");
}
return Err(ExitCode::FAILURE);
}
match try_all_kinds(file, args.offset, pb, args.quiet) {
Ok((detected_kind, kind_name)) => {
if !args.quiet {
let line = format!("{:>16} {}", blue_bold.apply_to("Detected kind"), kind_name);
pb.finish_and_clear();
pb.println(line);
}
match create_squashfs_from_kind(file, args.offset, detected_kind.clone()) {
Ok(filesystem) => Ok((filesystem, detected_kind)),
Err(e) => {
error(pb, &format!("Could not read image: {e:?}"));
Err(ExitCode::FAILURE)
}
}
}
Err(e) => {
if !args.quiet {
error(pb, &e);
}
Err(ExitCode::FAILURE)
}
}
}
fn main() -> ExitCode {
let mut args = Args::parse();
if !args.quiet {
tracing_subscriber::fmt::init();
}
if let Some(completions) = args.completions {
let mut cmd = Args::command();
let name = cmd.get_name().to_string();
generate(completions, &mut cmd, name, &mut io::stdout());
return ExitCode::SUCCESS;
}
let blue_bold: console::Style = console::Style::new().blue().bold();
let pb = ProgressBar::new_spinner();
let filesystem_path = args.filesystem.as_ref().unwrap();
match fs::metadata(filesystem_path) {
Ok(metadata) if !metadata.is_file() => {
error(&pb, &format!("{} is not a regular file", filesystem_path.display()));
return ExitCode::FAILURE;
}
Err(e) => {
error(&pb, &format!("Failed to access {}: {}", filesystem_path.display(), e));
return ExitCode::FAILURE;
}
_ => {}
}
let mut file = match File::open(filesystem_path) {
Ok(f) => BufReader::with_capacity(DEFAULT_BLOCK_SIZE as usize, f),
Err(e) => {
error(&pb, &format!("Failed to open file: {}", e));
return ExitCode::FAILURE;
}
};
if args.stat {
if let Some(kind_str) = &args.kind {
let kind = match Kind::from_target(kind_str) {
Ok(k) => k,
Err(e) => {
error(&pb, &format!("Invalid kind: {e}"));
return ExitCode::FAILURE;
}
};
stat(args, file, kind, &pb);
return ExitCode::SUCCESS;
} else {
if !args.quiet {
error(&pb, "--stat requires --kind to be specified");
}
return ExitCode::FAILURE;
}
}
let (filesystem, _) = if let Some(kind_str) = args.kind.clone() {
match handle_with_kind(&mut args, &mut file, &kind_str, &pb, &blue_bold) {
Ok(result) => result,
Err(exit_code) => return exit_code,
}
} else {
match handle_auto_detect_kind(&args, &mut file, &pb, &blue_bold) {
Ok(result) => result,
Err(exit_code) => return exit_code,
}
};
process_filesystem(filesystem.as_ref(), args, pb, blue_bold)
}
fn process_filesystem(
filesystem: &dyn FilesystemReaderTrait,
args: Args,
_pb: ProgressBar,
blue_bold: console::Style,
) -> ExitCode {
let root_process = unsafe { geteuid() == 0 };
if root_process {
umask(Mode::from_bits(0).unwrap());
}
let start = Instant::now();
let pb = ProgressBar::new_spinner();
if !args.quiet {
pb.enable_steady_tick(Duration::from_millis(120));
let line = format!("{:>14}", blue_bold.apply_to("Reading image"));
pb.set_message(line);
}
if !args.quiet {
let line = format!("{:>14}", blue_bold.apply_to("Read image"));
pb.finish_with_message(line);
}
let mut files: Vec<BackhandNode> = vec![];
if args.path_filter.parent().is_some() {
let mut current = PathBuf::new();
current.push("/");
for part in args.path_filter.iter() {
current.push(part);
if let Some(exact) = filesystem.files().into_iter().find(|a| a.fullpath == current) {
files.push(exact);
} else {
if !args.quiet {
error(&pb, "Invalid --path-filter, path doesn't exist");
}
return ExitCode::FAILURE;
}
}
files.pop();
}
let nodes = files
.into_iter()
.chain(filesystem.files().filter(|a| a.fullpath.starts_with(&args.path_filter)));
if args.list {
list(nodes);
} else {
let all_nodes: Vec<BackhandNode> = nodes.collect();
let n_nodes = if !args.quiet { Some(all_nodes.len()) } else { None };
extract_all(&args, filesystem, root_process, all_nodes, n_nodes, start);
}
ExitCode::SUCCESS
}
fn list(nodes: impl Iterator<Item = BackhandNode>) {
for node in nodes {
let path = &node.fullpath;
println!("{}", path.display());
}
}
fn stat_v4(args: Args, mut file: BufReader<File>, kind: Kind) {
if let Err(e) = file.seek(SeekFrom::Start(args.offset)) {
eprintln!("Failed to seek to offset: {e}");
return;
}
let (superblock, compression_options) =
match V4::superblock_and_compression_options(&mut file, &kind) {
Ok(result) => result,
Err(e) => {
eprintln!("Failed to read superblock: {e}");
return;
}
};
println!("{superblock:#08x?}");
println!("Compression Options: {compression_options:#x?}");
if superblock.inodes_uncompressed() {
println!("flag: inodes uncompressed");
}
if superblock.data_block_stored_uncompressed() {
println!("flag: data blocks stored uncompressed");
}
if superblock.fragments_stored_uncompressed() {
println!("flag: fragments stored uncompressed");
}
if superblock.fragments_are_not_used() {
println!("flag: fragments are not used");
}
if superblock.fragments_are_always_generated() {
println!("flag: fragments are always generated");
}
if superblock.data_has_been_deduplicated() {
println!("flag: data has been deduplicated");
}
if superblock.nfs_export_table_exists() {
println!("flag: nfs export table exists");
}
if superblock.xattrs_are_stored_uncompressed() {
println!("flag: xattrs are stored uncompressed");
}
if superblock.compressor_options_are_present() {
println!("flag: compressor options are present");
}
}
#[cfg(feature = "v3")]
fn stat_v3(args: Args, mut file: BufReader<File>, kind: Kind) {
if let Err(e) = file.seek(SeekFrom::Start(args.offset)) {
eprintln!("Failed to seek to offset: {e}");
return;
}
let (superblock, _compression_options) =
match V3::superblock_and_compression_options(&mut file, &kind) {
Ok(result) => result,
Err(e) => {
eprintln!("Failed to read superblock: {e}");
return;
}
};
println!("{superblock:#08x?}");
}
fn stat(args: Args, file: BufReader<File>, kind: Kind, pb: &ProgressBar) {
match (kind.version_major(), kind.version_minor()) {
(4, 0) => stat_v4(args, file, kind),
#[cfg(feature = "v3")]
(3, 0) => stat_v3(args, file, kind),
_ => {
error(
pb,
&format!(
"Unsupported SquashFS version: {}.{}",
kind.version_major(),
kind.version_minor()
),
);
}
}
}
fn set_attributes(
pb: &ProgressBar,
args: &Args,
path: &Path,
header: &BackhandNodeHeader,
root_process: bool,
is_file: bool,
) {
let timeval = TimeVal::new(header.mtime as _, 0);
if let Err(e) = utimes(path, &timeval, &timeval) {
if !args.quiet {
let line = format!("{} : could not set times: {e}", path.display());
failed(pb, &line);
}
return;
}
let mut mode = u32::from(header.permissions);
if root_process {
match lchown(path, Some(header.uid), Some(header.gid)) {
Ok(_) => (),
Err(e) => {
if !args.quiet {
let line =
format!("lchown {} {} {} : {e}", path.display(), header.uid, header.gid,);
failed(pb, &line);
}
return;
}
}
} else if is_file {
mode &= !0o022;
}
if let Err(e) = fs::set_permissions(path, Permissions::from_mode(mode))
&& e.kind() == std::io::ErrorKind::PermissionDenied
{
if fs::set_permissions(path, Permissions::from_mode(mode & !1000)).is_err() && !args.quiet {
let line = format!("{} : could not set permissions", path.display());
failed(pb, &line);
}
}
}
fn extract_all(
args: &Args,
filesystem: &dyn FilesystemReaderTrait,
root_process: bool,
nodes: Vec<BackhandNode>,
n_nodes: Option<usize>,
start: Instant,
) {
let pb = ProgressBar::new(n_nodes.unwrap_or(0) as u64);
if !args.quiet {
pb.set_style(ProgressStyle::default_spinner());
pb.set_style(
ProgressStyle::with_template(
if Term::stdout().size().1 > 80 {
"{prefix:>16.cyan.bold} [{bar:57}] {pos}/{len} {wide_msg}"
} else {
"{prefix:>16.cyan.bold} [{bar:57}] {pos}/{len}"
},
)
.unwrap()
.progress_chars("=> "),
);
pb.set_prefix("Extracting");
pb.inc(1);
}
let processing = Mutex::new(HashSet::new());
tracing::trace!("{:?}", nodes);
nodes.into_par_iter().for_each(|node| {
let path = &node.fullpath;
let fullpath = path.strip_prefix(Component::RootDir).unwrap_or(path);
if !args.quiet {
let mut p = processing.lock().unwrap();
p.insert(fullpath.to_path_buf());
pb.set_message(p.iter().map(|a| a.to_string_lossy()).collect::<Vec<_>>().join(", "));
pb.inc(1);
}
let filepath = Path::new(&args.dest).join(fullpath);
let _ = fs::create_dir_all(filepath.parent().unwrap());
match &node.inner {
BackhandInnerNode::File(file) => {
if !args.force && filepath.exists() {
if !args.quiet {
exists(&pb, &filepath.display().to_string());
let mut p = processing.lock().unwrap();
p.remove(&fullpath.to_path_buf());
}
return;
}
let file_data = match filesystem.file_data(file) {
Ok(file_data) => file_data,
Err(e) => {
if !args.quiet {
let line = format!("{} : {e}", filepath.display());
failed(&pb, &line);
let mut p = processing.lock().unwrap();
p.remove(&fullpath.to_path_buf());
}
return;
}
};
let fd = match File::create(&filepath) {
Ok(f) => f,
Err(e) => {
if !args.quiet {
let line = format!("{} : {e}", filepath.display());
failed(&pb, &line);
let mut p = processing.lock().unwrap();
p.remove(&fullpath.to_path_buf());
}
return;
}
};
let mut writer = BufWriter::with_capacity(file_data.len(), &fd);
match writer.write_all(&file_data) {
Ok(_) => {
if args.info && !args.quiet {
extracted(&pb, &filepath.display().to_string());
}
set_attributes(&pb, args, &filepath, &node.header, root_process, true);
}
Err(e) => {
if !args.quiet {
let line = format!("{} : {e}", filepath.display());
failed(&pb, &line);
let mut p = processing.lock().unwrap();
p.remove(&fullpath.to_path_buf());
}
return;
}
}
if let Err(e) = writer.flush()
&& !args.quiet
{
let line = format!("{} : flush failed: {e}", filepath.display());
failed(&pb, &line);
}
}
BackhandInnerNode::Symlink { link } => {
let link_display = link.display();
if !args.force && filepath.exists() {
exists(&pb, &filepath.display().to_string());
let mut p = processing.lock().unwrap();
p.remove(&fullpath.to_path_buf());
return;
}
match std::os::unix::fs::symlink(link, &filepath) {
Ok(_) => {
if args.info && !args.quiet {
let line = format!("{}->{link_display}", filepath.display());
created(&pb, &line);
}
}
Err(e) => {
if !args.quiet {
let line = format!("{}->{link_display} : {e}", filepath.display());
failed(&pb, &line);
let mut p = processing.lock().unwrap();
p.remove(&fullpath.to_path_buf());
}
return;
}
}
if root_process {
match lchown(&filepath, Some(node.header.uid), Some(node.header.gid)) {
Ok(_) => (),
Err(e) => {
if !args.quiet {
let line = format!(
"lchown {} {} {} : {e}",
filepath.display(),
node.header.uid,
node.header.gid,
);
failed(&pb, &line);
}
let mut p = processing.lock().unwrap();
p.remove(&fullpath.to_path_buf());
return;
}
}
}
let timespec = TimeSpec::new(node.header.mtime as _, 0);
if let Err(e) = utimensat(
AT_FDCWD,
&filepath,
×pec,
×pec,
UtimensatFlags::NoFollowSymlink,
) && !args.quiet
{
let line = format!("{} : could not set times: {e}", filepath.display());
failed(&pb, &line);
}
}
BackhandInnerNode::Dir => {
if std::fs::create_dir(&filepath).is_ok() && args.info && !args.quiet {
created(&pb, &filepath.display().to_string())
}
}
BackhandInnerNode::CharacterDevice { device_number } => {
if root_process {
#[allow(clippy::unnecessary_fallible_conversions)]
match mknod(
&filepath,
SFlag::S_IFCHR,
Mode::from_bits(mode_t::from(node.header.permissions)).unwrap(),
dev_t::try_from(*device_number).unwrap(),
) {
Ok(_) => {
if args.info && !args.quiet {
created(&pb, &filepath.display().to_string());
}
set_attributes(&pb, args, &filepath, &node.header, root_process, true);
}
Err(_) => {
if !args.quiet {
let line = format!(
"char device {}, are you superuser?",
filepath.display()
);
failed(&pb, &line);
let mut p = processing.lock().unwrap();
p.remove(&fullpath.to_path_buf());
}
return;
}
}
} else {
if !args.quiet {
let line =
format!("char device {}, are you superuser?", filepath.display());
failed(&pb, &line);
}
let mut p = processing.lock().unwrap();
p.remove(&fullpath.to_path_buf());
return;
}
}
BackhandInnerNode::BlockDevice { device_number } => {
#[allow(clippy::unnecessary_fallible_conversions)]
match mknod(
&filepath,
SFlag::S_IFBLK,
Mode::from_bits(mode_t::from(node.header.permissions)).unwrap(),
dev_t::try_from(*device_number).unwrap(),
) {
Ok(_) => {
if args.info && !args.quiet {
created(&pb, &filepath.display().to_string());
}
set_attributes(&pb, args, &filepath, &node.header, root_process, true);
}
Err(_) => {
if args.info && !args.quiet {
created(&pb, &filepath.display().to_string());
let mut p = processing.lock().unwrap();
p.remove(&fullpath.to_path_buf());
}
return;
}
}
}
BackhandInnerNode::NamedPipe => {
match mkfifo(
&filepath,
Mode::from_bits(mode_t::from(node.header.permissions)).unwrap(),
) {
Ok(_) => {
if args.info && !args.quiet {
created(&pb, &filepath.display().to_string());
}
set_attributes(&pb, args, &filepath, &node.header, root_process, true);
}
Err(_) => {
if args.info && !args.quiet {
created(&pb, &filepath.display().to_string());
}
let mut p = processing.lock().unwrap();
p.remove(&fullpath.to_path_buf());
return;
}
}
}
BackhandInnerNode::Socket => {
#[allow(clippy::unnecessary_fallible_conversions)]
match mknod(
&filepath,
SFlag::S_IFSOCK,
Mode::from_bits(mode_t::from(node.header.permissions)).unwrap(),
dev_t::try_from(0_u64).unwrap(),
) {
Ok(_) => {
if args.info && !args.quiet {
created(&pb, &filepath.display().to_string());
}
set_attributes(&pb, args, &filepath, &node.header, root_process, true);
}
Err(_) => {
if args.info && !args.quiet {
created(&pb, &filepath.display().to_string());
let mut p = processing.lock().unwrap();
p.remove(&fullpath.to_path_buf());
}
return;
}
}
}
}
let mut p = processing.lock().unwrap();
p.remove(&fullpath.to_path_buf());
});
for node in filesystem.files().filter(|a| a.fullpath.starts_with(&args.path_filter)) {
if let BackhandInnerNode::Dir = &node.inner {
let path = &node.fullpath;
let path = path.strip_prefix(Component::RootDir).unwrap_or(path);
let path = Path::new(&args.dest).join(path);
set_attributes(&pb, args, &path, &node.header, root_process, false);
}
}
pb.finish_and_clear();
let green_bold: console::Style = console::Style::new().green().bold();
if !args.quiet {
println!(
"{:>16} extraction of {} nodes in {}",
green_bold.apply_to("Finished"),
n_nodes.unwrap(),
HumanDuration(start.elapsed())
);
}
}