Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/uu/install/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ clap = { workspace = true }
filetime = { workspace = true }
file_diff = { workspace = true }
libc = { workspace = true }
thiserror = { workspace = true }
uucore = { workspace = true, features = [
"backup-control",
"buf-copy",
Expand Down
97 changes: 40 additions & 57 deletions src/uu/install/src/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ mod mode;
use clap::{Arg, ArgAction, ArgMatches, Command};
use file_diff::diff;
use filetime::{set_file_times, FileTime};
use std::error::Error;
use std::fmt::{Debug, Display};
use std::fmt::Debug;
use std::fs::File;
use std::fs::{self, metadata};
use std::path::{Path, PathBuf, MAIN_SEPARATOR};
use std::process;
use thiserror::Error;
use uucore::backup_control::{self, BackupMode};
use uucore::buf_copy::copy_stream;
use uucore::display::Quotable;
use uucore::entries::{grp2gid, usr2uid};
use uucore::error::{FromIo, UError, UIoError, UResult, UUsageError};
use uucore::error::{FromIo, UError, UResult, UUsageError};
use uucore::fs::dir_strip_dot_for_creation;
use uucore::mode::get_umask;
use uucore::perms::{wrap_chown, Verbosity, VerbosityLevel};
use uucore::process::{getegid, geteuid};
use uucore::{format_usage, help_about, help_usage, show, show_error, show_if_err, uio_error};
use uucore::{format_usage, help_about, help_usage, show, show_error, show_if_err};

#[cfg(unix)]
use std::os::unix::fs::{FileTypeExt, MetadataExt};
Expand All @@ -52,22 +52,51 @@ pub struct Behavior {
target_dir: Option<String>,
}

#[derive(Debug)]
#[derive(Error, Debug)]
enum InstallError {
#[error("Unimplemented feature: {0}")]
Unimplemented(String),
DirNeedsArg(),
CreateDirFailed(PathBuf, std::io::Error),

#[error("{} with -d requires at least one argument.", uucore::util_name())]
DirNeedsArg,

#[error("failed to create {0}")]
CreateDirFailed(PathBuf, #[source] std::io::Error),

#[error("failed to chmod {}", .0.quote())]
ChmodFailed(PathBuf),

#[error("failed to chown {}: {}", .0.quote(), .1)]
ChownFailed(PathBuf, String),

#[error("invalid target {}: No such file or directory", .0.quote())]
InvalidTarget(PathBuf),

#[error("target {} is not a directory", .0.quote())]
TargetDirIsntDir(PathBuf),
BackupFailed(PathBuf, PathBuf, std::io::Error),
InstallFailed(PathBuf, PathBuf, std::io::Error),

#[error("cannot backup {0} to {1}")]
BackupFailed(PathBuf, PathBuf, #[source] std::io::Error),

#[error("cannot install {0} to {1}")]
InstallFailed(PathBuf, PathBuf, #[source] std::io::Error),

#[error("strip program failed: {0}")]
StripProgramFailed(String),
MetadataFailed(std::io::Error),

#[error("metadata error")]
MetadataFailed(#[source] std::io::Error),

#[error("invalid user: {}", .0.quote())]
InvalidUser(String),

#[error("invalid group: {}", .0.quote())]
InvalidGroup(String),

#[error("omitting directory {}", .0.quote())]
OmittingDirectory(PathBuf),

#[error("failed to access {}: Not a directory", .0.quote())]
NotADirectory(PathBuf),
}

Expand All @@ -84,52 +113,6 @@ impl UError for InstallError {
}
}

impl Error for InstallError {}

impl Display for InstallError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Unimplemented(opt) => write!(f, "Unimplemented feature: {opt}"),
Self::DirNeedsArg() => {
write!(
f,
"{} with -d requires at least one argument.",
uucore::util_name()
)
}
Self::CreateDirFailed(dir, e) => {
Display::fmt(&uio_error!(e, "failed to create {}", dir.quote()), f)
}
Self::ChmodFailed(file) => write!(f, "failed to chmod {}", file.quote()),
Self::ChownFailed(file, msg) => write!(f, "failed to chown {}: {}", file.quote(), msg),
Self::InvalidTarget(target) => write!(
f,
"invalid target {}: No such file or directory",
target.quote()
),
Self::TargetDirIsntDir(target) => {
write!(f, "target {} is not a directory", target.quote())
}
Self::BackupFailed(from, to, e) => Display::fmt(
&uio_error!(e, "cannot backup {} to {}", from.quote(), to.quote()),
f,
),
Self::InstallFailed(from, to, e) => Display::fmt(
&uio_error!(e, "cannot install {} to {}", from.quote(), to.quote()),
f,
),
Self::StripProgramFailed(msg) => write!(f, "strip program failed: {msg}"),
Self::MetadataFailed(e) => Display::fmt(&uio_error!(e, ""), f),
Self::InvalidUser(user) => write!(f, "invalid user: {}", user.quote()),
Self::InvalidGroup(group) => write!(f, "invalid group: {}", group.quote()),
Self::OmittingDirectory(dir) => write!(f, "omitting directory {}", dir.quote()),
Self::NotADirectory(dir) => {
write!(f, "failed to access {}: Not a directory", dir.quote())
}
}
}
}

#[derive(Clone, Eq, PartialEq)]
pub enum MainFunction {
/// Create directories
Expand Down Expand Up @@ -456,7 +439,7 @@ fn behavior(matches: &ArgMatches) -> UResult<Behavior> {
///
fn directory(paths: &[String], b: &Behavior) -> UResult<()> {
if paths.is_empty() {
Err(InstallError::DirNeedsArg().into())
Err(InstallError::DirNeedsArg.into())
} else {
for path in paths.iter().map(Path::new) {
// if the path already exist, don't try to create it again
Expand Down
1 change: 1 addition & 0 deletions src/uu/join/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ path = "src/join.rs"
clap = { workspace = true }
uucore = { workspace = true }
memchr = { workspace = true }
thiserror = { workspace = true }

[[bin]]
name = "join"
Expand Down
28 changes: 7 additions & 21 deletions src/uu/join/src/join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ use clap::builder::ValueParser;
use clap::{Arg, ArgAction, Command};
use memchr::{memchr_iter, memmem::Finder, Memchr3};
use std::cmp::Ordering;
use std::error::Error;
use std::ffi::OsString;
use std::fmt::Display;
use std::fs::File;
use std::io::{stdin, stdout, BufRead, BufReader, BufWriter, Split, Stdin, Write};
use std::num::IntErrorKind;
#[cfg(unix)]
use std::os::unix::ffi::OsStrExt;
use thiserror::Error;
use uucore::display::Quotable;
use uucore::error::{set_exit_code, FromIo, UError, UResult, USimpleError};
use uucore::line_ending::LineEnding;
Expand All @@ -25,35 +24,22 @@ use uucore::{format_usage, help_about, help_usage};
const ABOUT: &str = help_about!("join.md");
const USAGE: &str = help_usage!("join.md");

#[derive(Debug)]
#[derive(Debug, Error)]
enum JoinError {
IOError(std::io::Error),
#[error("io error: {0}")]
IOError(#[from] std::io::Error),

#[error("{0}")]
UnorderedInput(String),
}

// If you still need the UError implementation for compatibility:
impl UError for JoinError {
fn code(&self) -> i32 {
1
}
}

impl Error for JoinError {}

impl Display for JoinError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::IOError(e) => write!(f, "io error: {e}"),
Self::UnorderedInput(e) => f.write_str(e),
}
}
}

impl From<std::io::Error> for JoinError {
fn from(error: std::io::Error) -> Self {
Self::IOError(error)
}
}

#[derive(Copy, Clone, PartialEq)]
enum FileNum {
File1,
Expand Down
1 change: 1 addition & 0 deletions src/uu/ln/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ path = "src/ln.rs"
[dependencies]
clap = { workspace = true }
uucore = { workspace = true, features = ["backup-control", "fs"] }
thiserror = { workspace = true }

[[bin]]
name = "ln"
Expand Down
44 changes: 17 additions & 27 deletions src/uu/ln/src/ln.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ use uucore::{format_usage, help_about, help_section, help_usage, prompt_yes, sho

use std::borrow::Cow;
use std::collections::HashSet;
use std::error::Error;
use std::ffi::OsString;
use std::fmt::Display;
use std::fs;
use thiserror::Error;

#[cfg(any(unix, target_os = "redox"))]
use std::os::unix::fs::symlink;
Expand Down Expand Up @@ -46,38 +45,25 @@ pub enum OverwriteMode {
Force,
}

#[derive(Debug)]
#[derive(Error, Debug)]
enum LnError {
#[error("target {} is not a directory", _0.quote())]
TargetIsDirectory(PathBuf),

#[error("")]
SomeLinksFailed,

#[error("{} and {} are the same file", _0.quote(), _1.quote())]
SameFile(PathBuf, PathBuf),

#[error("missing destination file operand after {}", _0.quote())]
MissingDestination(PathBuf),
ExtraOperand(OsString),
}

impl Display for LnError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::TargetIsDirectory(s) => write!(f, "target {} is not a directory", s.quote()),
Self::SameFile(s, d) => {
write!(f, "{} and {} are the same file", s.quote(), d.quote())
}
Self::SomeLinksFailed => Ok(()),
Self::MissingDestination(s) => {
write!(f, "missing destination file operand after {}", s.quote())
}
Self::ExtraOperand(s) => write!(
f,
"extra operand {}\nTry '{} --help' for more information.",
s.quote(),
uucore::execution_phrase()
),
}
}
#[error("extra operand {}\nTry '{} --help' for more information.",
format!("{:?}", _0).trim_matches('"'), _1)]
ExtraOperand(OsString, String),
}

impl Error for LnError {}

impl UError for LnError {
fn code(&self) -> i32 {
1
Expand Down Expand Up @@ -284,7 +270,11 @@ fn exec(files: &[PathBuf], settings: &Settings) -> UResult<()> {
return Err(LnError::MissingDestination(files[0].clone()).into());
}
if files.len() > 2 {
return Err(LnError::ExtraOperand(files[2].clone().into()).into());
return Err(LnError::ExtraOperand(
files[2].clone().into(),
uucore::execution_phrase().to_string(),
)
.into());
}
assert!(!files.is_empty());

Expand Down
1 change: 1 addition & 0 deletions src/uu/ls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ lscolors = { workspace = true }
number_prefix = { workspace = true }
selinux = { workspace = true, optional = true }
terminal_size = { workspace = true }
thiserror = { workspace = true }
uucore = { workspace = true, features = [
"colors",
"custom-tz-fmt",
Expand Down
Loading
Loading