Fixed directory deletion. Changed way of displaying shred iterations.

This commit is contained in:
Alexey Zinchenko 2021-03-06 16:52:42 +03:00
parent f12e042393
commit b694eb3724
2 changed files with 30 additions and 27 deletions

View File

@ -7,6 +7,7 @@ use std::process::exit;
use indicatif::{ProgressBar, ProgressStyle}; use indicatif::{ProgressBar, ProgressStyle};
use std::fmt::{Display, Formatter, Result}; use std::fmt::{Display, Formatter, Result};
use std::path::Path;
use walkdir::WalkDir; use walkdir::WalkDir;
const BATCH_SIZE: usize = 8192; const BATCH_SIZE: usize = 8192;
@ -47,6 +48,7 @@ impl<'a> Shredder<'a> {
} else { } else {
if self.configuration.is_recursive { if self.configuration.is_recursive {
Shredder::run_directory_shredding(&configuration, &configuration.raw_path); Shredder::run_directory_shredding(&configuration, &configuration.raw_path);
fs::remove_dir_all(Path::new(&configuration.raw_path)).unwrap();
} else { } else {
println!("Target is a directory!"); println!("Target is a directory!");
exit(1); exit(1);
@ -79,14 +81,12 @@ impl<'a> Shredder<'a> {
} }
println!("{}", absolute_path); println!("{}", absolute_path);
for iteration in 0..configuration.rewrite_iterations { <Shredder<'a>>::shred_file(
<Shredder<'a>>::shred_file( &file,
&file, file_length,
file_length, absolute_path,
absolute_path, &configuration.rewrite_iterations,
&iteration, );
);
}
if !configuration.keep_files { if !configuration.keep_files {
fs::remove_file(absolute_path).unwrap(); fs::remove_file(absolute_path).unwrap();
@ -109,37 +109,39 @@ impl<'a> Shredder<'a> {
}; };
} }
fn shred_file(file: &File, file_length: u64, absolute_path: &str, iteration: &u8) { fn shred_file(file: &File, file_length: u64, absolute_path: &str, iterations: &u8) {
let mut buffer = BufWriter::new(file); let mut buffer = BufWriter::new(file);
let mut bytes_processed = 0;
let pb = ProgressBar::new(file_length); let pb = ProgressBar::new(file_length);
pb.set_prefix(&format!("Iteration #{}", iteration + 1));
pb.set_message(absolute_path); pb.set_message(absolute_path);
pb.set_style(ProgressStyle::default_bar() pb.set_style(ProgressStyle::default_bar()
.template("{prefix} {spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {bytes}/{total_bytes} {bytes_per_sec} ({eta})") .template("{prefix} {spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {bytes}/{total_bytes} {bytes_per_sec} ({eta})")
.progress_chars("#>-")); .progress_chars("#>-"));
while bytes_processed < file_length { for i in 0..*iterations {
let bytes_to_write = if file_length - bytes_processed > BATCH_SIZE as u64 { pb.set_prefix(&format!("Iteration {}/{}", i + 1, *iterations));
BATCH_SIZE let mut bytes_processed = 0;
} else { while bytes_processed < file_length {
(file_length - bytes_processed) as usize let bytes_to_write = if file_length - bytes_processed > BATCH_SIZE as u64 {
}; BATCH_SIZE
} else {
(file_length - bytes_processed) as usize
};
let random_bytes: Vec<u8> = (0..bytes_to_write).map(|_| rand::random::<u8>()).collect(); let random_bytes: Vec<u8> =
buffer.write(&random_bytes).unwrap(); (0..bytes_to_write).map(|_| rand::random::<u8>()).collect();
buffer.write(&random_bytes).unwrap();
bytes_processed = bytes_processed + bytes_to_write as u64; bytes_processed = bytes_processed + bytes_to_write as u64;
pb.set_position(bytes_processed); pb.set_position(bytes_processed);
}
pb.finish_with_message("shredded");
buffer.flush().unwrap();
file.sync_all().unwrap();
buffer.seek(SeekFrom::Start(0)).unwrap();
} }
pb.finish_with_message("shredded");
buffer.flush().unwrap();
file.sync_all().unwrap();
buffer.seek(SeekFrom::Start(0)).unwrap();
} }
fn run_directory_shredding(configuration: &ShredConfiguration, relative_path: &str) { fn run_directory_shredding(configuration: &ShredConfiguration, relative_path: &str) {

View File

@ -43,6 +43,7 @@ fn main() {
.arg( .arg(
Arg::with_name("n") Arg::with_name("n")
.short("n") .short("n")
.takes_value(true)
.help("How many times the file must be overridden"), .help("How many times the file must be overridden"),
) )
.get_matches(); .get_matches();