Skip to content

Commit 477431c

Browse files
authored
added dry run skipping (#13)
1 parent f94ed19 commit 477431c

File tree

2 files changed

+33
-23
lines changed

2 files changed

+33
-23
lines changed

Diff for: src/cli.rs

+26-21
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub struct Cli {
1616
Outputs the contents of MFT found in the root of the volume and calculates what will be restored.
1717
"#
1818
)]
19-
dry_run: bool,
19+
pub dry_run: bool,
2020

2121
#[arg(
2222
long,
@@ -25,7 +25,7 @@ Outputs the contents of MFT found in the root of the volume and calculates what
2525
The path to which the restored files will be written.
2626
"#
2727
)]
28-
output_dir: PathBuf,
28+
pub output_dir: PathBuf,
2929

3030
#[arg(
3131
long,
@@ -34,39 +34,44 @@ The path to which the restored files will be written.
3434
The path to the image of file system to be restored that was retrieved with dd.
3535
"#
3636
)]
37-
image: PathBuf,
38-
}
39-
40-
pub struct CliParsed {
41-
pub dry_run: bool,
42-
pub output_dir: PathBuf,
4337
pub image: PathBuf,
4438
}
4539

46-
impl CliParsed {
40+
impl Cli {
4741
pub fn display(&self) {
4842
info!("Configured with dry-run: {}", self.dry_run);
4943
info!("Configured with output-dir: {}", self.output_dir.display());
50-
info!("Configured with volume: {}", self.image.display());
44+
info!("Configured with image: {}", self.image.display());
5145
}
5246

53-
pub fn parse_and_validate(args: Cli) -> Result<Self, UndeleteError> {
54-
if !args.image.exists() {
47+
pub fn parse_and_validate(self) -> Result<Self, UndeleteError> {
48+
if !self.image.exists() {
5549
return Err(UndeleteError::Parse(
56-
"Specified volume is Non-existant!".to_string(),
50+
"Specified image is Non-existant!".to_string(),
5751
));
5852
}
5953

60-
Ok(args.into())
54+
if !self.output_dir.exists() && !self.dry_run {
55+
return Err(UndeleteError::Parse(
56+
"Specified output directory is Non-existant!".to_string(),
57+
));
58+
}
59+
60+
Ok(self)
6161
}
6262
}
6363

64-
impl From<Cli> for CliParsed {
65-
fn from(value: Cli) -> Self {
66-
CliParsed {
67-
dry_run: value.dry_run,
68-
output_dir: value.output_dir,
69-
image: value.image,
70-
}
64+
#[cfg(test)]
65+
mod tests {
66+
use super::*;
67+
68+
#[test]
69+
fn test_parse_and_validate_non_existant_image() {
70+
let args = Cli::parse_and_validate(Cli {
71+
dry_run: false,
72+
image: PathBuf::from("non-existant/ntfs.dd"),
73+
output_dir: PathBuf::from("src"),
74+
});
75+
assert!(args.is_err());
7176
}
7277
}

Diff for: src/main.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::io::Read;
22

3-
use crate::cli::Cli;
43
use clap::Parser;
4+
use cli::Cli;
55
use dialoguer::MultiSelect;
66
use errors::UndeleteError;
77
use log::info;
@@ -19,7 +19,7 @@ fn main() -> Result<(), UndeleteError> {
1919
.filter_level(log::LevelFilter::Info)
2020
.init();
2121

22-
let args = cli::CliParsed::parse_and_validate(Cli::parse())?;
22+
let args = cli::Cli::parse_and_validate(Cli::parse())?;
2323
args.display();
2424

2525
let image_info = tsk::TskImg::from_utf8_sing(args.image.as_path()).map_err(|err| {
@@ -79,6 +79,11 @@ fn main() -> Result<(), UndeleteError> {
7979
))
8080
})?;
8181

82+
if args.dry_run {
83+
info!("Would write '{}' to disk", new_path.display());
84+
continue;
85+
}
86+
8287
if std::fs::metadata(new_path.parent().unwrap()).is_err() {
8388
std::fs::create_dir_all(new_path.parent().unwrap()).map_err(|err| {
8489
UndeleteError::General(format!(

0 commit comments

Comments
 (0)