Skip to content
Closed
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
6 changes: 3 additions & 3 deletions src/uu/cat/src/splice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use super::{CatResult, FdReadable, InputHandle};
use rustix::io::{read, write};
use std::os::{fd::AsFd, unix::io::AsRawFd};

use uucore::pipes::{MAX_ROOTLESS_PIPE_SIZE, pipe, splice, splice_exact};
use uucore::pipes::{MAX_ROOTLESS_PIPE_SIZE, is_end_of_file, pipe, splice, splice_exact};

const BUF_SIZE: usize = 1024 * 16;

Expand All @@ -30,15 +30,15 @@ pub(super) fn write_fast_using_splice<R: FdReadable, S: AsRawFd + AsFd>(
loop {
match splice(&handle.reader, &write_fd, MAX_ROOTLESS_PIPE_SIZE) {
Ok(1..) => {}
Ok(0) => return Ok(false),
Ok(0) => return Ok(!is_end_of_file(&handle.reader)),
Err(_) => return Ok(true),
}
}
} else if let Ok((pipe_rd, pipe_wr)) = pipe() {
// both of in/output are not pipe. needs broker to use splice() with additional costs
loop {
match splice(&handle.reader, &pipe_wr, MAX_ROOTLESS_PIPE_SIZE) {
Ok(0) => return Ok(false),
Ok(0) => return Ok(!is_end_of_file(&handle.reader)),
Ok(n) => {
if splice_exact(&pipe_rd, write_fd, n).is_err() {
// If the first splice manages to copy to the intermediate
Expand Down
2 changes: 1 addition & 1 deletion src/uucore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jiff = { workspace = true, optional = true, features = [
"tzdb-concatenated",
] }
rustc-hash = { workspace = true }
rustix = { workspace = true, features = ["fs", "pipe"] }
rustix = { workspace = true, features = ["event", "fs", "time", "pipe"] }
time = { workspace = true, optional = true, features = [
"formatting",
"local-offset",
Expand Down
21 changes: 21 additions & 0 deletions src/uucore/src/lib/features/pipes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,27 @@ pub fn splice(source: &impl AsFd, target: &impl AsFd, len: usize) -> std::io::Re
)?)
}

/// check that splice reached to end of file
/// ignore 2nd Ctrl + D
/// <https://github.com/uutils/coreutils/issues/9609>
#[inline]
#[cfg(any(target_os = "linux", target_os = "android"))]
pub fn is_end_of_file(source: &impl AsFd) -> bool {
use rustix::event::{PollFd, PollFlags, poll};
use rustix::time::Timespec;
let mut pfd = [PollFd::new(source, PollFlags::IN)];
matches!(
poll(
&mut pfd,
Some(&Timespec {
tv_sec: 0,
tv_nsec: 0
})
),
Ok(0)
)
}

/// Splice wrapper which fully finishes the write.
///
/// Exactly `len` bytes are moved from `source` into `target`.
Expand Down
Loading