Skip to content

Commit 331e311

Browse files
committed
Impl new API std::os::unix::fs::mkfifo under feature unix_fifo
Tracking issue #139324 Signed-off-by: Jiahao XU <[email protected]>
1 parent 1bc5618 commit 331e311

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-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/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
@@ -2148,6 +2148,12 @@ pub fn chroot(dir: &Path) -> io::Result<()> {
21482148
Err(io::const_error!(io::ErrorKind::Unsupported, "chroot not supported by vxworks"))
21492149
}
21502150

2151+
pub fn mkfifo(path: &Path, mode: u32) -> io::Result<()> {
2152+
run_path_with_cstr(path, &|path| {
2153+
cvt(unsafe { libc::mkfifo(path.as_ptr(), mode.try_into().unwrap()) }).map(|_| ())
2154+
})
2155+
}
2156+
21512157
pub use remove_dir_impl::remove_dir_all;
21522158

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

0 commit comments

Comments
 (0)