Skip to content

Impl new API std::os::unix::fs::mkfifo under feature unix_fifo #139450

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions library/std/src/os/unix/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this test is to be run (I think it's a good idea to have some form of test), it may be better to call mkdtemp and create the FIFO there to ensure it doesn't interfere with anyone who actually has a /tmp/fifo.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if we can use libc::mktemp here...honestly maybe we should also add a function for that

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, it would be nice to have. libc isn't available but the binding is pretty easy, the setup is a bit clunky but I think it's worth avoiding any possible issues

/// # // Use a temporary file to avoid conflicts or cleanup issues
/// # extern "C" { fn mktemp(template: *mut c_char) -> *mut c_char; }
/// # let mut buf = b"rust-fifo-XXXXXX\0";
/// # let fname = unsafe {
/// #     let ptr = mktemp(buf.as_ptr().cast::<c_char>()) };
/// #     assert!(!ptr.is_null(), "could not create tempfile");
/// #     CStr::from_ptr(ptr).to_str().unwrap()
/// # };
/// mkfifo(fname, Permissions::from_mode(0o774))?;
/// // ...

The rest LGTM

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My problem with this is portability, not sure if it would work on musl, and probably android and other libc reliably.

If we want to do this, might be easier to random generate the filename using std::random?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point. We have tempfile for the std testsuite, maybe just make the doc example no_run and then add the actual test there?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that makes more sense, I would do that

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see this was made no_run but it doesn't look like a test wasn't added. If you don't have it yet, just copy+paste this example to std/src/os/unix/fs/tests.rs using a file name from crate_helpers::tmpdir().join("fifo").

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, I put it in std/tests/mkfifo.rs and forgot to git add it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tgross35 I have updated std/src/os/unix/fs/tests.rs

///
/// 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())
}
20 changes: 20 additions & 0 deletions library/std/src/os/unix/fs/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
}
2 changes: 1 addition & 1 deletion library/std/src/sys/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions library/std/src/sys/fs/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2148,6 +2148,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
Expand Down
Loading