diff --git a/src/exec/job.rs b/src/exec/job.rs index 2707238..017b8ef 100644 --- a/src/exec/job.rs +++ b/src/exec/job.rs @@ -21,7 +21,8 @@ pub fn job( cmd: Arc, out_perm: Arc>, show_filesystem_errors: bool, -) { +) -> ExitCode { + let mut results: Vec = Vec::new(); loop { // Create a lock on the shared receiver for this thread. let lock = rx.lock().unwrap(); @@ -39,11 +40,13 @@ pub fn job( Err(_) => break, }; - // Drop the lock so that other threads can read from the the receiver. + // Drop the lock so that other threads can read from the receiver. drop(lock); - // Generate a command and execute it. - cmd.generate_and_execute(&value, Arc::clone(&out_perm)); + // Generate a command, executes it and store its exit code. + results.push(cmd.generate_and_execute(&value, Arc::clone(&out_perm))) } + // Returns error in case of any error. + ExitCode::error_if_any_error(results) } pub fn batch( diff --git a/src/exit_codes.rs b/src/exit_codes.rs index 0265627..6905f47 100644 --- a/src/exit_codes.rs +++ b/src/exit_codes.rs @@ -7,9 +7,21 @@ pub enum ExitCode { impl Into for ExitCode { fn into(self) -> i32 { match self { - ExitCode::Success => 0, - ExitCode::GeneralError => 1, - ExitCode::KilledBySigint => 130, + Self::Success => 0, + Self::GeneralError => 1, + Self::KilledBySigint => 130, } } } + +impl ExitCode { + pub fn error_if_any_error(results: Vec) -> Self { + if results.iter().any(|s| match s { + Self::GeneralError => true, + _ => false, + }) { + return Self::GeneralError; + } + Self::Success + } +} diff --git a/src/walk.rs b/src/walk.rs index 77ec7c9..e0716be 100644 --- a/src/walk.rs +++ b/src/walk.rs @@ -177,11 +177,12 @@ fn spawn_receiver( } // Wait for all threads to exit before exiting the program. + let mut results: Vec = Vec::new(); for h in handles { - h.join().unwrap(); + results.push(h.join().unwrap()); } - ExitCode::Success + ExitCode::error_if_any_error(results) } } else { let start = time::Instant::now();