use more idiomatic rust

This commit is contained in:
beerpsi
2025-07-16 15:47:53 +00:00
parent 53c389ba05
commit 6cdf6a8e52
+31 -32
View File
@@ -1,10 +1,10 @@
use std::{
fs::{File, create_dir_all},
fs::{create_dir_all, File},
io::{BufReader, BufWriter, Read, Seek, SeekFrom, Write},
path::Path,
};
use exfat_fs::dir::{Root, entry::fs::FsElement};
use exfat_fs::dir::{entry::fs::FsElement, Root};
use aes::{
cipher::{block_padding::NoPadding, BlockDecryptMut, InnerIvInit, KeyInit, KeyIvInit},
@@ -25,39 +25,38 @@ const PAGE_SIZE: u64 = 4096;
fn extract_exfat_contents(exfat_path: &Path) -> Result<()> {
println!("Extracting contents of {}", exfat_path.display());
let file = File::open(exfat_path)?;
let mut root = Root::open(file)?;
// Create output directory with same name as exfat file (without extension)
let output_dir = exfat_path.with_extension("");
create_dir_all(&output_dir)?;
extract_fs_elements(root.items(), &output_dir)?;
Ok(())
}
fn extract_fs_elements(elements: &mut [FsElement<File>], output_dir: &Path) -> Result<()> {
let item_count = elements.len();
for element in elements {
match element {
FsElement::F(ref mut file) => {
let dest_path = output_dir.join(file.name());
let mut dest = File::create(dest_path)?;
for i in 0..item_count {
if let Some(FsElement::F(ref mut file_ref)) = elements.get_mut(i) {
// Extract file
let filename = file_ref.name().to_owned();
let file_path = output_dir.join(&filename);
let mut buffer = Vec::new();
file_ref.read_to_end(&mut buffer)?;
std::fs::write(file_path, buffer)?;
} else if let Some(FsElement::D(ref dir_ref)) = elements.get(i) {
// Handle directory
let dirname = dir_ref.name();
let dir_path = output_dir.join(dirname);
create_dir_all(&dir_path)?;
let mut children = dir_ref.open()?;
// Extract children recursively
extract_fs_elements(&mut children, &dir_path)?;
std::io::copy(file, &mut dest)?;
}
FsElement::D(directory) => {
let dest_path = output_dir.join(directory.name());
create_dir_all(&dest_path)?;
let mut children = directory.open()?;
extract_fs_elements(&mut children, &dest_path)?;
}
}
}
Ok(())
}
@@ -123,11 +122,7 @@ fn main() -> Result<()> {
let data_offset = bootid.header_block_count * bootid.block_size;
let key = keys.key;
let iv = if bootid.use_custom_iv {
None
} else {
keys.iv
};
let iv = if bootid.use_custom_iv { None } else { keys.iv };
let iv = match iv {
Some(iv) => iv,
None => {
@@ -225,13 +220,17 @@ fn main() -> Result<()> {
pb.finish();
// Extract exfat contents if this is an exfat file
if bootid.container_type == ContainerType::OPTION && output_path.extension().unwrap_or_default() == "exfat" {
if bootid.container_type == ContainerType::OPTION
&& output_path.extension().unwrap_or_default() == "exfat"
{
if let Err(e) = extract_exfat_contents(&output_path) {
println!("WARNING: Failed to extract exfat contents: {e}");
println!("WARNING: Failed to extract exfat contents: {e:#?}");
} else {
println!("Extracted exfat contents: {:?}", output_path);
println!("Deleting exfat file: {:?}", output_path);
std::fs::remove_file(output_path)?;
}
println!("Extracted exfat contents: {}", output_path.display());
println!("Deleting exfat file: {}", output_path.display());
std::fs::remove_file(output_path)?;
}
page.clear();