Merge pull request #2643 from jhscheer/tail_fix_for_gnutestsuite

tail: add fixes to pass "tail-2/tail-c.sh" from GNU's test suite
This commit is contained in:
Sylvestre Ledru 2021-09-08 17:42:22 +02:00 committed by GitHub
commit ebc801c57a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 2 deletions

View file

@ -22,7 +22,7 @@ use chunks::ReverseChunks;
use clap::{App, Arg};
use std::collections::VecDeque;
use std::fmt;
use std::fs::File;
use std::fs::{File, Metadata};
use std::io::{stdin, stdout, BufRead, BufReader, Read, Seek, SeekFrom, Write};
use std::path::Path;
use std::thread::sleep;
@ -32,6 +32,8 @@ use uucore::ringbuffer::RingBuffer;
#[cfg(unix)]
use crate::platform::stdin_is_pipe_or_fifo;
#[cfg(unix)]
use std::os::unix::fs::MetadataExt;
pub mod options {
pub mod verbosity {
@ -189,7 +191,8 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
continue;
}
let mut file = File::open(&path).unwrap();
if is_seekable(&mut file) {
let md = file.metadata().unwrap();
if is_seekable(&mut file) && get_block_size(&md) > 0 {
bounded_tail(&mut file, &settings);
if settings.follow {
let reader = BufReader::new(file);
@ -437,6 +440,8 @@ fn unbounded_tail<T: Read>(reader: &mut BufReader<T>, settings: &Settings) {
fn is_seekable<T: Seek>(file: &mut T) -> bool {
file.seek(SeekFrom::Current(0)).is_ok()
&& file.seek(SeekFrom::End(0)).is_ok()
&& file.seek(SeekFrom::Start(0)).is_ok()
}
#[inline]
@ -464,3 +469,14 @@ fn parse_num(src: &str) -> Result<(usize, bool), ParseSizeError> {
parse_size(size_string).map(|n| (n, starting_with))
}
fn get_block_size(md: &Metadata) -> u64 {
#[cfg(unix)]
{
md.blocks()
}
#[cfg(not(unix))]
{
md.len()
}
}

View file

@ -425,3 +425,23 @@ fn test_tail_num_with_undocumented_sign_bytes() {
.succeeds()
.stdout_is("efghijklmnopqrstuvwxyz");
}
#[test]
#[cfg(unix)]
fn test_tail_bytes_for_funny_files() {
// gnu/tests/tail-2/tail-c.sh
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
for &file in &["/proc/version", "/sys/kernel/profiling"] {
if !at.file_exists(file) {
continue;
}
let args = ["--bytes", "1", file];
let result = ts.ucmd().args(&args).run();
let exp_result = unwrap_or_return!(expected_result(&ts, &args));
result
.stdout_is(exp_result.stdout_str())
.stderr_is(exp_result.stderr_str())
.code_is(exp_result.code());
}
}