Skip to content

Commit 10732e1

Browse files
authored
Rollup merge of #139450 - NobodyXu:new-api/make-fifo, r=tgross35
Impl new API `std::os::unix::fs::mkfifo` under feature `unix_fifo` Tracking issue #139324
2 parents cb3c5d7 + 780f95d commit 10732e1

File tree

4 files changed

+63
-1
lines changed

4 files changed

+63
-1
lines changed

library/std/src/os/unix/fs.rs

+36
Original file line numberDiff line numberDiff line change
@@ -1100,3 +1100,39 @@ pub fn lchown<P: AsRef<Path>>(dir: P, uid: Option<u32>, gid: Option<u32>) -> io:
11001100
pub fn chroot<P: AsRef<Path>>(dir: P) -> io::Result<()> {
11011101
sys::fs::chroot(dir.as_ref())
11021102
}
1103+
1104+
/// Create a FIFO special file at the specified path with the specified mode.
1105+
///
1106+
/// # Examples
1107+
///
1108+
/// ```no_run
1109+
/// # #![feature(unix_mkfifo)]
1110+
/// # #[cfg(not(unix))]
1111+
/// # fn main() {}
1112+
/// # #[cfg(unix)]
1113+
/// # fn main() -> std::io::Result<()> {
1114+
/// # use std::{
1115+
/// # os::unix::fs::{mkfifo, PermissionsExt},
1116+
/// # fs::{File, Permissions, remove_file},
1117+
/// # io::{Write, Read},
1118+
/// # };
1119+
/// # let _ = remove_file("/tmp/fifo");
1120+
/// mkfifo("/tmp/fifo", Permissions::from_mode(0o774))?;
1121+
///
1122+
/// let mut wx = File::options().read(true).write(true).open("/tmp/fifo")?;
1123+
/// let mut rx = File::open("/tmp/fifo")?;
1124+
///
1125+
/// wx.write_all(b"hello, world!")?;
1126+
/// drop(wx);
1127+
///
1128+
/// let mut s = String::new();
1129+
/// rx.read_to_string(&mut s)?;
1130+
///
1131+
/// assert_eq!(s, "hello, world!");
1132+
/// # Ok(())
1133+
/// # }
1134+
/// ```
1135+
#[unstable(feature = "unix_mkfifo", issue = "139324")]
1136+
pub fn mkfifo<P: AsRef<Path>>(path: P, permissions: Permissions) -> io::Result<()> {
1137+
sys::fs::mkfifo(path.as_ref(), permissions.mode())
1138+
}

library/std/src/os/unix/fs/tests.rs

+20
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,23 @@ fn write_vectored_at() {
5555
let content = fs::read(&filename).unwrap();
5656
assert_eq!(&content, expected);
5757
}
58+
59+
#[test]
60+
fn test_mkfifo() {
61+
let tmp_dir = crate::test_helpers::tmpdir();
62+
63+
let fifo = tmp_dir.path().join("fifo");
64+
65+
mkfifo(&fifo, Permissions::from_mode(0o774)).unwrap();
66+
67+
let mut wx = fs::File::options().read(true).write(true).open(&fifo).unwrap();
68+
let mut rx = fs::File::open(fifo).unwrap();
69+
70+
wx.write_all(b"hello, world!").unwrap();
71+
drop(wx);
72+
73+
let mut s = String::new();
74+
rx.read_to_string(&mut s).unwrap();
75+
76+
assert_eq!(s, "hello, world!");
77+
}

library/std/src/sys/fs/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ cfg_if::cfg_if! {
99
if #[cfg(target_family = "unix")] {
1010
mod unix;
1111
use unix as imp;
12-
pub use unix::{chown, fchown, lchown};
12+
pub use unix::{chown, fchown, lchown, mkfifo};
1313
#[cfg(not(target_os = "fuchsia"))]
1414
pub use unix::chroot;
1515
pub(crate) use unix::debug_assert_fd_is_open;

library/std/src/sys/fs/unix.rs

+6
Original file line numberDiff line numberDiff line change
@@ -2137,6 +2137,12 @@ pub fn chroot(dir: &Path) -> io::Result<()> {
21372137
Err(io::const_error!(io::ErrorKind::Unsupported, "chroot not supported by vxworks"))
21382138
}
21392139

2140+
pub fn mkfifo(path: &Path, mode: u32) -> io::Result<()> {
2141+
run_path_with_cstr(path, &|path| {
2142+
cvt(unsafe { libc::mkfifo(path.as_ptr(), mode.try_into().unwrap()) }).map(|_| ())
2143+
})
2144+
}
2145+
21402146
pub use remove_dir_impl::remove_dir_all;
21412147

21422148
// Fallback for REDOX, ESP-ID, Horizon, Vita, Vxworks and Miri

0 commit comments

Comments
 (0)