diff options
| author | Jiahao XU <Jiahao_XU@outlook.com> | 2025-04-07 00:04:57 +1000 |
|---|---|---|
| committer | Jiahao XU <Jiahao_XU@outlook.com> | 2025-04-23 23:02:52 +1000 |
| commit | 780f95dd182b1a432159430add57e9ab7cb45dd6 (patch) | |
| tree | 134bdcc279d91cbb6b702cc66cf1975a457d73e5 /library/std/src/os/unix | |
| parent | 1bc56185ee257ed829a0aea7abdc3b03c5fed887 (diff) | |
| download | rust-780f95dd182b1a432159430add57e9ab7cb45dd6.tar.gz rust-780f95dd182b1a432159430add57e9ab7cb45dd6.zip | |
Impl new API `std::os::unix::fs::mkfifo` under feature `unix_fifo`
Tracking issue #139324 Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
Diffstat (limited to 'library/std/src/os/unix')
| -rw-r--r-- | library/std/src/os/unix/fs.rs | 36 | ||||
| -rw-r--r-- | library/std/src/os/unix/fs/tests.rs | 20 |
2 files changed, 56 insertions, 0 deletions
diff --git a/library/std/src/os/unix/fs.rs b/library/std/src/os/unix/fs.rs index 0427feb2955..4f9259f39c1 100644 --- a/library/std/src/os/unix/fs.rs +++ b/library/std/src/os/unix/fs.rs @@ -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()) +} diff --git a/library/std/src/os/unix/fs/tests.rs b/library/std/src/os/unix/fs/tests.rs index db9621c8c20..1840bb38c17 100644 --- a/library/std/src/os/unix/fs/tests.rs +++ b/library/std/src/os/unix/fs/tests.rs @@ -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!"); +} |
