diff --git a/src/uu/chroot/src/chroot.rs b/src/uu/chroot/src/chroot.rs index 7828d2377d5..458ae5dc9ba 100644 --- a/src/uu/chroot/src/chroot.rs +++ b/src/uu/chroot/src/chroot.rs @@ -8,7 +8,7 @@ mod error; use crate::error::ChrootError; use clap::{Arg, ArgAction, Command}; -use std::ffi::CString; +use std::ffi::{CString, OsStr}; use std::io::{Error, ErrorKind}; use std::os::unix::prelude::OsStrExt; use std::os::unix::process::CommandExt; @@ -159,9 +159,9 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { let matches = uucore::clap_localization::handle_clap_result_with_exit_code(uu_app(), args, 125)?; - let default_shell: &'static str = "/bin/sh"; - let default_option: &'static str = "-i"; - let user_shell = std::env::var("SHELL"); + let default_shell: &'static OsStr = OsStr::new("/bin/sh"); + let default_option: &'static OsStr = OsStr::new("-i"); + let user_shell = std::env::var_os("SHELL"); let options = Options::from(&matches)?; @@ -186,22 +186,19 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { return Err(ChrootError::NoSuchDirectory(options.newroot).into()); } - let commands = match matches.get_many::(options::COMMAND) { - Some(v) => v.map(String::as_str).collect(), - None => vec![], - }; + let commands: Vec<&OsStr> = matches + .get_many::(options::COMMAND) + .map_or_else(Vec::new, |v| v.map(OsStr::new).collect()); // TODO: refactor the args and command matching // See: https://github.com/uutils/coreutils/pull/2365#discussion_r647849967 - let command: Vec<&str> = match commands.len() { - 0 => { - let shell: &str = match user_shell { - Err(_) => default_shell, - Ok(ref s) => s.as_ref(), - }; - vec![shell, default_option] - } - _ => commands, + let command = if commands.is_empty() { + vec![ + user_shell.as_deref().unwrap_or(default_shell), + default_option, + ] + } else { + commands }; assert!(!command.is_empty()); diff --git a/src/uu/chroot/src/error.rs b/src/uu/chroot/src/error.rs index 15922ad835e..0592d72efad 100644 --- a/src/uu/chroot/src/error.rs +++ b/src/uu/chroot/src/error.rs @@ -4,6 +4,7 @@ // file that was distributed with this source code. // spell-checker:ignore NEWROOT Userspec userspec //! Errors returned by chroot. +use std::ffi::OsString; use std::io::Error; use std::path::PathBuf; use thiserror::Error; @@ -21,11 +22,11 @@ pub enum ChrootError { /// Failed to execute the specified command. #[error("{}", translate!("chroot-error-command-failed", "cmd" => _0.quote(), "err" => _1))] - CommandFailed(String, #[source] Error), + CommandFailed(OsString, #[source] Error), /// Failed to find the specified command. #[error("{}", translate!("chroot-error-command-not-found", "cmd" => _0.quote(), "err" => _1))] - CommandNotFound(String, #[source] Error), + CommandNotFound(OsString, #[source] Error), #[error("{}", translate!("chroot-error-groups-parsing-failed"))] GroupsParsingFailed,