2016-10-24 02:12:48 +02:00
|
|
|
#[macro_use]
|
|
|
|
extern crate clap;
|
2016-11-03 22:04:39 +01:00
|
|
|
extern crate globset;
|
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-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-09-23 21:52:50 +02:00
|
|
|
mod notification_filter;
|
2016-10-26 17:01:55 +02:00
|
|
|
mod process;
|
2016-12-15 02:19:58 +01:00
|
|
|
mod signal;
|
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;
|
2017-02-04 22:18:02 +01:00
|
|
|
use std::path::{Path, PathBuf};
|
2016-11-09 15:44:00 +01:00
|
|
|
use std::sync::{Arc, RwLock};
|
2016-10-24 15:53:06 +02:00
|
|
|
use std::sync::mpsc::{channel, Receiver};
|
|
|
|
use std::time::Duration;
|
2016-09-18 22:42:11 +02:00
|
|
|
|
2016-09-23 21:52:50 +02:00
|
|
|
use notification_filter::NotificationFilter;
|
2016-11-09 23:25:52 +01:00
|
|
|
use process::Process;
|
2017-03-14 17:30:19 +01:00
|
|
|
use signal::Signal;
|
2016-10-20 20:27:11 +02:00
|
|
|
use watcher::{Event, Watcher};
|
2016-09-14 15:30:59 +02:00
|
|
|
|
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
|
|
|
|
2017-03-23 23:39:38 +01: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-12-19 17:37:12 +01:00
|
|
|
let args = cli::get_args();
|
2016-11-09 15:44:00 +01:00
|
|
|
let child_process: Arc<RwLock<Option<Process>>> = Arc::new(RwLock::new(None));
|
|
|
|
let weak_child = Arc::downgrade(&child_process);
|
2017-03-13 18:55:12 +01:00
|
|
|
|
|
|
|
// Convert signal string to the corresponding integer
|
2017-03-23 23:50:39 +01:00
|
|
|
let signal = signal::new(args.signal);
|
2016-11-09 23:00:24 +01:00
|
|
|
|
2016-12-15 02:19:58 +01:00
|
|
|
signal::install_handler(move |sig: Signal| {
|
2016-11-09 15:44:00 +01:00
|
|
|
if let Some(lock) = weak_child.upgrade() {
|
|
|
|
let strong = lock.read().unwrap();
|
|
|
|
if let Some(ref child) = *strong {
|
2016-12-15 02:19:58 +01:00
|
|
|
match sig {
|
2017-03-14 17:30:19 +01:00
|
|
|
Signal::SIGCHLD => child.reap(), // SIGCHLD is special, initiate reap()
|
2017-03-14 00:52:50 +01:00
|
|
|
_ => child.signal(sig),
|
2016-12-15 02:19:58 +01:00
|
|
|
}
|
2016-11-09 15:44:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-10-24 02:07:20 +02:00
|
|
|
init_logger(args.debug);
|
2016-09-21 23:02:20 +02:00
|
|
|
|
2017-02-04 22:18:02 +01:00
|
|
|
let paths: Vec<PathBuf> = args.paths
|
|
|
|
.iter()
|
2017-02-04 22:26:59 +01:00
|
|
|
.map(|p| {
|
2017-03-23 23:39:38 +01:00
|
|
|
Path::new(&p)
|
|
|
|
.canonicalize()
|
|
|
|
.expect(&format!("unable to canonicalize \"{}\"", &p))
|
|
|
|
.to_owned()
|
|
|
|
})
|
2017-02-04 22:18:02 +01:00
|
|
|
.collect();
|
2016-10-12 04:43:53 +02:00
|
|
|
|
2017-02-04 20:52:38 +01:00
|
|
|
let gitignore = if !args.no_vcs_ignore {
|
2017-02-04 22:18:02 +01:00
|
|
|
gitignore::load(&paths)
|
2017-01-27 19:00:13 +01:00
|
|
|
} else {
|
2017-02-04 22:26:59 +01:00
|
|
|
gitignore::load(&[])
|
2017-01-27 19:00:13 +01:00
|
|
|
};
|
2016-10-12 04:43:53 +02:00
|
|
|
|
2017-02-04 20:52:38 +01:00
|
|
|
let filter = NotificationFilter::new(args.filters, args.ignores, gitignore)
|
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();
|
2017-03-23 23:39:38 +01:00
|
|
|
let watcher =
|
|
|
|
Watcher::new(tx, &paths, args.poll, args.poll_interval).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-11-09 15:44:00 +01:00
|
|
|
// Start child process initially, if necessary
|
2017-01-27 21:27:44 +01:00
|
|
|
if args.run_initially && !args.once {
|
2016-11-09 23:25:52 +01: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-11-09 15:44:00 +01:00
|
|
|
let mut guard = child_process.write().unwrap();
|
2017-04-06 22:31:52 +02:00
|
|
|
*guard = Some(process::spawn(&args.cmd, vec![], args.no_shell));
|
2016-11-09 15:44:00 +01:00
|
|
|
}
|
2016-10-18 23:36:12 +02:00
|
|
|
|
2016-11-09 15:44:00 +01:00
|
|
|
loop {
|
2016-12-20 17:44:18 +01:00
|
|
|
debug!("Waiting for filesystem activity");
|
2017-01-27 21:27:44 +01:00
|
|
|
let paths = wait_fs(&rx, &filter);
|
2016-11-09 15:44:00 +01:00
|
|
|
if let Some(path) = paths.get(0) {
|
|
|
|
debug!("Path updated: {:?}", path);
|
|
|
|
}
|
2016-10-26 17:01:55 +02:00
|
|
|
|
2017-03-23 23:50:39 +01:00
|
|
|
// We have three scenarios here:
|
|
|
|
//
|
|
|
|
// 1. Make sure the previous run was ended, then run the command again
|
|
|
|
// 2. Just send a specified signal to the child, do nothing more
|
|
|
|
// 3. Send SIGTERM to the child, wait for it to exit, then run the command again
|
|
|
|
// 4. Send a specified signal to the child, wait for it to exit, then run the command again
|
|
|
|
//
|
2017-04-02 21:28:00 +02:00
|
|
|
let scenario = (args.restart, signal.is_some());
|
|
|
|
|
|
|
|
match scenario {
|
2017-03-23 23:50:39 +01:00
|
|
|
// Custom restart behaviour (--restart was given, and --signal specified):
|
|
|
|
// Send specified signal to the child, wait for it to exit, then run the command again
|
2017-04-02 21:28:00 +02:00
|
|
|
(true, true) => {
|
2017-04-02 20:53:56 +02:00
|
|
|
signal_process(&child_process, signal, true);
|
2017-03-23 23:50:39 +01:00
|
|
|
|
|
|
|
// Launch child process
|
|
|
|
if args.clear_screen {
|
|
|
|
cli::clear_screen();
|
|
|
|
}
|
2016-10-26 17:01:55 +02:00
|
|
|
|
2017-03-23 23:50:39 +01:00
|
|
|
debug!("Launching child process");
|
|
|
|
{
|
|
|
|
let mut guard = child_process.write().unwrap();
|
2017-04-06 22:31:52 +02:00
|
|
|
*guard = Some(process::spawn(&args.cmd, paths, args.no_shell));
|
2017-03-23 23:50:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default restart behaviour (--restart was given, but --signal wasn't specified):
|
|
|
|
// Send SIGTERM to the child, wait for it to exit, then run the command again
|
2017-04-02 21:28:00 +02:00
|
|
|
(true, false) => {
|
2017-03-23 23:50:39 +01:00
|
|
|
let sigterm = signal::new(Some("SIGTERM".to_owned()));
|
2017-04-02 20:53:56 +02:00
|
|
|
signal_process(&child_process, sigterm, true);
|
2017-03-23 23:50:39 +01:00
|
|
|
|
|
|
|
// Launch child process
|
|
|
|
if args.clear_screen {
|
|
|
|
cli::clear_screen();
|
|
|
|
}
|
|
|
|
|
|
|
|
debug!("Launching child process");
|
|
|
|
{
|
|
|
|
let mut guard = child_process.write().unwrap();
|
2017-04-06 22:31:52 +02:00
|
|
|
*guard = Some(process::spawn(&args.cmd, paths, args.no_shell));
|
2017-03-23 23:50:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SIGHUP scenario: --signal was given, but --restart was not
|
|
|
|
// Just send a signal (e.g. SIGHUP) to the child, do nothing more
|
2017-04-02 21:28:00 +02:00
|
|
|
(false, true) => signal_process(&child_process, signal, false),
|
2017-03-23 23:50:39 +01:00
|
|
|
|
|
|
|
// Default behaviour (neither --signal nor --restart specified):
|
|
|
|
// Make sure the previous run was ended, then run the command again
|
2017-04-02 21:28:00 +02:00
|
|
|
(false, false) => {
|
2017-04-02 20:53:56 +02:00
|
|
|
signal_process(&child_process, None, true);
|
2017-03-23 23:50:39 +01:00
|
|
|
|
|
|
|
// Launch child process
|
|
|
|
if args.clear_screen {
|
|
|
|
cli::clear_screen();
|
|
|
|
}
|
2016-10-26 17:01:55 +02:00
|
|
|
|
2017-03-23 23:50:39 +01:00
|
|
|
debug!("Launching child process");
|
|
|
|
{
|
|
|
|
let mut guard = child_process.write().unwrap();
|
2017-04-06 22:31:52 +02:00
|
|
|
*guard = Some(process::spawn(&args.cmd, paths, args.no_shell));
|
2017-03-23 23:50:39 +01:00
|
|
|
}
|
|
|
|
}
|
2016-10-24 15:53:06 +02:00
|
|
|
}
|
2017-01-27 21:27:44 +01:00
|
|
|
|
|
|
|
// Handle once option for integration testing
|
|
|
|
if args.once {
|
2017-04-02 20:53:56 +02:00
|
|
|
signal_process(&child_process, signal, false);
|
2017-01-27 21:27:44 +01:00
|
|
|
break;
|
|
|
|
}
|
2016-09-14 15:30:59 +02:00
|
|
|
}
|
|
|
|
}
|
2016-10-12 15:43:22 +02:00
|
|
|
|
2017-01-27 21:27:44 +01:00
|
|
|
fn wait_fs(rx: &Receiver<Event>, filter: &NotificationFilter) -> 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-11-09 15:44:00 +01:00
|
|
|
let e = rx.recv().expect("error when reading event");
|
|
|
|
|
|
|
|
if let Some(ref path) = e.path {
|
|
|
|
// Ignore cache for the initial file. Otherwise, in
|
|
|
|
// debug mode it's hard to track what's going on
|
|
|
|
let excluded = filter.is_excluded(path);
|
|
|
|
if !cache.contains_key(path) {
|
|
|
|
cache.insert(path.to_owned(), excluded);
|
|
|
|
}
|
|
|
|
|
|
|
|
if !excluded {
|
|
|
|
paths.push(path.to_owned());
|
|
|
|
break;
|
2016-10-12 15:43:22 +02:00
|
|
|
}
|
2016-11-09 15:44:00 +01: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
|
|
|
|
let timeout = Duration::from_millis(500);
|
|
|
|
while let Ok(e) = rx.recv_timeout(timeout) {
|
|
|
|
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
|
|
|
|
2016-11-09 15:44:00 +01:00
|
|
|
paths
|
2016-10-12 15:43:22 +02:00
|
|
|
}
|
2017-01-27 21:27:44 +01:00
|
|
|
|
2017-04-02 20:53:56 +02:00
|
|
|
// signal_process sends signal to process. It waits for the process to exit if wait is true
|
|
|
|
fn signal_process(process: &RwLock<Option<Process>>, signal: Option<Signal>, wait: bool) {
|
2017-01-27 21:27:44 +01:00
|
|
|
let guard = process.read().unwrap();
|
|
|
|
|
|
|
|
if let Some(ref child) = *guard {
|
2017-03-23 23:50:39 +01:00
|
|
|
if let Some(s) = signal {
|
|
|
|
child.signal(s);
|
2017-01-27 21:27:44 +01:00
|
|
|
}
|
|
|
|
|
2017-03-23 23:50:39 +01:00
|
|
|
if wait {
|
|
|
|
debug!("Waiting for process to exit...");
|
|
|
|
child.wait();
|
|
|
|
}
|
2017-01-27 21:27:44 +01:00
|
|
|
}
|
|
|
|
}
|