From dff0ca5fa187ce44acec8a532728cf1c86dfcc36 Mon Sep 17 00:00:00 2001 From: Gunter Schmidt Date: Mon, 9 Mar 2026 13:44:53 +0100 Subject: [PATCH 1/2] Fix broken pipe error, issue #192 --- Cargo.lock | 303 +++++++++++++++++++++++++++++++++++++++++++++++++++- Cargo.toml | 1 + src/diff.rs | 22 +++- 3 files changed, 323 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c28f80b..0db1988 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -20,12 +20,56 @@ dependencies = [ "libc", ] +[[package]] +name = "anstream" +version = "0.6.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43d5b281e737544384e969a5ccad3f1cdd24b48086a0fc1b2a5262a26b8f4f4a" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + [[package]] name = "anstyle" version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8901269c6307e8d93993578286ac0edf7f195079ffff5ebdeea6a59ffb7e36bc" +[[package]] +name = "anstyle-parse" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e7644824f0aa2c7b9384579234ef10eb7efb6a0deb83f9630a49594dd9c15c2" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" +dependencies = [ + "windows-sys 0.61.1", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" +dependencies = [ + "anstyle", + "once_cell_polyfill", + "windows-sys 0.61.1", +] + [[package]] name = "anyhow" version = "1.0.102" @@ -142,8 +186,10 @@ version = "4.5.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "64b17d7ea74e9f833c7dbf2cbe4fb12ff26783eda4782a8975b72f895c9b4d99" dependencies = [ + "anstream", "anstyle", "clap_lex", + "strsim", "terminal_size", ] @@ -213,6 +259,12 @@ dependencies = [ "regex-lite", ] +[[package]] +name = "colorchoice" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75" + [[package]] name = "colored" version = "2.2.0" @@ -272,6 +324,18 @@ dependencies = [ "same-file", "tempfile", "unicode-width", + "uucore", +] + +[[package]] +name = "displaydoc" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97369cbbc041bc366949bc74d34658d6cda5621039731c6310521892a3a20ae0" +dependencies = [ + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -322,6 +386,51 @@ dependencies = [ "num-traits", ] +[[package]] +name = "fluent" +version = "0.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8137a6d5a2c50d6b0ebfcb9aaa91a28154e0a70605f112d30cb0cd4a78670477" +dependencies = [ + "fluent-bundle", + "unic-langid", +] + +[[package]] +name = "fluent-bundle" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01203cb8918f5711e73891b347816d932046f95f54207710bda99beaeb423bf4" +dependencies = [ + "fluent-langneg", + "fluent-syntax", + "intl-memoizer", + "intl_pluralrules", + "rustc-hash", + "self_cell", + "smallvec", + "unic-langid", +] + +[[package]] +name = "fluent-langneg" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7eebbe59450baee8282d71676f3bfed5689aeab00b27545e83e5f14b1195e8b0" +dependencies = [ + "unic-langid", +] + +[[package]] +name = "fluent-syntax" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "54f0d287c53ffd184d04d8677f590f4ac5379785529e5e08b1c8083acdd5c198" +dependencies = [ + "memchr", + "thiserror", +] + [[package]] name = "foldhash" version = "0.1.5" @@ -421,6 +530,31 @@ dependencies = [ "serde_core", ] +[[package]] +name = "intl-memoizer" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "310da2e345f5eb861e7a07ee182262e94975051db9e4223e909ba90f392f163f" +dependencies = [ + "type-map", + "unic-langid", +] + +[[package]] +name = "intl_pluralrules" +version = "7.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "078ea7b7c29a2b4df841a7f6ac8775ff6074020c6776d48491ce2268e068f972" +dependencies = [ + "unic-langid", +] + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" + [[package]] name = "itertools" version = "0.14.0" @@ -520,6 +654,21 @@ version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" +[[package]] +name = "once_cell_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" + +[[package]] +name = "os_display" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad5fd71b79026fb918650dde6d125000a233764f1c2f1659a1c71118e33ea08f" +dependencies = [ + "unicode-width", +] + [[package]] name = "predicates" version = "3.1.4" @@ -590,9 +739,9 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.35" +version = "1.0.45" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" +checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924" dependencies = [ "proc-macro2", ] @@ -655,6 +804,12 @@ version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" +[[package]] +name = "rustc-hash" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "357703d41365b4b27c590e3ed91eabb1b663f07c4c084095e60cbed4362dff0d" + [[package]] name = "rustix" version = "0.38.44" @@ -690,6 +845,12 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "self_cell" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b12e76d157a900eb52e81bc6e9f3069344290341720e9178cde2407113ac8d89" + [[package]] name = "semver" version = "1.0.27" @@ -739,6 +900,12 @@ dependencies = [ "zmij", ] +[[package]] +name = "smallvec" +version = "1.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03" + [[package]] name = "statrs" version = "0.18.0" @@ -749,6 +916,12 @@ dependencies = [ "num-traits", ] +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + [[package]] name = "syn" version = "2.0.117" @@ -789,6 +962,37 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3369f5ac52d5eb6ab48c6b4ffdc8efbcad6b89c765749064ba298f2c68a16a76" +[[package]] +name = "thiserror" +version = "2.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4288b5bcbc7920c07a1149a35cf9590a2aa808e0bc1eafaade0b80947865fbc4" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "2.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebc4ee7f67670e9b64d05fa4253e753e016c6c95ff35b89b7941d6b856dec1d5" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tinystr" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42d3e9c45c09de15d06dd8acf5f4e0e399e85927b7f00711024eb7ae10fa4869" +dependencies = [ + "displaydoc", + "serde_core", + "zerovec", +] + [[package]] name = "toml_datetime" version = "1.0.0+spec-1.1.0" @@ -819,6 +1023,33 @@ dependencies = [ "winnow", ] +[[package]] +name = "type-map" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb30dbbd9036155e74adad6812e9898d03ec374946234fbcebd5dfc7b9187b90" +dependencies = [ + "rustc-hash", +] + +[[package]] +name = "unic-langid" +version = "0.9.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a28ba52c9b05311f4f6e62d5d9d46f094bd6e84cb8df7b3ef952748d752a7d05" +dependencies = [ + "unic-langid-impl", +] + +[[package]] +name = "unic-langid-impl" +version = "0.9.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dce1bf08044d4b7a94028c93786f8566047edc11110595914de93362559bc658" +dependencies = [ + "tinystr", +] + [[package]] name = "unicode-ident" version = "1.0.12" @@ -837,6 +1068,40 @@ version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853" +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + +[[package]] +name = "uucore" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b157ba598d7f7ed06f6dbc62999edb9d730b4d3fb58e503d8ad6d5fbe1e04391" +dependencies = [ + "clap", + "fluent", + "fluent-bundle", + "fluent-syntax", + "nix", + "os_display", + "thiserror", + "unic-langid", + "uucore_procs", + "wild", +] + +[[package]] +name = "uucore_procs" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "daa291a52608ac5a2f8539e119666e021baa6b8c01f22f02ed201bbae54cbbc0" +dependencies = [ + "proc-macro2", + "quote", +] + [[package]] name = "wait-timeout" version = "0.2.0" @@ -958,6 +1223,15 @@ dependencies = [ "semver", ] +[[package]] +name = "wild" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3131afc8c575281e1e80f36ed6a092aa502c08b18ed7524e86fbbb12bb410e1" +dependencies = [ + "glob", +] + [[package]] name = "winapi" version = "0.3.9" @@ -1022,6 +1296,15 @@ dependencies = [ "windows-targets 0.52.6", ] +[[package]] +name = "windows-sys" +version = "0.61.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f109e41dd4a3c848907eb83d5a42ea98b3769495597450cf6d153507b166f0f" +dependencies = [ + "windows-link", +] + [[package]] name = "windows-targets" version = "0.48.5" @@ -1246,6 +1529,22 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cfe53a6657fd280eaa890a3bc59152892ffa3e30101319d168b781ed6529b049" +[[package]] +name = "zerofrom" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50cc42e0333e05660c3587f3bf9d0478688e15d870fab3346451ce7f8c9fbea5" + +[[package]] +name = "zerovec" +version = "0.11.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c28719294829477f525be0186d13efa9a3c602f7ec202ca9e353d310fb9a002" +dependencies = [ + "serde", + "zerofrom", +] + [[package]] name = "zmij" version = "1.0.21" diff --git a/Cargo.toml b/Cargo.toml index 1673839..092efee 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,7 @@ itoa = "1.0.11" regex = "1.10.4" same-file = "1.0.6" unicode-width = "0.2.0" +uucore = "0.6.0" [dev-dependencies] assert_cmd = "2.0.14" diff --git a/src/diff.rs b/src/diff.rs index f4c0614..a538573 100644 --- a/src/diff.rs +++ b/src/diff.rs @@ -12,6 +12,7 @@ use std::fs; use std::io::{self, stdout, Read, Write}; use std::iter::Peekable; use std::process::{exit, ExitCode}; +use uucore::error::FromIo; // Exit codes are documented at // https://www.gnu.org/software/diffutils/manual/html_node/Invoking-diff.html. @@ -91,7 +92,26 @@ pub fn main(opts: Peekable) -> ExitCode { params.to.to_string_lossy() ); } else { - io::stdout().write_all(&result).unwrap(); + let result = io::stdout().write_all(&result); + match result { + // This code is taken from coreutils. + // + Ok(()) => {} + Err(err) if err.kind() == std::io::ErrorKind::BrokenPipe => { + // GNU seq prints the Broken pipe message but still exits with status 0 + // unless SIGPIPE was explicitly ignored, in which case it should fail. + let err = err.map_err_context(|| "write error".into()); + uucore::show_error!("{err}"); + #[cfg(unix)] + if uucore::signals::sigpipe_was_ignored() { + uucore::error::set_exit_code(1); + } + } + Err(err) => { + eprintln!("{err}"); + return ExitCode::FAILURE; + } + } } if result.is_empty() { maybe_report_identical_files(); From b00dd7b5cb4882d14dc8f22ca335aaf0f4224c92 Mon Sep 17 00:00:00 2001 From: Gunter Schmidt Date: Thu, 12 Mar 2026 07:28:17 +0100 Subject: [PATCH 2/2] fix: panic on write to /dev/full * fixes issue #165 * simpler format of io errors (remove (os error 2)) --- Cargo.lock | 4 ++-- src/cmp.rs | 25 ++++++++++++++++++++----- src/diff.rs | 6 +++--- src/utils.rs | 9 ++++++--- 4 files changed, 31 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b111edc..e40c876 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -833,7 +833,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys 0.12.1", - "windows-sys 0.59.0", + "windows-sys 0.61.1", ] [[package]] @@ -943,7 +943,7 @@ dependencies = [ "getrandom 0.4.2", "once_cell", "rustix 1.1.4", - "windows-sys 0.59.0", + "windows-sys 0.61.1", ] [[package]] diff --git a/src/cmp.rs b/src/cmp.rs index 587d5cc..8a06f7b 100644 --- a/src/cmp.rs +++ b/src/cmp.rs @@ -3,7 +3,7 @@ // For the full copyright and license information, please view the LICENSE-* // files that was distributed with this source code. -use crate::utils::format_failure_to_read_input_file; +use crate::utils::{format_failure_to_read_input_file, format_io_error}; use std::env::{self, ArgsOs}; use std::ffi::OsString; use std::io::{BufRead, BufReader, BufWriter, Read, Write}; @@ -441,7 +441,7 @@ pub fn cmp(params: &Params) -> Result { })?; output.clear(); } else { - report_difference(from_byte, to_byte, at_byte, at_line, params); + report_difference(from_byte, to_byte, at_byte, at_line, params)?; return Ok(Cmp::Different); } } @@ -707,9 +707,15 @@ fn is_posix_locale() -> bool { } #[inline] -fn report_difference(from_byte: u8, to_byte: u8, at_byte: usize, at_line: usize, params: &Params) { +fn report_difference( + from_byte: u8, + to_byte: u8, + at_byte: usize, + at_line: usize, + params: &Params, +) -> Result<(), String> { if params.quiet { - return; + return Ok(()); } let term = if is_posix_locale() && !params.print_bytes { @@ -734,7 +740,16 @@ fn report_difference(from_byte: u8, to_byte: u8, at_byte: usize, at_line: usize, format_visible_byte(to_byte) ); } - println!(); + // Instead of println!(), which panics in case of error (> /dev/full). + let mut stdout = io::stdout(); + if let Err(e) = writeln!(stdout) { + return Err(format_io_error(&e)); + }; + if let Err(e) = stdout.flush() { + return Err(format_io_error(&e)); + }; + + Ok(()) } #[cfg(test)] diff --git a/src/diff.rs b/src/diff.rs index a538573..0610f5e 100644 --- a/src/diff.rs +++ b/src/diff.rs @@ -4,7 +4,7 @@ // files that was distributed with this source code. use crate::params::{parse_params, Format}; -use crate::utils::report_failure_to_read_input_file; +use crate::utils::{format_io_error, report_failure_to_read_input_file}; use crate::{context_diff, ed_diff, normal_diff, side_diff, unified_diff}; use std::env::ArgsOs; use std::ffi::OsString; @@ -107,8 +107,8 @@ pub fn main(opts: Peekable) -> ExitCode { uucore::error::set_exit_code(1); } } - Err(err) => { - eprintln!("{err}"); + Err(error) => { + eprintln!("{}", format_io_error(&error)); return ExitCode::FAILURE; } } diff --git a/src/utils.rs b/src/utils.rs index daca18d..3fd901a 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -3,7 +3,6 @@ // For the full copyright and license information, please view the LICENSE-* // files that was distributed with this source code. -use regex::Regex; use std::{ffi::OsString, io::Write}; use unicode_width::UnicodeWidthStr; @@ -78,15 +77,19 @@ pub fn format_failure_to_read_input_file( ) -> String { // std::io::Error's display trait outputs "{detail} (os error {code})" // but we want only the {detail} (error string) part - let error_code_re = Regex::new(r"\ \(os\ error\ \d+\)$").unwrap(); format!( "{}: {}: {}", executable.to_string_lossy(), filepath.to_string_lossy(), - error_code_re.replace(error.to_string().as_str(), ""), + format_io_error(&error), ) } +pub fn format_io_error(error: &dyn std::error::Error) -> String { + let s = error.to_string(); + s.split(" (os error").next().unwrap_or(&s).to_string() +} + pub fn report_failure_to_read_input_file( executable: &OsString, filepath: &OsString,