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
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 fuzz/Cargo.lock

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

5 changes: 4 additions & 1 deletion src/uu/tr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ uucore = { workspace = true, features = ["fs", "signals"] }
fluent = { workspace = true }
bytecount = { workspace = true, features = ["runtime-dispatch-simd"] }

[target.'cfg(unix)'.dependencies]
rustix = { workspace = true, features = ["stdio", "fs"] }

[dev-dependencies]
divan = { workspace = true }
rustix = { workspace = true, features = ["stdio", "fs"] }
tempfile = { workspace = true }
uucore = { workspace = true, features = ["benchmark"] }

[[bin]]
Expand Down
2 changes: 2 additions & 0 deletions src/uu/tr/src/tr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
}

let stdin = stdin();
#[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd"))]
let _ = rustix::fs::fadvise(&stdin, 0, None, rustix::fs::Advice::Sequential);
let mut locked_stdin = stdin.lock();
let mut locked_stdout = stdout().lock();

Expand Down
35 changes: 35 additions & 0 deletions tests/by-util/test_tr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@
// spell-checker:ignore aabbaa aabbcc aabc abbb abbbcddd abcc abcdefabcdef abcdefghijk abcdefghijklmn abcdefghijklmnop ABCDEFGHIJKLMNOPQRS abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ ABCDEFZZ abcxyz ABCXYZ abcxyzabcxyz ABCXYZABCXYZ acbdef alnum amzamz AMZXAMZ bbbd cclass cefgm cntrl compl dabcdef dncase fooclass Gzabcdefg PQRST upcase wxyzz xdigit XXXYYY xycde xyyye xyyz xyzzzzxyzzzz ZABCDEF Zamz Cdefghijkl Cdefghijklmn asdfqqwweerr qwerr asdfqwer qwer aassddffqwer asdfqwer
use uutests::at_and_ucmd;
use uutests::new_ucmd;
#[cfg(target_os = "linux")]
use uutests::util::get_tests_binary;

#[cfg(unix)]
use std::{ffi::OsStr, os::unix::ffi::OsStrExt};

#[cfg(target_os = "linux")]
use std::process::Command;

#[test]
fn test_invalid_arg() {
new_ucmd!().arg("--definitely-invalid").fails_with_code(1);
Expand Down Expand Up @@ -1628,6 +1633,36 @@ fn test_octal_escape_ambiguous_followed_by_non_utf8() {
.stderr_contains("warning: invalid utf8 sequence");
}

#[test]
#[cfg(target_os = "linux")]
#[cfg_attr(wasi_runner, ignore = "WASI: strace cannot trace WebAssembly")]
fn test_sequential_fadvise() {
let (at, _ucmd) = at_and_ucmd!();
at.write("input", "abc");

let trace_file = at.plus_as_string("strace.out");
let result = Command::new("strace")
.args(["-o", &trace_file, "-e", "fadvise64,fadvise64_64"])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test does not check that fadvise is properly called for file's fd.

.arg(get_tests_binary())
.args(["tr", "a", "b"])
.stdin(std::fs::File::open(at.plus("input")).unwrap())
.output();

if result.is_err() {
return; // strace not available
}

let trace = at.read("strace.out");
let advised_stdin = trace.lines().any(|line| {
(line.starts_with("fadvise64(0,") || line.starts_with("fadvise64_64(0,"))
&& line.contains("POSIX_FADV_SEQUENTIAL") // spell-checker:disable-line
});
assert!(
advised_stdin,
"Expected sequential fadvise on stdin: {trace}"
);
}

#[cfg(target_os = "linux")]
#[test]
fn test_failed_write_is_reported() {
Expand Down
Loading