Skip to content

[WIP] Add size-limited command interface, take 2 #82930

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,14 @@ dependencies = [
"cargo-util",
"clap",
"crates-io",
<<<<<<< HEAD
"crossbeam-utils 0.8.3",
||||||| parent of 1725fb7e1bd (actually get size adjustment to work)
"crossbeam-utils 0.8.0",
=======
"crossbeam-utils 0.8.0",
"crypto-hash",
>>>>>>> 1725fb7e1bd (actually get size adjustment to work)
"curl",
"curl-sys",
"env_logger 0.8.1",
Expand Down Expand Up @@ -5569,11 +5576,12 @@ checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"

[[package]]
name = "vergen"
version = "5.1.0"
version = "5.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dfbc87f9a7a9d61b15d51d1d3547284f67b6b4f1494ce3fc5814c101f35a5183"
checksum = "adf0b57f76a4f7e9673db1e7ffa4541d215ae8336fee45f5c1378bdeb22a0314"
dependencies = [
"anyhow",
"cfg-if 1.0.0",
"chrono",
"enum-iterator",
"getset",
Expand Down
26 changes: 25 additions & 1 deletion library/std/src/os/unix/process.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//! Unix-specific extensions to primitives in the `std::process` module.

#![stable(feature = "rust1", since = "1.0.0")]

use crate::ffi::OsStr;
Expand All @@ -10,6 +9,9 @@ use crate::sealed::Sealed;
use crate::sys;
use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};

use crate::sys_common::process_ext;
impl_command_sized! { prelude }

/// Unix-specific extensions to the [`process::Command`] builder.
///
/// This trait is sealed: it cannot be implemented outside the standard library.
Expand Down Expand Up @@ -335,3 +337,25 @@ impl IntoRawFd for process::ChildStderr {
pub fn parent_id() -> u32 {
crate::sys::os::getppid()
}

fn maybe_arg_ext(celf: &mut sys::process::Command, arg: impl Arg) -> io::Result<()> {
celf.maybe_arg(arg.to_plain())
}

fn args_ext(
celf: &mut process::Command,
args: impl IntoIterator<Item = impl Arg>,
) -> &mut process::Command {
for arg in args {
celf.arg(arg.to_plain());
}
celf
}

#[unstable(feature = "command_sized", issue = "74549")]
#[cfg(unix)] // doc hack
impl process_ext::CommandSized for process::Command {
impl_command_sized! { marg maybe_arg_ext }
impl_command_sized! { margs maybe_arg_ext }
impl_command_sized! { xargs args_ext }
}
57 changes: 56 additions & 1 deletion library/std/src/os/windows/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,38 @@

#![stable(feature = "process_extensions", since = "1.2.0")]

use crate::ffi::{OsStr, OsString};
use crate::io;
use crate::os::windows::io::{AsRawHandle, FromRawHandle, IntoRawHandle, RawHandle};
use crate::process;
use crate::sealed::Sealed;
use crate::sys;
use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};
use crate::os::windows::ffi::OsStrExt;
#[unstable(feature = "windows_raw_cmdline", issue = "74549")]
pub use crate::sys_common::process_ext::{Arg, Problem};
use crate::sys_common::{process_ext, AsInner, AsInnerMut, FromInner, IntoInner};
use core::convert::TryFrom;

/// Argument type with no escaping.
#[unstable(feature = "windows_raw_cmdline", issue = "74549")]
pub struct RawArg<'a>(&'a OsStr);

// FIXME: Inhibiting doc on non-Windows due to mismatching trait methods.
#[cfg(any(windows))]
#[doc(cfg(windows))]
#[unstable(feature = "windows_raw_cmdline", issue = "74549")]
impl Arg for RawArg<'_> {
fn append_to(&self, cmd: &mut Vec<u16>, _fq: bool) -> Result<usize, Problem> {
cmd.extend(self.0.encode_wide());
self.arg_size(_fq)
}
fn arg_size(&self, _: bool) -> Result<usize, Problem> {
Ok(self.0.encode_wide().count() + 1)
}
fn to_os_string(&self) -> OsString {
OsStr::to_os_string(&(self.0))
}
}

#[stable(feature = "process_extensions", since = "1.2.0")]
impl FromRawHandle for process::Stdio {
Expand Down Expand Up @@ -125,6 +152,13 @@ pub trait CommandExt: Sealed {
/// [2]: <https://msdn.microsoft.com/en-us/library/17w5ykft.aspx>
#[unstable(feature = "windows_process_extensions_force_quotes", issue = "82227")]
fn force_quotes(&mut self, enabled: bool) -> &mut process::Command;
/// Pass an argument with custom escape rules.
#[unstable(feature = "windows_raw_cmdline", issue = "74549")]
fn arg_ext(&mut self, arg: impl Arg) -> &mut process::Command;

/// Pass arguments with custom escape rules.
#[unstable(feature = "windows_raw_cmdline", issue = "74549")]
fn args_ext(&mut self, args: impl IntoIterator<Item = impl Arg>) -> &mut process::Command;
}

#[stable(feature = "windows_process_extensions", since = "1.16.0")]
Expand All @@ -138,4 +172,25 @@ impl CommandExt for process::Command {
self.as_inner_mut().force_quotes(enabled);
self
}

fn arg_ext(&mut self, arg: impl Arg) -> &mut process::Command {
self.as_inner_mut().arg_ext(arg);
self
}

fn args_ext(&mut self, args: impl IntoIterator<Item = impl Arg>) -> &mut process::Command {
for arg in args {
self.arg_ext(arg);
}
self
}
}

// FIXME: export maybe_arg_ext so the macro doesn't explicitly reach for as_inner_mut()
#[unstable(feature = "command_sized", issue = "74549")]
#[cfg(windows)] // doc hack
impl process_ext::CommandSized for process::Command {
impl_command_sized! { marg sys::process::Command::maybe_arg_ext }
impl_command_sized! { margs sys::process::Command::maybe_arg_ext }
impl_command_sized! { xargs process::Command::args_ext }
}
2 changes: 1 addition & 1 deletion library/std/src/sys/unix/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub fn errno() -> i32 {
}

/// Sets the platform-specific value of errno
#[cfg(all(not(target_os = "linux"), not(target_os = "dragonfly"), not(target_os = "vxworks")))] // needed for readdir and syscall!
#[cfg(not(any(target_os = "dragonfly", target_os = "vxworks")))] // needed for readdir and syscall!
#[allow(dead_code)] // but not all target cfgs actually end up using it
pub fn set_errno(e: i32) {
unsafe { *errno_location() = e as c_int }
Expand Down
2 changes: 1 addition & 1 deletion library/std/src/sys/unix/process/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub use self::process_common::{Command, CommandArgs, ExitCode, Stdio, StdioPipes};
pub use self::process_common::{Arg, Command, CommandArgs, ExitCode, Problem, Stdio, StdioPipes};
pub use self::process_inner::{ExitStatus, Process};
pub use crate::ffi::OsString as EnvKey;
pub use crate::sys_common::process::CommandEnvs;
Expand Down
Loading