Initial commit

This commit is contained in:
Matt Green 2016-09-14 09:30:59 -04:00
commit 5cf5ce73ca
3 changed files with 47 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
target
Cargo.lock

8
Cargo.toml Normal file
View File

@ -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"

37
src/main.rs Normal file
View File

@ -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),
}
}
}