Create `path` with `contents` and mode 0700.
(path: &Path, contents: impl AsRef<[u8]>)
| 18 | |
| 19 | /// Create `path` with `contents` and mode 0700. |
| 20 | pub fn write_executable(path: &Path, contents: impl AsRef<[u8]>) { |
| 21 | let mut writer = Command::new("/bin/sh") |
| 22 | .args(["-c", "umask 077 && cat > \"$1\" && chmod 700 \"$1\"", "sh"]) |
| 23 | .arg(path) |
| 24 | .stdin(Stdio::piped()) |
| 25 | .spawn() |
| 26 | .expect("cannot start /bin/sh to write a test executable"); |
| 27 | writer |
| 28 | .stdin |
| 29 | .take() |
| 30 | .expect("piped stdin") |
| 31 | .write_all(contents.as_ref()) |
| 32 | .expect("cannot send a test executable to /bin/sh"); |
| 33 | let status = writer.wait().expect("cannot wait for /bin/sh"); |
| 34 | assert!( |
| 35 | status.success(), |
| 36 | "cannot write {}: {status}", |
| 37 | path.display() |
| 38 | ); |
| 39 | } |