Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 14 additions & 26 deletions src/uu/chroot/src/chroot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod error;

use crate::error::ChrootError;
use clap::{Arg, ArgAction, Command};
use std::ffi::OsStr;
use std::ffi::OsString;
use std::io::{Error, ErrorKind};
use std::os::unix::process::CommandExt;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -158,10 +158,6 @@ 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 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)?;

// We are resolving the path in case it is a symlink or /. or /../
Expand All @@ -188,35 +184,27 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
return Err(ChrootError::NoSuchDirectory(options.newroot).into());
}

let commands: Vec<&OsStr> = matches
.get_many::<String>(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 = if commands.is_empty() {
vec![
user_shell.as_deref().unwrap_or(default_shell),
default_option,
]
} else {
commands
let mut cmd_iter = matches
.get_many::<OsString>(options::COMMAND)
.into_iter()
.flatten();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you could add map(OsString::from) here so you have OsStrings in the Some(c) case of the match. An alternative might be to use OsString with clap.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used clap. Thankyou.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GnuTest failed.

let (chroot_command, args) = match cmd_iter.next() {
Some(c) => (c.clone(), cmd_iter.cloned().collect::<Vec<OsString>>()),
None => (
std::env::var_os("SHELL").unwrap_or_else(|| "/bin/sh".into()),
vec!["-i".into()],
),
Comment thread
oech3 marked this conversation as resolved.
};

assert!(!command.is_empty());
let chroot_command = command[0];

// NOTE: Tests can only trigger code beyond this point if they're invoked with root permissions
set_context(&options)?;

let err = process::Command::new(chroot_command)
.args(&command[1..])
.exec();
let err = process::Command::new(&chroot_command).args(&args).exec();

Err(if err.kind() == ErrorKind::NotFound {
ChrootError::CommandNotFound(chroot_command.to_owned(), err)
ChrootError::CommandNotFound(chroot_command, err)
} else {
ChrootError::CommandFailed(chroot_command.to_owned(), err)
ChrootError::CommandFailed(chroot_command, err)
}
.into())
}
Expand Down
Loading