Repository navigation
feat(tac): add tac with -s and -b - #287
ferdingler wants to merge 3 commits into
Conversation
|
@strandly-the-agent Please review this PR |
strandly-the-agent
left a comment
There was a problem hiding this comment.
Changes requested. The happy paths match GNU tac byte for byte, but a second missing file makes tac exit early and silently drop the rest of its output. The fix is small; I left it inline, and built and re-tested it.
- 🔴
tac.rs:62,70: stdin and stderr are taken inside the per-file loop, and each fd can only be taken once.tac nope1 f nope2 fprintsfonce and loses thenope2error.tac - -fails withbad fd 0and exits 1, where GNU exits 0. - 🟡
tac.rs:67: piped stdin is read with no size cap.sedandjquseread_to_end_limitedfor this. - Tests:
tac_reports_a_missing_file_and_continuesonly has one missing file, so it can't catch the 🔴. Addingtac /tmp/nope /tmp/nope2 /tmp/t1andprintf 'a\n' | tac - -would.
CI is green at 7569308.
What I verified
- Head
756930829c.cargo test --test shell_integration --test differential: 1969 + 1 passed. All 6 newtac_*tests pass, and the recorded differential case matches GNU tac 9.1. - I ran about 27 hand cases plus random fuzzing against
/usr/bin/tac(stdin,-, unterminated last line, empty input,-b, multi-byte-s,-bs,,--separator=, a directory operand,-r,--, broken pipe, a 20 MB input with a 1000-byte separator). Everything matched except the items here. - I applied the suggested fix locally and rebuilt.
tac nope1 f nope2 fnow prints2 1 2 1with both errors, andtac - -exits 0, both as in GNU. The repro log is uploaded as proof of work (strands-agents/shell/pr/287/tac-repro.log).
Appendix: non-blocking (3)
- ⚪
tac.rs:18-29: the scan runs left to right, so a separator that overlaps itself splits differently from GNU, which scans right to left. Example:printf 'xaaay' | tac -s aagivesayxaahere andyxaaain GNU. It also shows up with-s $'\n\n'on uneven blank lines. Fix: scan from the end and step backsep.len()after each match. The reviewer pass checked this approach against GNU on 3000 random cases with no differences. - Pre-existing, filed #299: the shell drops a double-quoted empty word, so
tac -s "" freportsmissing argument. This is not caused by this PR. - Pre-existing, not filed: a non-UTF-8
-svalue reaches the command as U+FFFD. Every command takesargs: &[String]andgrepbehaves the same way, so this is a shell-wide argv design question, not atacbug.
| let mut w = io::stdout()?; | ||
| let mut status = 0; | ||
| for path in &files { | ||
| let mut data = Vec::new(); | ||
| let read = async { | ||
| let mut reader: Box<dyn tokio::io::AsyncRead + Unpin + Send> = if path == "-" { | ||
| Box::new(io::stdin()?) | ||
| } else { | ||
| let fd = io::open(os, path, OpenFlags::read()).await?; | ||
| Box::new(io::take_reader(fd)?) | ||
| }; | ||
| reader.read_to_end(&mut data).await | ||
| }; | ||
| if let Err(e) = read.await { | ||
| let mut ew = io::stderr()?; | ||
| wprintln!(ew, "tac: {path}: {e}")?; |
There was a problem hiding this comment.
A second missing file (or a second -) makes tac exit early and silently drop the remaining output. io::stderr() and io::stdin() remove the fd from the table (os.rs:665-673). The second call returns bad fd, and the ? exits cmd_tac.
$ tac nope1 f nope2 f # GNU: 2 1 2 1, two errors
2
1
tac: nope1: ... # second f and nope2 error missing
An agent hits this when it runs tac over a guessed list of log files. grep.rs:697 handles this by taking stderr once. A second - reads empty, as in GNU.
Suggestion (built and re-tested; both cases now match GNU):
| let mut w = io::stdout()?; | |
| let mut status = 0; | |
| for path in &files { | |
| let mut data = Vec::new(); | |
| let read = async { | |
| let mut reader: Box<dyn tokio::io::AsyncRead + Unpin + Send> = if path == "-" { | |
| Box::new(io::stdin()?) | |
| } else { | |
| let fd = io::open(os, path, OpenFlags::read()).await?; | |
| Box::new(io::take_reader(fd)?) | |
| }; | |
| reader.read_to_end(&mut data).await | |
| }; | |
| if let Err(e) = read.await { | |
| let mut ew = io::stderr()?; | |
| wprintln!(ew, "tac: {path}: {e}")?; | |
| let mut w = io::stdout()?; | |
| // stdin and stderr can each be taken only once per process. | |
| let mut ew = io::stderr()?; | |
| let mut stdin_taken = false; | |
| let mut status = 0; | |
| for path in &files { | |
| let mut data = Vec::new(); | |
| let read = async { | |
| let mut reader: Box<dyn tokio::io::AsyncRead + Unpin + Send> = if path == "-" { | |
| if std::mem::replace(&mut stdin_taken, true) { | |
| Box::new(tokio::io::empty()) | |
| } else { | |
| Box::new(io::stdin()?) | |
| } | |
| } else { | |
| let fd = io::open(os, path, OpenFlags::read()).await?; | |
| Box::new(io::take_reader(fd)?) | |
| }; | |
| reader.read_to_end(&mut data).await | |
| }; | |
| if let Err(e) = read.await { | |
| wprintln!(ew, "tac: {path}: {e}")?; |
There was a problem hiding this comment.
Fixed in 7cbd91a and 2245ca0. Stderr is now taken once, on the first error, rather than up front, so an error later in the run can still be reported by the shell. A second - reads nothing, like GNU. The test now has two missing files, and there's a new one for - -. I checked that the two-missing-files test fails on the old code.
There was a problem hiding this comment.
Verified at 2245ca0: both errors are reported, all files are printed, and tac - - / tac - f - match GNU.
| let fd = io::open(os, path, OpenFlags::read()).await?; | ||
| Box::new(io::take_reader(fd)?) | ||
| }; | ||
| reader.read_to_end(&mut data).await |
There was a problem hiding this comment.
Piped stdin is read into memory with no cap, then copied again into reversed. max_file_size bounds file operands, but nothing bounds a pipe. In the reviewer pass, cat a×40 | tac | wc -c (93 MB) peaked at 292 MB RSS, against 21 MB without tac. sed.rs:518 and jq.rs:260 cap reads with crate::os::read_to_end_limited(&mut r, io::with_process(|p| p.max_output)).
sort.rs has the same uncapped read, so I'm not blocking on this. Question: should stdin be capped at max_output here, as in sed/jq? Writing each record straight to w instead of collecting reversed would also avoid holding a second full copy.
There was a problem hiding this comment.
I'm leaving the size cap off. Unlike sed and jq, tac gives back exactly as many bytes as it reads, and tac big.log > out is a fair thing to run on a file bigger than the output limit. Files are already bounded by the file size limit, and a pipe by the timeout. sort reads the same way. I did drop the second full copy in 2245ca0: records are now written out in chunks.
|
Thanks. Pushed fixes for the two blocking items and the overlap nit (replies inline). The separator is now searched from the end like GNU, so |
|
@strandly-the-agent Please review this PR |
strandly-the-agent
left a comment
There was a problem hiding this comment.
Approve. All three fixes hold up at 2245ca0.
- 🔴 Missing files and a second
-: fixed.tac nope1 f nope2 fnow prints2 1 2 1and reports both errors, andtac - f -matches GNU. - 🟡 Size cap: withdrawn. Your reasoning holds, since
tacwrites out exactly as many bytes as it reads. Writing in chunks also removes the second full copy. - ⚪ Overlapping separator: fixed. The scan now runs from the end, as in GNU.
CI is green.
What I verified
cargo test --test shell_integration --test differentialat2245ca0: 1971 + 1 passed, including all 8tac_*tests.- I ran a differential fuzz against GNU tac 9.1: 600 random inputs, with separators
\n,aa,aaa,aba,abab,\n\n,,and others, half of them with-b. There were 0 differences. - A 30,000-line file (larger than one 8 KiB output chunk) reversed byte-identical to GNU.
- Scripts and logs are uploaded under
strands-agents/shell/pr/287/followup-*.
Description
There's no
tacin the shell, so an agent that wants a log or a file newest-line-first gets command not found.This adds
tac. It reverses each file in turn, reads stdin when there are no files or for-, and supports-sfor a custom separator string and-bto attach it before each record instead of after. A missing file is reported and skipped, and the exit status is 1, like GNU. Regex separators (-r) are left out.I compared it byte for byte against GNU tac 8.32 on this machine, including an unterminated last line, empty input, multi-character separators and a missing file, and stdout and exit status all match. I added a recorded differential case and integration tests. The full Rust suite passes. I didn't run the Python or Node suites since the bindings are unchanged. This touches the same command lists as the other new-command PRs for #207, so whichever lands second will need a small rebase.
Related Issues
Part of #207
Documentation PR
Type of Change
New feature
Testing
How have you tested the change? Verify that the changes do not break functionality or introduce new warnings.
cargo test --workspace --all-targets,pytest tests/python,npm test)cargo fmtandcargo clippyChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.