From 418aaaffde0f6da32649800a37d3a1c7d8c4c676 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Mon, 20 Jan 2025 00:04:53 +0100 Subject: [PATCH] chroot: move to thiserror --- src/uu/chroot/Cargo.toml | 1 + src/uu/chroot/src/error.rs | 67 ++++++++++++++------------------------ 2 files changed, 26 insertions(+), 42 deletions(-) diff --git a/src/uu/chroot/Cargo.toml b/src/uu/chroot/Cargo.toml index 9a9a8290f..65fc9c8f3 100644 --- a/src/uu/chroot/Cargo.toml +++ b/src/uu/chroot/Cargo.toml @@ -18,6 +18,7 @@ path = "src/chroot.rs" [dependencies] clap = { workspace = true } +thiserror = { workspace = true } uucore = { workspace = true, features = ["entries", "fs"] } [[bin]] diff --git a/src/uu/chroot/src/error.rs b/src/uu/chroot/src/error.rs index b8109d419..e88f70760 100644 --- a/src/uu/chroot/src/error.rs +++ b/src/uu/chroot/src/error.rs @@ -4,59 +4,75 @@ // file that was distributed with this source code. // spell-checker:ignore NEWROOT Userspec userspec //! Errors returned by chroot. -use std::fmt::Display; use std::io::Error; +use thiserror::Error; use uucore::display::Quotable; use uucore::error::UError; use uucore::libc; /// Errors that can happen while executing chroot. -#[derive(Debug)] +#[derive(Debug, Error)] pub enum ChrootError { /// 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. - 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. - 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, + #[error("invalid group: {group}", group = .0.quote())] InvalidGroup(String), + #[error("invalid group list: {list}", list = .0.quote())] InvalidGroupList(String), /// The given user and group specification was invalid. + #[error("invalid userspec: {spec}", spec = .0.quote())] InvalidUserspec(String), /// The new root directory was not given. + #[error( + "Missing operand: NEWROOT\nTry '{0} --help' for more information.", + uucore::execution_phrase() + )] MissingNewRoot, + #[error("no group specified for unknown uid: {0}")] NoGroupSpecified(libc::uid_t), /// Failed to find the specified user. + #[error("invalid user")] NoSuchUser, /// Failed to find the specified group. + #[error("invalid group")] NoSuchGroup, /// The given directory does not exist. + #[error("cannot change root directory to {dir}: no such directory", dir = .0.quote())] NoSuchDirectory(String), /// 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. + #[error("cannot set groups: {0}")] SetGroupsFailed(Error), /// 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 { // 125 if chroot itself fails // 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) - } - } - } -}