diff --git a/Cargo.lock b/Cargo.lock index 64d8dd8..eba5bc6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -125,6 +125,16 @@ version = "0.1.0" dependencies = [ "clap", "rand", + "walkdir", +] + +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", ] [[package]] @@ -154,6 +164,17 @@ version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" +[[package]] +name = "walkdir" +version = "2.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "777182bc735b6424e1a57516d35ed72cb8019d85c8c9bf536dccb3445c1a2f7d" +dependencies = [ + "same-file", + "winapi", + "winapi-util", +] + [[package]] name = "wasi" version = "0.10.1+wasi-snapshot-preview1" @@ -176,6 +197,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index 5648fec..26349fd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,4 +10,5 @@ license-file = "LICENSE" [dependencies] clap = "2.33.3" -rand = "0.8.3" \ No newline at end of file +rand = "0.8.3" +walkdir = "2.3.1" \ No newline at end of file diff --git a/src/shred/mod.rs b/src/shred/mod.rs index d09f8a5..83c3556 100644 --- a/src/shred/mod.rs +++ b/src/shred/mod.rs @@ -1,10 +1,12 @@ use std::cmp::Ordering; use std::fs; use std::fs::File; -use std::io::{BufWriter, Write, Seek, SeekFrom}; +use std::io::{BufWriter, Seek, SeekFrom, Write}; use std::io; use std::process::exit; +use walkdir::WalkDir; + pub struct Shredder { options: ShredOptions, } @@ -18,7 +20,7 @@ impl Shredder { pub fn run(&self) { let verbosity = &self.options.verbosity; - let metadata_result = fs::metadata(&self.options.path); + let metadata_result = fs::metadata(&self.options.raw_path); match &metadata_result { Ok(metadata) => { if *verbosity >= Verbosity::Average { @@ -37,33 +39,29 @@ impl Shredder { } } if *verbosity > Verbosity::None { - println!("Using input file: {}", &self.options.path); + println!("Using input file: {}", &self.options.raw_path); } if metadata_result.unwrap().is_file() { - Shredder::shred_file(&self.options); + Shredder::shred_file(&self.options, &self.options.raw_path); } else if self.options.is_recursive { - + Shredder::shred_dir(&self.options, &self.options.raw_path); } else { println!("Target is a directory!"); exit(1); } } - fn shred_file(options: &ShredOptions) { - match std::fs::canonicalize(&options.path) { + fn shred_file(options: &ShredOptions, path: &str) { + if options.verbosity > Verbosity::Low { + println!("Trying to shred {}", path); + } + match std::fs::canonicalize(path) { Ok(path) => { let file_length = path.metadata().unwrap().len(); let absolute_path = path.to_str().unwrap(); if options.is_interactive { - print!("Do you really want to shred '{}'? [Y/n] ", absolute_path); - io::stdout().flush().unwrap(); - - let mut input = String::new(); - io::stdin().read_line(&mut input).expect("Failed to read input."); - let input = input.trim(); - - if input.len() != 1 || !input.to_lowercase().eq("y") { + if !Shredder::user_prompt(absolute_path) { return; } } @@ -98,6 +96,33 @@ impl Shredder { } } } + + fn shred_dir(options: &ShredOptions, dir: &str) { + let mut files_count = 0; + for entry in WalkDir::new(dir).into_iter().filter_map(|e| e.ok()) { + if entry.metadata().unwrap().is_file() { + Shredder::shred_file(options, entry.path().to_str().unwrap()); + files_count = files_count + 1; + } + } + if options.verbosity != Verbosity::None { + println!("Processed {} files.", files_count); + } + } + + fn user_prompt(path: &str) -> bool { + print!("Do you really want to shred '{}'? [Y/n] ", absolute_path); + io::stdout().flush().unwrap(); + + let mut input = String::new(); + io::stdin().read_line(&mut input).expect("Failed to read input."); + let input = input.trim(); + + if input.len() != 1 || !input.to_lowercase().eq("y") { + return false; + } + true + } } pub struct ShredOptions { @@ -106,13 +131,13 @@ pub struct ShredOptions { is_interactive: bool, rewrite_iterations: u8, keep_files: bool, - path: String, + raw_path: String, } impl ShredOptions { pub fn new(path: String) -> ShredOptions { ShredOptions { - path, + raw_path: path, is_interactive: true, is_recursive: false, rewrite_iterations: 3, @@ -156,7 +181,7 @@ pub enum Verbosity { None, Low, Average, - High + High, } impl Verbosity {