diff options
author | Dock <dockconnellyoneal@gmail.com> | 2018-01-03 03:26:11 -0500 |
---|---|---|
committer | David Peter <sharkdp@users.noreply.github.com> | 2018-01-03 09:26:11 +0100 |
commit | 26ad7da347f8fd4d511849b492491e40ed185a08 (patch) | |
tree | 86b34cdf34550914e21a2682348871991d7a544c | |
parent | c5b920b91302a3bbec41861c469821a1c76d2efb (diff) |
Using wants_to_quit to exit the sender as well as the reciever (#211)
Closes #210
-rw-r--r-- | src/exit_codes.rs | 4 | ||||
-rw-r--r-- | src/main.rs | 1 | ||||
-rw-r--r-- | src/output.rs | 8 | ||||
-rw-r--r-- | src/walk.rs | 21 |
4 files changed, 25 insertions, 9 deletions
diff --git a/src/exit_codes.rs b/src/exit_codes.rs new file mode 100644 index 0000000..257f0b2 --- /dev/null +++ b/src/exit_codes.rs @@ -0,0 +1,4 @@ +/// exit code 1 represents a general error +pub const ERROR: i32 = 1; +/// exit code 130 represents a process killed by signal SIGINT +pub const SIGINT: i32 = 130; diff --git a/src/main.rs b/src/main.rs index 36900c7..ac2f23e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,6 +19,7 @@ extern crate num_cpus; extern crate regex; extern crate regex_syntax; +mod exit_codes; pub mod fshelper; pub mod lscolors; mod app; diff --git a/src/output.rs b/src/output.rs index a3cd36f..7ba8fb0 100644 --- a/src/output.rs +++ b/src/output.rs @@ -6,6 +6,7 @@ // notice may not be copied, modified, or distributed except // according to those terms. +use exit_codes; use internal::FdOptions; use lscolors::LsColors; @@ -20,9 +21,6 @@ use std::os::unix::fs::PermissionsExt; use ansi_term; -const EXIT_CODE_ERROR: i32 = 1; -const EXIT_CODE_SIGINT: i32 = 130; - pub fn print_entry(entry: &PathBuf, config: &FdOptions, wants_to_quit: &Arc<AtomicBool>) { let path = entry.strip_prefix(".").unwrap_or(entry); @@ -34,7 +32,7 @@ pub fn print_entry(entry: &PathBuf, config: &FdOptions, wants_to_quit: &Arc<Atom if r.is_err() { // Probably a broken pipe. Exit gracefully. - process::exit(EXIT_CODE_ERROR); + process::exit(exit_codes::ERROR); } } @@ -76,7 +74,7 @@ fn print_entry_colorized( if wants_to_quit.load(Ordering::Relaxed) { write!(handle, "\n")?; - process::exit(EXIT_CODE_SIGINT); + process::exit(exit_codes::SIGINT); } } diff --git a/src/walk.rs b/src/walk.rs index 9b8dd5d..c604c8e 100644 --- a/src/walk.rs +++ b/src/walk.rs @@ -13,6 +13,8 @@ use fshelper; use internal::{error, FdOptions}; use output; +use exit_codes; +use std::process; use std::path::PathBuf; use std::sync::{Arc, Mutex}; use std::sync::atomic::{AtomicBool, Ordering}; @@ -86,8 +88,10 @@ pub fn scan(path_vec: &[PathBuf], pattern: Arc<Regex>, config: Arc<FdOptions>) { let parallel_walker = walker.threads(threads).build_parallel(); let wants_to_quit = Arc::new(AtomicBool::new(false)); + let receiver_wtq = Arc::clone(&wants_to_quit); + let sender_wtq = Arc::clone(&wants_to_quit); if config.ls_colors.is_some() { - let wq = Arc::clone(&wants_to_quit); + let wq = Arc::clone(&receiver_wtq); ctrlc::set_handler(move || { wq.store(true, Ordering::Relaxed); }).unwrap(); @@ -148,7 +152,7 @@ pub fn scan(path_vec: &[PathBuf], pattern: Arc<Regex>, config: Arc<FdOptions>) { if time::Instant::now() - start > max_buffer_time { // Flush the buffer for v in &buffer { - output::print_entry(v, &rx_config, &wants_to_quit); + output::print_entry(v, &rx_config, &receiver_wtq); } buffer.clear(); @@ -157,7 +161,7 @@ pub fn scan(path_vec: &[PathBuf], pattern: Arc<Regex>, config: Arc<FdOptions>) { } } ReceiverMode::Streaming => { - output::print_entry(&value, &rx_config, &wants_to_quit); + output::print_entry(&value, &rx_config, &receiver_wtq); } } } @@ -167,7 +171,7 @@ pub fn scan(path_vec: &[PathBuf], pattern: Arc<Regex>, config: Arc<FdOptions>) { if !buffer.is_empty() { buffer.sort(); for value in buffer { - output::print_entry(&value, &rx_config, &wants_to_quit); + output::print_entry(&value, &rx_config, &receiver_wtq); } } } @@ -178,8 +182,13 @@ pub fn scan(path_vec: &[PathBuf], pattern: Arc<Regex>, config: Arc<FdOptions>) { let config = Arc::clone(&config); let pattern = Arc::clone(&pattern); let tx_thread = tx.clone(); + let wants_to_quit = Arc::clone(&sender_wtq); Box::new(move |entry_o| { + if wants_to_quit.load(Ordering::Relaxed) { + return ignore::WalkState::Quit; + } + let entry = match entry_o { Ok(e) => e, Err(_) => return ignore::WalkState::Continue, @@ -238,4 +247,8 @@ pub fn scan(path_vec: &[PathBuf], pattern: Arc<Regex>, config: Arc<FdOptions>) { // Wait for the receiver thread to print out all results. receiver_thread.join().unwrap(); + + if wants_to_quit.load(Ordering::Relaxed) { + process::exit(exit_codes::SIGINT); + } } |