2016-10-24 15:53:06 +02:00
|
|
|
#![feature(mpsc_select)]
|
2016-10-20 20:27:11 +02:00
|
|
|
#![feature(process_exec)]
|
|
|
|
|
2016-10-24 02:12:48 +02:00
|
|
|
#[macro_use]
|
|
|
|
extern crate clap;
|
2016-10-13 23:59:46 +02:00
|
|
|
extern crate env_logger;
|
2016-10-20 17:38:51 +02:00
|
|
|
extern crate libc;
|
2016-10-24 02:12:48 +02:00
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate lazy_static;
|
2016-09-18 22:42:11 +02:00
|
|
|
extern crate notify;
|
2016-10-25 03:30:34 +02:00
|
|
|
extern crate threadpool;
|
2016-09-14 15:30:59 +02:00
|
|
|
|
2016-10-24 02:12:48 +02:00
|
|
|
#[cfg(unix)]
|
|
|
|
extern crate nix;
|
|
|
|
#[cfg(windows)]
|
|
|
|
extern crate winapi;
|
|
|
|
#[cfg(windows)]
|
|
|
|
extern crate kernel32;
|
2016-10-22 21:37:03 +02:00
|
|
|
|
2016-10-25 13:05:53 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
extern crate mktemp;
|
|
|
|
|
2016-10-26 17:01:55 +02:00
|
|
|
mod cli;
|
2016-10-12 04:43:53 +02:00
|
|
|
mod gitignore;
|
2016-10-22 21:37:03 +02:00
|
|
|
mod interrupt_handler;
|
2016-09-23 21:52:50 +02:00
|
|
|
mod notification_filter;
|
2016-10-26 17:01:55 +02:00
|
|
|
mod process;
|
2016-10-20 20:27:11 +02:00
|
|
|
mod watcher;
|
2016-09-21 23:02:20 +02:00
|
|
|
|
2016-10-28 14:58:15 +02:00
|
|
|
use std::collections::HashMap;
|
2016-10-24 15:53:06 +02:00
|
|
|
use std::env;
|
|
|
|
use std::sync::mpsc::{channel, Receiver};
|
|
|
|
use std::time::Duration;
|
2016-10-20 00:54:20 +02:00
|
|
|
use std::path::{Path, PathBuf};
|
2016-09-18 22:42:11 +02:00
|
|
|
|
2016-09-23 21:52:50 +02:00
|
|
|
use notification_filter::NotificationFilter;
|
2016-10-26 17:01:55 +02:00
|
|
|
use process::{Process, ProcessReaper};
|
2016-10-20 20:27:11 +02:00
|
|
|
use watcher::{Event, Watcher};
|
2016-09-14 15:30:59 +02:00
|
|
|
|
2016-10-20 00:54:20 +02:00
|
|
|
// Starting at the specified path, search for gitignore files,
|
|
|
|
// stopping at the first one found.
|
|
|
|
fn find_gitignore_file(path: &Path) -> Option<PathBuf> {
|
|
|
|
let mut gitignore_path = path.join(".gitignore");
|
|
|
|
if gitignore_path.exists() {
|
|
|
|
return Some(gitignore_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
let p = path.to_owned();
|
|
|
|
|
|
|
|
while let Some(p) = p.parent() {
|
|
|
|
gitignore_path = p.join(".gitignore");
|
|
|
|
if gitignore_path.exists() {
|
|
|
|
return Some(gitignore_path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2016-10-14 02:21:29 +02:00
|
|
|
fn init_logger(debug: bool) {
|
2016-10-13 23:59:46 +02:00
|
|
|
let mut log_builder = env_logger::LogBuilder::new();
|
2016-10-18 15:39:40 +02:00
|
|
|
let level = if debug {
|
|
|
|
log::LogLevelFilter::Debug
|
|
|
|
} else {
|
2016-10-24 02:12:48 +02:00
|
|
|
log::LogLevelFilter::Warn
|
2016-10-18 15:39:40 +02:00
|
|
|
};
|
2016-10-13 23:59:46 +02:00
|
|
|
|
2016-10-24 02:12:48 +02:00
|
|
|
log_builder.format(|r| format!("*** {}", r.args()))
|
2016-10-13 23:59:46 +02:00
|
|
|
.filter(None, level);
|
|
|
|
log_builder.init().expect("unable to initialize logger");
|
|
|
|
}
|
|
|
|
|
2016-10-12 15:43:22 +02:00
|
|
|
fn main() {
|
2016-10-24 15:53:06 +02:00
|
|
|
let interrupt_rx = interrupt_handler::install();
|
2016-10-26 17:01:55 +02:00
|
|
|
let args = cli::get_args();
|
2016-10-13 23:59:46 +02:00
|
|
|
|
2016-10-24 02:07:20 +02:00
|
|
|
init_logger(args.debug);
|
2016-09-21 23:02:20 +02:00
|
|
|
|
2016-10-12 15:43:22 +02:00
|
|
|
let cwd = env::current_dir()
|
|
|
|
.expect("unable to get cwd")
|
|
|
|
.canonicalize()
|
|
|
|
.expect("unable to canonicalize cwd");
|
2016-10-12 04:43:53 +02:00
|
|
|
|
|
|
|
let mut gitignore_file = None;
|
2016-10-24 02:07:20 +02:00
|
|
|
if !args.no_vcs_ignore {
|
2016-10-20 00:54:20 +02:00
|
|
|
if let Some(gitignore_path) = find_gitignore_file(&cwd) {
|
2016-10-14 01:47:04 +02:00
|
|
|
debug!("Found .gitignore file: {:?}", gitignore_path);
|
2016-10-12 04:43:53 +02:00
|
|
|
|
2016-10-12 15:43:22 +02:00
|
|
|
gitignore_file = gitignore::parse(&gitignore_path).ok();
|
|
|
|
}
|
2016-10-12 04:43:53 +02:00
|
|
|
}
|
|
|
|
|
2016-10-27 14:27:16 +02:00
|
|
|
let filter = NotificationFilter::new(&cwd, args.filters, args.ignores, gitignore_file)
|
2016-10-24 02:12:48 +02:00
|
|
|
.expect("unable to create notification filter");
|
2016-09-21 23:02:20 +02:00
|
|
|
|
2016-09-14 15:30:59 +02:00
|
|
|
let (tx, rx) = channel();
|
2016-10-24 02:07:20 +02:00
|
|
|
let mut watcher = Watcher::new(tx, args.poll, args.poll_interval)
|
2016-10-20 20:27:11 +02:00
|
|
|
.expect("unable to create watcher");
|
2016-10-20 02:19:01 +02:00
|
|
|
|
2016-10-20 20:27:11 +02:00
|
|
|
if watcher.is_polling() {
|
2016-10-24 02:07:20 +02:00
|
|
|
warn!("Polling for changes every {} ms", args.poll_interval);
|
2016-10-20 20:27:11 +02:00
|
|
|
}
|
2016-09-18 22:42:11 +02:00
|
|
|
|
2016-10-24 02:07:20 +02:00
|
|
|
for path in args.paths {
|
|
|
|
match Path::new(&path).canonicalize() {
|
2016-10-24 02:12:48 +02:00
|
|
|
Ok(canonicalized) => watcher.watch(canonicalized).expect("unable to watch path"),
|
|
|
|
Err(_) => {
|
2016-09-27 15:43:28 +02:00
|
|
|
println!("invalid path: {}", path);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-09-18 22:42:11 +02:00
|
|
|
}
|
|
|
|
|
2016-10-24 02:07:20 +02:00
|
|
|
let cmd = args.cmd;
|
2016-09-14 15:30:59 +02:00
|
|
|
|
2016-10-26 17:01:55 +02:00
|
|
|
let (child_finish_tx, child_finish_rx) = channel();
|
|
|
|
let reaper = ProcessReaper::new(child_finish_tx);
|
|
|
|
|
|
|
|
let mut child_process = if args.run_initially {
|
2016-10-25 03:30:34 +02:00
|
|
|
if args.clear_screen {
|
2016-10-26 17:01:55 +02:00
|
|
|
cli::clear_screen();
|
2016-10-25 03:30:34 +02:00
|
|
|
}
|
|
|
|
|
2016-10-26 22:14:57 +02:00
|
|
|
Process::new(&cmd, vec![]).ok()
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2016-10-18 23:36:12 +02:00
|
|
|
|
2016-10-24 02:07:20 +02:00
|
|
|
while !interrupt_handler::interrupt_requested() {
|
2016-10-26 17:01:55 +02:00
|
|
|
if let Some(paths) = wait(&rx, &interrupt_rx, &filter) {
|
|
|
|
let updated = paths.iter()
|
|
|
|
.map(|p| p.to_str().unwrap())
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
if let Some(mut child) = child_process {
|
|
|
|
if args.restart {
|
|
|
|
debug!("Killing child process");
|
|
|
|
child.kill();
|
2016-10-25 03:30:34 +02:00
|
|
|
}
|
|
|
|
|
2016-10-26 17:01:55 +02:00
|
|
|
debug!("Waiting for process to exit...");
|
|
|
|
reaper.wait_process(child);
|
|
|
|
select! {
|
|
|
|
_ = child_finish_rx.recv() => {},
|
|
|
|
_ = interrupt_rx.recv() => break
|
|
|
|
};
|
2016-10-25 03:30:34 +02:00
|
|
|
}
|
2016-10-26 17:01:55 +02:00
|
|
|
|
|
|
|
if args.clear_screen {
|
|
|
|
cli::clear_screen();
|
2016-10-24 15:53:06 +02:00
|
|
|
}
|
2016-10-26 17:01:55 +02:00
|
|
|
|
2016-10-26 22:14:57 +02:00
|
|
|
child_process = Process::new(&cmd, updated).ok();
|
2016-10-24 15:53:06 +02:00
|
|
|
}
|
2016-09-14 15:30:59 +02:00
|
|
|
}
|
|
|
|
}
|
2016-10-12 15:43:22 +02:00
|
|
|
|
2016-10-25 03:30:34 +02:00
|
|
|
fn wait(rx: &Receiver<Event>,
|
|
|
|
interrupt_rx: &Receiver<()>,
|
|
|
|
filter: &NotificationFilter)
|
|
|
|
-> Option<Vec<PathBuf>> {
|
2016-10-24 15:53:06 +02:00
|
|
|
let mut paths = vec![];
|
2016-10-28 14:58:15 +02:00
|
|
|
let mut cache = HashMap::new();
|
2016-10-24 15:53:06 +02:00
|
|
|
|
2016-10-12 15:43:22 +02:00
|
|
|
loop {
|
2016-10-24 15:53:06 +02:00
|
|
|
select! {
|
|
|
|
_ = interrupt_rx.recv() => { return None; },
|
|
|
|
ev = rx.recv() => {
|
|
|
|
let e = ev.expect("error when reading event");
|
|
|
|
|
|
|
|
if let Some(ref path) = e.path {
|
2016-10-28 14:58:15 +02:00
|
|
|
if cache.contains_key(path) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let excluded = filter.is_excluded(path);
|
|
|
|
|
|
|
|
let p = path.to_owned();
|
|
|
|
cache.insert(p.clone(), excluded);
|
|
|
|
|
|
|
|
if !excluded {
|
|
|
|
paths.push(p);
|
2016-10-24 15:53:06 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-10-12 15:43:22 +02:00
|
|
|
}
|
2016-10-24 15:53:06 +02:00
|
|
|
};
|
|
|
|
}
|
2016-10-12 15:43:22 +02:00
|
|
|
|
2016-10-24 15:53:06 +02:00
|
|
|
// Wait for filesystem activity to cool off
|
|
|
|
// Unfortunately, we can't use select! with recv_timeout :(
|
|
|
|
let timeout = Duration::from_millis(500);
|
|
|
|
while let Ok(e) = rx.recv_timeout(timeout) {
|
|
|
|
if interrupt_handler::interrupt_requested() {
|
|
|
|
break;
|
2016-10-12 15:43:22 +02:00
|
|
|
}
|
|
|
|
|
2016-10-24 15:53:06 +02:00
|
|
|
if let Some(ref path) = e.path {
|
2016-10-28 14:58:15 +02:00
|
|
|
if cache.contains_key(path) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let excluded = filter.is_excluded(path);
|
|
|
|
|
|
|
|
let p = path.to_owned();
|
|
|
|
cache.insert(p.clone(), excluded);
|
|
|
|
|
|
|
|
if !excluded {
|
|
|
|
paths.push(p);
|
|
|
|
}
|
2016-10-24 15:53:06 +02:00
|
|
|
}
|
2016-10-12 15:43:22 +02:00
|
|
|
}
|
2016-10-24 15:53:06 +02:00
|
|
|
|
|
|
|
Some(paths)
|
2016-10-12 15:43:22 +02:00
|
|
|
}
|