Skip to content

Commit 81e68ed

Browse files
committed
coreutils: support --coreutils-prog utility selection
Accept --coreutils-prog=UTIL for explicit utility selection, including when the executable has an unrecognized name. Parse the option in the multicall entry point and provide the selected display name through a generic uucore override. Cover argument forwarding, exit status, invalid utilities, and aliases. Closes #9575.
1 parent aa74c61 commit 81e68ed

4 files changed

Lines changed: 103 additions & 6 deletions

File tree

‎src/bin/coreutils.rs‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ Currently defined functions:
5151
#[allow(clippy::cognitive_complexity)]
5252
fn main() {
5353
let utils = util_map();
54-
let mut args = uucore::args_os();
54+
let mut args = uucore::args_os().peekable();
5555

5656
let binary = validation::binary_path(&mut args);
5757
let binary_as_util = validation::name(&binary).unwrap_or_else(|| {
@@ -68,6 +68,19 @@ fn main() {
6868

6969
let util_name = if let Some(&util) = matched_util {
7070
Some(OsString::from(util))
71+
} else if let Some(util) = args
72+
.peek()
73+
.and_then(|arg| arg.to_str())
74+
.and_then(|arg| arg.strip_prefix("--coreutils-prog="))
75+
{
76+
if !utils.contains_key(util) {
77+
validation::not_found(&OsString::from(util));
78+
}
79+
uucore::set_utility_name(util.to_owned());
80+
let util = OsString::from(util);
81+
uucore::set_utility_is_second_arg();
82+
args.next();
83+
Some(util)
7184
} else if is_coreutils || binary_as_util.ends_with("box") {
7285
// todo: Remove support of "*box" from binary
7386
uucore::set_utility_is_second_arg();

‎src/uucore/src/lib/lib.rs‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ use std::os::unix::ffi::{OsStrExt, OsStringExt};
182182
use std::os::wasi::ffi::{OsStrExt, OsStringExt};
183183
use std::str;
184184
use std::str::Utf8Chunk;
185-
use std::sync::{LazyLock, atomic::Ordering};
185+
use std::sync::{LazyLock, OnceLock, atomic::Ordering};
186186

187187
/// Disables the custom signal handlers installed by Rust for stack-overflow handling. With those custom signal handlers processes ignore the first SIGBUS and SIGSEGV signal they receive.
188188
/// See <https://github.com/rust-lang/rust/blob/8ac1525e091d3db28e67adcbbd6db1e1deaa37fb/src/libstd/sys/unix/stack_overflow.rs#L71-L92> for details.
@@ -397,9 +397,19 @@ static UTIL_NAME: LazyLock<String> = LazyLock::new(|| {
397397
.into_owned()
398398
});
399399

400+
static UTILITY_NAME_OVERRIDE: OnceLock<String> = OnceLock::new();
401+
402+
/// Set the name used in diagnostics before dispatching a utility.
403+
/// The first explicit name is retained for the lifetime of the process.
404+
pub fn set_utility_name(name: String) {
405+
let _ = UTILITY_NAME_OVERRIDE.set(name);
406+
}
407+
400408
/// Derive the utility name.
401409
pub fn util_name() -> &'static str {
402-
&UTIL_NAME
410+
UTILITY_NAME_OVERRIDE
411+
.get()
412+
.map_or_else(|| &*UTIL_NAME, String::as_str)
403413
}
404414

405415
static EXECUTION_PHRASE: LazyLock<String> = LazyLock::new(|| {

‎tests/test_util_name.rs‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,3 +341,80 @@ fn test_sorted_utils() {
341341
.unwrap();
342342
assert!(out.lines().filter(|s| !s.is_empty()).is_sorted());
343343
}
344+
345+
#[test]
346+
#[cfg(all(feature = "echo", feature = "true", feature = "false"))]
347+
fn coreutils_prog_option() {
348+
use std::process::Command;
349+
let scene = TestScenario::new("dispatch");
350+
let output = Command::new(&scene.bin_path)
351+
.args(["--coreutils-prog=echo", "hello", "world"])
352+
.output()
353+
.unwrap();
354+
assert!(output.status.success());
355+
assert_eq!(output.stdout, b"hello world\n");
356+
assert!(output.stderr.is_empty());
357+
for (name, expected) in [("true", 0), ("false", 1)] {
358+
let output = Command::new(&scene.bin_path)
359+
.arg(format!("--coreutils-prog={name}"))
360+
.output()
361+
.unwrap();
362+
assert_eq!(output.status.code(), Some(expected), "{name}: {output:?}");
363+
}
364+
for name in ["missing_program", "", "--list", "--version"] {
365+
let output = Command::new(&scene.bin_path)
366+
.arg(format!("--coreutils-prog={name}"))
367+
.output()
368+
.unwrap();
369+
assert_eq!(output.status.code(), Some(1), "{name}: {output:?}");
370+
assert!(output.stdout.is_empty());
371+
let expected = if name.is_empty() {
372+
"coreutils: unknown program ''''\n".to_owned()
373+
} else {
374+
format!("coreutils: unknown program '{name}'\n")
375+
};
376+
assert_eq!(String::from_utf8_lossy(&output.stderr), expected);
377+
}
378+
}
379+
380+
#[test]
381+
#[cfg(all(unix, feature = "true", feature = "echo"))]
382+
fn coreutils_prog_option_under_unknown_name() {
383+
use std::process::Command;
384+
let scene = TestScenario::new("dispatch");
385+
let alias = scene.fixtures.plus("invalid_name");
386+
symlink_file(&scene.bin_path, &alias).unwrap();
387+
let output = Command::new(&alias)
388+
.arg("--coreutils-prog=true")
389+
.output()
390+
.unwrap();
391+
assert!(output.status.success(), "{output:?}");
392+
assert!(output.stdout.is_empty());
393+
assert!(output.stderr.is_empty());
394+
395+
let output = Command::new(&alias)
396+
.args(["--coreutils-prog=echo", "--help"])
397+
.output()
398+
.unwrap();
399+
assert!(output.status.success());
400+
assert!(
401+
String::from_utf8(output.stdout)
402+
.unwrap()
403+
.contains("Usage: echo")
404+
);
405+
}
406+
407+
#[test]
408+
#[cfg(feature = "ls")]
409+
fn coreutils_prog_diagnostics_use_selected_name() {
410+
let scene = TestScenario::new("dispatch");
411+
let output = std::process::Command::new(&scene.bin_path)
412+
.args([
413+
"--coreutils-prog=ls",
414+
"/coreutils-nonexistent-diagnostic-path",
415+
])
416+
.output()
417+
.unwrap();
418+
assert!(!output.status.success());
419+
assert!(String::from_utf8_lossy(&output.stderr).starts_with("ls:"));
420+
}

‎util/build-gnu.sh‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,6 @@ sed -i 's/^print_ver_.*/require_selinux_/' tests/runcon/runcon-compute.sh
210210
sed -i 's/^print_ver_.*/require_selinux_/' tests/runcon/runcon-no-reorder.sh
211211
sed -i 's/^print_ver_.*/require_selinux_/' tests/chcon/chcon-fail.sh
212212

213-
# We use coreutils yes
214-
sed -i "s|--coreutils-prog=||g" tests/misc/coreutils.sh
215-
216213
# Use the system coreutils where the test fails due to error in a util that is not the one being tested
217214
sed -i "s|grep '^#define HAVE_CAP 1' \$CONFIG_HEADER > /dev/null|true|" tests/ls/capability.sh
218215

0 commit comments

Comments
 (0)