From 3ae273a0d1be12680791705f813e66a42c5eec12 Mon Sep 17 00:00:00 2001 From: Daniel Hofstetter Date: Thu, 23 Oct 2025 16:17:13 +0200 Subject: [PATCH 1/2] du: handle --files0-from=- with piped in '-' --- src/uu/du/locales/en-US.ftl | 1 + src/uu/du/locales/fr-FR.ftl | 1 + src/uu/du/src/du.rs | 10 ++++++---- tests/by-util/test_du.rs | 16 +++++++++++++--- 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/uu/du/locales/en-US.ftl b/src/uu/du/locales/en-US.ftl index b503d8d539f..bd6c095bacb 100644 --- a/src/uu/du/locales/en-US.ftl +++ b/src/uu/du/locales/en-US.ftl @@ -69,6 +69,7 @@ du-error-printing-thread-panicked = Printing thread panicked. du-error-invalid-suffix = invalid suffix in --{ $option } argument { $value } du-error-invalid-argument = invalid --{ $option } argument { $value } du-error-argument-too-large = --{ $option } argument { $value } too large +du-error-hyphen-file-name-not-allowed = when reading file names from standard input, no file name of '-' allowed # Verbose/status messages du-verbose-ignored = { $path } ignored diff --git a/src/uu/du/locales/fr-FR.ftl b/src/uu/du/locales/fr-FR.ftl index e89385213aa..81bc80c719f 100644 --- a/src/uu/du/locales/fr-FR.ftl +++ b/src/uu/du/locales/fr-FR.ftl @@ -69,6 +69,7 @@ du-error-printing-thread-panicked = Le thread d'affichage a paniqué. du-error-invalid-suffix = suffixe invalide dans l'argument --{ $option } { $value } du-error-invalid-argument = argument --{ $option } invalide { $value } du-error-argument-too-large = argument --{ $option } { $value } trop grand +du-error-hyphen-file-name-not-allowed = le nom de fichier '-' n'est pas autorisé lors de la lecture de l'entrée standard # Messages verbeux/de statut du-verbose-ignored = { $path } ignoré diff --git a/src/uu/du/src/du.rs b/src/uu/du/src/du.rs index 4c29d07d3fb..f4b2edf36cd 100644 --- a/src/uu/du/src/du.rs +++ b/src/uu/du/src/du.rs @@ -2,16 +2,15 @@ // // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. +// // spell-checker:ignore fstatat openat dirfd use clap::{Arg, ArgAction, ArgMatches, Command, builder::PossibleValue}; use glob::Pattern; use std::collections::HashSet; use std::env; -use std::ffi::OsStr; -use std::ffi::OsString; -use std::fs::Metadata; -use std::fs::{self, DirEntry, File}; +use std::ffi::{OsStr, OsString}; +use std::fs::{self, DirEntry, File, Metadata}; use std::io::{BufRead, BufReader, stdout}; #[cfg(not(windows))] use std::os::unix::fs::MetadataExt; @@ -942,6 +941,9 @@ fn read_files_from(file_name: &OsStr) -> Result, std::io::Error> { translate!("du-error-invalid-zero-length-file-name", "file" => file_name.to_string_lossy(), "line" => line_number) ); set_exit_code(1); + } else if path == b"-" && file_name == "-" { + show_error!("{}", translate!("du-error-hyphen-file-name-not-allowed")); + set_exit_code(1); } else { let p = PathBuf::from(&*uucore::os_str_from_bytes(&path).unwrap()); if !paths.contains(&p) { diff --git a/tests/by-util/test_du.rs b/tests/by-util/test_du.rs index 7c6dcd6c33a..4913860c7fc 100644 --- a/tests/by-util/test_du.rs +++ b/tests/by-util/test_du.rs @@ -5,17 +5,16 @@ // spell-checker:ignore (paths) atim sublink subwords azerty azeaze xcwww azeaz amaz azea qzerty tazerty tsublink testfile1 testfile2 filelist fpath testdir testfile // spell-checker:ignore selfref ELOOP smallfile + #[cfg(not(windows))] use regex::Regex; -use uutests::at_and_ucmd; -use uutests::new_ucmd; #[cfg(not(target_os = "windows"))] use uutests::unwrap_or_return; use uutests::util::TestScenario; #[cfg(not(target_os = "windows"))] use uutests::util::expected_result; -use uutests::util_name; +use uutests::{at_and_ucmd, new_ucmd, util_name}; #[cfg(not(target_os = "openbsd"))] const SUB_DIR: &str = "subdir/deeper"; @@ -1392,6 +1391,17 @@ fn test_du_files0_from_stdin_with_invalid_zero_length_file_names() { .stderr_contains("-:2: invalid zero-length file name"); } +#[test] +fn test_du_files0_from_stdin_with_stdin_as_input() { + new_ucmd!() + .arg("--files0-from=-") + .pipe_in("-") + .fails_with_code(1) + .stderr_is( + "du: when reading file names from standard input, no file name of '-' allowed\n", + ); +} + #[test] fn test_du_files0_from_dir() { let ts = TestScenario::new(util_name!()); From 4f0acb788f1d0da56f834d9d3aa936a26121cbb4 Mon Sep 17 00:00:00 2001 From: Daniel Hofstetter Date: Fri, 24 Oct 2025 16:29:58 +0200 Subject: [PATCH 2/2] build-gnu.sh: remove incorrect string replacement in tests/du/files0-from.pl --- util/build-gnu.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/util/build-gnu.sh b/util/build-gnu.sh index 734088252f4..2003b72b62c 100755 --- a/util/build-gnu.sh +++ b/util/build-gnu.sh @@ -326,7 +326,6 @@ sed -i -e "s/du: invalid -t argument/du: invalid --threshold argument/" -e "s/du # Remove the extra output check sed -i -e "s|Try '\$prog --help' for more information.\\\n||" tests/du/files0-from.pl -sed -i -e "s|when reading file names from stdin, no file name of\"|-: No such file or directory\n\"|" -e "s| '-' allowed\\\n||" tests/du/files0-from.pl sed -i -e "s|-: No such file or directory|cannot access '-': No such file or directory|g" tests/du/files0-from.pl # Skip the move-dir-while-traversing test - our implementation uses safe traversal with openat()