shell.rs
This file
Path
src/shell.rs
88 lines · 2.1 KiB · Rust
Last changed by
Add environment randomization on WindowsDavid Peterbefore adoption197e5df
History of this fileTake it
Parse --shell option value as shell commandrhysdbefore adoptione2fdace
line 1why, one step back1use std::process::{ExitStatus, Stdio};Flat directory/module structureDavid Peterbefore adoption30c381f
line 2why, one step back2Parse --shell option value as shell commandrhysdbefore adoptione2fdace
line 3why, one step back3use crate::options::Shell;Flat directory/module structureDavid Peterbefore adoption30c381f
lines 4–5why, one step back4use crate::timer::get_cpu_timer;5Unified error handlingDavid Peterbefore adoption2040810
lines 6–7why, one step back6use anyhow::{Context, Result};7Flat directory/module structureDavid Peterbefore adoption30c381f
lines 8–20why, one step back8/// Used to indicate the result of running a command9#[derive(Debug, Copy, Clone)]10pub struct ExecuteResult {11 /// The amount of user time the process used12 pub user_time: f64,1314 /// The amount of cpu time the process used15 pub system_time: f64,1617 /// The exit status of the process18 pub status: ExitStatus,19}20Add environment randomization on WindowsDavid Peterbefore adoption197e5df
lines 21–22why, one step back21fn randomized_environment_offset_value() -> String {22 "X".repeat(rand::random::<usize>() % 4096usize)Flat directory/module structureDavid Peterbefore adoption30c381f
lines 23–30why, one step back23}2425/// Execute the given command and return a timing summary26#[cfg(not(windows))]27pub fn execute_and_time(28 stdout: Stdio,29 stderr: Stdio,30 command: &str,Parse --shell option value as shell commandrhysdbefore adoptione2fdace
line 31why, one step back31 shell: &Shell,Unified error handlingDavid Peterbefore adoption2040810
line 32why, one step back32) -> Result<ExecuteResult> {Flat directory/module structureDavid Peterbefore adoption30c381f
lines 33–34why, one step back33 let cpu_timer = get_cpu_timer();34Add environment randomization on WindowsDavid Peterbefore adoption197e5df
line 35why, one step back35 let status = shellParse --shell option value as shell commandrhysdbefore adoptione2fdace
line 36why, one step back36 .command()Flat directory/module structureDavid Peterbefore adoption30c381f
lines 37–40why, one step back37 .arg("-c")38 .arg(command)39 .env(40 "HYPERFINE_RANDOMIZED_ENVIRONMENT_OFFSET",Add environment randomization on WindowsDavid Peterbefore adoption197e5df
line 41why, one step back41 randomized_environment_offset_value(),Flat directory/module structureDavid Peterbefore adoption30c381f
lines 42–46why, one step back42 )43 .stdin(Stdio::null())44 .stdout(stdout)45 .stderr(stderr)46 .status()Add environment randomization on WindowsDavid Peterbefore adoption197e5df
lines 47–55why, one step back47 .with_context(|| format!("Failed to run command '{}'", command))?;4849 let (user_time, system_time) = cpu_timer.stop();5051 Ok(ExecuteResult {52 user_time,53 system_time,54 status,55 })Flat directory/module structureDavid Peterbefore adoption30c381f
lines 56–57why, one step back56}57Add environment randomization on WindowsDavid Peterbefore adoption197e5df
line 58why, one step back58/// Execute the given command and return a timing summaryFlat directory/module structureDavid Peterbefore adoption30c381f
line 59why, one step back59#[cfg(windows)]Add environment randomization on WindowsDavid Peterbefore adoption197e5df
line 60why, one step back60pub fn execute_and_time(Flat directory/module structureDavid Peterbefore adoption30c381f
lines 61–63why, one step back61 stdout: Stdio,62 stderr: Stdio,63 command: &str,Parse --shell option value as shell commandrhysdbefore adoptione2fdace
line 64why, one step back64 shell: &Shell,Add environment randomization on WindowsDavid Peterbefore adoption197e5df
lines 65–66why, one step back65) -> Result<ExecuteResult> {66 let mut child = shellParse --shell option value as shell commandrhysdbefore adoptione2fdace
line 67why, one step back67 .command()Flat directory/module structureDavid Peterbefore adoption30c381f
lines 68–69why, one step back68 .arg("/C")69 .arg(command)Add environment randomization on WindowsDavid Peterbefore adoption197e5df
lines 70–73why, one step back70 .env(71 "HYPERFINE_RANDOMIZED_ENVIRONMENT_OFFSET",72 randomized_environment_offset_value(),73 )Flat directory/module structureDavid Peterbefore adoption30c381f
lines 74–77why, one step back74 .stdin(Stdio::null())75 .stdout(stdout)76 .stderr(stderr)77 .spawn()Add environment randomization on WindowsDavid Peterbefore adoption197e5df
lines 78–87why, one step back78 .with_context(|| format!("Failed to run command '{}'", command))?;79 let cpu_timer = get_cpu_timer(&child);80 let status = child.wait()?;8182 let (user_time, system_time) = cpu_timer.stop();83 Ok(ExecuteResult {84 user_time,85 system_time,86 status,87 })Flat directory/module structureDavid Peterbefore adoption30c381f
line 88why, one step back88}