Commit a421acc1 authored by Stefan Schindler's avatar Stefan Schindler
Browse files

Protect mounted disks

parent e07c1ca0
......@@ -69,7 +69,13 @@ fn show_disk_status(disks: &Vec<Arc<DiskInformation>>) {
fn parse_disks() -> Vec<Arc<DiskInformation>> {
let partitions = get_file_contents("/proc/partitions");
parse_partition_file(&partitions)
let disks = parse_partition_file(&partitions);
let mounts = get_command_output("mount");
mark_mounted_disks(&disks, mounts);
disks
}
fn parse_partition_file(partitions: &String) -> Vec<Arc<DiskInformation>> {
......@@ -104,6 +110,21 @@ fn parse_partition_file(partitions: &String) -> Vec<Arc<DiskInformation>> {
disks
}
fn mark_mounted_disks(disks: &Vec<Arc<DiskInformation>>, mounts: String) {
let mut mount_lines = mounts.split('\n').filter(|e| {
e.starts_with("/dev/")
});
for ref disk in disks {
let disk = *disk;
let disk_path = format!("/dev/{}", disk.name);
if mount_lines.any(|e| e.starts_with(&*disk_path)) {
*disk.state.lock().unwrap() = Protected;
}
}
}
fn get_file_contents(path: &str) -> String {
let f = match File::open(path) {
Ok(f) => f,
......@@ -122,6 +143,15 @@ fn get_file_contents(path: &str) -> String {
buffer
}
fn get_command_output<S: Into<String>>(command: S) -> String {
let command = command.into();
let output = std::process::Command::new(&command)
.output()
.expect(&*format!("unable to execute: {:?}", command));
String::from_utf8_lossy(&output.stdout).to_string()
}
fn read_one_line_from_stdin() -> String {
print!("> ");
io::stdout().flush().expect("Unable to flush stdout inside fn read_one_line_from_stdin()");
......@@ -589,4 +619,20 @@ mod test {
};
assert_eq!(mmcblk0, *partitions[1]);
}
#[test]
fn protect_sda() {
let disks = vec![ Arc::new(DiskInformation {
id: 0,
name: "sda".into(),
size: Byte(0),
state: Mutex::new(Detected),
}) ];
let mounts = "/dev/sda3 on / type ext4 (rw,noatime,nodiratime,errors=remount-ro,data=ordered)".into();
mark_mounted_disks(&disks, mounts);
assert_eq!(Protected, *(*disks[0]).state.lock().unwrap());
}
}
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