Skip to content

Commit 71bfad0

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 65899c0 commit 71bfad0

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

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

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

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

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

2168+
pub fn mkfifo(path: &Path, mode: u32) -> io::Result<()> {
2169+
run_path_with_cstr(path, &|path| {
2170+
cvt(unsafe { libc::mkfifo(path.as_ptr(), mode.try_into().unwrap()) }).map(|_| ())
2171+
})
2172+
}
2173+
21682174
pub use remove_dir_impl::remove_dir_all;
21692175

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

0 commit comments

Comments
 (0)