Commit 39bb8caf authored by Stefan Schindler's avatar Stefan Schindler
Browse files

Add command s for shutdown

parent 144d3d53
...@@ -44,6 +44,7 @@ fn main() { ...@@ -44,6 +44,7 @@ fn main() {
"h" => print_help(), "h" => print_help(),
"l" => show_disk_status(&disks), "l" => show_disk_status(&disks),
"w" => select_disk_to_wipe(&disks), "w" => select_disk_to_wipe(&disks),
"s" => try_shutdown(&disks),
"" => show_disk_status(&disks), "" => show_disk_status(&disks),
com => println!("unknown command: {}", com), com => println!("unknown command: {}", com),
} }
...@@ -145,9 +146,19 @@ fn get_file_contents(path: &str) -> String { ...@@ -145,9 +146,19 @@ fn get_file_contents(path: &str) -> String {
buffer buffer
} }
fn replace_file_contents(path: &str, contents: &String) -> io::Result<()> {
let mut file = File::create(path)?;
file.write_all(contents.as_bytes())
}
fn get_command_output<S: Into<String>>(command: S) -> String { fn get_command_output<S: Into<String>>(command: S) -> String {
let command = command.into(); let command = command.into();
let output = std::process::Command::new(&command) let mut iter = command.split_whitespace();
let programm = iter.next().expect("No binary supplied");
let args: Vec<&str> = iter.collect();
let output = std::process::Command::new(&programm)
.args(&args)
.output() .output()
.expect(&*format!("unable to execute: {:?}", command)); .expect(&*format!("unable to execute: {:?}", command));
...@@ -162,6 +173,18 @@ fn read_one_line_from_stdin() -> String { ...@@ -162,6 +173,18 @@ fn read_one_line_from_stdin() -> String {
input input
} }
/// Shutdown system if everything is done or errored
fn try_shutdown(disks: &Vec<Arc<DiskInformation>>) {
if disks.iter().all(|e| {
match *e.state.lock().unwrap() {
Detected | Protected | Wiped{ took:_ } | WipeFailed => true,
QueueForWipe | Wiping{ progress: _, per_second: _ } => false,
}
}) {
get_command_output("shutdown -h now");
}
}
#[derive(Debug)] #[derive(Debug)]
struct DiskInformation { struct DiskInformation {
id: u64, id: u64,
...@@ -353,6 +376,7 @@ fn print_help() { ...@@ -353,6 +376,7 @@ fn print_help() {
("h", "print this help"), ("h", "print this help"),
("l", "list found disks and status"), ("l", "list found disks and status"),
("w", "start wiping a disk"), ("w", "start wiping a disk"),
("s", "shutdown this system"),
]; ];
for option in options.iter() { for option in options.iter() {
......
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