2023-01-19 07:24:05 +01:00
|
|
|
use std::sync::Mutex;
|
2017-10-14 18:04:11 +02:00
|
|
|
|
2022-09-27 22:09:21 +02:00
|
|
|
use crate::config::Config;
|
2020-04-03 21:18:54 +02:00
|
|
|
use crate::error::print_error;
|
|
|
|
use crate::exit_codes::{merge_exitcodes, ExitCode};
|
|
|
|
use crate::walk::WorkerResult;
|
|
|
|
|
2022-02-18 09:21:00 +01:00
|
|
|
use super::CommandSet;
|
2020-04-03 21:18:54 +02:00
|
|
|
|
2017-10-14 18:04:11 +02:00
|
|
|
/// 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.
|
2018-09-30 22:56:32 +02:00
|
|
|
pub fn job(
|
2023-11-05 20:55:37 +01:00
|
|
|
results: impl IntoIterator<Item = WorkerResult>,
|
2023-01-19 07:24:05 +01:00
|
|
|
cmd: &CommandSet,
|
|
|
|
out_perm: &Mutex<()>,
|
2022-09-27 22:09:21 +02:00
|
|
|
config: &Config,
|
2020-01-25 17:13:47 +01:00
|
|
|
) -> ExitCode {
|
2022-09-27 22:09:21 +02:00
|
|
|
// Output should be buffered when only running a single thread
|
|
|
|
let buffer_output: bool = config.threads > 1;
|
|
|
|
|
2023-11-05 20:55:37 +01:00
|
|
|
let mut ret = ExitCode::Success;
|
|
|
|
for result in results {
|
2018-10-01 21:29:54 +02:00
|
|
|
// Obtain the next result from the receiver, else if the channel
|
2017-10-14 18:04:11 +02:00
|
|
|
// has closed, exit from the loop
|
2023-11-05 20:55:37 +01:00
|
|
|
let dir_entry = match result {
|
|
|
|
WorkerResult::Entry(dir_entry) => dir_entry,
|
|
|
|
WorkerResult::Error(err) => {
|
2022-09-27 22:09:21 +02:00
|
|
|
if config.show_filesystem_errors {
|
2020-04-03 21:18:54 +02:00
|
|
|
print_error(err.to_string());
|
2018-10-22 14:20:08 +02:00
|
|
|
}
|
2018-10-01 21:29:54 +02:00
|
|
|
continue;
|
|
|
|
}
|
2017-10-14 18:04:11 +02:00
|
|
|
};
|
2021-08-09 09:02:30 +02:00
|
|
|
|
2020-01-26 14:06:18 +01:00
|
|
|
// Generate a command, execute it and store its exit code.
|
2023-11-05 20:55:37 +01:00
|
|
|
let code = cmd.execute(
|
2022-09-27 22:09:21 +02:00
|
|
|
dir_entry.stripped_path(config),
|
2022-10-09 09:00:48 +02:00
|
|
|
config.path_separator.as_deref(),
|
2023-01-19 07:24:05 +01:00
|
|
|
out_perm,
|
2022-06-17 10:08:24 +02:00
|
|
|
buffer_output,
|
2023-11-05 20:55:37 +01:00
|
|
|
);
|
|
|
|
ret = merge_exitcodes([ret, code]);
|
2017-10-14 18:04:11 +02:00
|
|
|
}
|
2020-01-25 17:13:47 +01:00
|
|
|
// Returns error in case of any error.
|
2023-11-05 20:55:37 +01:00
|
|
|
ret
|
2017-10-14 20:04:04 +02:00
|
|
|
}
|
2018-11-11 18:00:01 +01:00
|
|
|
|
2023-11-05 20:55:37 +01:00
|
|
|
pub fn batch(
|
|
|
|
results: impl IntoIterator<Item = WorkerResult>,
|
|
|
|
cmd: &CommandSet,
|
|
|
|
config: &Config,
|
|
|
|
) -> ExitCode {
|
|
|
|
let paths = results
|
2021-11-30 08:52:39 +01:00
|
|
|
.into_iter()
|
|
|
|
.filter_map(|worker_result| match worker_result {
|
2022-09-27 22:09:21 +02:00
|
|
|
WorkerResult::Entry(dir_entry) => Some(dir_entry.into_stripped_path(config)),
|
2021-11-30 08:52:39 +01:00
|
|
|
WorkerResult::Error(err) => {
|
2022-09-27 22:09:21 +02:00
|
|
|
if config.show_filesystem_errors {
|
2021-11-30 08:52:39 +01:00
|
|
|
print_error(err.to_string());
|
|
|
|
}
|
|
|
|
None
|
2018-11-11 18:00:01 +01:00
|
|
|
}
|
2021-11-30 08:52:39 +01:00
|
|
|
});
|
2021-10-22 08:05:13 +02:00
|
|
|
|
2022-10-09 09:00:48 +02:00
|
|
|
cmd.execute_batch(paths, config.batch_size, config.path_separator.as_deref())
|
2018-11-11 18:00:01 +01:00
|
|
|
}
|