mirror of
https://github.com/Prominence/rshred.git
synced 2026-01-08 09:46:46 +03:00
Applied rustfmt.
This commit is contained in:
parent
fd24601359
commit
f12e042393
69
src/lib.rs
69
src/lib.rs
@ -1,13 +1,13 @@
|
||||
use std::cmp::Ordering;
|
||||
use std::fs;
|
||||
use std::fs::{File, Metadata};
|
||||
use std::io::{BufWriter, Seek, SeekFrom, Write};
|
||||
use std::io;
|
||||
use std::io::{BufWriter, Seek, SeekFrom, Write};
|
||||
use std::process::exit;
|
||||
|
||||
use walkdir::WalkDir;
|
||||
use indicatif::{ProgressBar, ProgressStyle};
|
||||
use std::fmt::{Display, Formatter, Result};
|
||||
use walkdir::WalkDir;
|
||||
|
||||
const BATCH_SIZE: usize = 8192;
|
||||
|
||||
@ -15,11 +15,9 @@ pub struct Shredder<'a> {
|
||||
configuration: ShredConfiguration<'a>,
|
||||
}
|
||||
|
||||
impl <'a>Shredder<'a> {
|
||||
impl<'a> Shredder<'a> {
|
||||
pub fn with_options(configuration: ShredConfiguration<'a>) -> Shredder<'a> {
|
||||
Shredder {
|
||||
configuration
|
||||
}
|
||||
Shredder { configuration }
|
||||
}
|
||||
|
||||
pub fn run(&self) {
|
||||
@ -44,7 +42,6 @@ impl <'a>Shredder<'a> {
|
||||
println!("Is file: {}", metadata.is_file());
|
||||
}
|
||||
|
||||
|
||||
if metadata.is_file() {
|
||||
Shredder::run_file_shredding(&configuration, &configuration.raw_path, metadata);
|
||||
} 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 {
|
||||
println!("Trying to shred {}", relative_path);
|
||||
}
|
||||
@ -79,7 +80,12 @@ impl <'a>Shredder<'a> {
|
||||
|
||||
println!("{}", absolute_path);
|
||||
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 {
|
||||
@ -100,7 +106,7 @@ impl <'a>Shredder<'a> {
|
||||
println!("{}", error);
|
||||
false
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
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
|
||||
};
|
||||
|
||||
let random_bytes: Vec<u8> = (0..bytes_to_write).map(|_| {
|
||||
rand::random::<u8>()
|
||||
}).collect();
|
||||
let random_bytes: Vec<u8> = (0..bytes_to_write).map(|_| rand::random::<u8>()).collect();
|
||||
buffer.write(&random_bytes).unwrap();
|
||||
|
||||
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) {
|
||||
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() {
|
||||
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;
|
||||
}
|
||||
}
|
||||
@ -158,7 +169,9 @@ impl <'a>Shredder<'a> {
|
||||
io::stdout().flush().unwrap();
|
||||
|
||||
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();
|
||||
|
||||
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!("Is recursive: {}", self.is_recursive))?;
|
||||
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!("Path: {}", self.raw_path))?;
|
||||
|
||||
@ -273,18 +290,10 @@ impl PartialEq for Verbosity {
|
||||
impl Display for Verbosity {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
|
||||
let string_value = match self {
|
||||
Verbosity::None => {
|
||||
"None"
|
||||
}
|
||||
Verbosity::Low => {
|
||||
"Low"
|
||||
}
|
||||
Verbosity::Average => {
|
||||
"Average"
|
||||
}
|
||||
Verbosity::High => {
|
||||
"High"
|
||||
}
|
||||
Verbosity::None => "None",
|
||||
Verbosity::Low => "Low",
|
||||
Verbosity::Average => "Average",
|
||||
Verbosity::High => "High",
|
||||
};
|
||||
write!(f, "{}", string_value)?;
|
||||
Ok(())
|
||||
@ -303,4 +312,4 @@ mod tests {
|
||||
assert!(Verbosity::Average < Verbosity::High, true);
|
||||
assert!(Verbosity::None < Verbosity::High, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
84
src/main.rs
84
src/main.rs
@ -1,38 +1,49 @@
|
||||
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::str::FromStr;
|
||||
use rshred::{Verbosity, Shredder, ShredConfiguration};
|
||||
|
||||
fn main() {
|
||||
let params = App::new(crate_name!())
|
||||
.version(crate_version!())
|
||||
.author(crate_authors!())
|
||||
.about("TODO")
|
||||
.arg(Arg::with_name("PATH")
|
||||
.help("Sets the path of file or directory to use")
|
||||
.required(true)
|
||||
.index(1))
|
||||
.arg(Arg::with_name("v")
|
||||
.short("v")
|
||||
.multiple(true)
|
||||
.help("Sets the level of verbosity"))
|
||||
.arg(Arg::with_name("r")
|
||||
.short("r")
|
||||
.help("Do shredding operations recursively"))
|
||||
.arg(Arg::with_name("i")
|
||||
.short("i")
|
||||
.long("interactive")
|
||||
.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")
|
||||
.arg(
|
||||
Arg::with_name("PATH")
|
||||
.help("Sets the path of file or directory to use")
|
||||
.required(true)
|
||||
.index(1),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("v")
|
||||
.short("v")
|
||||
.multiple(true)
|
||||
.help("Sets the level of verbosity"),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("r")
|
||||
.short("r")
|
||||
.help("Do shredding operations recursively"),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("i")
|
||||
.short("i")
|
||||
.long("interactive")
|
||||
.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();
|
||||
|
||||
@ -40,7 +51,7 @@ fn main() {
|
||||
1 => Verbosity::Low,
|
||||
2 => Verbosity::Average,
|
||||
3 => Verbosity::High,
|
||||
_ => Verbosity::None
|
||||
_ => Verbosity::None,
|
||||
};
|
||||
|
||||
let is_recursively = params.is_present("r");
|
||||
@ -53,17 +64,13 @@ fn main() {
|
||||
println!("No argument passed to the 'n' option!");
|
||||
exit(1);
|
||||
}
|
||||
Some(value) => {
|
||||
match u8::from_str(value) {
|
||||
Ok(number) => {
|
||||
number
|
||||
}
|
||||
Err(error) => {
|
||||
println!("{}", error.to_string());
|
||||
exit(1);
|
||||
}
|
||||
Some(value) => match u8::from_str(value) {
|
||||
Ok(number) => number,
|
||||
Err(error) => {
|
||||
println!("{}", error.to_string());
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
} else {
|
||||
3
|
||||
@ -80,6 +87,7 @@ fn main() {
|
||||
.set_keep_files(keep_files)
|
||||
.set_verbosity(verbosity)
|
||||
.set_rewrite_iterations(iterations_count)
|
||||
.build()
|
||||
).run();
|
||||
}
|
||||
.build(),
|
||||
)
|
||||
.run();
|
||||
}
|
||||
|
||||
@ -26,7 +26,10 @@ pub fn setup(data_type: TestDataType) -> EnvironmentDetails {
|
||||
EnvironmentDetails::single(tmp_file_path)
|
||||
}
|
||||
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() {
|
||||
let path = std::path::Path::new(&file);
|
||||
let directory = path.parent().unwrap();
|
||||
@ -65,13 +68,11 @@ pub fn cleanup(env_details: EnvironmentDetails) {
|
||||
std::fs::remove_file(&filename).unwrap();
|
||||
}
|
||||
}
|
||||
EnvironmentDetails::Multiple(files) => {
|
||||
files.iter().for_each(|file| {
|
||||
if std::path::Path::new(file).exists() {
|
||||
std::fs::remove_file(file).unwrap();
|
||||
}
|
||||
})
|
||||
}
|
||||
EnvironmentDetails::Multiple(files) => files.iter().for_each(|file| {
|
||||
if std::path::Path::new(file).exists() {
|
||||
std::fs::remove_file(file).unwrap();
|
||||
}
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
@ -92,4 +93,4 @@ impl EnvironmentDetails {
|
||||
pub enum TestDataType {
|
||||
RandomSingleFile,
|
||||
MultipleFiles(Vec<String>),
|
||||
}
|
||||
}
|
||||
|
||||
@ -57,21 +57,19 @@ fn shredding_with_file_deletion() {
|
||||
#[test]
|
||||
#[timeout(10000)]
|
||||
fn shredding_directory_recursively() {
|
||||
let env_details = common::setup(common::TestDataType::MultipleFiles(
|
||||
vec![
|
||||
String::from("test3/subdir1/1.txt"),
|
||||
String::from("test3/subdir1/2.txt"),
|
||||
String::from("test3/subdir1/3.txt"),
|
||||
String::from("test3/subdir1/subdir11/1.txt"),
|
||||
String::from("test3/subdir1/subdir11/2.txt"),
|
||||
String::from("test3/subdir2/subdir1/111.txt"),
|
||||
String::from("test3/subdir3/1231.txt"),
|
||||
String::from("test3/subdir3/1222.txt"),
|
||||
String::from("test3/subdir3/1286.txt"),
|
||||
String::from("test3/subdir3/1286/anotherdir/abs.txt"),
|
||||
String::from("test3/subdir3/1286/anotherdir/abc.txt"),
|
||||
]
|
||||
));
|
||||
let env_details = common::setup(common::TestDataType::MultipleFiles(vec![
|
||||
String::from("test3/subdir1/1.txt"),
|
||||
String::from("test3/subdir1/2.txt"),
|
||||
String::from("test3/subdir1/3.txt"),
|
||||
String::from("test3/subdir1/subdir11/1.txt"),
|
||||
String::from("test3/subdir1/subdir11/2.txt"),
|
||||
String::from("test3/subdir2/subdir1/111.txt"),
|
||||
String::from("test3/subdir3/1231.txt"),
|
||||
String::from("test3/subdir3/1222.txt"),
|
||||
String::from("test3/subdir3/1286.txt"),
|
||||
String::from("test3/subdir3/1286/anotherdir/abs.txt"),
|
||||
String::from("test3/subdir3/1286/anotherdir/abc.txt"),
|
||||
]));
|
||||
|
||||
match &env_details {
|
||||
common::EnvironmentDetails::Single(_) => {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user