1use async_std::fs;
2use nix::{sys::stat::Mode, NixPath};
3use std::path::Path;
4
5pub fn create_pipe<P: ?Sized + NixPath>(path: &P, mode: Option<Mode>) -> nix::Result<()> {
7 nix::unistd::mkfifo(path, mode.unwrap_or_else(|| Mode::from_bits_truncate(0o660)))
8}
9
10pub async fn remove_pipe<P: AsRef<Path>>(path: P) -> async_std::io::Result<()> {
12 fs::remove_file(&path).await
13}
14
15#[cfg(test)]
16mod tests {
17 use async_std::task::block_on;
18 #[test]
19 fn creation_deletion() {
20 const FILENAME: &str = "./test_pipe_1";
21 super::create_pipe(FILENAME, None).expect("Failed to create pipe");
22 block_on(super::remove_pipe(FILENAME)).expect("Failed to remove pipe");
23 }
24 #[test]
25 fn permissions() {
26 use nix::sys::stat::{self, Mode};
27 let path = std::path::Path::new("./test_pipe_2");
28 let assert_stats_eq = |input| {
29 super::create_pipe(path, input).expect("Failed to create pipe");
30 let file_stat = stat::stat(path).expect("Failed to get file stat");
31 let mode = Mode::from_bits_truncate(file_stat.st_mode);
32 if let Some(new_mode) = input {
33 assert_eq!(mode, new_mode);
34 } else {
35 assert_eq!(mode, Mode::from_bits_truncate(0o660));
36 }
37 block_on(super::remove_pipe(path)).expect("Failed to remove pipe");
38 };
39 assert_stats_eq(None);
41 assert_stats_eq(Some(Mode::from_bits_truncate(0o644)));
43 }
44}