use std::io;
use std::fs;
use std::path::PathBuf;
use libc::daemon;
fn is_alive(pid: i32) -> bool {
unsafe { libc::kill(pid, 0) == 0 }
}
fn main() {
let parent = std::os::unix::process::parent_id() as i32;
unsafe { daemon(1, 1); }
let mut dirs_to_delete: Vec<PathBuf> = vec![];
let mut files_to_delete: Vec<PathBuf> = vec![];
while is_alive(parent) {
let stdin = io::stdin();
for line in stdin.lines() {
match line {
Err(e) => if e.kind() == std::io::ErrorKind::BrokenPipe {
break;
} else {
eprintln!("janitor: unexpected error reading from stdin: {}", e);
break;
}
Ok(ln) => {
if ln.is_empty() {
continue;
}
let arg = ln[1..].to_string();
match ln.bytes().nth(0).unwrap() {
b'#' => {},
b'f' => files_to_delete.push(arg.into()),
b'd' => dirs_to_delete.push(arg.into()),
_ => eprintln!("janitor: unsure how to process {:?}", &ln),
}
}
}
}
}
for dir in &dirs_to_delete {
let _ = fs::remove_dir_all(dir);
}
for f in &files_to_delete {
let _ = fs::remove_file(f);
}
}