Skip to content

Commit b7a7b08

Browse files
afnan-mojumdersylvestre
authored andcommitted
mv: report specific errors instead of generic permission denied on cross-device fallback
Previously, rename_file_fallback unconditionally mapped errors during cross-device fallback moves to translate!("mv-error-permission-denied"), causing misleading errors such as "Permission denied" when the disk is full (ENOSPC), or missing contextual path information (e.g. "cannot open ... for reading", "cannot create regular file ...", "cannot remove ..."). Additionally, when moving directories across devices, rename_dir_fallback previously attempted to open the destination directory for applying xattrs before checking if copy_dir_contents succeeded, and directory/entry errors were reported against the top-level move rather than the failing path. - Introduce RenameError to distinguish raw I/O errors from pre-formatted diagnostics, and preserve synthetic non-OS I/O messages as-is. - Report accurate GNU Coreutils-compatible messages for cross-device fallback failures (unreadable source, unwritable dest dir, ENOSPC, unremovable source or target, cannot create directory, cannot access subdirectory). - Unify regular file copying in copy_regular_file so fallback paths use fd-pinned safe copying while preserving xattrs and setuid/setgid rules. - Handle declined interactive prompts cleanly without printing empty errors. - Add regression tests covering cross-device file and directory permission failures. Fixes #15939 Closes #9458
1 parent 4735bbe commit b7a7b08

5 files changed

Lines changed: 611 additions & 205 deletions

File tree

‎src/uu/mv/locales/en-US.ftl‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ mv-error-dangling-symlink = can't determine symlink type, since it is dangling
3838
mv-error-no-symlink-support = your operating system does not support symlinks
3939
mv-error-permission-denied = Permission denied
4040
mv-error-inter-device-move-failed = inter-device move failed: {$from} to {$to}; unable to remove target: {$err}
41+
mv-error-cannot-open-for-reading = cannot open {$from} for reading: {$err}
42+
mv-error-cannot-create-regular-file = cannot create regular file {$to}: {$err}
43+
mv-error-cannot-create-directory = cannot create directory {$path}: {$err}
44+
mv-error-cannot-access = cannot access {$path}: {$err}
45+
mv-error-error-writing = error writing {$to}: {$err}
46+
mv-error-cannot-remove = cannot remove {$from}: {$err}
4147
mv-error-setting-attribute = setting attribute {$name}: {$err}
4248
mv-error-setting-attributes = setting attributes for {$path}: {$err}
4349
mv-error-exchange-two-operands = --exchange requires exactly two operands

‎src/uu/mv/locales/fr-FR.ftl‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ mv-error-dangling-symlink = impossible de déterminer le type de lien symbolique
3838
mv-error-no-symlink-support = votre système d'exploitation ne prend pas en charge les liens symboliques
3939
mv-error-permission-denied = Permission refusée
4040
mv-error-inter-device-move-failed = échec du déplacement inter-périphérique : {$from} vers {$to} ; impossible de supprimer la cible : {$err}
41+
mv-error-cannot-open-for-reading = impossible d'ouvrir {$from} en lecture : {$err}
42+
mv-error-cannot-create-regular-file = impossible de créer le fichier standard {$to} : {$err}
43+
mv-error-cannot-create-directory = impossible de créer le répertoire {$path} : {$err}
44+
mv-error-cannot-access = impossible d'accéder à {$path} : {$err}
45+
mv-error-error-writing = erreur d'écriture de {$to} : {$err}
46+
mv-error-cannot-remove = impossible de supprimer {$from} : {$err}
4147
mv-error-setting-attribute = définition de l'attribut {$name} : {$err}
4248
mv-error-setting-attributes = définition des attributs pour {$path} : {$err}
4349
mv-error-exchange-two-operands = --exchange nécessite exactement deux opérandes

‎src/uu/mv/src/error.rs‎

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
// For the full copyright and license information, please view the LICENSE
44
// file that was distributed with this source code.
55

6-
use uucore::error::UError;
6+
use std::io;
7+
use std::path::Path;
8+
use uucore::display::Quotable;
9+
use uucore::error::{FromIo, UError, USimpleError};
710
use uucore::translate;
811

912
#[derive(Debug, thiserror::Error)]
@@ -29,3 +32,84 @@ pub enum MvError {
2932
}
3033

3134
impl UError for MvError {}
35+
36+
#[derive(Debug)]
37+
pub enum RenameError {
38+
/// A raw I/O error from `fs::rename` (or general failure) that should be
39+
/// contextualized as "cannot move 'source' to 'target': {err}" or
40+
/// "cannot overwrite 'target': Directory not empty".
41+
Io(io::Error),
42+
/// A specific diagnostic that already includes the file path and operation,
43+
/// such as "cannot open 'src' for reading: Permission denied" or
44+
/// "error writing 'dst': No space left on device".
45+
Formatted(String),
46+
}
47+
48+
impl std::fmt::Display for RenameError {
49+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50+
match self {
51+
Self::Io(e) => write!(f, "{e}"),
52+
Self::Formatted(s) => write!(f, "{s}"),
53+
}
54+
}
55+
}
56+
57+
impl std::error::Error for RenameError {
58+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
59+
match self {
60+
Self::Io(e) => Some(e),
61+
Self::Formatted(_) => None,
62+
}
63+
}
64+
}
65+
66+
impl From<io::Error> for RenameError {
67+
fn from(err: io::Error) -> Self {
68+
Self::Io(err)
69+
}
70+
}
71+
72+
impl From<RenameError> for io::Error {
73+
fn from(err: RenameError) -> Self {
74+
match err {
75+
RenameError::Io(e) => e,
76+
RenameError::Formatted(s) => Self::other(s),
77+
}
78+
}
79+
}
80+
81+
impl RenameError {
82+
pub fn is_empty(&self) -> bool {
83+
match self {
84+
Self::Io(e) => e.to_string().is_empty(),
85+
Self::Formatted(s) => s.is_empty(),
86+
}
87+
}
88+
89+
pub fn into_uerror(self, source: &Path, target: &Path) -> Box<dyn UError> {
90+
match self {
91+
Self::Formatted(s) => USimpleError::new(1, s),
92+
Self::Io(e) if e.to_string().is_empty() => USimpleError::new(1, ""),
93+
Self::Io(e) => {
94+
if e.kind() == io::ErrorKind::DirectoryNotEmpty {
95+
e.map_err_context(|| {
96+
translate!(
97+
"mv-error-cannot-overwrite-non-empty-directory",
98+
"target" => target.quote()
99+
)
100+
})
101+
} else if e.raw_os_error().is_some() {
102+
e.map_err_context(|| {
103+
translate!(
104+
"mv-error-cannot-move",
105+
"source" => source.quote(),
106+
"target" => target.quote()
107+
)
108+
})
109+
} else {
110+
USimpleError::new(1, format!("{e}"))
111+
}
112+
}
113+
}
114+
}
115+
}

0 commit comments

Comments
 (0)