Cleanup and renaming of bindings.

This commit is contained in:
Cutieguwu
2025-02-20 19:13:58 -05:00
parent c8a98ea3cb
commit 85dbcc5618
3 changed files with 67 additions and 67 deletions

View File

@@ -20,15 +20,15 @@ const FB_SECTOR_SIZE: u16 = 2048;
struct Args {
/// Path to source file or block device
#[arg(short, long, value_hint = clap::ValueHint::DirPath)]
input_file: PathBuf,
input: PathBuf,
/// Path to output file. Defaults to {input_file}.iso
/// Path to output file. Defaults to {input}.iso
#[arg(short, long, value_hint = clap::ValueHint::DirPath)]
output_file: Option<PathBuf>,
output: Option<PathBuf>,
/// Path to rescue map. Defaults to {input_file}.map
/// Path to rescue map. Defaults to {input}.map
#[arg(short, long, value_hint = clap::ValueHint::DirPath)]
map_file: Option<PathBuf>,
map: Option<PathBuf>,
/// Max number of consecutive sectors to test as a group
#[arg(short, long, default_value_t = 128)]
@@ -51,25 +51,25 @@ fn main() {
// I'm lazy and don't want to mess around with comparing error types.
// Thus, any error in I/O here should be treated as fatal.
let mut input_file: File = {
let mut input: File = {
match OpenOptions::new()
.custom_flags(O_DIRECT)
.read(true)
.write(false)
.append(false)
.create(false)
.open(&config.input_file.as_path())
.open(&config.input.as_path())
{
Ok(f) => f,
Err(err) => panic!("Failed to open input file: {:?}", err)
}
};
let mut output_file: File = {
let mut output: File = {
// Keep this clean, make a short-lived binding.
let path = get_path(
&config.output_file,
&config.input_file.to_str().unwrap(),
&config.output,
&config.input.to_str().unwrap(),
"iso"
);
@@ -88,21 +88,21 @@ fn main() {
// Check if output file is shorter than input.
// If so, autoextend the output file.
{
let input_len = get_stream_length(&mut input_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_file)
let output_len = get_stream_length(&mut output)
.expect("Failed to get the length of the output file.");
if output_len < input_len {
output_file.set_len(input_len)
output.set_len(input_len)
.expect("Failed to autofill output file.")
}
}
let map: MapFile = {
let path = get_path(
&config.output_file,
&config.input_file.to_str().unwrap(),
&config.output,
&config.input.to_str().unwrap(),
"map"
);
@@ -122,7 +122,7 @@ fn main() {
}
};
let recover_tool = Recover::new(config, input_file, output_file, map);
let recover_tool = Recover::new(config, input, output, map);
recover_tool.run_full();
@@ -130,18 +130,18 @@ fn main() {
}
/// Generates a file path if one not provided.
/// source_file for fallback name.
/// source_name for fallback name.
fn get_path(
output_file: &Option<PathBuf>,
source_file: &str,
output: &Option<PathBuf>,
source_name: &str,
extention: &str
) -> PathBuf {
if let Some(f) = output_file {
if let Some(f) = output {
f.to_owned()
} else {
PathBuf::from(format!(
"{:?}.{}",
source_file,
source_name,
extention,
))
.as_path()

View File

@@ -27,12 +27,11 @@ impl Domain {
}
/// A map for data stored in memory for processing and saving to disk.
#[allow(unused)]
#[derive(Clone, Debug, Deserialize)]
pub struct Cluster {
data: Option<Vec<u8>>,
domain: Domain,
status: Status,
stage: Stage,
}
impl Default for Cluster {
@@ -40,7 +39,7 @@ impl Default for Cluster {
Cluster {
data: None,
domain: Domain::default(),
status: Status::default()
stage: Stage::default()
}
}
}
@@ -52,12 +51,12 @@ impl Default for Cluster {
#[derive(Clone, Copy, Debug, Deserialize)]
pub struct MapCluster {
pub domain: Domain,
pub status: Status,
pub stage: Stage,
}
impl Default for MapCluster {
fn default() -> Self {
MapCluster { domain: Domain::default(), status: Status::default() }
MapCluster { domain: Domain::default(), stage: Stage::default() }
}
}
@@ -65,27 +64,26 @@ impl From<Cluster> for MapCluster {
fn from(cluster: Cluster) -> Self {
MapCluster {
domain: cluster.domain,
status: cluster.status,
stage: cluster.stage,
}
}
}
#[derive(Clone, Copy, Debug, Deserialize, PartialEq, PartialOrd)]
pub enum Status {
pub enum Stage {
Untested,
ForIsolation(u8),
Damaged,
}
impl Default for Status {
impl Default for Stage {
fn default() -> Self {
Status::Untested
Stage::Untested
}
}
#[allow(unused)]
#[derive(Clone, Debug, Deserialize)]
pub struct MapFile {
pub sector_size: u16,
@@ -108,13 +106,12 @@ impl Default for MapFile {
domain: Domain::default(),
map: vec![MapCluster {
domain: Domain::default(),
status: Status::Untested,
stage: Stage::Untested,
}],
}
}
}
#[allow(dead_code)]
impl MapFile {
pub fn new(sector_size: u16) -> Self {
MapFile::default()
@@ -128,13 +125,13 @@ impl MapFile {
}
/// Recalculate cluster mappings.
fn update(self, new_cluster: Cluster) {
fn update(&mut self, new_cluster: Cluster) {
let mut new_map: Vec<MapCluster> = vec![MapCluster::from(new_cluster.to_owned())];
for map_cluster in self.map.iter() {
let mut map_cluster = *map_cluster;
// If new_cluster doesn't start ahead and end short, map_cluster is forgotten.
// If new_cluster doesn't start ahead and ends short, map_cluster is forgotten.
if new_cluster.domain.start < map_cluster.domain.start
&& new_cluster.domain.end < map_cluster.domain.end {
/*
@@ -173,7 +170,7 @@ impl MapFile {
start: new_cluster.domain.end,
end: domain_end,
},
status: map_cluster.status.to_owned()
stage: map_cluster.stage.to_owned()
});
}
} else {
@@ -186,44 +183,46 @@ impl MapFile {
new_map.push(map_cluster);
}
}
self.map = new_map;
}
/// Get current recovery status.
pub fn get_state(self) -> Status {
let mut recover_status = Status::Damaged;
/// Get current recovery stage.
pub fn get_stage(&self) -> Stage {
let mut recover_stage = Stage::Damaged;
for cluster in self.map {
match cluster.status {
Status::Untested => return Status::Untested,
Status::ForIsolation(_) => {
if recover_status == Status::Damaged
|| cluster.status < recover_status {
// Note that recover_status after first condition is
// only ever Status::ForIsolation(_), thus PartialEq,
for cluster in self.map.iter() {
match cluster.stage {
Stage::Untested => return Stage::Untested,
Stage::ForIsolation(_) => {
if recover_stage == Stage::Damaged
|| cluster.stage < recover_stage {
// Note that recover_stage after first condition is
// only ever Stage::ForIsolation(_), thus PartialEq,
// PartialOrd are useful for comparing the internal value.
recover_status = cluster.status
recover_stage = cluster.stage
}
},
Status::Damaged => (),
Stage::Damaged => (),
}
}
recover_status
recover_stage
}
/// Get clusters of common status.
pub fn get_clusters(self, state: Status) -> Vec<MapCluster> {
/// Get clusters of common stage.
pub fn get_clusters(self, stage: Stage) -> Vec<MapCluster> {
self.map.iter()
.filter_map(|mc| {
if mc.status == state { Some(mc.to_owned()) } else { None }
if mc.stage == stage { Some(mc.to_owned()) } else { None }
})
.collect()
}
/// Defragments cluster groups.
/// I.E. check forwards every cluster from current until status changes,
/// I.E. check forwards every cluster from current until stage changes,
/// then group at once.
fn defrag(&mut self) {
fn defrag(&mut self) -> &mut Self {
let mut new_map: Vec<MapCluster> = vec![];
let mut pos: usize = 0;
@@ -237,33 +236,34 @@ impl MapFile {
let mut end_cluster = MapCluster::default();
let mut new_cluster: MapCluster;
let mut status_common: bool;
let mut stage_common: bool;
while pos < self.domain.end {
status_common = true;
stage_common = true;
// Start a new cluster based on the cluster following
// the end of last new_cluster.
new_cluster = start_cluster;
// While status is common, find each trailing cluster.
while status_common {
// start_cluster was of common status to end_cluster.
// While stage is common, find each trailing cluster.
while stage_common {
// start_cluster was of common stage to end_cluster.
end_cluster = start_cluster;
start_cluster = *self.map.iter()
.find(|c| end_cluster.domain.end == c.domain.start)
.unwrap();
status_common = new_cluster.status == start_cluster.status
stage_common = new_cluster.stage == start_cluster.stage
}
// Set the new ending, encapsulating any clusters of common status.
// Set the new ending, encapsulating any clusters of common stage.
new_cluster.domain.end = end_cluster.domain.end;
pos = new_cluster.domain.end;
new_map.push(new_cluster);
}
self.map = new_map;
self
}
}

View File

@@ -5,11 +5,10 @@ use std::{
use crate::{
Args,
mapping::{MapFile, Status},
mapping::{MapFile, Stage},
};
#[allow(unused)]
#[derive(Debug)]
pub struct Recover {
buf_capacity: usize,
@@ -17,12 +16,13 @@ pub struct Recover {
input: BufReader<File>,
output: BufWriter<File>,
map: MapFile,
stage: Status,
stage: Stage,
}
#[allow(dead_code)]
impl Recover {
pub fn new(config: Args, input: File, output: File, map: MapFile) -> Self {
let stage = map.get_stage();
// Temporarily make buffer length one sector.
let buf_capacity = config.sector_size as usize;
let mut r = Recover {
@@ -37,7 +37,7 @@ impl Recover {
output,
),
map,
stage: Status::Untested,
stage: stage,
};
// Ensure that buffer capacity is adjusted based on progress.