1
Fork 0

Add accessors to Command.

This commit is contained in:
Eric Huss 2020-09-21 11:32:06 -07:00
parent 623fb90b5a
commit c297e20e03
8 changed files with 303 additions and 8 deletions

View file

@ -1,6 +1,7 @@
pub use self::process_common::{Command, ExitCode, Stdio, StdioPipes};
pub use self::process_common::{Command, CommandArgs, ExitCode, Stdio, StdioPipes};
pub use self::process_inner::{ExitStatus, Process};
pub use crate::ffi::OsString as EnvKey;
pub use crate::sys_common::process::CommandEnvs;
mod process_common;
#[cfg(not(target_os = "fuchsia"))]

View file

@ -7,11 +7,12 @@ use crate::collections::BTreeMap;
use crate::ffi::{CStr, CString, OsStr, OsString};
use crate::fmt;
use crate::io;
use crate::path::Path;
use crate::ptr;
use crate::sys::fd::FileDesc;
use crate::sys::fs::File;
use crate::sys::pipe::{self, AnonPipe};
use crate::sys_common::process::CommandEnv;
use crate::sys_common::process::{CommandEnv, CommandEnvs};
#[cfg(not(target_os = "fuchsia"))]
use crate::sys::fs::OpenOptions;
@ -184,11 +185,30 @@ impl Command {
pub fn saw_nul(&self) -> bool {
self.saw_nul
}
pub fn get_program(&self) -> &OsStr {
OsStr::from_bytes(self.program.as_bytes())
}
pub fn get_args(&self) -> CommandArgs<'_> {
let mut iter = self.args.iter();
iter.next();
CommandArgs { iter }
}
pub fn get_envs(&self) -> CommandEnvs<'_> {
self.env.iter()
}
pub fn get_current_dir(&self) -> Option<&Path> {
self.cwd.as_ref().map(|cs| Path::new(OsStr::from_bytes(cs.as_bytes())))
}
pub fn get_argv(&self) -> &Vec<*const c_char> {
&self.argv.0
}
pub fn get_program(&self) -> &CStr {
pub fn get_program_cstr(&self) -> &CStr {
&*self.program
}
@ -402,3 +422,32 @@ impl ExitCode {
self.0 as i32
}
}
pub struct CommandArgs<'a> {
iter: crate::slice::Iter<'a, CString>,
}
impl<'a> Iterator for CommandArgs<'a> {
type Item = &'a OsStr;
fn next(&mut self) -> Option<&'a OsStr> {
self.iter.next().map(|cs| OsStr::from_bytes(cs.as_bytes()))
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
}
impl<'a> ExactSizeIterator for CommandArgs<'a> {
fn len(&self) -> usize {
self.iter.len()
}
fn is_empty(&self) -> bool {
self.iter.is_empty()
}
}
impl<'a> fmt::Debug for CommandArgs<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list().entries(self.iter.clone()).finish()
}
}

View file

@ -120,7 +120,7 @@ impl Command {
| FDIO_SPAWN_CLONE_NAMESPACE
| FDIO_SPAWN_CLONE_ENVIRON // this is ignored when envp is non-null
| FDIO_SPAWN_CLONE_UTC_CLOCK,
self.get_program().as_ptr(),
self.get_program_cstr().as_ptr(),
self.get_argv().as_ptr(),
envp,
actions.len() as size_t,

View file

@ -245,7 +245,7 @@ impl Command {
*sys::os::environ() = envp.as_ptr();
}
libc::execvp(self.get_program().as_ptr(), self.get_argv().as_ptr());
libc::execvp(self.get_program_cstr().as_ptr(), self.get_argv().as_ptr());
Err(io::Error::last_os_error())
}
@ -383,7 +383,7 @@ impl Command {
let envp = envp.map(|c| c.as_ptr()).unwrap_or_else(|| *sys::os::environ() as *const _);
let ret = libc::posix_spawnp(
&mut p.pid,
self.get_program().as_ptr(),
self.get_program_cstr().as_ptr(),
file_actions.0.as_ptr(),
attrs.0.as_ptr(),
self.get_argv().as_ptr() as *const _,