Implemented basic shreding functionality.

This commit is contained in:
2021-01-26 13:02:48 +03:00
parent 285c7596fb
commit 794546fe3a
4 changed files with 221 additions and 29 deletions
+15 -28
View File
@@ -1,12 +1,15 @@
extern crate clap;
use clap::{App, Arg};
use std::fs;
use std::process::exit;
use clap::{App, Arg, crate_authors, crate_name, crate_version};
use crate::shred::{Shredder, Verbosity};
mod shred;
fn main() {
let params = App::new("shred")
.version("0.1.0")
.author("Alexey Zinchenko <alexey.zinchenko@protonmail.com>")
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")
@@ -39,27 +42,11 @@ fn main() {
// Calling .unwrap() is safe here because "INPUT" is required (if "INPUT" wasn't
// required we could have used an 'if let' to conditionally get the value)
let path = params.value_of("PATH").unwrap();
match fs::metadata(path) {
Ok(metadata) => {
println!("Exists: OK");
println!("Is directory: {}", metadata.is_dir());
println!("Is file: {}", metadata.is_file());
println!("Is recursively: {}", is_recursively);
println!("Is interactive: {}", is_interactive);
println!("Verbosity: {:?}", verbosity);
}
Err(_) => {
println!("Provided path is invalid!");
exit(1);
}
}
println!("Using input file: {}", path);
}
#[derive(Debug)]
enum Verbosity {
None,
Low,
Average,
High
Shredder::new(
path.to_string(),
is_recursively,
is_interactive,
verbosity
).run();
}
+134
View File
@@ -0,0 +1,134 @@
use std::cmp::Ordering;
use std::fs;
use std::fs::File;
use std::io::{BufWriter, Write};
use std::io;
use std::process::exit;
pub struct Shredder {
path: String,
is_recursively: bool,
is_interactive: bool,
verbosity: Verbosity,
}
impl Shredder {
pub fn new(path: String, is_recursively: bool, is_interactive: bool, verbosity: Verbosity) -> Shredder {
Shredder {
path,
is_recursively,
is_interactive,
verbosity
}
}
pub fn run(&self) {
match fs::metadata(&self.path) {
Ok(metadata) => {
if self.verbosity >= Verbosity::Average {
println!("Is directory: {}", metadata.is_dir());
println!("Is file: {}", metadata.is_file());
if self.verbosity == Verbosity::High {
println!("Is recursively: {}", self.is_recursively);
println!("Is interactive: {}", self.is_interactive);
println!("Verbosity: {:?}", &self.verbosity);
}
}
}
Err(_) => {
println!("Provided path is invalid!");
exit(1);
}
}
if self.verbosity > Verbosity::None {
println!("Using input file: {}", &self.path);
}
if fs::metadata(&self.path).unwrap().is_file() {
Shredder::shred_file(&self.path, self.is_interactive);
} else if self.is_recursively {
} else {
println!("Target is a directory!");
exit(1);
}
}
fn shred_file(path: &String, is_interactive: bool) {
match std::fs::canonicalize(path) {
Ok(path) => {
if is_interactive {
let file_length = path.metadata().unwrap().len();
let absolute_path = path.to_str().unwrap();
println!("Do you really want to shred '{}'? [Y/n]", absolute_path);
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") {
match File::create(absolute_path) {
Ok(file) => {
println!("File's size: {}", file_length);
let mut buffer = BufWriter::new(&file);
let random_bytes: Vec<u8> = (0..file_length).map(|_| {
rand::random::<u8>()
}).collect();
buffer.write(&random_bytes).unwrap();
buffer.flush().unwrap();
file.sync_all().unwrap();
}
Err(error) => {
println!("{}", error);
}
}
}
}
}
Err(error) => {
println!("{}", error);
}
}
}
}
#[derive(Debug, Eq)]
pub enum Verbosity {
None,
Low,
Average,
High
}
impl Verbosity {
pub fn discriminant(&self) -> i8 {
match self {
Verbosity::None => 0,
Verbosity::Low => 1,
Verbosity::Average => 2,
Verbosity::High => 3,
}
}
}
impl Ord for Verbosity {
fn cmp(&self, other: &Self) -> Ordering {
self.discriminant().cmp(&other.discriminant())
}
}
impl PartialOrd for Verbosity {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.discriminant().cmp(&other.discriminant()))
}
}
impl PartialEq for Verbosity {
fn eq(&self, other: &Self) -> bool {
self.discriminant() == other.discriminant()
}
}