Clean up and reformat.
This commit is contained in:
68
src/main.rs
68
src/main.rs
@@ -1,5 +1,5 @@
|
||||
mod recovery;
|
||||
mod mapping;
|
||||
mod recovery;
|
||||
|
||||
use clap::Parser;
|
||||
use libc::O_DIRECT;
|
||||
@@ -12,10 +12,8 @@ use std::{
|
||||
path::PathBuf,
|
||||
};
|
||||
|
||||
|
||||
const FB_SECTOR_SIZE: u16 = 2048;
|
||||
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
struct Args {
|
||||
/// Path to source file or block device
|
||||
@@ -43,7 +41,6 @@ struct Args {
|
||||
sector_size: u16,
|
||||
}
|
||||
|
||||
|
||||
fn main() {
|
||||
let config = Args::parse();
|
||||
|
||||
@@ -55,23 +52,16 @@ fn main() {
|
||||
match OpenOptions::new()
|
||||
.custom_flags(O_DIRECT)
|
||||
.read(true)
|
||||
.write(false)
|
||||
.append(false)
|
||||
.create(false)
|
||||
.open(&config.input.as_path())
|
||||
{
|
||||
Ok(f) => f,
|
||||
Err(err) => panic!("Failed to open input file: {:?}", err)
|
||||
Err(err) => panic!("Failed to open input file: {:?}", err),
|
||||
}
|
||||
};
|
||||
|
||||
let mut output: File = {
|
||||
// Keep this clean, make a short-lived binding.
|
||||
let path = get_path(
|
||||
&config.output,
|
||||
&config.input.to_str().unwrap(),
|
||||
"iso"
|
||||
);
|
||||
let path = get_path(&config.output, &config.input.to_str().unwrap(), "iso");
|
||||
|
||||
match OpenOptions::new()
|
||||
.custom_flags(O_DIRECT)
|
||||
@@ -81,40 +71,33 @@ fn main() {
|
||||
.open(path)
|
||||
{
|
||||
Ok(f) => f,
|
||||
Err(err) => panic!("Failed to open/create output file. {:?}", err)
|
||||
Err(err) => panic!("Failed to open/create output file. {:?}", err),
|
||||
}
|
||||
};
|
||||
|
||||
// Check if output file is shorter than input.
|
||||
// If so, autoextend the output file.
|
||||
{
|
||||
let input_len = get_stream_length(&mut input)
|
||||
.expect("Failed to get the length of the input data.");
|
||||
let output_len = get_stream_length(&mut output)
|
||||
.expect("Failed to get the length of the output file.");
|
||||
let input_len =
|
||||
get_stream_length(&mut input).expect("Failed to get the length of the input data.");
|
||||
let output_len =
|
||||
get_stream_length(&mut output).expect("Failed to get the length of the output file.");
|
||||
|
||||
if output_len < input_len {
|
||||
output.set_len(input_len)
|
||||
output
|
||||
.set_len(input_len)
|
||||
.expect("Failed to autofill output file.")
|
||||
}
|
||||
}
|
||||
|
||||
let map: MapFile = {
|
||||
let path = get_path(
|
||||
&config.output,
|
||||
&config.input.to_str().unwrap(),
|
||||
"map"
|
||||
);
|
||||
let path = get_path(&config.output, &config.input.to_str().unwrap(), "map");
|
||||
|
||||
let file = match OpenOptions::new()
|
||||
.read(true)
|
||||
.create(true)
|
||||
.open(path)
|
||||
{
|
||||
let file = match OpenOptions::new().read(true).create(true).open(path) {
|
||||
Ok(f) => f,
|
||||
Err(err) => panic!("Failed to open/create mapping file. {:?}", err)
|
||||
};
|
||||
|
||||
Err(err) => panic!("Failed to open/create mapping file. {:?}", err),
|
||||
};
|
||||
|
||||
if let Ok(map) = MapFile::try_from(file) {
|
||||
map
|
||||
} else {
|
||||
@@ -122,7 +105,7 @@ fn main() {
|
||||
}
|
||||
};
|
||||
|
||||
let mut recover_tool = Recover::new(config, input, output, map);
|
||||
let mut recover_tool = Recover::new(config, input, output, map);
|
||||
|
||||
recover_tool.run();
|
||||
|
||||
@@ -131,21 +114,13 @@ fn main() {
|
||||
|
||||
/// Generates a file path if one not provided.
|
||||
/// source_name for fallback name.
|
||||
fn get_path(
|
||||
output: &Option<PathBuf>,
|
||||
source_name: &str,
|
||||
extention: &str
|
||||
) -> PathBuf {
|
||||
fn get_path(output: &Option<PathBuf>, source_name: &str, extension: &str) -> PathBuf {
|
||||
if let Some(f) = output {
|
||||
f.to_owned()
|
||||
} else {
|
||||
PathBuf::from(format!(
|
||||
"{:?}.{}",
|
||||
source_name,
|
||||
extention,
|
||||
))
|
||||
.as_path()
|
||||
.to_owned()
|
||||
PathBuf::from(format!("{:?}.{}", source_name, extension,))
|
||||
.as_path()
|
||||
.to_owned()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -160,7 +135,6 @@ fn get_stream_length<S: Seek>(input: &mut S) -> io::Result<u64> {
|
||||
Ok(len)
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
#[allow(unused)]
|
||||
mod tests {
|
||||
@@ -173,4 +147,4 @@ mod tests {
|
||||
|
||||
// Test for get_stream_length
|
||||
// Need to determine how to test with Seek-able objects.
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user