Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
14 changes: 8 additions & 6 deletions src/uu/tail/src/tail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ fn tail_file(
&& file.is_seekable(if input.is_stdin() { offset } else { 0 })
&& (!st.is_file() || st.len() > blksize_limit)
{
bounded_tail(&mut file, settings);
bounded_tail(&mut file, settings)?;
reader = BufReader::new(file);
} else {
reader = BufReader::new(file);
Expand Down Expand Up @@ -466,7 +466,7 @@ fn backwards_thru_file(file: &mut File, num_delimiters: u64, delimiter: u8) {
/// end of the file, and then read the file "backwards" in blocks of size
/// `BLOCK_SIZE` until we find the location of the first line/byte. This ends up
/// being a nice performance win for very large files.
fn bounded_tail(file: &mut File, settings: &Settings) {
fn bounded_tail(file: &mut File, settings: &Settings) -> UResult<()> {
debug_assert!(!settings.presume_input_pipe);
let mut limit = None;

Expand Down Expand Up @@ -499,7 +499,8 @@ fn bounded_tail(file: &mut File, settings: &Settings) {
_ => {}
}

print_target_section(file, limit);
print_target_section(file, limit)?;
Ok(())
}

fn unbounded_tail<T: Read>(reader: &mut BufReader<T>, settings: &Settings) -> UResult<()> {
Expand Down Expand Up @@ -580,7 +581,7 @@ fn unbounded_tail<T: Read>(reader: &mut BufReader<T>, settings: &Settings) -> UR
Ok(())
}

fn print_target_section<R>(file: &mut R, limit: Option<u64>)
fn print_target_section<R>(file: &mut R, limit: Option<u64>) -> UResult<()>
where
R: Read + ?Sized,
{
Expand All @@ -589,10 +590,11 @@ where
let mut stdout = stdout.lock();
if let Some(limit) = limit {
let mut reader = file.take(limit);
io::copy(&mut reader, &mut stdout).unwrap();
io::copy(&mut reader, &mut stdout)?;
} else {
io::copy(file, &mut stdout).unwrap();
io::copy(file, &mut stdout)?;
}
Ok(())
}

#[cfg(test)]
Expand Down
15 changes: 15 additions & 0 deletions tests/by-util/test_tail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5086,6 +5086,21 @@ fn test_failed_write_is_reported() {
.stderr_is("tail: No space left on device\n");
}

#[cfg(target_os = "linux")]
#[test]
fn test_failed_write_is_reported_on_seekable_input() {
let ts = TestScenario::new("tail");
let at = &ts.fixtures;

at.write("bigfile", &"x\n".repeat(100_000));

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.

I don't know if it's machine-specific, but on my machine I have to use a much higher value (1_100_000) to make this test fail when running it without your changes to tail.rs.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hey @cakebaker, thank you for your review! I think it's system-specific, but I'd like to use a value high enough to exceed any realistic block size. I bumped it to 1_100_000.


ts.ucmd()
.arg("bigfile")
.set_stdout(File::create("/dev/full").unwrap())
.fails()
.stderr_is("tail: No space left on device\n");
}

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