Skip to content

Commit

Permalink
Merge pull request uutils#2886 from jfinkels/tail-io-error
Browse files Browse the repository at this point in the history
tail: improve error handling when file not found
  • Loading branch information
sylvestre authored Jan 18, 2022
2 parents ec386fa + e575007 commit 13de6ca
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/uu/tail/src/tail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use std::path::Path;
use std::thread::sleep;
use std::time::Duration;
use uucore::display::Quotable;
use uucore::error::{UResult, USimpleError};
use uucore::error::{FromIo, UResult, USimpleError};
use uucore::parse_size::{parse_size, ParseSizeError};
use uucore::ringbuffer::RingBuffer;

Expand Down Expand Up @@ -220,7 +220,8 @@ fn uu_tail(settings: &Settings) -> UResult<()> {
if path.is_dir() {
continue;
}
let mut file = File::open(&path).unwrap();
let mut file = File::open(&path)
.map_err_context(|| format!("cannot open {} for reading", filename.quote()))?;
let md = file.metadata().unwrap();
if is_seekable(&mut file) && get_block_size(&md) > 0 {
bounded_tail(&mut file, settings);
Expand Down
11 changes: 10 additions & 1 deletion tests/by-util/test_tail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// * For the full copyright and license information, please view the LICENSE
// * file that was distributed with this source code.

// spell-checker:ignore (ToDO) abcdefghijklmnopqrstuvwxyz efghijklmnopqrstuvwxyz vwxyz emptyfile
// spell-checker:ignore (ToDO) abcdefghijklmnopqrstuvwxyz efghijklmnopqrstuvwxyz vwxyz emptyfile bogusfile

extern crate tail;

Expand Down Expand Up @@ -475,3 +475,12 @@ fn test_tail_bytes_for_funny_files() {
.code_is(exp_result.code());
}
}

#[test]
fn test_no_such_file() {
new_ucmd!()
.arg("bogusfile")
.fails()
.no_stdout()
.stderr_contains("cannot open 'bogusfile' for reading: No such file or directory");
}

0 comments on commit 13de6ca

Please sign in to comment.