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
This commit is contained in:
@@ -1100,3 +1100,39 @@ pub fn lchown<P: AsRef<Path>>(dir: P, uid: Option<u32>, gid: Option<u32>) -> io:
|
||||
pub fn chroot<P: AsRef<Path>>(dir: P) -> io::Result<()> {
|
||||
sys::fs::chroot(dir.as_ref())
|
||||
}
|
||||
|
||||
/// Create a FIFO special file at the specified path with the specified mode.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```no_run
|
||||
/// # #![feature(unix_mkfifo)]
|
||||
/// # #[cfg(not(unix))]
|
||||
/// # fn main() {}
|
||||
/// # #[cfg(unix)]
|
||||
/// # fn main() -> std::io::Result<()> {
|
||||
/// # use std::{
|
||||
/// # os::unix::fs::{mkfifo, PermissionsExt},
|
||||
/// # fs::{File, Permissions, remove_file},
|
||||
/// # io::{Write, Read},
|
||||
/// # };
|
||||
/// # let _ = remove_file("/tmp/fifo");
|
||||
/// mkfifo("/tmp/fifo", Permissions::from_mode(0o774))?;
|
||||
///
|
||||
/// let mut wx = File::options().read(true).write(true).open("/tmp/fifo")?;
|
||||
/// let mut rx = File::open("/tmp/fifo")?;
|
||||
///
|
||||
/// wx.write_all(b"hello, world!")?;
|
||||
/// drop(wx);
|
||||
///
|
||||
/// let mut s = String::new();
|
||||
/// rx.read_to_string(&mut s)?;
|
||||
///
|
||||
/// assert_eq!(s, "hello, world!");
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
#[unstable(feature = "unix_mkfifo", issue = "139324")]
|
||||
pub fn mkfifo<P: AsRef<Path>>(path: P, permissions: Permissions) -> io::Result<()> {
|
||||
sys::fs::mkfifo(path.as_ref(), permissions.mode())
|
||||
}
|
||||
|
||||
@@ -55,3 +55,23 @@ fn write_vectored_at() {
|
||||
let content = fs::read(&filename).unwrap();
|
||||
assert_eq!(&content, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_mkfifo() {
|
||||
let tmp_dir = crate::test_helpers::tmpdir();
|
||||
|
||||
let fifo = tmp_dir.path().join("fifo");
|
||||
|
||||
mkfifo(&fifo, Permissions::from_mode(0o774)).unwrap();
|
||||
|
||||
let mut wx = fs::File::options().read(true).write(true).open(&fifo).unwrap();
|
||||
let mut rx = fs::File::open(fifo).unwrap();
|
||||
|
||||
wx.write_all(b"hello, world!").unwrap();
|
||||
drop(wx);
|
||||
|
||||
let mut s = String::new();
|
||||
rx.read_to_string(&mut s).unwrap();
|
||||
|
||||
assert_eq!(s, "hello, world!");
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ cfg_if::cfg_if! {
|
||||
if #[cfg(target_family = "unix")] {
|
||||
mod unix;
|
||||
use unix as imp;
|
||||
pub use unix::{chown, fchown, lchown};
|
||||
pub use unix::{chown, fchown, lchown, mkfifo};
|
||||
#[cfg(not(target_os = "fuchsia"))]
|
||||
pub use unix::chroot;
|
||||
pub(crate) use unix::debug_assert_fd_is_open;
|
||||
|
||||
@@ -2137,6 +2137,12 @@ pub fn chroot(dir: &Path) -> io::Result<()> {
|
||||
Err(io::const_error!(io::ErrorKind::Unsupported, "chroot not supported by vxworks"))
|
||||
}
|
||||
|
||||
pub fn mkfifo(path: &Path, mode: u32) -> io::Result<()> {
|
||||
run_path_with_cstr(path, &|path| {
|
||||
cvt(unsafe { libc::mkfifo(path.as_ptr(), mode.try_into().unwrap()) }).map(|_| ())
|
||||
})
|
||||
}
|
||||
|
||||
pub use remove_dir_impl::remove_dir_all;
|
||||
|
||||
// Fallback for REDOX, ESP-ID, Horizon, Vita, Vxworks and Miri
|
||||
|
||||
Reference in New Issue
Block a user