Applied rustfmt.

This commit is contained in:
Alexey Zinchenko 2021-03-06 13:04:05 +03:00
parent fd24601359
commit f12e042393
4 changed files with 108 additions and 92 deletions

View File

@ -1,13 +1,13 @@
use std::cmp::Ordering; use std::cmp::Ordering;
use std::fs; use std::fs;
use std::fs::{File, Metadata}; use std::fs::{File, Metadata};
use std::io::{BufWriter, Seek, SeekFrom, Write};
use std::io; use std::io;
use std::io::{BufWriter, Seek, SeekFrom, Write};
use std::process::exit; use std::process::exit;
use walkdir::WalkDir;
use indicatif::{ProgressBar, ProgressStyle}; use indicatif::{ProgressBar, ProgressStyle};
use std::fmt::{Display, Formatter, Result}; use std::fmt::{Display, Formatter, Result};
use walkdir::WalkDir;
const BATCH_SIZE: usize = 8192; const BATCH_SIZE: usize = 8192;
@ -15,11 +15,9 @@ pub struct Shredder<'a> {
configuration: ShredConfiguration<'a>, configuration: ShredConfiguration<'a>,
} }
impl <'a>Shredder<'a> { impl<'a> Shredder<'a> {
pub fn with_options(configuration: ShredConfiguration<'a>) -> Shredder<'a> { pub fn with_options(configuration: ShredConfiguration<'a>) -> Shredder<'a> {
Shredder { Shredder { configuration }
configuration
}
} }
pub fn run(&self) { pub fn run(&self) {
@ -44,7 +42,6 @@ impl <'a>Shredder<'a> {
println!("Is file: {}", metadata.is_file()); println!("Is file: {}", metadata.is_file());
} }
if metadata.is_file() { if metadata.is_file() {
Shredder::run_file_shredding(&configuration, &configuration.raw_path, metadata); Shredder::run_file_shredding(&configuration, &configuration.raw_path, metadata);
} else { } else {
@ -57,7 +54,11 @@ impl <'a>Shredder<'a> {
} }
} }
fn run_file_shredding(configuration: &ShredConfiguration, relative_path: &str, metadata: Metadata) -> bool { fn run_file_shredding(
configuration: &ShredConfiguration,
relative_path: &str,
metadata: Metadata,
) -> bool {
if configuration.verbosity > Verbosity::Low { if configuration.verbosity > Verbosity::Low {
println!("Trying to shred {}", relative_path); println!("Trying to shred {}", relative_path);
} }
@ -79,7 +80,12 @@ impl <'a>Shredder<'a> {
println!("{}", absolute_path); println!("{}", absolute_path);
for iteration in 0..configuration.rewrite_iterations { for iteration in 0..configuration.rewrite_iterations {
<Shredder<'a>>::shred_file(&file, file_length, absolute_path, &iteration); <Shredder<'a>>::shred_file(
&file,
file_length,
absolute_path,
&iteration,
);
} }
if !configuration.keep_files { if !configuration.keep_files {
@ -100,7 +106,7 @@ impl <'a>Shredder<'a> {
println!("{}", error); println!("{}", error);
false false
} }
} };
} }
fn shred_file(file: &File, file_length: u64, absolute_path: &str, iteration: &u8) { fn shred_file(file: &File, file_length: u64, absolute_path: &str, iteration: &u8) {
@ -122,9 +128,7 @@ impl <'a>Shredder<'a> {
(file_length - bytes_processed) as usize (file_length - bytes_processed) as usize
}; };
let random_bytes: Vec<u8> = (0..bytes_to_write).map(|_| { let random_bytes: Vec<u8> = (0..bytes_to_write).map(|_| rand::random::<u8>()).collect();
rand::random::<u8>()
}).collect();
buffer.write(&random_bytes).unwrap(); buffer.write(&random_bytes).unwrap();
bytes_processed = bytes_processed + bytes_to_write as u64; bytes_processed = bytes_processed + bytes_to_write as u64;
@ -140,10 +144,17 @@ impl <'a>Shredder<'a> {
fn run_directory_shredding(configuration: &ShredConfiguration, relative_path: &str) { fn run_directory_shredding(configuration: &ShredConfiguration, relative_path: &str) {
let mut files_count = 0; let mut files_count = 0;
for entry in WalkDir::new(relative_path).into_iter().filter_map(|e| e.ok()) { for entry in WalkDir::new(relative_path)
.into_iter()
.filter_map(|e| e.ok())
{
if entry.metadata().unwrap().is_file() { if entry.metadata().unwrap().is_file() {
let file_path = entry.path().to_str().unwrap(); let file_path = entry.path().to_str().unwrap();
if Shredder::run_file_shredding(configuration, file_path, fs::metadata(file_path).unwrap()) { if Shredder::run_file_shredding(
configuration,
file_path,
fs::metadata(file_path).unwrap(),
) {
files_count = files_count + 1; files_count = files_count + 1;
} }
} }
@ -158,7 +169,9 @@ impl <'a>Shredder<'a> {
io::stdout().flush().unwrap(); io::stdout().flush().unwrap();
let mut input = String::new(); let mut input = String::new();
io::stdin().read_line(&mut input).expect("Failed to read input."); io::stdin()
.read_line(&mut input)
.expect("Failed to read input.");
let input = input.trim(); let input = input.trim();
if input.len() != 1 || !input.to_lowercase().eq("y") { if input.len() != 1 || !input.to_lowercase().eq("y") {
@ -225,7 +238,11 @@ impl<'a> Display for ShredConfiguration<'a> {
writeln!(f, "{}", format!("Verbosity: {}", self.verbosity))?; writeln!(f, "{}", format!("Verbosity: {}", self.verbosity))?;
writeln!(f, "{}", format!("Is recursive: {}", self.is_recursive))?; writeln!(f, "{}", format!("Is recursive: {}", self.is_recursive))?;
writeln!(f, "{}", format!("Is interactive: {}", self.is_interactive))?; writeln!(f, "{}", format!("Is interactive: {}", self.is_interactive))?;
writeln!(f, "{}", format!("Rewrite iterations: {}", self.rewrite_iterations))?; writeln!(
f,
"{}",
format!("Rewrite iterations: {}", self.rewrite_iterations)
)?;
writeln!(f, "{}", format!("Keep files: {}", self.keep_files))?; writeln!(f, "{}", format!("Keep files: {}", self.keep_files))?;
writeln!(f, "{}", format!("Path: {}", self.raw_path))?; writeln!(f, "{}", format!("Path: {}", self.raw_path))?;
@ -273,18 +290,10 @@ impl PartialEq for Verbosity {
impl Display for Verbosity { impl Display for Verbosity {
fn fmt(&self, f: &mut Formatter<'_>) -> Result { fn fmt(&self, f: &mut Formatter<'_>) -> Result {
let string_value = match self { let string_value = match self {
Verbosity::None => { Verbosity::None => "None",
"None" Verbosity::Low => "Low",
} Verbosity::Average => "Average",
Verbosity::Low => { Verbosity::High => "High",
"Low"
}
Verbosity::Average => {
"Average"
}
Verbosity::High => {
"High"
}
}; };
write!(f, "{}", string_value)?; write!(f, "{}", string_value)?;
Ok(()) Ok(())

View File

@ -1,38 +1,49 @@
extern crate clap; extern crate clap;
use clap::{App, Arg, crate_authors, crate_name, crate_version}; use clap::{crate_authors, crate_name, crate_version, App, Arg};
use rshred::{ShredConfiguration, Shredder, Verbosity};
use std::process::exit; use std::process::exit;
use std::str::FromStr; use std::str::FromStr;
use rshred::{Verbosity, Shredder, ShredConfiguration};
fn main() { fn main() {
let params = App::new(crate_name!()) let params = App::new(crate_name!())
.version(crate_version!()) .version(crate_version!())
.author(crate_authors!()) .author(crate_authors!())
.about("TODO") .about("TODO")
.arg(Arg::with_name("PATH") .arg(
.help("Sets the path of file or directory to use") Arg::with_name("PATH")
.required(true) .help("Sets the path of file or directory to use")
.index(1)) .required(true)
.arg(Arg::with_name("v") .index(1),
.short("v") )
.multiple(true) .arg(
.help("Sets the level of verbosity")) Arg::with_name("v")
.arg(Arg::with_name("r") .short("v")
.short("r") .multiple(true)
.help("Do shredding operations recursively")) .help("Sets the level of verbosity"),
.arg(Arg::with_name("i") )
.short("i") .arg(
.long("interactive") Arg::with_name("r")
.help("Enables interactive mode")) .short("r")
.arg(Arg::with_name("k") .help("Do shredding operations recursively"),
.short("k") )
.long("keep") .arg(
.help("Don't delete files after shredding")) Arg::with_name("i")
.arg(Arg::with_name("n") .short("i")
.short("n") .long("interactive")
.help("How many times the file must be overridden") .help("Enables interactive mode"),
)
.arg(
Arg::with_name("k")
.short("k")
.long("keep")
.help("Don't delete files after shredding"),
)
.arg(
Arg::with_name("n")
.short("n")
.help("How many times the file must be overridden"),
) )
.get_matches(); .get_matches();
@ -40,7 +51,7 @@ fn main() {
1 => Verbosity::Low, 1 => Verbosity::Low,
2 => Verbosity::Average, 2 => Verbosity::Average,
3 => Verbosity::High, 3 => Verbosity::High,
_ => Verbosity::None _ => Verbosity::None,
}; };
let is_recursively = params.is_present("r"); let is_recursively = params.is_present("r");
@ -53,17 +64,13 @@ fn main() {
println!("No argument passed to the 'n' option!"); println!("No argument passed to the 'n' option!");
exit(1); exit(1);
} }
Some(value) => { Some(value) => match u8::from_str(value) {
match u8::from_str(value) { Ok(number) => number,
Ok(number) => { Err(error) => {
number println!("{}", error.to_string());
} exit(1);
Err(error) => {
println!("{}", error.to_string());
exit(1);
}
} }
} },
} }
} else { } else {
3 3
@ -80,6 +87,7 @@ fn main() {
.set_keep_files(keep_files) .set_keep_files(keep_files)
.set_verbosity(verbosity) .set_verbosity(verbosity)
.set_rewrite_iterations(iterations_count) .set_rewrite_iterations(iterations_count)
.build() .build(),
).run(); )
.run();
} }

View File

@ -26,7 +26,10 @@ pub fn setup(data_type: TestDataType) -> EnvironmentDetails {
EnvironmentDetails::single(tmp_file_path) EnvironmentDetails::single(tmp_file_path)
} }
TestDataType::MultipleFiles(files) => { TestDataType::MultipleFiles(files) => {
let files = files.iter().map(|file| format!("{}/{}", TEST_DIR, file)).collect::<Vec<String>>(); let files = files
.iter()
.map(|file| format!("{}/{}", TEST_DIR, file))
.collect::<Vec<String>>();
for file in files.iter() { for file in files.iter() {
let path = std::path::Path::new(&file); let path = std::path::Path::new(&file);
let directory = path.parent().unwrap(); let directory = path.parent().unwrap();
@ -65,13 +68,11 @@ pub fn cleanup(env_details: EnvironmentDetails) {
std::fs::remove_file(&filename).unwrap(); std::fs::remove_file(&filename).unwrap();
} }
} }
EnvironmentDetails::Multiple(files) => { EnvironmentDetails::Multiple(files) => files.iter().for_each(|file| {
files.iter().for_each(|file| { if std::path::Path::new(file).exists() {
if std::path::Path::new(file).exists() { std::fs::remove_file(file).unwrap();
std::fs::remove_file(file).unwrap(); }
} }),
})
}
} }
} }

View File

@ -57,21 +57,19 @@ fn shredding_with_file_deletion() {
#[test] #[test]
#[timeout(10000)] #[timeout(10000)]
fn shredding_directory_recursively() { fn shredding_directory_recursively() {
let env_details = common::setup(common::TestDataType::MultipleFiles( let env_details = common::setup(common::TestDataType::MultipleFiles(vec![
vec![ String::from("test3/subdir1/1.txt"),
String::from("test3/subdir1/1.txt"), String::from("test3/subdir1/2.txt"),
String::from("test3/subdir1/2.txt"), String::from("test3/subdir1/3.txt"),
String::from("test3/subdir1/3.txt"), String::from("test3/subdir1/subdir11/1.txt"),
String::from("test3/subdir1/subdir11/1.txt"), String::from("test3/subdir1/subdir11/2.txt"),
String::from("test3/subdir1/subdir11/2.txt"), String::from("test3/subdir2/subdir1/111.txt"),
String::from("test3/subdir2/subdir1/111.txt"), String::from("test3/subdir3/1231.txt"),
String::from("test3/subdir3/1231.txt"), String::from("test3/subdir3/1222.txt"),
String::from("test3/subdir3/1222.txt"), String::from("test3/subdir3/1286.txt"),
String::from("test3/subdir3/1286.txt"), String::from("test3/subdir3/1286/anotherdir/abs.txt"),
String::from("test3/subdir3/1286/anotherdir/abs.txt"), String::from("test3/subdir3/1286/anotherdir/abc.txt"),
String::from("test3/subdir3/1286/anotherdir/abc.txt"), ]));
]
));
match &env_details { match &env_details {
common::EnvironmentDetails::Single(_) => { common::EnvironmentDetails::Single(_) => {