tail: fix behaviour of tail -n0 in follow mode (#9114)

* tail: fix behaviour of -n0 in follow mode

* tests: added test for checking -n0 in follow mode

---------

Co-authored-by: Sylvestre Ledru <sylvestre@debian.org>
This commit is contained in:
AnandajithS
2026-01-18 18:21:48 +01:00
committed by GitHub
co-authored by Sylvestre Ledru
parent ca13e3391a
commit 7654075306
2 changed files with 29 additions and 2 deletions
+7 -2
View File
@@ -470,7 +470,7 @@ fn bounded_tail(file: &mut File, settings: &Settings) {
file.seek(SeekFrom::Start(i as u64)).unwrap();
}
FilterMode::Lines(Signum::MinusZero, _) => {
return;
file.seek(SeekFrom::End(0)).unwrap();
}
FilterMode::Bytes(Signum::Negative(count)) => {
if file.seek(SeekFrom::End(-(*count as i64))).is_err() {
@@ -484,7 +484,7 @@ fn bounded_tail(file: &mut File, settings: &Settings) {
file.seek(SeekFrom::Start(*count - 1)).unwrap();
}
FilterMode::Bytes(Signum::MinusZero) => {
return;
file.seek(SeekFrom::End(0)).unwrap();
}
_ => {}
}
@@ -524,6 +524,11 @@ fn unbounded_tail<T: Read>(reader: &mut BufReader<T>, settings: &Settings) -> UR
chunks.fill(reader)?;
chunks.print(&mut writer)?;
}
FilterMode::Lines(Signum::MinusZero, sep) => {
let mut chunks = chunks::LinesChunkBuffer::new(*sep, 0);
chunks.fill(reader)?;
chunks.print(&mut writer)?;
}
FilterMode::Bytes(Signum::PlusZero | Signum::Positive(1)) => {
io::copy(reader, &mut writer)?;
}
+22
View File
@@ -233,6 +233,28 @@ fn test_nc_0_wo_follow2() {
.no_output();
}
#[test]
#[cfg(not(target_os = "windows"))]
fn test_n0_with_follow() {
let (at, mut ucmd) = at_and_ucmd!();
let test_file = "test.txt";
// Create file with multiple lines
at.write(test_file, "line1\nline2\nline3\n");
let mut child = ucmd.arg("-n0").arg("-f").arg(test_file).run_no_wait();
child.make_assertion_with_delay(500).is_alive();
// Append a new line
at.append(test_file, "new\n");
// Should only print the newly appended line
child
.make_assertion_with_delay(DEFAULT_SLEEP_INTERVAL_MILLIS)
.with_current_output()
.stdout_only("new\n");
child.kill();
}
// TODO: Add similar test for windows
#[test]
#[cfg(unix)]