1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
use crate::{process, CmdResult, FunResult};
use log::{info, warn};
use os_pipe::PipeReader;
use std::io::{BufRead, BufReader, Error, ErrorKind, Read, Result};
use std::process::{Child, ExitStatus};
use std::thread::JoinHandle;

/// Representation of running or exited children processes, connected with pipes
/// optionally.
///
/// Calling `spawn!` macro will return `Result<CmdChildren>`
pub struct CmdChildren {
    children: Vec<CmdChild>,
    ignore_error: bool,
}

impl CmdChildren {
    pub(crate) fn new(children: Vec<CmdChild>, ignore_error: bool) -> Self {
        Self {
            children,
            ignore_error,
        }
    }

    pub(crate) fn into_fun_children(self) -> FunChildren {
        FunChildren {
            children: self.children,
            ignore_error: self.ignore_error,
        }
    }

    pub fn wait(&mut self) -> CmdResult {
        // wait for the last child result
        let handle = self.children.pop().unwrap();
        if let Err(e) = handle.wait(true) {
            let _ = Self::wait_children(&mut self.children);
            return Err(e);
        }
        Self::wait_children(&mut self.children)
    }

    fn wait_children(children: &mut Vec<CmdChild>) -> CmdResult {
        let mut ret = Ok(());
        while !children.is_empty() {
            let child_handle = children.pop().unwrap();
            if let Err(e) = child_handle.wait(false) {
                ret = Err(e);
            }
        }
        ret
    }
}

/// Representation of running or exited children processes with output, connected with pipes
/// optionally.
///
/// Calling `spawn_with_output!` macro will return `Result<FunChildren>`
pub struct FunChildren {
    children: Vec<CmdChild>,
    ignore_error: bool,
}

impl FunChildren {
    pub fn wait_with_output(&mut self) -> FunResult {
        // wait for the last child result
        let handle = self.children.pop().unwrap();
        let wait_last = handle.wait_with_output(self.ignore_error);
        match wait_last {
            Err(e) => {
                let _ = CmdChildren::wait_children(&mut self.children);
                Err(e)
            }
            Ok(output) => {
                let mut s = String::from_utf8_lossy(&output).to_string();
                if s.ends_with('\n') {
                    s.pop();
                }
                let ret = CmdChildren::wait_children(&mut self.children);
                if let Err(e) = ret {
                    if !self.ignore_error {
                        return Err(e);
                    }
                }
                Ok(s)
            }
        }
    }

    pub fn wait_with_pipe(&mut self, f: &mut dyn FnMut(Box<dyn Read>)) -> CmdResult {
        let child = self.children.pop().unwrap();
        let polling_stderr = StderrLogging::new(&child.cmd, child.stderr);
        match child.handle {
            CmdChildHandle::Proc(proc) => match proc {
                Err(e) => return Err(CmdChildHandle::cmd_io_error(e, &child.cmd, true)),
                Ok(mut proc) => {
                    if let Some(stdout) = child.stdout {
                        f(Box::new(stdout));
                        let _ = proc.kill();
                    }
                }
            },
            CmdChildHandle::Thread(thread) => match thread {
                Err(e) => return Err(CmdChildHandle::cmd_io_error(e, &child.cmd, true)),
                Ok(_) => {
                    if let Some(stdout) = child.stdout {
                        f(Box::new(stdout));
                    }
                }
            },
            CmdChildHandle::SyncFn(sync_fn) => match sync_fn {
                Err(e) => return Err(CmdChildHandle::cmd_io_error(e, &child.cmd, true)),
                Ok(_) => {
                    if let Some(stdout) = child.stdout {
                        f(Box::new(stdout));
                    }
                }
            },
        };
        drop(polling_stderr);
        CmdChildren::wait_children(&mut self.children)
    }
}

#[derive(Debug)]
pub(crate) struct CmdChild {
    handle: CmdChildHandle,
    cmd: String,
    stdout: Option<PipeReader>,
    stderr: Option<PipeReader>,
}

impl CmdChild {
    pub(crate) fn new(
        handle: CmdChildHandle,
        cmd: String,
        stdout: Option<PipeReader>,
        stderr: Option<PipeReader>,
    ) -> Self {
        Self {
            handle,
            cmd,
            stdout,
            stderr,
        }
    }

    fn wait(self, is_last: bool) -> CmdResult {
        let res = self.handle.wait_with_stderr(self.stderr, &self.cmd);
        if let Err(e) = res {
            if is_last || process::pipefail_enabled() {
                return Err(e);
            }
        }
        Ok(())
    }

    fn wait_with_output(self, ignore_error: bool) -> Result<Vec<u8>> {
        let buf = {
            if let Some(mut out) = self.stdout {
                let mut buf = vec![];
                if let Err(e) = out.read_to_end(&mut buf) {
                    if !ignore_error {
                        return Err(CmdChildHandle::cmd_io_error(e, &self.cmd, false));
                    }
                }
                buf
            } else {
                vec![]
            }
        };
        let res = self.handle.wait_with_stderr(self.stderr, &self.cmd);
        if let Err(e) = res {
            if !ignore_error {
                return Err(e);
            }
        }
        Ok(buf)
    }
}

#[derive(Debug)]
pub(crate) enum CmdChildHandle {
    Proc(Result<Child>),
    Thread(Result<JoinHandle<CmdResult>>),
    SyncFn(CmdResult),
}

impl CmdChildHandle {
    fn wait_with_stderr(self, stderr: Option<PipeReader>, cmd: &str) -> CmdResult {
        let polling_stderr = StderrLogging::new(cmd, stderr);
        match self {
            CmdChildHandle::Proc(proc) => match proc {
                Err(e) => return Err(CmdChildHandle::cmd_io_error(e, cmd, true)),
                Ok(mut proc) => {
                    let status = proc.wait();
                    match status {
                        Err(e) => return Err(CmdChildHandle::cmd_io_error(e, cmd, false)),
                        Ok(status) => {
                            if !status.success() {
                                return Err(Self::status_to_io_error(
                                    status,
                                    &format!("Running {} exited with error", cmd),
                                ));
                            }
                        }
                    }
                }
            },
            CmdChildHandle::Thread(thread) => match thread {
                Err(e) => return Err(CmdChildHandle::cmd_io_error(e, cmd, true)),
                Ok(thread) => {
                    let status = thread.join();
                    match status {
                        Ok(result) => {
                            if let Err(e) = result {
                                return Err(CmdChildHandle::cmd_io_error(e, cmd, false));
                            }
                        }
                        Err(e) => {
                            return Err(Error::new(
                                ErrorKind::Other,
                                format!("Running {} thread joined with error: {:?}", cmd, e),
                            ))
                        }
                    }
                }
            },
            CmdChildHandle::SyncFn(sync_fn) => {
                if let Err(e) = sync_fn {
                    return Err(CmdChildHandle::cmd_io_error(e, cmd, false));
                }
            }
        }
        drop(polling_stderr);
        Ok(())
    }

    fn cmd_io_error(e: Error, command: &str, spawning: bool) -> Error {
        Error::new(
            e.kind(),
            format!(
                "{} {} failed: {}",
                if spawning { "Spawning" } else { "Running" },
                command,
                e
            ),
        )
    }

    fn status_to_io_error(status: ExitStatus, command: &str) -> Error {
        if let Some(code) = status.code() {
            Error::new(
                ErrorKind::Other,
                format!("{}; status code: {}", command, code),
            )
        } else {
            Error::new(
                ErrorKind::Other,
                format!("{}; terminated by {}", command, status),
            )
        }
    }
}

struct StderrLogging {
    thread: Option<JoinHandle<()>>,
    cmd: String,
}

impl StderrLogging {
    fn new(cmd: &str, stderr: Option<PipeReader>) -> Self {
        if let Some(stderr) = stderr {
            let thread = std::thread::spawn(move || {
                BufReader::new(stderr)
                    .lines()
                    .filter_map(|line| line.ok())
                    .for_each(|line| info!("{}", line))
            });
            Self {
                cmd: cmd.into(),
                thread: Some(thread),
            }
        } else {
            Self {
                cmd: cmd.into(),
                thread: None,
            }
        }
    }
}

impl Drop for StderrLogging {
    fn drop(&mut self) {
        if let Some(thread) = self.thread.take() {
            if let Err(e) = thread.join() {
                warn!("{} logging thread exited with error: {:?}", self.cmd, e);
            }
        }
    }
}