From dc828fed2dddf263620dd3e0e8e232e3ea7aebd6 Mon Sep 17 00:00:00 2001 From: Terts Diepraam Date: Mon, 28 Nov 2022 22:18:08 +0100 Subject: [PATCH] uucore/process: remove custom ExitStatus --- src/uu/timeout/src/timeout.rs | 1 + src/uucore/src/lib/features/process.rs | 63 ++------------------------ 2 files changed, 4 insertions(+), 60 deletions(-) diff --git a/src/uu/timeout/src/timeout.rs b/src/uu/timeout/src/timeout.rs index 715a802cf..246537a30 100644 --- a/src/uu/timeout/src/timeout.rs +++ b/src/uu/timeout/src/timeout.rs @@ -11,6 +11,7 @@ mod status; use crate::status::ExitStatus; use clap::{crate_version, Arg, ArgAction, Command}; use std::io::ErrorKind; +use std::os::unix::process::ExitStatusExt; use std::process::{self, Child, Stdio}; use std::time::Duration; use uucore::display::Quotable; diff --git a/src/uucore/src/lib/features/process.rs b/src/uucore/src/lib/features/process.rs index 816b06a75..933b1f21b 100644 --- a/src/uucore/src/lib/features/process.rs +++ b/src/uucore/src/lib/features/process.rs @@ -12,10 +12,9 @@ //! Set of functions to manage IDs use libc::{gid_t, pid_t, uid_t}; -use std::fmt; use std::io; use std::process::Child; -use std::process::ExitStatus as StdExitStatus; +use std::process::ExitStatus; use std::thread; use std::time::{Duration, Instant}; @@ -41,60 +40,6 @@ pub fn getuid() -> uid_t { unsafe { libc::getuid() } } -// This is basically sys::unix::process::ExitStatus -#[derive(PartialEq, Eq, Clone, Copy, Debug)] -pub enum ExitStatus { - Code(i32), - Signal(i32), -} - -#[allow(clippy::trivially_copy_pass_by_ref)] -impl ExitStatus { - fn from_std_status(status: StdExitStatus) -> Self { - #[cfg(unix)] - { - use std::os::unix::process::ExitStatusExt; - - if let Some(signal) = status.signal() { - return Self::Signal(signal); - } - } - - // NOTE: this should never fail as we check if the program exited through a signal above - Self::Code(status.code().unwrap()) - } - - pub fn success(&self) -> bool { - match *self { - Self::Code(code) => code == 0, - _ => false, - } - } - - pub fn code(&self) -> Option { - match *self { - Self::Code(code) => Some(code), - _ => None, - } - } - - pub fn signal(&self) -> Option { - match *self { - Self::Signal(code) => Some(code), - _ => None, - } - } -} - -impl fmt::Display for ExitStatus { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match *self { - Self::Code(code) => write!(f, "exit code: {}", code), - Self::Signal(code) => write!(f, "exit code: {}", code), - } - } -} - /// Missing methods for Child objects pub trait ChildExt { /// Send a signal to a Child process. @@ -119,9 +64,7 @@ impl ChildExt for Child { fn wait_or_timeout(&mut self, timeout: Duration) -> io::Result> { if timeout == Duration::from_micros(0) { - return self - .wait() - .map(|status| Some(ExitStatus::from_std_status(status))); + return self.wait().map(Some); } // .try_wait() doesn't drop stdin, so we do it manually drop(self.stdin.take()); @@ -129,7 +72,7 @@ impl ChildExt for Child { let start = Instant::now(); loop { if let Some(status) = self.try_wait()? { - return Ok(Some(ExitStatus::from_std_status(status))); + return Ok(Some(status)); } if start.elapsed() >= timeout {