Initial commit
This commit is contained in:
commit
5cf5ce73ca
|
@ -0,0 +1,2 @@
|
||||||
|
target
|
||||||
|
Cargo.lock
|
|
@ -0,0 +1,8 @@
|
||||||
|
[package]
|
||||||
|
name = "dirwatcher"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Matt Green <mattgreenrocks@gmail.com>"]
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
notify = { git = "https://github.com/passcod/rsnotify" }
|
||||||
|
libc = "0.2.16"
|
|
@ -0,0 +1,37 @@
|
||||||
|
extern crate notify;
|
||||||
|
extern crate libc;
|
||||||
|
|
||||||
|
use libc::system;
|
||||||
|
use notify::{RecommendedWatcher, Watcher};
|
||||||
|
use notify::RecursiveMode;
|
||||||
|
use std::ffi::CString;
|
||||||
|
use std::string::String;
|
||||||
|
use std::sync::mpsc::channel;
|
||||||
|
|
||||||
|
fn invoke(cmd: &String) {
|
||||||
|
let s = CString::new(cmd.clone()).unwrap();
|
||||||
|
unsafe {
|
||||||
|
system(s.as_ptr());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let cmd = std::env::args().nth(1).expect("Argument 1 needs to be a command");
|
||||||
|
|
||||||
|
let (tx, rx) = channel();
|
||||||
|
let mut watcher: RecommendedWatcher = Watcher::new(tx)
|
||||||
|
.expect("unable to create watcher");
|
||||||
|
watcher.watch(".", RecursiveMode::Recursive)
|
||||||
|
.expect("unable to start watching directory");
|
||||||
|
|
||||||
|
loop {
|
||||||
|
match rx.recv() {
|
||||||
|
Ok(notify::Event{ path: Some(path), op:Ok(op), cookie:_ }) => {
|
||||||
|
println!("{:?} {:?}", op, path);
|
||||||
|
invoke(&cmd);
|
||||||
|
},
|
||||||
|
Ok(event) => println!("broken event: {:?}", event),
|
||||||
|
Err(e) => println!("watch error: {}", e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue