Using wants_to_quit to exit the sender as well as the reciever (#211)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Closes #210
authored by Dock · committed by David Peter · parent c5b920b · Browse files · 4 files·+25 added−9 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+25 added−9 removed
- other:+25 added−9 removed
Provenance
- Commit
26ad7da- Tree
86b34cd- Author
Dock <dockconnellyoneal@gmail.com>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
c5b920b
4 files changed+25 added−9 removed
added:src/exit_codes.rs+4 added
1+/// exit code 1 represents a general error2+pub const ERROR: i32 = 1;3+/// exit code 130 represents a process killed by signal SIGINT4+pub const SIGINT: i32 = 130;modified:src/main.rs+1 added
17 unmodified lines18 extern crate num_cpus;19 extern crate regex;20 extern crate regex_syntax;21 22+mod exit_codes;23 pub mod fshelper;24 pub mod lscolors;25 mod app;26 mod exec;147 unmodified linesmodified:src/output.rs+3 added-5 removed
4 unmodified lines5 // at your option. All files in the project carrying such6 // notice may not be copied, modified, or distributed except7 // according to those terms.8 9+use exit_codes;10 use internal::FdOptions;11 use lscolors::LsColors;12 13 use std::{fs, process};6 unmodified lines20 use std::os::unix::fs::PermissionsExt;21 22 use ansi_term;23 23-const EXIT_CODE_ERROR: i32 = 1;24-const EXIT_CODE_SIGINT: i32 = 130;25-24 pub fn print_entry(entry: &PathBuf, config: &FdOptions, wants_to_quit: &Arc<AtomicBool>) {25 let path = entry.strip_prefix(".").unwrap_or(entry);26 27 let r = if let Some(ref ls_colors) = config.ls_colors {3 unmodified lines31 };32 33 if r.is_err() {34 // Probably a broken pipe. Exit gracefully.37- process::exit(EXIT_CODE_ERROR);35+ process::exit(exit_codes::ERROR);36 }37 }38 39 fn print_entry_colorized(33 unmodified lines73 };74 75 if wants_to_quit.load(Ordering::Relaxed) {76 write!(handle, "\n")?;79- process::exit(EXIT_CODE_SIGINT);77+ process::exit(exit_codes::SIGINT);78 }79 }80 81 if config.null_separator {51 unmodified linesmodified:src/walk.rs+17 added-4 removed
11 unmodified lines12 use fshelper;13 use internal::{error, FdOptions};14 use output;15 16+use exit_codes;17+use std::process;18 use std::path::PathBuf;19 use std::sync::{Arc, Mutex};20 use std::sync::atomic::{AtomicBool, Ordering};21 use std::sync::mpsc::channel;65 unmodified lines87 88 let parallel_walker = walker.threads(threads).build_parallel();89 90 let wants_to_quit = Arc::new(AtomicBool::new(false));91+ let receiver_wtq = Arc::clone(&wants_to_quit);92+ let sender_wtq = Arc::clone(&wants_to_quit);93 if config.ls_colors.is_some() {90- let wq = Arc::clone(&wants_to_quit);94+ let wq = Arc::clone(&receiver_wtq);95 ctrlc::set_handler(move || {96 wq.store(true, Ordering::Relaxed);97 }).unwrap();98 }52 unmodified lines151 // Have we reached the maximum time?152 if time::Instant::now() - start > max_buffer_time {153 // Flush the buffer154 for v in &buffer {151- output::print_entry(v, &rx_config, &wants_to_quit);155+ output::print_entry(v, &rx_config, &receiver_wtq);156 }157 buffer.clear();158 159 // Start streaming160 mode = ReceiverMode::Streaming;161 }162 }163 ReceiverMode::Streaming => {160- output::print_entry(&value, &rx_config, &wants_to_quit);164+ output::print_entry(&value, &rx_config, &receiver_wtq);165 }166 }167 }168 1 unmodified line170 // anything to the console, yet. In this case, sort the results and print them:171 if !buffer.is_empty() {172 buffer.sort();173 for value in buffer {170- output::print_entry(&value, &rx_config, &wants_to_quit);174+ output::print_entry(&value, &rx_config, &receiver_wtq);175 }176 }177 }178 });2 unmodified lines181 parallel_walker.run(|| {182 let config = Arc::clone(&config);183 let pattern = Arc::clone(&pattern);184 let tx_thread = tx.clone();185+ let wants_to_quit = Arc::clone(&sender_wtq);186 187 Box::new(move |entry_o| {188+ if wants_to_quit.load(Ordering::Relaxed) {189+ return ignore::WalkState::Quit;190+ }191+192 let entry = match entry_o {193 Ok(e) => e,194 Err(_) => return ignore::WalkState::Continue,195 };50 unmodified lines246 drop(tx);247 248 // Wait for the receiver thread to print out all results.249 receiver_thread.join().unwrap();250+251+ if wants_to_quit.load(Ordering::Relaxed) {252+ process::exit(exit_codes::SIGINT);253+ }254 }