From 2ebef2d46f3b0295624bde4a1582ad3cd9b06686 Mon Sep 17 00:00:00 2001 From: psinghal20 Date: Sun, 30 Sep 2018 18:31:23 +0530 Subject: [PATCH] feat: enable display of error messages --- src/exec/job.rs | 14 ++++++++++--- src/walk.rs | 54 ++++++++++++++++++++++++++++++++----------------- 2 files changed, 46 insertions(+), 22 deletions(-) diff --git a/src/exec/job.rs b/src/exec/job.rs index 3b0fa9c..808b015 100644 --- a/src/exec/job.rs +++ b/src/exec/job.rs @@ -9,13 +9,14 @@ use std::path::PathBuf; use std::sync::mpsc::Receiver; use std::sync::{Arc, Mutex}; - +use::walk::WorkerResult; +use::internal::error; use super::CommandTemplate; /// An event loop that listens for inputs from the `rx` receiver. Each received input will /// generate a command with the supplied command template. The generated command will then /// be executed, and this process will continue until the receiver's sender has closed. -pub fn job(rx: Arc>>, cmd: Arc, out_perm: Arc>) { +pub fn job(rx: Arc>>, cmd: Arc, out_perm: Arc>) { loop { // Create a lock on the shared receiver for this thread. let lock = rx.lock().unwrap(); @@ -23,7 +24,14 @@ pub fn job(rx: Arc>>, cmd: Arc, out_per // Obtain the next path from the receiver, else if the channel // has closed, exit from the loop let value: PathBuf = match lock.recv() { - Ok(value) => value, + Ok(value) => { + match value { + WorkerResult::Entry(val) => val, + WorkerResult::Error(err) => { + error(&format!("{}", err)); + } + } + }, Err(_) => break, }; diff --git a/src/walk.rs b/src/walk.rs index 689f765..d39279b 100644 --- a/src/walk.rs +++ b/src/walk.rs @@ -36,6 +36,12 @@ enum ReceiverMode { Streaming, } +/// The Worker threads can result in a valid entry having PathBuf or an error. +pub enum WorkerResult { + Entry(PathBuf), + Error(ignore::Error) +} + /// Recursively scan the given search path for files / pathnames matching the pattern. /// /// If the `--exec` argument was supplied, this will create a thread pool for executing @@ -156,27 +162,34 @@ pub fn scan(path_vec: &[PathBuf], pattern: Arc, config: Arc) { .max_buffer_time .unwrap_or_else(|| time::Duration::from_millis(100)); - for value in rx { - match mode { - ReceiverMode::Buffering => { - buffer.push(value); + for worker_result in rx { + match worker_result { + WorkerResult::Entry(value) => { + match mode { + ReceiverMode::Buffering => { + buffer.push(value); - // Have we reached the maximum buffer size or maximum buffering time? - if buffer.len() > MAX_BUFFER_LENGTH - || time::Instant::now() - start > max_buffer_time - { - // Flush the buffer - for v in &buffer { - output::print_entry(v, &rx_config, &receiver_wtq); + // Have we reached the maximum buffer size or maximum buffering time? + if buffer.len() > MAX_BUFFER_LENGTH + || time::Instant::now() - start > max_buffer_time + { + // Flush the buffer + for v in &buffer { + output::print_entry(v, &rx_config, &receiver_wtq); + } + buffer.clear(); + + // Start streaming + mode = ReceiverMode::Streaming; + } + } + ReceiverMode::Streaming => { + output::print_entry(&value, &rx_config, &receiver_wtq); } - buffer.clear(); - - // Start streaming - mode = ReceiverMode::Streaming; } } - ReceiverMode::Streaming => { - output::print_entry(&value, &rx_config, &receiver_wtq); + WorkerResult::Error(err) => { + error(&format!("{}", err)); } } } @@ -206,7 +219,10 @@ pub fn scan(path_vec: &[PathBuf], pattern: Arc, config: Arc) { let entry = match entry_o { Ok(e) => e, - Err(_) => return ignore::WalkState::Continue, + Err(err) => { + tx_thread.send(WorkerResult::Error(err)).unwrap(); + return ignore::WalkState::Continue; + } }; if entry.depth() == 0 { @@ -284,7 +300,7 @@ pub fn scan(path_vec: &[PathBuf], pattern: Arc, config: Arc) { if let Some(search_str) = search_str_o { if pattern.is_match(&*search_str) { // TODO: take care of the unwrap call - tx_thread.send(entry_path.to_owned()).unwrap() + tx_thread.send(WorkerResult::Entry(entry_path.to_owned())).unwrap() } }