Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
44 changes: 28 additions & 16 deletions src/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ use std::os::fd::{AsRawFd, FromRawFd};
#[cfg(unix)]
use std::os::unix::fs::MetadataExt;

#[cfg(target_os = "windows")]
use std::os::windows::fs::MetadataExt;

/// for --bytes, so really large number limits can be expressed, like 1Y.
pub type BytesLimitU64 = u64;
// ignore initial is currently limited to u64, as take(skip) is used.
Expand Down Expand Up @@ -295,6 +292,20 @@ fn prepare_reader(
Ok(reader)
}

/// Returns the size of `path` when it is a regular file, and `None` for standard input
/// (`-`), pipes, devices and paths that cannot be stat'ed.
fn regular_file_size(path: &OsString) -> Option<u64> {
if path == "-" {
return None;
}
let meta = fs::metadata(path).ok()?;
if !meta.is_file() {
return None;
}

Some(meta.len())
}

#[derive(Debug)]
pub enum Cmp {
Equal,
Expand All @@ -307,24 +318,25 @@ pub fn cmp(params: &Params) -> Result<Cmp, String> {

let mut offset_width = params.max_bytes.unwrap_or(BytesLimitU64::MAX);

if let (Ok(a_meta), Ok(b_meta)) = (fs::metadata(&params.from), fs::metadata(&params.to)) {
#[cfg(not(target_os = "windows"))]
let (a_size, b_size) = (a_meta.len(), b_meta.len());

#[cfg(target_os = "windows")]
let (a_size, b_size) = (a_meta.file_size(), b_meta.file_size());
let a_size = regular_file_size(&params.from);
let b_size = regular_file_size(&params.to);

// If the files have different sizes, we already know they are not identical. If we have not
// been asked to show even the first difference, we can quit early.
// If the files have different sizes, we already know they are not identical. If we have not
// been asked to show even the first difference, we can quit early.
if let (Some(a_size), Some(b_size)) = (a_size, b_size) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

this also changes -s with a pipe/device on one side, could you please add a test for it?

if params.quiet && a_size != b_size {
return Ok(Cmp::Different);
}
}

// The offsets count from the end of the skipped bytes.
let a_left = a_size.saturating_sub(params.skip_a.unwrap_or(0));
let b_left = b_size.saturating_sub(params.skip_b.unwrap_or(0));
let smaller = cmp::min(a_left, b_left) as BytesLimitU64;
offset_width = cmp::min(smaller, offset_width);
// Like GNU cmp, size the offset column from every operand that is a regular file, so that
// standard input or a pipe on one side does not widen it to the u64::MAX width. The offsets
// count from the end of the skipped bytes.
for (size, skip) in [(a_size, params.skip_a), (b_size, params.skip_b)] {
if let Some(size) = size {
let left = size.saturating_sub(skip.unwrap_or(0)) as BytesLimitU64;
offset_width = cmp::min(left, offset_width);
}
}

let offset_width = 1 + offset_width.checked_ilog10().unwrap_or(1) as usize;
Expand Down
50 changes: 50 additions & 0 deletions tests/by-util/test_cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,56 @@ fn cmp_verbose_offset_width_after_skip() {
.stderr_str_check(|s| s.ends_with(" after byte 9\n"));
}

// With stdin on one side, the -l offset column is sized from the other operand,
// which is a regular file.
#[test]
fn cmp_verbose_offset_width_stdin() {
let (at, ts) = at_and_ts!();
at.write("b", "abcdeXghiY");

ts.ucmd()
.env("LC_ALL", "C")
.args(&["-l", "-", "b"])
.pipe_in("abcdefghij")
.fails_with_code(1)
.stdout_is(" 6 146 130\n10 152 131\n");

ts.ucmd()
.env("LC_ALL", "C")
.args(&["-l", "b", "-"])
.pipe_in("abcdefghij")
.fails_with_code(1)
.stdout_is(" 6 130 146\n10 131 152\n");
}

// /dev/stdin is a pipe here, so its metadata reports a size of 0. That size must
// not be used for the -s early exit, or equal input is reported as different.
#[test]
#[cfg(unix)]
fn cmp_silent_pipe_operand_is_read() {
let (at, ts) = at_and_ts!();
at.write("b", "abcdefghij");
at.write("empty", "");

for args in [["-s", "/dev/stdin", "b"], ["-s", "b", "/dev/stdin"]] {
ts.ucmd()
.args(&args)
.pipe_in("abcdefghij")
.succeeds()
.no_output();
ts.ucmd()
.args(&args)
.pipe_in("abcdeXghij")
.fails_with_code(1)
.no_output();
}

// A character device on one side: /dev/null is empty, so it equals an empty
// file and differs from a non-empty one.
ts.ucmd().args(&["-s", "/dev/null", "empty"]).succeeds();
ts.ucmd().args(&["-s", "/dev/null", "b"]).fails_with_code(1);
}

#[test]
fn cmp_incompatible_params() {
new_ucmd!()
Expand Down
Loading