1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 12:07:46 +00:00

pattern name generating function handles random as well

This commit is contained in:
Fort 2015-12-30 13:53:59 -08:00
parent 67698a9f2e
commit 649aa2693d

View file

@ -42,14 +42,14 @@ const PATTERNS: [&'static [u8]; 22] = [
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
enum PassType<'a> { enum PassType<'a> {
Pattern(&'a [u8]), Pattern(&'a [u8]),
Random, Random
} }
// Used to generate all possible filenames of a certain length using NAMESET as an alphabet // Used to generate all possible filenames of a certain length using NAMESET as an alphabet
struct FilenameGenerator { struct FilenameGenerator {
name_len: usize, name_len: usize,
nameset_indices: RefCell<Vec<usize>>, // Store the indices of the letters of our filename in NAMESET nameset_indices: RefCell<Vec<usize>>, // Store the indices of the letters of our filename in NAMESET
exhausted: Cell<bool>, exhausted: Cell<bool>
} }
impl FilenameGenerator { impl FilenameGenerator {
@ -110,7 +110,7 @@ struct BytesGenerator<'a> {
block_size: usize, block_size: usize,
exact: bool, // if false, every block's size is block_size exact: bool, // if false, every block's size is block_size
gen_type: PassType<'a>, gen_type: PassType<'a>,
rng: Option<RefCell<ThreadRng>>, rng: Option<RefCell<ThreadRng>>
} }
impl<'a> BytesGenerator<'a> { impl<'a> BytesGenerator<'a> {
@ -311,16 +311,20 @@ fn get_size(size_str_opt: Option<String>) -> Option<u64> {
Some(coeff * unit) Some(coeff * unit)
} }
fn bytes_to_string(bytes: &[u8]) -> String { fn pass_name(pass_type: &PassType) -> String {
let mut s: String = String::new(); match *pass_type {
while s.len() < 6 { PassType::Random => String::from("random"),
for b in bytes { PassType::Pattern(bytes) => {
let readable: String = format!("{:x}", b); let mut s: String = String::new();
s.push_str(&readable); while s.len() < 6 {
for b in bytes {
let readable: String = format!("{:x}", b);
s.push_str(&readable);
}
}
s
} }
} }
s
} }
fn wipe_file(path_str: &str, n_passes: usize, remove: bool, fn wipe_file(path_str: &str, n_passes: usize, remove: bool,
@ -377,15 +381,12 @@ fn wipe_file(path_str: &str, n_passes: usize, remove: bool,
for (i, pass_type) in pass_sequence.iter().enumerate() { for (i, pass_type) in pass_sequence.iter().enumerate() {
if verbose { if verbose {
let pattern_str: String = match *pass_type { let pass_name: String = pass_name(pass_type);
PassType::Random => String::from("random"),
PassType::Pattern(p) => bytes_to_string(p)
};
if total_passes.to_string().len() == 1 { if total_passes.to_string().len() == 1 {
println!("{}: {}: pass {}/{} ({})... ", NAME, path.display(), i + 1, total_passes, pattern_str); println!("{}: {}: pass {}/{} ({})... ", NAME, path.display(), i + 1, total_passes, pass_name);
} }
else { else {
println!("{}: {}: pass {:2.0}/{:2.0} ({})... ", NAME, path.display(), i + 1, total_passes, pattern_str); println!("{}: {}: pass {:2.0}/{:2.0} ({})... ", NAME, path.display(), i + 1, total_passes, pass_name);
} }
} }
// size is an optional argument for exactly how many bytes we want to shred // size is an optional argument for exactly how many bytes we want to shred