I have the following program that attempts to spawn a new process on Linux and write a bunch of stuff into the child process via STDIN.
use std::io::Write;
use std::process::{Command, Stdio};
fn gpg_decrypt(file: Vec<u8>) -> Result<(), ()> {
println!("start");
let mut proc = Command::new("gpg")
.args(&[
"--quiet",
"--output",
"-",
"--trust-model",
"always",
"--decrypt",
])
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
.map_err(|e| {
eprintln!("error spawning process: {}", e);
})?;
proc.stdin
.take()
.unwrap()
.write_all(file.as_slice())
.map_err(|e| {
eprintln!("error writing to child process: {}", e);
})?;
println!("xxx");
let output = proc.wait_with_output().map_err(|e| {
eprintln!("error getting output: {}", e);
})?;
println!("output: {:?}", output);
Ok(())
}
fn main() {
let bytes = std::fs::read("foo.tar.gpg").unwrap();
gpg_decrypt(bytes).unwrap();
println!("Hello, world!");
}
The program does not work when a 600K file is sent to STDIN. It also does not work if I attempt to chunk the writes as follows:
{
let mut total = 0;
let stdin = proc.stdin.as_mut().unwrap();
for c in file.as_slice().chunks(1024) {
println!("total: {}", total);
stdin
.write_all(c)
.map_err(|e| {
eprintln!("error writing to child process: {}", e);
})?;
stdin.flush().unwrap();
total += 1024;
}
Running the program under strace
tells me that the write hangs after a certain limit.
Is there anything I am doing wrong?