From 5443aac767083584a8368301d198d78d16bf2b9a Mon Sep 17 00:00:00 2001 From: sxyazi Date: Sat, 23 Sep 2023 23:15:43 +0800 Subject: [PATCH] .. --- core/src/external/shell.rs | 7 +++++++ core/src/tasks/workers/process.rs | 18 ++++++++++++++---- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/core/src/external/shell.rs b/core/src/external/shell.rs index a0eaea75..2ca8f185 100644 --- a/core/src/external/shell.rs +++ b/core/src/external/shell.rs @@ -10,6 +10,13 @@ pub struct ShellOpt { pub orphan: bool, } +impl ShellOpt { + pub fn with_piped(mut self) -> Self { + self.piped = true; + self + } +} + pub fn shell(opt: ShellOpt) -> Result { #[cfg(not(target_os = "windows"))] { diff --git a/core/src/tasks/workers/process.rs b/core/src/tasks/workers/process.rs index b5f04ac6..6bed7b44 100644 --- a/core/src/tasks/workers/process.rs +++ b/core/src/tasks/workers/process.rs @@ -1,4 +1,4 @@ -use std::ffi::OsString; +use std::{ffi::OsString, mem}; use anyhow::Result; use tokio::{io::{AsyncBufReadExt, BufReader}, select, sync::{mpsc, oneshot}}; @@ -20,6 +20,17 @@ pub(crate) struct ProcessOpOpen { pub cancel: oneshot::Sender<()>, } +impl From<&mut ProcessOpOpen> for ShellOpt { + fn from(value: &mut ProcessOpOpen) -> Self { + Self { + cmd: mem::take(&mut value.cmd), + args: mem::take(&mut value.args), + piped: false, + orphan: value.orphan, + } + } +} + impl Process { pub(crate) fn new(sch: mpsc::UnboundedSender) -> Self { Self { sch } } @@ -30,12 +41,11 @@ impl Process { fn done(&self, id: usize) -> Result<()> { Ok(self.sch.send(TaskOp::Done(id))?) } pub(crate) async fn open(&self, mut task: ProcessOpOpen) -> Result<()> { - let opt = ShellOpt { cmd: task.cmd, args: task.args, piped: true, orphan: task.orphan }; if task.block { let _guard = BLOCKER.acquire().await.unwrap(); emit!(Stop(true)).await; - match external::shell(ShellOpt { piped: false, ..opt }) { + match external::shell(ShellOpt::from(&mut task)) { Ok(mut child) => { child.wait().await.ok(); } @@ -50,7 +60,7 @@ impl Process { } self.sch.send(TaskOp::New(task.id, 0))?; - let mut child = external::shell(opt)?; + let mut child = external::shell(ShellOpt::from(&mut task).with_piped())?; let mut stdout = BufReader::new(child.stdout.take().unwrap()).lines(); let mut stderr = BufReader::new(child.stderr.take().unwrap()).lines();