Skip to content
Merged
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
2 changes: 1 addition & 1 deletion fuzz/uufuzz/src/lib.rs
Comment thread
xtqqczze marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ use rand::RngExt;
use rand::prelude::IndexedRandom;
use rustix::io::dup;
use rustix::io::read;
use rustix::pipe::pipe;
use rustix::stdio::{dup2_stderr, dup2_stdin, dup2_stdout};
use std::env::temp_dir;
use std::ffi::OsString;
use std::fs::File;
use std::io::pipe;
use std::io::{Seek, SeekFrom, Write};
use std::process::{Command, Stdio};
use std::sync::atomic::Ordering;
Expand Down
4 changes: 1 addition & 3 deletions src/uucore/src/lib/features/buf_copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ mod tests {
fn test_copy_stream() {
let mut dest_file = new_temp_file();

let (pipe_read, pipe_write) = rustix::pipe::pipe().unwrap();
let mut pipe_read: File = pipe_read.into();
let mut pipe_write: File = pipe_write.into();
let (mut pipe_read, mut pipe_write) = std::io::pipe().unwrap();
let data = b"Hello, world!";
let thread = thread::spawn(move || {
pipe_write.write_all(data).unwrap();
Expand Down
14 changes: 9 additions & 5 deletions src/uucore/src/lib/features/pipes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ const KERNEL_DEFAULT_PIPE_SIZE: usize = 64 * 1024;
/// used for resolving the limitation for splice: one of a input or output should be pipe
#[inline]
#[cfg(any(target_os = "linux", target_os = "android"))]
pub fn pipe<const SIZE_REQUIRED: bool>(s: usize) -> std::io::Result<(File, File)> {
let (read, write) = rustix::pipe::pipe()?;
pub fn pipe<const SIZE_REQUIRED: bool>(
s: usize,
) -> std::io::Result<(std::io::PipeReader, std::io::PipeWriter)> {
Comment thread
xtqqczze marked this conversation as resolved.
let (read, write) = std::io::pipe()?;
// guard unnecessary syscall
if s > KERNEL_DEFAULT_PIPE_SIZE {
let r = fcntl_setpipe_size(&read, s);
Expand All @@ -37,7 +39,7 @@ pub fn pipe<const SIZE_REQUIRED: bool>(s: usize) -> std::io::Result<(File, File)
}
}

Ok((File::from(read), File::from(write)))
Ok((read, write))
}

/// Less noisy wrapper around [`rustix::pipe::splice`].
Expand Down Expand Up @@ -116,7 +118,8 @@ where
R: Read + AsFd,
S: AsFd,
{
static PIPE_CACHE: OnceLock<Option<(File, File)>> = OnceLock::new();
static PIPE_CACHE: OnceLock<Option<(std::io::PipeReader, std::io::PipeWriter)>> =
OnceLock::new();
let Some((pipe_rd, pipe_wr)) = PIPE_CACHE
.get_or_init(|| pipe::<false>(MAX_ROOTLESS_PIPE_SIZE).ok())
.as_ref()
Expand Down Expand Up @@ -160,7 +163,8 @@ pub fn send_n_bytes(
mut target: impl Write + AsFd,
n: u64,
) -> std::io::Result<u64> {
static PIPE_CACHE: OnceLock<Option<(File, File)>> = OnceLock::new();
static PIPE_CACHE: OnceLock<Option<(std::io::PipeReader, std::io::PipeWriter)>> =
OnceLock::new();
let pipe_size = MAX_ROOTLESS_PIPE_SIZE.min(n as usize);
let mut n = n;
let mut bytes_written: u64 = 0;
Expand Down
3 changes: 1 addition & 2 deletions tests/by-util/test_cat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ use uutests::util_name;
#[test]
fn test_cat_broken_pipe_nonzero_and_message() {
use uutests::new_ucmd;
let (read, write) = rustix::pipe::pipe().expect("Failed to create pipe");
let (read, write) = std::io::pipe().expect("Failed to create pipe");
// Close the read end to simulate a broken pipe on stdout
drop(read);
let write: File = write.into();
let content = (0..10000).map(|_| "x").collect::<String>();

// On Unix, SIGPIPE should lead to a non-zero exit; ensure process exits and fails
Expand Down
6 changes: 2 additions & 4 deletions tests/by-util/test_comm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -691,10 +691,8 @@ fn test_comm_anonymous_pipes() {
let scene = TestScenario::new(util_name!());

// Open two anonymous pipes
let (comm1_reader, comm1_writer) = rustix::pipe::pipe().unwrap();
let mut comm1_writer: std::fs::File = comm1_writer.into();
let (comm2_reader, comm2_writer) = rustix::pipe::pipe().unwrap();
let mut comm2_writer: std::fs::File = comm2_writer.into();
let (comm1_reader, mut comm1_writer) = std::io::pipe().unwrap();
let (comm2_reader, mut comm2_writer) = std::io::pipe().unwrap();

// comm reads the data in chunks
// make content large enough, so that at least two chunks are read
Expand Down
8 changes: 4 additions & 4 deletions tests/by-util/test_paste.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,12 +450,12 @@ fn test_paste_non_utf8_paths() {
}

#[cfg(target_os = "linux")]
fn make_broken_pipe() -> std::fs::File {
let (read, write) = rustix::pipe::pipe().expect("Failed to create pipe");
fn make_broken_pipe() -> std::io::PipeWriter {
let (read, write) = std::io::pipe().expect("Failed to create pipe");
// Drop the read end so writes fail with EPIPE.
drop(read);

write.into()
// Return the write end of the pipe
write
}

#[test]
Expand Down
21 changes: 9 additions & 12 deletions tests/by-util/test_tee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,26 +256,25 @@ mod linux_only {
use uutests::util::{AtPath, CmdResult, UCommand};

use std::fmt::Write;
use std::fs::File;
use std::process::Stdio;
use std::time::Duration;
use uutests::at_and_ucmd;
use uutests::new_ucmd;

fn make_broken_pipe() -> File {
let (read, write) = rustix::pipe::pipe().expect("Failed to create pipe");
fn make_broken_pipe() -> std::io::PipeWriter {
let (read, write) = std::io::pipe().expect("Failed to create pipe");
// Drop the read end of the pipe
drop(read);
// Make the write end of the pipe into a Rust File
write.into()
// Return the write end of the pipe
write
}

fn make_hanging_read() -> File {
let (read, write) = rustix::pipe::pipe().expect("Failed to create pipe");
fn make_hanging_read() -> std::io::PipeReader {
let (read, write) = std::io::pipe().expect("Failed to create pipe");
// PURPOSELY leak the write end of the pipe, so the read end hangs.
std::mem::forget(write);
// Return the read end of the pipe
read.into()
read
}

fn run_tee(proc: &mut UCommand) -> (String, CmdResult) {
Expand Down Expand Up @@ -726,10 +725,9 @@ fn test_output_error_flag_without_value_defaults_warn_nopipe() {
#[cfg(all(unix, not(target_os = "freebsd")))]
#[test]
fn test_output_error_presence_only_broken_pipe_unix() {
let (read, write) = rustix::pipe::pipe().expect("Failed to create pipe");
let (read, write) = std::io::pipe().expect("Failed to create pipe");
// Close the read end to simulate a broken pipe on stdout
drop(read);
let write: std::fs::File = write.into();
let content = (0..10_000).map(|_| "x").collect::<String>();
let result = new_ucmd!()
.arg("--output-error") // presence-only flag
Expand All @@ -745,10 +743,9 @@ fn test_output_error_presence_only_broken_pipe_unix() {
#[cfg(all(unix, not(target_os = "freebsd")))]
#[test]
fn test_broken_pipe_early_termination_stdout_only() {
let (read, write) = rustix::pipe::pipe().expect("Failed to create pipe");
let (read, write) = std::io::pipe().expect("Failed to create pipe");
// Create a broken stdout
drop(read);
let write: std::fs::File = write.into();
let content = (0..10_000).map(|_| "x").collect::<String>();
let mut proc = new_ucmd!();
let result = proc
Expand Down
Loading