#![feature(exit_status_error)]
use inotify::{Inotify, WatchMask};
use std::error::Error;
use std::ffi::OsString;
use std::fs;
use std::path::Path;
use std::process::Command;
fn process(filename: &Path) -> Result<(), Box<dyn Error>> {
let mut newname = OsString::from(filename);
newname.push(".q");
println!("Renaming into {:?}", newname);
fs::rename(&filename, &newname)?;
Command::new(
"/home/maniac/.cache/pypoetry/virtualenvs/flickr-upload-AGKUL9KW-py3.9/bin/python",
)
.arg("/home/maniac/code/flickr-upload/upload.py")
.arg(&newname)
.status()?
.exit_ok()?;
println!("Removing {:?}", &newname);
fs::remove_file(&newname)?;
Ok(())
}
fn main() -> Result<(), Box<dyn Error>> {
let root = Path::new("/tmp/flickr");
let mut inotify = Inotify::init()?;
inotify.add_watch(root, WatchMask::CLOSE_WRITE)?;
let mut buffer = [0; 1024];
loop {
println!("Watching {}...", root.to_str().unwrap());
let events = inotify.read_events_blocking(&mut buffer)?;
for event in events {
let filename = event.name.unwrap();
println!("Processing {:?}", root.join(filename));
let _ = process(&root.join(filename));
}
}
}