Skip to content

Commit 9e3ab20

Browse files
authored
uucore: remove unsafe make_fifo (#12243)
1 parent dfd6297 commit 9e3ab20

6 files changed

Lines changed: 11 additions & 67 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/uu/cp/src/cp.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ use thiserror::Error;
2929
use platform::copy_on_write;
3030
use uucore::display::Quotable;
3131
use uucore::error::{UError, UResult, UUsageError, set_exit_code};
32-
#[cfg(unix)]
33-
use uucore::fs::make_fifo;
3432
use uucore::fs::{
3533
FileInformation, MissingHandling, ResolveMode, are_hardlinks_to_same_file, canonicalize,
3634
get_filename, is_symlink_loop, normalize_path, path_ends_with_terminator,
@@ -2830,8 +2828,8 @@ fn copy_fifo(dest: &Path, overwrite: OverwriteMode, debug: bool) -> CopyResult<(
28302828
overwrite.verify(dest, debug)?;
28312829
fs::remove_file(dest)?;
28322830
}
2833-
2834-
make_fifo(dest)
2831+
// rustix::fs::mkfifoat is linux only
2832+
nix::unistd::mkfifo(dest, Mode::from_bits_truncate(0o666))
28352833
.map_err(|_| translate!("cp-error-cannot-create-fifo", "path" => dest.quote()).into())
28362834
}
28372835

src/uu/mv/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ clap = { workspace = true }
2323
fs_extra = { workspace = true }
2424
indicatif = { workspace = true }
2525
libc = { workspace = true }
26+
nix = { workspace = true }
2627
rustc-hash = { workspace = true }
2728
thiserror = { workspace = true }
2829
uucore = { workspace = true, features = [

src/uu/mv/src/mv.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ use uucore::display::Quotable;
3939
use uucore::error::{FromIo, UResult, USimpleError, UUsageError, set_exit_code};
4040
#[cfg(unix)]
4141
use uucore::fs::display_permissions_unix;
42-
#[cfg(unix)]
43-
use uucore::fs::make_fifo;
4442
use uucore::fs::{
4543
MissingHandling, ResolveMode, are_hardlinks_or_one_way_symlink_to_same_file,
4644
are_hardlinks_to_same_file, canonicalize, path_ends_with_terminator,
@@ -885,7 +883,8 @@ fn rename_fifo_fallback(from: &Path, to: &Path) -> io::Result<()> {
885883
if to.try_exists()? {
886884
fs::remove_file(to)?;
887885
}
888-
make_fifo(to)?;
886+
// rustix::fs::mkfifoat is linux only
887+
nix::unistd::mkfifo(to, nix::sys::stat::Mode::from_bits_truncate(0o666))?;
889888
fs::remove_file(from)
890889
}
891890

@@ -1162,7 +1161,8 @@ fn copy_file_with_hardlinks_helper(
11621161
// rename_symlink_fallback already preserves ownership and removes the source.
11631162
rename_symlink_fallback(from, to)?;
11641163
} else if is_fifo(from.symlink_metadata()?.file_type()) {
1165-
make_fifo(to)?;
1164+
// rustix::fs::mkfifoat is linux only
1165+
nix::unistd::mkfifo(to, nix::sys::stat::Mode::from_bits_truncate(0o666))?;
11661166
// Preserve ownership (uid/gid) from the source
11671167
let _ = preserve_ownership(from, to);
11681168
} else {

src/uucore/src/lib/features/fs.rs

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,11 @@
77
88
// spell-checker:ignore backport
99

10-
#[cfg(unix)]
11-
use libc::mkfifo;
1210
#[cfg(all(unix, not(target_os = "redox")))]
1311
pub use libc::{major, makedev, minor};
1412
use std::collections::HashSet;
1513
use std::collections::VecDeque;
1614
use std::env;
17-
#[cfg(unix)]
18-
use std::ffi::CString;
1915
use std::ffi::{OsStr, OsString};
2016
use std::fs;
2117
use std::fs::read_dir;
@@ -892,37 +888,6 @@ pub fn get_filename(file: &Path) -> Option<&str> {
892888
file.file_name().and_then(|filename| filename.to_str())
893889
}
894890

895-
/// Make a FIFO, also known as a named pipe.
896-
///
897-
/// This is a safe wrapper for the unsafe [`libc::mkfifo`] function,
898-
/// which makes a [named
899-
/// pipe](https://en.wikipedia.org/wiki/Named_pipe) on Unix systems.
900-
///
901-
/// # Errors
902-
///
903-
/// If the named pipe cannot be created.
904-
///
905-
/// # Examples
906-
///
907-
/// ```ignore
908-
/// use uucore::fs::make_fifo;
909-
///
910-
/// make_fifo("my-pipe").expect("failed to create the named pipe");
911-
///
912-
/// std::thread::spawn(|| { std::fs::write("my-pipe", b"hello").unwrap(); });
913-
/// assert_eq!(std::fs::read("my-pipe").unwrap(), b"hello");
914-
/// ```
915-
#[cfg(unix)]
916-
pub fn make_fifo(path: &Path) -> std::io::Result<()> {
917-
let name = CString::new(path.to_str().unwrap()).unwrap();
918-
let err = unsafe { mkfifo(name.as_ptr(), 0o666) };
919-
if err == -1 {
920-
Err(Error::last_os_error())
921-
} else {
922-
Ok(())
923-
}
924-
}
925-
926891
// Redox's libc appears not to include the following utilities
927892

928893
#[cfg(target_os = "redox")]
@@ -950,8 +915,6 @@ mod tests {
950915
#[cfg(unix)]
951916
use std::os::unix;
952917
#[cfg(unix)]
953-
use std::os::unix::fs::FileTypeExt;
954-
#[cfg(unix)]
955918
use tempfile::{NamedTempFile, tempdir};
956919

957920
struct NormalizePathTestCase<'a> {
@@ -1216,25 +1179,4 @@ mod tests {
12161179
let file_path = PathBuf::from("~/foo.txt");
12171180
assert!(matches!(get_filename(&file_path), Some("foo.txt")));
12181181
}
1219-
1220-
#[cfg(unix)]
1221-
#[test]
1222-
fn test_make_fifo() {
1223-
// Create the FIFO in a temporary directory.
1224-
let tempdir = tempdir().unwrap();
1225-
let path = tempdir.path().join("f");
1226-
assert!(make_fifo(&path).is_ok());
1227-
1228-
// Check that it is indeed a FIFO.
1229-
assert!(fs::metadata(&path).unwrap().file_type().is_fifo());
1230-
1231-
// Check that we can write to it and read from it.
1232-
//
1233-
// Write and read need to happen in different threads,
1234-
// otherwise `write` would block indefinitely while waiting
1235-
// for the `read`.
1236-
let path2 = path.clone();
1237-
std::thread::spawn(move || assert!(fs::write(&path2, b"foo").is_ok()));
1238-
assert_eq!(fs::read(&path).unwrap(), b"foo");
1239-
}
12401182
}

src/uucore/src/lib/features/selinux.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,9 @@ mod tests {
618618

619619
// Create a FIFO (pipe)
620620
let fifo_path = dir_path.join("my_fifo");
621-
crate::fs::make_fifo(&fifo_path).expect("Failed to create FIFO");
621+
// todo: use rustix::fs::mkfifoat since selinux is linux specific
622+
nix::unistd::mkfifo(&fifo_path, nix::sys::stat::Mode::from_bits_truncate(0o666))
623+
.expect("Failed to create FIFO");
622624

623625
// Just getting a context is good enough
624626
get_selinux_security_context(&fifo_path, false).expect("Cannot get fifo context");

0 commit comments

Comments
 (0)