From fb23353453c1c5c8640d09588bdcc6f9bff9d04a Mon Sep 17 00:00:00 2001 From: Adrian Heine Date: Sun, 26 Dec 2021 00:07:11 +0100 Subject: [PATCH] Use bytes for command stdin --- src/command_runner.rs | 20 ++++++++++---------- src/symbols/cron.rs | 4 ++-- src/symbols/git/checkout.rs | 4 ++-- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/command_runner.rs b/src/command_runner.rs index 5f37ca1..5be8ddc 100644 --- a/src/command_runner.rs +++ b/src/command_runner.rs @@ -33,17 +33,17 @@ pub fn get_output(output: Output) -> Result, Box> { #[async_trait(?Send)] pub trait CommandRunner { - async fn run(&self, program: &str, args: &[&OsStr], stdin: &str) -> IoResult; + async fn run(&self, program: &str, args: &[&OsStr], stdin: &[u8]) -> IoResult; async fn run_with_args(&self, program: &str, args: &[&OsStr]) -> IoResult { - self.run(program, args, "").await + self.run(program, args, b"").await } async fn get_output(&self, program: &str, args: &[&OsStr]) -> Result, Box> { let output = self.run_with_args(program, args).await?; get_output(output) } async fn run_successfully(&self, program: &str, args: &[&OsStr]) -> Result<(), Box> { - is_success(self.run(program, args, "").await)?; + is_success(self.run(program, args, b"").await)?; Ok(()) } async fn get_stderr(&self, program: &str, args: &[&OsStr]) -> Result, Box> { @@ -56,7 +56,7 @@ pub struct StdCommandRunner; #[async_trait(?Send)] impl CommandRunner for StdCommandRunner { - async fn run(&self, program: &str, args: &[&OsStr], input: &str) -> IoResult { + async fn run(&self, program: &str, args: &[&OsStr], input: &[u8]) -> IoResult { //println!("{} {:?}", program, args); let mut child = Command::new(program) .args(args) @@ -67,7 +67,7 @@ impl CommandRunner for StdCommandRunner { .expect("Failed to spawn child process"); let stdin = child.stdin.as_mut().expect("Failed to open stdin"); stdin - .write_all(input.as_bytes()) + .write_all(input) .await .expect("Failed to write to stdin"); let res = child.wait_with_output().await; @@ -122,7 +122,7 @@ impl Drop for TempSetEnv<'_> { #[async_trait(?Send)] impl, C: CommandRunner> CommandRunner for SetuidCommandRunner<'_, U, C> { - async fn run(&self, program: &str, args: &[&OsStr], input: &str) -> IoResult { + async fn run(&self, program: &str, args: &[&OsStr], input: &[u8]) -> IoResult { let uid = get_user_by_name(self.user_name.as_ref()) .expect("User does not exist") .uid(); @@ -140,7 +140,7 @@ impl, C: CommandRunner> CommandRunner for SetuidCommandRunner<'_, .expect("Failed to spawn child process"); let stdin = child.stdin.as_mut().expect("Failed to open stdin"); stdin - .write_all(input.as_bytes()) + .write_all(input) .await .expect("Failed to write to stdin"); let res = child.wait_with_output().await; @@ -179,7 +179,7 @@ impl<'a, C> CommandRunner for SuCommandRunner<'a, C> where C: 'a + CommandRunner, { - async fn run(&self, program: &str, args: &[&OsStr], input: &str) -> IoResult { + async fn run(&self, program: &str, args: &[&OsStr], input: &[u8]) -> IoResult { let raw_new_args = [self.user_name, "-s", "/usr/bin/env", "--", program]; let mut new_args: Vec<&OsStr> = raw_new_args.iter().map(AsRef::as_ref).collect(); new_args.extend_from_slice(args); @@ -201,8 +201,8 @@ mod test { run(async { let args = args!["1"]; let start = Instant::now(); - let res = c.run("sleep", args, "").fuse(); - let ps = c.run("ps", args![], "").fuse(); + let res = c.run("sleep", args, b"").fuse(); + let ps = c.run("ps", args![], b"").fuse(); futures_util::pin_mut!(res, ps); loop { futures_util::select! { diff --git a/src/symbols/cron.rs b/src/symbols/cron.rs index 2a452ce..45571ed 100644 --- a/src/symbols/cron.rs +++ b/src/symbols/cron.rs @@ -21,13 +21,13 @@ impl<'r, U, R> Cron<'r, String, U, R> { } #[async_trait(?Send)] -impl, U: AsRef, R: CommandRunner> Symbol for Cron<'_, C, U, R> { +impl, U: AsRef, R: CommandRunner> Symbol for Cron<'_, C, U, R> { async fn target_reached(&self) -> Result> { let tab = self .command_runner .get_output("crontab", args!["-l", "-u", self.user.as_ref()]) .await?; - Ok(tab == self.content.as_ref().as_bytes()) + Ok(tab == self.content.as_ref()) } async fn execute(&self) -> Result<(), Box> { diff --git a/src/symbols/git/checkout.rs b/src/symbols/git/checkout.rs index 4063aa9..f5399c7 100644 --- a/src/symbols/git/checkout.rs +++ b/src/symbols/git/checkout.rs @@ -110,9 +110,9 @@ mod test { } #[async_trait(?Send)] impl CommandRunner for DummyCommandRunner { - async fn run(&self, program: &str, args: &[&OsStr], stdin: &str) -> IoResult { + async fn run(&self, program: &str, args: &[&OsStr], stdin: &[u8]) -> IoResult { assert_eq!(program, "git"); - assert_eq!(stdin, ""); + assert_eq!(stdin, b""); sleep(Duration::from_millis(50)).await; self .args