Commit 625d6123 authored by Stefan Schindler's avatar Stefan Schindler
Browse files

Implement disk whiping

parent 4f2d1adc
......@@ -13,6 +13,8 @@ use std::sync::{Arc, Mutex};
use ansi_term::Style;
use ansi_term::Colour::*;
use rand::Rng;
lazy_static! {
static ref BOLD: Style = Style::new().bold();
static ref UNDERLINE: Style = Style::new().underline();
......@@ -203,10 +205,11 @@ enum DiskState {
QueueForWhipe,
Whiping{ progress: ByteUnit, per_second: ByteUnit },
Whiped,
WhipeFailed,
}
use DiskState::*;
impl Display for ByteUnit {
impl Display for ByteUnit {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
&Byte(size) => {
......@@ -290,22 +293,48 @@ fn update_jobs(disks: &Vec<Arc<DiskInformation>>, pool: &threadpool::ThreadPool)
println!("Queued {} for whipe", BOLD.paint(&*disk.name));
let disk = disk.clone();
pool.execute(move || {
let mut rng = rand::thread_rng();
match whipe_disk(&disk) {
Ok(_) => {
*disk.state.lock().unwrap() = Whiped;
println!("{}{}", Green.paint("Whiped "), BOLD.paint(&*disk.name));
},
Err(e) => {
*disk.state.lock().unwrap() = WhipeFailed;
println!("{}{}{}",
Red.paint("Whipe of "),
BOLD.paint(&*disk.name),
Red.paint(format!(" failed: {:?}", e))
);
return
}
}
*disk.state.lock().unwrap() = Whiping{ progress: Byte(0), per_second: Byte(0) };
let mut buf = [0; WRITE_BUFFER_SIZE];
let mut position = Byte(0);
/*
while position < disk.size {
buf = rng.get();
position += WRITE_BUFFER_SIZE as u64;
}*/
})
}
}
}
fn whipe_disk(disk: &Arc<DiskInformation>) -> std::io::Result<()> {
let mut rng = rand::thread_rng();
*disk.state.lock().unwrap() = Whiping{ progress: Byte(0), per_second: Byte(0) };
let mut buf = [0; WRITE_BUFFER_SIZE];
let mut position = Byte(0);
let mut disk_file = std::fs::OpenOptions::new()
.write(true)
.open(&*format!("/dev/{}", disk.name))?;
while position < disk.size {
rng.fill_bytes(&mut buf);
disk_file.write_all(&buf)?;
position += Byte(WRITE_BUFFER_SIZE as u64)
}
Ok( () )
}
#[cfg(test)]
mod test {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment