Skip to content

Commit c64b886

Browse files
limadog9sylvestre
authored andcommitted
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 85dd1ff commit c64b886

4 files changed

Lines changed: 103 additions & 7 deletions

File tree

‎src/bin/coreutils.rs‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ Currently defined functions:
6565
#[allow(clippy::cognitive_complexity)]
6666
fn main() -> ExitCode {
6767
let utils = util_map();
68-
let mut args = uucore::args_os();
68+
let mut args = uucore::args_os().peekable();
6969

7070
let binary = validation::binary_path(&mut args);
7171
let Some(binary_as_util) = validation::name(&binary) else {
@@ -77,6 +77,19 @@ fn main() -> ExitCode {
7777
let is_coreutils = binary_as_util.ends_with("utils");
7878
let util_name = if let Some(util) = matched_utility(binary_as_util, &utils) {
7979
Some(OsString::from(util))
80+
} else if let Some(util) = args
81+
.peek()
82+
.and_then(|arg| arg.to_str())
83+
.and_then(|arg| arg.strip_prefix("--coreutils-prog="))
84+
{
85+
if !utils.contains_key(util) {
86+
return validation::not_found(&OsString::from(util));
87+
}
88+
uucore::set_utility_name(util.to_owned());
89+
let util = OsString::from(util);
90+
uucore::set_utility_is_second_arg();
91+
args.next();
92+
Some(util)
8093
} else if is_coreutils || binary_as_util.ends_with("box") {
8194
// todo: Remove support of "*box" from binary
8295
uucore::set_utility_is_second_arg();

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,7 @@ use std::os::unix::ffi::{OsStrExt, OsStringExt};
189189
#[cfg(all(target_os = "wasi", target_env = "p1"))]
190190
use std::os::wasi::ffi::{OsStrExt, OsStringExt};
191191
use std::str::Utf8Chunk;
192-
use std::sync::LazyLock;
193-
use std::sync::atomic::Ordering;
192+
use std::sync::{LazyLock, OnceLock, atomic::Ordering};
194193
use std::{iter, str};
195194

196195
/// 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.
@@ -406,9 +405,19 @@ static UTIL_NAME: LazyLock<String> = LazyLock::new(|| {
406405
.into_owned()
407406
});
408407

408+
static UTILITY_NAME_OVERRIDE: OnceLock<String> = OnceLock::new();
409+
410+
/// Set the name used in diagnostics before dispatching a utility.
411+
/// The first explicit name is retained for the lifetime of the process.
412+
pub fn set_utility_name(name: String) {
413+
let _ = UTILITY_NAME_OVERRIDE.set(name);
414+
}
415+
409416
/// Derive the utility name.
410417
pub fn util_name() -> &'static str {
411-
&UTIL_NAME
418+
UTILITY_NAME_OVERRIDE
419+
.get()
420+
.map_or_else(|| &*UTIL_NAME, String::as_str)
412421
}
413422

414423
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
@@ -401,3 +401,80 @@ fn test_utility_alias_rejects_unknown_target(#[case] chain: bool) {
401401
assert!(output.stdout.is_empty());
402402
assert_eq!(output.stderr, b"coreutils: unknown program 'dummy'\n");
403403
}
404+
405+
#[test]
406+
#[cfg(all(feature = "echo", feature = "true", feature = "false"))]
407+
fn coreutils_prog_option() {
408+
use std::process::Command;
409+
let scene = TestScenario::new("dispatch");
410+
let output = Command::new(&scene.bin_path)
411+
.args(["--coreutils-prog=echo", "hello", "world"])
412+
.output()
413+
.unwrap();
414+
assert!(output.status.success());
415+
assert_eq!(output.stdout, b"hello world\n");
416+
assert!(output.stderr.is_empty());
417+
for (name, expected) in [("true", 0), ("false", 1)] {
418+
let output = Command::new(&scene.bin_path)
419+
.arg(format!("--coreutils-prog={name}"))
420+
.output()
421+
.unwrap();
422+
assert_eq!(output.status.code(), Some(expected), "{name}: {output:?}");
423+
}
424+
for name in ["missing_program", "", "--list", "--version"] {
425+
let output = Command::new(&scene.bin_path)
426+
.arg(format!("--coreutils-prog={name}"))
427+
.output()
428+
.unwrap();
429+
assert_eq!(output.status.code(), Some(1), "{name}: {output:?}");
430+
assert!(output.stdout.is_empty());
431+
let expected = if name.is_empty() {
432+
"coreutils: unknown program ''''\n".to_owned()
433+
} else {
434+
format!("coreutils: unknown program '{name}'\n")
435+
};
436+
assert_eq!(String::from_utf8_lossy(&output.stderr), expected);
437+
}
438+
}
439+
440+
#[test]
441+
#[cfg(all(unix, feature = "true", feature = "echo"))]
442+
fn coreutils_prog_option_under_unknown_name() {
443+
use std::process::Command;
444+
let scene = TestScenario::new("dispatch");
445+
let alias = scene.fixtures.plus("invalid_name");
446+
symlink_file(&scene.bin_path, &alias).unwrap();
447+
let output = Command::new(&alias)
448+
.arg("--coreutils-prog=true")
449+
.output()
450+
.unwrap();
451+
assert!(output.status.success(), "{output:?}");
452+
assert!(output.stdout.is_empty());
453+
assert!(output.stderr.is_empty());
454+
455+
let output = Command::new(&alias)
456+
.args(["--coreutils-prog=echo", "--help"])
457+
.output()
458+
.unwrap();
459+
assert!(output.status.success());
460+
assert!(
461+
String::from_utf8(output.stdout)
462+
.unwrap()
463+
.contains("Usage: echo")
464+
);
465+
}
466+
467+
#[test]
468+
#[cfg(feature = "ls")]
469+
fn coreutils_prog_diagnostics_use_selected_name() {
470+
let scene = TestScenario::new("dispatch");
471+
let output = std::process::Command::new(&scene.bin_path)
472+
.args([
473+
"--coreutils-prog=ls",
474+
"/coreutils-nonexistent-diagnostic-path",
475+
])
476+
.output()
477+
.unwrap();
478+
assert!(!output.status.success());
479+
assert!(String::from_utf8_lossy(&output.stderr).starts_with("ls:"));
480+
}

‎util/build-gnu.sh‎

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

212-
# We use coreutils yes
213-
sed -i "s|--coreutils-prog=||g" tests/misc/coreutils.sh
214-
215212
# Do not skip
216213
sed -i "s|skip_|true|g" tests/install/stdin.sh
217214

0 commit comments

Comments
 (0)