use std::fs::File;
use std::io::BufReader;
use std::path::PathBuf;
use std::process::ExitCode;
use backhand::{FilesystemReader, FilesystemWriter};
use backhand_cli::after_help;
use clap::Parser;
use tracing::error;
use tracing_subscriber::EnvFilter;
#[cfg(all(target_env = "musl", target_pointer_width = "64"))]
#[global_allocator]
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
#[derive(Parser, Debug)]
#[command(author,
version,
name = "replace-backhand",
after_help = after_help(false),
max_term_width = 98,
styles = clap_cargo::style::CLAP_STYLING,
)]
struct Args {
input_image: PathBuf,
file: PathBuf,
#[clap(name = "FILE_PATH_IN_IMAGE")]
file_path: PathBuf,
output_image: PathBuf,
#[clap(long)]
pad_len: Option<u32>,
#[clap(long)]
no_compression_options: bool,
}
fn main() -> ExitCode {
let env_filter =
EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("replace=info"));
tracing_subscriber::fmt().with_env_filter(env_filter).init();
let args = Args::parse();
let Ok(file) = File::open(&args.input_image) else {
error!("unable to open {}", args.input_image.display());
return ExitCode::FAILURE;
};
let file = BufReader::new(file);
let filesystem = FilesystemReader::from_reader(file).unwrap();
let mut filesystem = FilesystemWriter::from_fs_reader(&filesystem).unwrap();
let Ok(new_file) = File::open(&args.file) else {
error!("unable to open {}", args.file.display());
return ExitCode::FAILURE;
};
if let Err(e) = filesystem.replace_file(args.file_path, new_file) {
error!("{e}");
return ExitCode::FAILURE;
}
if let Some(pad_len) = args.pad_len {
filesystem.set_kib_padding(pad_len)
}
if args.no_compression_options {
filesystem.set_emit_compression_options(false);
}
let Ok(output) = File::create_new(&args.output_image) else {
error!("failed to open {}", args.output_image.display());
return ExitCode::FAILURE;
};
filesystem.write(output).unwrap();
println!("replaced file and wrote to {}", args.output_image.display());
ExitCode::SUCCESS
}