Skip to content
Myr

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

4 files+25 added−9 removed

added:src/exit_codes.rs+4 added
1/// exit code 1 represents a general error2pub const ERROR: i32 = 1;3/// exit code 130 represents a process killed by signal SIGINT4pub const SIGINT: i32 = 130;
modified:src/main.rs+1 added
17 unmodified lines
18extern crate num_cpus;19extern crate regex;20extern crate regex_syntax;2122mod exit_codes;23pub mod fshelper;24pub mod lscolors;25mod app;26mod exec;
147 unmodified lines
modified:src/output.rs+3 added-5 removed
4 unmodified lines
5// at your option. All files in the project carrying such6// notice may not be copied, modified, or distributed except7// according to those terms.89use exit_codes;10use internal::FdOptions;11use lscolors::LsColors;1213use std::{fs, process};
6 unmodified lines
20use std::os::unix::fs::PermissionsExt;2122use ansi_term;2323const EXIT_CODE_ERROR: i32 = 1;24const EXIT_CODE_SIGINT: i32 = 130;2524pub fn print_entry(entry: &PathBuf, config: &FdOptions, wants_to_quit: &Arc<AtomicBool>) {25 let path = entry.strip_prefix(".").unwrap_or(entry);2627 let r = if let Some(ref ls_colors) = config.ls_colors {
3 unmodified lines
31 };3233 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}3839fn print_entry_colorized(
33 unmodified lines
73 };7475 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 }8081 if config.null_separator {
51 unmodified lines
modified:src/walk.rs+17 added-4 removed
11 unmodified lines
12use fshelper;13use internal::{error, FdOptions};14use output;1516use exit_codes;17use std::process;18use std::path::PathBuf;19use std::sync::{Arc, Mutex};20use std::sync::atomic::{AtomicBool, Ordering};21use std::sync::mpsc::channel;
65 unmodified lines
8788 let parallel_walker = walker.threads(threads).build_parallel();8990 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 lines
151 // 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();158159 // 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 line
170 // 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 lines
181 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);186187 Box::new(move |entry_o| {188 if wants_to_quit.load(Ordering::Relaxed) {189 return ignore::WalkState::Quit;190 }191192 let entry = match entry_o {193 Ok(e) => e,194 Err(_) => return ignore::WalkState::Continue,195 };
50 unmodified lines
246 drop(tx);247248 // Wait for the receiver thread to print out all results.249 receiver_thread.join().unwrap();250251 if wants_to_quit.load(Ordering::Relaxed) {252 process::exit(exit_codes::SIGINT);253 }254}