Added possibility to shred files in directories recursively.

This commit is contained in:
Alexey Zinchenko 2021-01-31 02:17:13 +03:00
parent 31bfb7bf4c
commit 047d04134a
3 changed files with 75 additions and 19 deletions

30
Cargo.lock generated
View File

@ -125,6 +125,16 @@ version = "0.1.0"
dependencies = [ dependencies = [
"clap", "clap",
"rand", "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]] [[package]]
@ -154,6 +164,17 @@ version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" 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]] [[package]]
name = "wasi" name = "wasi"
version = "0.10.1+wasi-snapshot-preview1" 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" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" 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]] [[package]]
name = "winapi-x86_64-pc-windows-gnu" name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0" version = "0.4.0"

View File

@ -10,4 +10,5 @@ license-file = "LICENSE"
[dependencies] [dependencies]
clap = "2.33.3" clap = "2.33.3"
rand = "0.8.3" rand = "0.8.3"
walkdir = "2.3.1"

View File

@ -1,10 +1,12 @@
use std::cmp::Ordering; use std::cmp::Ordering;
use std::fs; use std::fs;
use std::fs::File; use std::fs::File;
use std::io::{BufWriter, Write, Seek, SeekFrom}; use std::io::{BufWriter, Seek, SeekFrom, Write};
use std::io; use std::io;
use std::process::exit; use std::process::exit;
use walkdir::WalkDir;
pub struct Shredder { pub struct Shredder {
options: ShredOptions, options: ShredOptions,
} }
@ -18,7 +20,7 @@ impl Shredder {
pub fn run(&self) { pub fn run(&self) {
let verbosity = &self.options.verbosity; 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 { match &metadata_result {
Ok(metadata) => { Ok(metadata) => {
if *verbosity >= Verbosity::Average { if *verbosity >= Verbosity::Average {
@ -37,33 +39,29 @@ impl Shredder {
} }
} }
if *verbosity > Verbosity::None { 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() { 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 { } else if self.options.is_recursive {
Shredder::shred_dir(&self.options, &self.options.raw_path);
} else { } else {
println!("Target is a directory!"); println!("Target is a directory!");
exit(1); exit(1);
} }
} }
fn shred_file(options: &ShredOptions) { fn shred_file(options: &ShredOptions, path: &str) {
match std::fs::canonicalize(&options.path) { if options.verbosity > Verbosity::Low {
println!("Trying to shred {}", path);
}
match std::fs::canonicalize(path) {
Ok(path) => { Ok(path) => {
let file_length = path.metadata().unwrap().len(); let file_length = path.metadata().unwrap().len();
let absolute_path = path.to_str().unwrap(); let absolute_path = path.to_str().unwrap();
if options.is_interactive { if options.is_interactive {
print!("Do you really want to shred '{}'? [Y/n] ", absolute_path); if !Shredder::user_prompt(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; 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 { pub struct ShredOptions {
@ -106,13 +131,13 @@ pub struct ShredOptions {
is_interactive: bool, is_interactive: bool,
rewrite_iterations: u8, rewrite_iterations: u8,
keep_files: bool, keep_files: bool,
path: String, raw_path: String,
} }
impl ShredOptions { impl ShredOptions {
pub fn new(path: String) -> ShredOptions { pub fn new(path: String) -> ShredOptions {
ShredOptions { ShredOptions {
path, raw_path: path,
is_interactive: true, is_interactive: true,
is_recursive: false, is_recursive: false,
rewrite_iterations: 3, rewrite_iterations: 3,
@ -156,7 +181,7 @@ pub enum Verbosity {
None, None,
Low, Low,
Average, Average,
High High,
} }
impl Verbosity { impl Verbosity {