mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-09-16 03:36:18 +00:00
chroot: move to thiserror
This commit is contained in:
parent
6866eedc83
commit
418aaaffde
2 changed files with 26 additions and 42 deletions
|
@ -18,6 +18,7 @@ path = "src/chroot.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clap = { workspace = true }
|
clap = { workspace = true }
|
||||||
|
thiserror = { workspace = true }
|
||||||
uucore = { workspace = true, features = ["entries", "fs"] }
|
uucore = { workspace = true, features = ["entries", "fs"] }
|
||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
|
|
|
@ -4,59 +4,75 @@
|
||||||
// file that was distributed with this source code.
|
// file that was distributed with this source code.
|
||||||
// spell-checker:ignore NEWROOT Userspec userspec
|
// spell-checker:ignore NEWROOT Userspec userspec
|
||||||
//! Errors returned by chroot.
|
//! Errors returned by chroot.
|
||||||
use std::fmt::Display;
|
|
||||||
use std::io::Error;
|
use std::io::Error;
|
||||||
|
use thiserror::Error;
|
||||||
use uucore::display::Quotable;
|
use uucore::display::Quotable;
|
||||||
use uucore::error::UError;
|
use uucore::error::UError;
|
||||||
use uucore::libc;
|
use uucore::libc;
|
||||||
|
|
||||||
/// Errors that can happen while executing chroot.
|
/// Errors that can happen while executing chroot.
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Error)]
|
||||||
pub enum ChrootError {
|
pub enum ChrootError {
|
||||||
/// Failed to enter the specified directory.
|
/// Failed to enter the specified directory.
|
||||||
CannotEnter(String, Error),
|
#[error("cannot chroot to {dir}: {err}", dir = .0.quote(), err = .1)]
|
||||||
|
CannotEnter(String, #[source] Error),
|
||||||
|
|
||||||
/// Failed to execute the specified command.
|
/// Failed to execute the specified command.
|
||||||
CommandFailed(String, Error),
|
#[error("failed to run command {cmd}: {err}", cmd = .0.to_string().quote(), err = .1)]
|
||||||
|
CommandFailed(String, #[source] Error),
|
||||||
|
|
||||||
/// Failed to find the specified command.
|
/// Failed to find the specified command.
|
||||||
CommandNotFound(String, Error),
|
#[error("failed to run command {cmd}: {err}", cmd = .0.to_string().quote(), err = .1)]
|
||||||
|
CommandNotFound(String, #[source] Error),
|
||||||
|
|
||||||
|
#[error("--groups parsing failed")]
|
||||||
GroupsParsingFailed,
|
GroupsParsingFailed,
|
||||||
|
|
||||||
|
#[error("invalid group: {group}", group = .0.quote())]
|
||||||
InvalidGroup(String),
|
InvalidGroup(String),
|
||||||
|
|
||||||
|
#[error("invalid group list: {list}", list = .0.quote())]
|
||||||
InvalidGroupList(String),
|
InvalidGroupList(String),
|
||||||
|
|
||||||
/// The given user and group specification was invalid.
|
/// The given user and group specification was invalid.
|
||||||
|
#[error("invalid userspec: {spec}", spec = .0.quote())]
|
||||||
InvalidUserspec(String),
|
InvalidUserspec(String),
|
||||||
|
|
||||||
/// The new root directory was not given.
|
/// The new root directory was not given.
|
||||||
|
#[error(
|
||||||
|
"Missing operand: NEWROOT\nTry '{0} --help' for more information.",
|
||||||
|
uucore::execution_phrase()
|
||||||
|
)]
|
||||||
MissingNewRoot,
|
MissingNewRoot,
|
||||||
|
|
||||||
|
#[error("no group specified for unknown uid: {0}")]
|
||||||
NoGroupSpecified(libc::uid_t),
|
NoGroupSpecified(libc::uid_t),
|
||||||
|
|
||||||
/// Failed to find the specified user.
|
/// Failed to find the specified user.
|
||||||
|
#[error("invalid user")]
|
||||||
NoSuchUser,
|
NoSuchUser,
|
||||||
|
|
||||||
/// Failed to find the specified group.
|
/// Failed to find the specified group.
|
||||||
|
#[error("invalid group")]
|
||||||
NoSuchGroup,
|
NoSuchGroup,
|
||||||
|
|
||||||
/// The given directory does not exist.
|
/// The given directory does not exist.
|
||||||
|
#[error("cannot change root directory to {dir}: no such directory", dir = .0.quote())]
|
||||||
NoSuchDirectory(String),
|
NoSuchDirectory(String),
|
||||||
|
|
||||||
/// The call to `setgid()` failed.
|
/// The call to `setgid()` failed.
|
||||||
SetGidFailed(String, Error),
|
#[error("cannot set gid to {gid}: {err}", gid = .0, err = .1)]
|
||||||
|
SetGidFailed(String, #[source] Error),
|
||||||
|
|
||||||
/// The call to `setgroups()` failed.
|
/// The call to `setgroups()` failed.
|
||||||
|
#[error("cannot set groups: {0}")]
|
||||||
SetGroupsFailed(Error),
|
SetGroupsFailed(Error),
|
||||||
|
|
||||||
/// The call to `setuid()` failed.
|
/// The call to `setuid()` failed.
|
||||||
SetUserFailed(String, Error),
|
#[error("cannot set user to {user}: {err}", user = .0.maybe_quote(), err = .1)]
|
||||||
|
SetUserFailed(String, #[source] Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::error::Error for ChrootError {}
|
|
||||||
|
|
||||||
impl UError for ChrootError {
|
impl UError for ChrootError {
|
||||||
// 125 if chroot itself fails
|
// 125 if chroot itself fails
|
||||||
// 126 if command is found but cannot be invoked
|
// 126 if command is found but cannot be invoked
|
||||||
|
@ -69,36 +85,3 @@ impl UError for ChrootError {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for ChrootError {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
|
||||||
match self {
|
|
||||||
Self::CannotEnter(s, e) => write!(f, "cannot chroot to {}: {}", s.quote(), e,),
|
|
||||||
Self::CommandFailed(s, e) | Self::CommandNotFound(s, e) => {
|
|
||||||
write!(f, "failed to run command {}: {}", s.to_string().quote(), e,)
|
|
||||||
}
|
|
||||||
Self::GroupsParsingFailed => write!(f, "--groups parsing failed"),
|
|
||||||
Self::InvalidGroup(s) => write!(f, "invalid group: {}", s.quote()),
|
|
||||||
Self::InvalidGroupList(s) => write!(f, "invalid group list: {}", s.quote()),
|
|
||||||
Self::InvalidUserspec(s) => write!(f, "invalid userspec: {}", s.quote(),),
|
|
||||||
Self::MissingNewRoot => write!(
|
|
||||||
f,
|
|
||||||
"Missing operand: NEWROOT\nTry '{} --help' for more information.",
|
|
||||||
uucore::execution_phrase(),
|
|
||||||
),
|
|
||||||
Self::NoGroupSpecified(uid) => write!(f, "no group specified for unknown uid: {}", uid),
|
|
||||||
Self::NoSuchUser => write!(f, "invalid user"),
|
|
||||||
Self::NoSuchGroup => write!(f, "invalid group"),
|
|
||||||
Self::NoSuchDirectory(s) => write!(
|
|
||||||
f,
|
|
||||||
"cannot change root directory to {}: no such directory",
|
|
||||||
s.quote(),
|
|
||||||
),
|
|
||||||
Self::SetGidFailed(s, e) => write!(f, "cannot set gid to {s}: {e}"),
|
|
||||||
Self::SetGroupsFailed(e) => write!(f, "cannot set groups: {e}"),
|
|
||||||
Self::SetUserFailed(s, e) => {
|
|
||||||
write!(f, "cannot set user to {}: {}", s.maybe_quote(), e)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue