Skip to content
Open
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
6 changes: 6 additions & 0 deletions src/uu/mv/locales/en-US.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ mv-error-dangling-symlink = can't determine symlink type, since it is dangling
mv-error-no-symlink-support = your operating system does not support symlinks
mv-error-permission-denied = Permission denied
mv-error-inter-device-move-failed = inter-device move failed: {$from} to {$to}; unable to remove target: {$err}
mv-error-cannot-open-for-reading = cannot open {$from} for reading: {$err}
mv-error-cannot-create-regular-file = cannot create regular file {$to}: {$err}
mv-error-cannot-create-directory = cannot create directory {$path}: {$err}
mv-error-cannot-access = cannot access {$path}: {$err}
mv-error-error-writing = error writing {$to}: {$err}
mv-error-cannot-remove = cannot remove {$from}: {$err}
mv-error-setting-attribute = setting attribute {$name}: {$err}
mv-error-setting-attributes = setting attributes for {$path}: {$err}
mv-error-exchange-two-operands = --exchange requires exactly two operands
Expand Down
6 changes: 6 additions & 0 deletions src/uu/mv/locales/fr-FR.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ mv-error-dangling-symlink = impossible de déterminer le type de lien symbolique
mv-error-no-symlink-support = votre système d'exploitation ne prend pas en charge les liens symboliques
mv-error-permission-denied = Permission refusée
mv-error-inter-device-move-failed = échec du déplacement inter-périphérique : {$from} vers {$to} ; impossible de supprimer la cible : {$err}
mv-error-cannot-open-for-reading = impossible d'ouvrir {$from} en lecture : {$err}
mv-error-cannot-create-regular-file = impossible de créer le fichier standard {$to} : {$err}
mv-error-cannot-create-directory = impossible de créer le répertoire {$path} : {$err}
mv-error-cannot-access = impossible d'accéder à {$path} : {$err}
mv-error-error-writing = erreur d'écriture de {$to} : {$err}
mv-error-cannot-remove = impossible de supprimer {$from} : {$err}
mv-error-setting-attribute = définition de l'attribut {$name} : {$err}
mv-error-setting-attributes = définition des attributs pour {$path} : {$err}
mv-error-exchange-two-operands = --exchange nécessite exactement deux opérandes
Expand Down
190 changes: 189 additions & 1 deletion src/uu/mv/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

use uucore::error::UError;
use std::io;
use std::path::{Path, PathBuf};
use uucore::display::Quotable;
use uucore::error::{FromIo, UError, USimpleError, strip_errno};
use uucore::translate;

#[derive(Debug, thiserror::Error)]
Expand All @@ -29,3 +32,188 @@ pub enum MvError {
}

impl UError for MvError {}

pub type RenameResult<T = ()> = Result<T, Box<RenameError>>;

#[derive(Debug, thiserror::Error)]
pub enum RenameError {
/// A raw I/O error from `fs::rename` (or general failure) that should be
/// contextualized as "cannot move 'source' to 'target': {err}" or
/// "cannot overwrite 'target': Directory not empty".
#[error("{0}")]
Io(#[from] io::Error),
#[error("")]
PromptDeclined,
#[error(
"{}",
translate!(
"mv-error-inter-device-move-failed",
"from" => .from.quote(),
"to" => .to.quote(),
"err" => strip_errno(.err)
)
)]
InterDeviceMoveFailed {
from: PathBuf,
to: PathBuf,
err: io::Error,
},
#[error(
"{}",
translate!(
"mv-error-cannot-open-for-reading",
"from" => .path.quote(),
"err" => strip_errno(.err)
)
)]
CannotOpenForReading { path: PathBuf, err: io::Error },
#[error(
"{}",
translate!(
"mv-error-cannot-create-regular-file",
"to" => .path.quote(),
"err" => strip_errno(.err)
)
)]
CannotCreateRegularFile { path: PathBuf, err: io::Error },
#[error(
"{}",
translate!(
"mv-error-cannot-create-directory",
"path" => .path.quote(),
"err" => strip_errno(.err)
)
)]
CannotCreateDirectory { path: PathBuf, err: io::Error },
#[error(
"{}",
translate!(
"mv-error-cannot-access",
"path" => .path.quote(),
"err" => strip_errno(.err)
)
)]
CannotAccess { path: PathBuf, err: io::Error },
#[cfg(unix)]
#[error(
"{}",
translate!(
"mv-error-error-writing",
"to" => .path.quote(),
"err" => strip_errno(.err)
)
)]
ErrorWriting { path: PathBuf, err: io::Error },
#[error(
"{}",
translate!(
"mv-error-cannot-remove",
"from" => .path.quote(),
"err" => strip_errno(.err)
)
)]
CannotRemove { path: PathBuf, err: io::Error },
}

impl From<io::Error> for Box<RenameError> {
fn from(err: io::Error) -> Self {
Self::new(RenameError::Io(err))
}
}

impl RenameError {
pub fn inter_device_move_failed<'a>(
from: &'a Path,
to: &'a Path,
) -> impl FnOnce(io::Error) -> Box<Self> + 'a {
move |err| {
Box::new(Self::InterDeviceMoveFailed {
from: from.to_path_buf(),
to: to.to_path_buf(),
err,
})
}
}

pub fn cannot_open_for_reading(path: &Path) -> impl FnOnce(io::Error) -> Box<Self> + '_ {
move |err| {
Box::new(Self::CannotOpenForReading {
path: path.to_path_buf(),
err,
})
}
}

pub fn cannot_create_regular_file(path: &Path) -> impl FnOnce(io::Error) -> Box<Self> + '_ {
move |err| {
Box::new(Self::CannotCreateRegularFile {
path: path.to_path_buf(),
err,
})
}
}

pub fn cannot_create_directory(path: &Path) -> impl FnOnce(io::Error) -> Box<Self> + '_ {
move |err| {
Box::new(Self::CannotCreateDirectory {
path: path.to_path_buf(),
err,
})
}
}

pub fn cannot_access(path: &Path) -> impl FnOnce(io::Error) -> Box<Self> + '_ {
move |err| {
Box::new(Self::CannotAccess {
path: path.to_path_buf(),
err,
})
}
}

#[cfg(unix)]
pub fn error_writing(path: &Path) -> impl FnOnce(io::Error) -> Box<Self> + '_ {
move |err| {
Box::new(Self::ErrorWriting {
path: path.to_path_buf(),
err,
})
}
}

pub fn cannot_remove(path: &Path) -> impl FnOnce(io::Error) -> Box<Self> + '_ {
move |err| {
Box::new(Self::CannotRemove {
path: path.to_path_buf(),
err,
})
}
}

pub fn into_uerror(self, source: &Path, target: &Path) -> Box<dyn UError> {
match self {
Self::PromptDeclined => USimpleError::new(1, ""),
Self::Io(e) => {
if e.kind() == io::ErrorKind::DirectoryNotEmpty {
e.map_err_context(|| {
translate!(
"mv-error-cannot-overwrite-non-empty-directory",
"target" => target.quote()
)
})
} else if e.raw_os_error().is_some() {
e.map_err_context(|| {
translate!(
"mv-error-cannot-move",
"source" => source.quote(),
"target" => target.quote()
)
})
} else {
USimpleError::new(1, format!("{e}"))
}
}
other => USimpleError::new(1, other.to_string()),
}
}
}
Loading
Loading