1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 19:47:45 +00:00

Merge pull request #2752 from Smicry/master

tail use UResult
This commit is contained in:
Sylvestre Ledru 2021-11-21 10:14:03 +01:00 committed by GitHub
commit 1d794e1a94
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -191,7 +191,7 @@ fn uu_tail(settings: &Settings) -> UResult<()> {
if use_stdin { if use_stdin {
let mut reader = BufReader::new(stdin()); let mut reader = BufReader::new(stdin());
unbounded_tail(&mut reader, settings); unbounded_tail(&mut reader, settings)?;
// Don't follow stdin since there are no checks for pipes/FIFOs // Don't follow stdin since there are no checks for pipes/FIFOs
// //
@ -230,7 +230,7 @@ fn uu_tail(settings: &Settings) -> UResult<()> {
} }
} else { } else {
let mut reader = BufReader::new(file); let mut reader = BufReader::new(file);
unbounded_tail(&mut reader, settings); unbounded_tail(&mut reader, settings)?;
if settings.follow { if settings.follow {
readers.push((Box::new(reader), filename)); readers.push((Box::new(reader), filename));
} }
@ -239,7 +239,7 @@ fn uu_tail(settings: &Settings) -> UResult<()> {
} }
if settings.follow { if settings.follow {
follow(&mut readers[..], settings); follow(&mut readers[..], settings)?;
} }
Ok(()) Ok(())
@ -342,10 +342,9 @@ pub fn uu_app() -> App<'static, 'static> {
) )
} }
fn follow<T: BufRead>(readers: &mut [(T, &String)], settings: &Settings) { fn follow<T: BufRead>(readers: &mut [(T, &String)], settings: &Settings) -> UResult<()> {
assert!(settings.follow); if readers.is_empty() || !settings.follow {
if readers.is_empty() { return Ok(());
return;
} }
let mut last = readers.len() - 1; let mut last = readers.len() - 1;
@ -372,7 +371,7 @@ fn follow<T: BufRead>(readers: &mut [(T, &String)], settings: &Settings) {
} }
print!("{}", datum); print!("{}", datum);
} }
Err(err) => panic!("{}", err), Err(err) => return Err(USimpleError::new(1, err.to_string())),
} }
} }
} }
@ -381,6 +380,7 @@ fn follow<T: BufRead>(readers: &mut [(T, &String)], settings: &Settings) {
break; break;
} }
} }
Ok(())
} }
/// Iterate over bytes in the file, in reverse, until we find the /// Iterate over bytes in the file, in reverse, until we find the
@ -475,7 +475,7 @@ where
} }
} }
fn unbounded_tail<T: Read>(reader: &mut BufReader<T>, settings: &Settings) { fn unbounded_tail<T: Read>(reader: &mut BufReader<T>, settings: &Settings) -> UResult<()> {
// Read through each line/char and store them in a ringbuffer that always // Read through each line/char and store them in a ringbuffer that always
// contains count lines/chars. When reaching the end of file, output the // contains count lines/chars. When reaching the end of file, output the
// data in the ringbuf. // data in the ringbuf.
@ -487,11 +487,13 @@ fn unbounded_tail<T: Read>(reader: &mut BufReader<T>, settings: &Settings) {
} }
FilterMode::Bytes(count) => { FilterMode::Bytes(count) => {
for byte in unbounded_tail_collect(reader.bytes(), count, settings.beginning) { for byte in unbounded_tail_collect(reader.bytes(), count, settings.beginning) {
let mut stdout = stdout(); if let Err(err) = stdout().write(&[byte]) {
print_byte(&mut stdout, byte); return Err(USimpleError::new(1, err.to_string()));
}
} }
} }
} }
Ok(())
} }
fn is_seekable<T: Seek>(file: &mut T) -> bool { fn is_seekable<T: Seek>(file: &mut T) -> bool {
@ -500,13 +502,6 @@ fn is_seekable<T: Seek>(file: &mut T) -> bool {
&& file.seek(SeekFrom::Start(0)).is_ok() && file.seek(SeekFrom::Start(0)).is_ok()
} }
#[inline]
fn print_byte<T: Write>(stdout: &mut T, ch: u8) {
if let Err(err) = stdout.write(&[ch]) {
crash!(1, "{}", err);
}
}
fn parse_num(src: &str) -> Result<(usize, bool), ParseSizeError> { fn parse_num(src: &str) -> Result<(usize, bool), ParseSizeError> {
let mut size_string = src.trim(); let mut size_string = src.trim();
let mut starting_with = false; let mut starting_with = false;