Skip to content
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
2 changes: 2 additions & 0 deletions .vscode/cspell.dictionaries/workspace.wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ advapi32-sys
aho-corasick
backtrace
blake2b_simd
rustix

# * uutils project
uutils
Expand Down Expand Up @@ -362,6 +363,7 @@ uutils

# * function names
getcwd
setpipe

# * other
getlimits
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ rstest = "0.26.0"
rstest_reuse = "0.7.0"
rustc-hash = "2.1.1"
rust-ini = "0.21.0"
rustix = "1.1.4"
same-file = "1.0.6"
self_cell = "1.0.4"
selinux = "=0.6.0"
Expand Down
1 change: 1 addition & 0 deletions src/uu/yes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ path = "src/yes.rs"
clap = { workspace = true }
itertools = { workspace = true }
fluent = { workspace = true }
rustix = { workspace = true, features = ["pipe"] }

[target.'cfg(unix)'.dependencies]
uucore = { workspace = true, features = ["pipes", "signals"] }
Expand Down
28 changes: 25 additions & 3 deletions src/uu/yes/src/yes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ use uucore::error::{UResult, USimpleError, strip_errno};
use uucore::format_usage;
use uucore::translate;

// it's possible that using a smaller or larger buffer might provide better performance on some
// systems, but honestly this is good enough
#[cfg(any(target_os = "linux", target_os = "android"))]
const MAX_ROOTLESS_PIPE_SIZE: usize = 1024 * 1024;
// todo: investigate best rate
#[cfg(any(target_os = "linux", target_os = "android"))]
const BUF_SIZE: usize = MAX_ROOTLESS_PIPE_SIZE;
#[cfg(not(any(target_os = "linux", target_os = "android")))]
const BUF_SIZE: usize = 16 * 1024;

#[uucore::main]
Expand Down Expand Up @@ -110,8 +114,25 @@ fn prepare_buffer(buf: &mut Vec<u8>) {

pub fn exec(bytes: &[u8]) -> io::Result<()> {
let stdout = io::stdout();
let mut stdout = stdout.lock();
#[cfg(any(target_os = "linux", target_os = "android"))]
{
use rustix::pipe::{SpliceFlags, fcntl_setpipe_size, pipe, tee};
// fast-path for pipe. todo: port the fast-path for > file
if let Ok((p_read, p_write)) = pipe() {
let _ = fcntl_setpipe_size(&p_read, MAX_ROOTLESS_PIPE_SIZE);
let _ = fcntl_setpipe_size(&stdout, MAX_ROOTLESS_PIPE_SIZE);
if std::fs::File::from(p_write).write_all(bytes).is_ok() {
while let Ok(1..) = tee(
&p_read,
&stdout,
MAX_ROOTLESS_PIPE_SIZE,
SpliceFlags::empty(),
) {}
}
}
}

let mut stdout = stdout.lock();
loop {
stdout.write_all(bytes)?;
}
Expand All @@ -122,6 +143,7 @@ mod tests {
use super::*;

#[test]
#[cfg(not(any(target_os = "linux", target_os = "android")))] // Linux uses different buffer size
fn test_prepare_buffer() {
let tests = [
(150, 16350),
Expand Down
2 changes: 1 addition & 1 deletion tests/by-util/test_yes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ fn test_long_input() {
#[cfg(windows)]
const TIMES: usize = 500;
let arg = "abcdef".repeat(TIMES) + "\n";
let expected_out = arg.repeat(30);
let expected_out = arg.repeat(5);
run(&[&arg[..arg.len() - 1]], expected_out.as_bytes());
}

Expand Down
Loading