Skip to content

Commit 3aad789

Browse files
committed
refactor: replace rustix::pipe with std::io::pipe
1 parent db25551 commit 3aad789

7 files changed

Lines changed: 27 additions & 31 deletions

File tree

fuzz/uufuzz/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ use rand::RngExt;
1111
use rand::prelude::IndexedRandom;
1212
use rustix::io::dup;
1313
use rustix::io::read;
14-
use rustix::pipe::pipe;
1514
use rustix::stdio::{dup2_stderr, dup2_stdin, dup2_stdout};
1615
use std::env::temp_dir;
1716
use std::ffi::OsString;
1817
use std::fs::File;
18+
use std::io::pipe;
1919
use std::io::{Seek, SeekFrom, Write};
2020
use std::process::{Command, Stdio};
2121
use std::sync::atomic::Ordering;

src/uucore/src/lib/features/buf_copy.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,7 @@ mod tests {
5151
fn test_copy_stream() {
5252
let mut dest_file = new_temp_file();
5353

54-
let (pipe_read, pipe_write) = rustix::pipe::pipe().unwrap();
55-
let mut pipe_read: File = pipe_read.into();
56-
let mut pipe_write: File = pipe_write.into();
54+
let (mut pipe_read, mut pipe_write) = std::io::pipe().unwrap();
5755
let data = b"Hello, world!";
5856
let thread = thread::spawn(move || {
5957
pipe_write.write_all(data).unwrap();

src/uucore/src/lib/features/pipes.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ const KERNEL_DEFAULT_PIPE_SIZE: usize = 64 * 1024;
2727
/// used for resolving the limitation for splice: one of a input or output should be pipe
2828
#[inline]
2929
#[cfg(any(target_os = "linux", target_os = "android"))]
30-
pub fn pipe<const SIZE_REQUIRED: bool>(s: usize) -> std::io::Result<(File, File)> {
31-
let (read, write) = rustix::pipe::pipe()?;
30+
pub fn pipe<const SIZE_REQUIRED: bool>(
31+
s: usize,
32+
) -> std::io::Result<(std::io::PipeReader, std::io::PipeWriter)> {
33+
let (read, write) = std::io::pipe()?;
3234
// guard unnecessary syscall
3335
if s > KERNEL_DEFAULT_PIPE_SIZE {
3436
let r = fcntl_setpipe_size(&read, s);
@@ -37,7 +39,7 @@ pub fn pipe<const SIZE_REQUIRED: bool>(s: usize) -> std::io::Result<(File, File)
3739
}
3840
}
3941

40-
Ok((File::from(read), File::from(write)))
42+
Ok((read, write))
4143
}
4244

4345
/// Less noisy wrapper around [`rustix::pipe::splice`].
@@ -116,7 +118,8 @@ where
116118
R: Read + AsFd,
117119
S: AsFd,
118120
{
119-
static PIPE_CACHE: OnceLock<Option<(File, File)>> = OnceLock::new();
121+
static PIPE_CACHE: OnceLock<Option<(std::io::PipeReader, std::io::PipeWriter)>> =
122+
OnceLock::new();
120123
let Some((pipe_rd, pipe_wr)) = PIPE_CACHE
121124
.get_or_init(|| pipe::<false>(MAX_ROOTLESS_PIPE_SIZE).ok())
122125
.as_ref()
@@ -160,7 +163,8 @@ pub fn send_n_bytes(
160163
mut target: impl Write + AsFd,
161164
n: u64,
162165
) -> std::io::Result<u64> {
163-
static PIPE_CACHE: OnceLock<Option<(File, File)>> = OnceLock::new();
166+
static PIPE_CACHE: OnceLock<Option<(std::io::PipeReader, std::io::PipeWriter)>> =
167+
OnceLock::new();
164168
let pipe_size = MAX_ROOTLESS_PIPE_SIZE.min(n as usize);
165169
let mut n = n;
166170
let mut bytes_written: u64 = 0;

tests/by-util/test_cat.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ use uutests::util_name;
2323
#[test]
2424
fn test_cat_broken_pipe_nonzero_and_message() {
2525
use uutests::new_ucmd;
26-
let (read, write) = rustix::pipe::pipe().expect("Failed to create pipe");
26+
let (read, write) = std::io::pipe().expect("Failed to create pipe");
2727
// Close the read end to simulate a broken pipe on stdout
2828
drop(read);
29-
let write: File = write.into();
3029
let content = (0..10000).map(|_| "x").collect::<String>();
3130

3231
// On Unix, SIGPIPE should lead to a non-zero exit; ensure process exits and fails

tests/by-util/test_comm.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -691,10 +691,8 @@ fn test_comm_anonymous_pipes() {
691691
let scene = TestScenario::new(util_name!());
692692

693693
// Open two anonymous pipes
694-
let (comm1_reader, comm1_writer) = rustix::pipe::pipe().unwrap();
695-
let mut comm1_writer: std::fs::File = comm1_writer.into();
696-
let (comm2_reader, comm2_writer) = rustix::pipe::pipe().unwrap();
697-
let mut comm2_writer: std::fs::File = comm2_writer.into();
694+
let (comm1_reader, mut comm1_writer) = std::io::pipe().unwrap();
695+
let (comm2_reader, mut comm2_writer) = std::io::pipe().unwrap();
698696

699697
// comm reads the data in chunks
700698
// make content large enough, so that at least two chunks are read

tests/by-util/test_paste.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -450,12 +450,12 @@ fn test_paste_non_utf8_paths() {
450450
}
451451

452452
#[cfg(target_os = "linux")]
453-
fn make_broken_pipe() -> std::fs::File {
454-
let (read, write) = rustix::pipe::pipe().expect("Failed to create pipe");
453+
fn make_broken_pipe() -> std::io::PipeWriter {
454+
let (read, write) = std::io::pipe().expect("Failed to create pipe");
455455
// Drop the read end so writes fail with EPIPE.
456456
drop(read);
457-
458-
write.into()
457+
// Return the write end of the pipe
458+
write
459459
}
460460

461461
#[test]

tests/by-util/test_tee.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -256,26 +256,25 @@ mod linux_only {
256256
use uutests::util::{AtPath, CmdResult, UCommand};
257257

258258
use std::fmt::Write;
259-
use std::fs::File;
260259
use std::process::Stdio;
261260
use std::time::Duration;
262261
use uutests::at_and_ucmd;
263262
use uutests::new_ucmd;
264263

265-
fn make_broken_pipe() -> File {
266-
let (read, write) = rustix::pipe::pipe().expect("Failed to create pipe");
264+
fn make_broken_pipe() -> std::io::PipeWriter {
265+
let (read, write) = std::io::pipe().expect("Failed to create pipe");
267266
// Drop the read end of the pipe
268267
drop(read);
269-
// Make the write end of the pipe into a Rust File
270-
write.into()
268+
// Return the write end of the pipe
269+
write
271270
}
272271

273-
fn make_hanging_read() -> File {
274-
let (read, write) = rustix::pipe::pipe().expect("Failed to create pipe");
272+
fn make_hanging_read() -> std::io::PipeReader {
273+
let (read, write) = std::io::pipe().expect("Failed to create pipe");
275274
// PURPOSELY leak the write end of the pipe, so the read end hangs.
276275
std::mem::forget(write);
277276
// Return the read end of the pipe
278-
read.into()
277+
read
279278
}
280279

281280
fn run_tee(proc: &mut UCommand) -> (String, CmdResult) {
@@ -726,10 +725,9 @@ fn test_output_error_flag_without_value_defaults_warn_nopipe() {
726725
#[cfg(all(unix, not(target_os = "freebsd")))]
727726
#[test]
728727
fn test_output_error_presence_only_broken_pipe_unix() {
729-
let (read, write) = rustix::pipe::pipe().expect("Failed to create pipe");
728+
let (read, write) = std::io::pipe().expect("Failed to create pipe");
730729
// Close the read end to simulate a broken pipe on stdout
731730
drop(read);
732-
let write: std::fs::File = write.into();
733731
let content = (0..10_000).map(|_| "x").collect::<String>();
734732
let result = new_ucmd!()
735733
.arg("--output-error") // presence-only flag
@@ -745,10 +743,9 @@ fn test_output_error_presence_only_broken_pipe_unix() {
745743
#[cfg(all(unix, not(target_os = "freebsd")))]
746744
#[test]
747745
fn test_broken_pipe_early_termination_stdout_only() {
748-
let (read, write) = rustix::pipe::pipe().expect("Failed to create pipe");
746+
let (read, write) = std::io::pipe().expect("Failed to create pipe");
749747
// Create a broken stdout
750748
drop(read);
751-
let write: std::fs::File = write.into();
752749
let content = (0..10_000).map(|_| "x").collect::<String>();
753750
let mut proc = new_ucmd!();
754751
let result = proc

0 commit comments

Comments
 (0)