From 7a125eb275399c0a81ea80b1c0ad293af8bf9367 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sat, 30 Jul 2016 00:07:37 -0700 Subject: [PATCH] Don't spawn processes for copying/probing We can copy files with normal libstd APIs as well as just take a look at `$PATH` if we're trying to find a command. --- src/rustup-utils/src/raw.rs | 69 +++++++++++-------------------------- 1 file changed, 21 insertions(+), 48 deletions(-) diff --git a/src/rustup-utils/src/raw.rs b/src/rustup-utils/src/raw.rs index d7709e0707..2bce4fe2ab 100644 --- a/src/rustup-utils/src/raw.rs +++ b/src/rustup-utils/src/raw.rs @@ -1,4 +1,5 @@ use std::char::from_u32; +use std::env; use std::error; use std::ffi::{OsStr, OsString}; use std::fmt; @@ -365,37 +366,20 @@ fn rm_rf(path: &Path) -> io::Result<()> { } } -pub fn copy_dir(src: &Path, dest: &Path) -> CommandResult<()> { - #[cfg(windows)] - fn copy_dir_inner(src: &Path, dest: &Path) -> CommandResult<()> { - Command::new("robocopy") - .arg(src) - .arg(dest) - .arg("/E") - .arg("/NFL") - .arg("/NDL") - .arg("/NJH") - .arg("/NJS") - .arg("/nc") - .arg("/ns") - .arg("/np") - .status() - .map_err(CommandError::Io) - .and_then(|s| { - match s.code() { - // Robocopy has non-zero exit codes for successful copies... - Some(value) if value < 8 => Ok(()), - _ => Err(CommandError::Status(s)), - } - }) - } - #[cfg(not(windows))] - fn copy_dir_inner(src: &Path, dest: &Path) -> CommandResult<()> { - cmd_status(Command::new("cp").arg("-R").arg(src).arg(dest)) +pub fn copy_dir(src: &Path, dest: &Path) -> io::Result<()> { + try!(fs::create_dir(dest)); + for entry in try!(src.read_dir()) { + let entry = try!(entry); + let kind = try!(entry.file_type()); + let src = entry.path(); + let dest = dest.join(entry.file_name()); + if kind.is_dir() { + try!(copy_dir(&src, &dest)); + } else { + try!(fs::copy(&src, &dest)); + } } - - let _ = remove_dir(dest); - copy_dir_inner(src, dest) + Ok(()) } pub fn prefix_arg>(name: &str, s: S) -> OsString { @@ -405,24 +389,13 @@ pub fn prefix_arg>(name: &str, s: S) -> OsString { } pub fn has_cmd(cmd: &str) -> bool { - #[cfg(not(windows))] - fn inner(cmd: &str) -> bool { - cmd_status(Command::new("which") - .arg(cmd) - .stdin(Stdio::null()) - .stdout(Stdio::null()) - .stderr(Stdio::null())) - .is_ok() - } - #[cfg(windows)] - fn inner(cmd: &str) -> bool { - cmd_status(Command::new("where") - .arg("/Q") - .arg(cmd)) - .is_ok() - } - - inner(cmd) + let cmd = format!("{}{}", cmd, env::consts::EXE_SUFFIX); + let path = env::var_os("PATH").unwrap_or(OsString::new()); + env::split_paths(&path).map(|p| { + p.join(&cmd) + }).any(|p| { + p.exists() + }) } pub fn find_cmd<'a>(cmds: &[&'a str]) -> Option<&'a str> {