Skip to content

Commit c32657c

Browse files
refactor(mv): Refactor HardlinkError for improved error handling
1 parent b5dda6b commit c32657c

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

src/uu/mv/src/hardlink.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,15 @@ pub type HardlinkResult<T> = Result<T, HardlinkError>;
4545
#[derive(Debug)]
4646
pub enum HardlinkError {
4747
Io(io::Error),
48-
Scan(String),
4948
Preservation { source: PathBuf, target: PathBuf },
5049
Metadata { path: PathBuf, error: io::Error },
50+
ScanReadDir { path: PathBuf, error: io::Error },
5151
}
5252

5353
impl std::fmt::Display for HardlinkError {
5454
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
5555
match self {
5656
HardlinkError::Io(e) => write!(f, "I/O error during hardlink operation: {e}"),
57-
HardlinkError::Scan(msg) => {
58-
write!(f, "Failed to scan files for hardlinks: {msg}")
59-
}
6057
HardlinkError::Preservation { source, target } => {
6158
write!(
6259
f,
@@ -68,6 +65,9 @@ impl std::fmt::Display for HardlinkError {
6865
HardlinkError::Metadata { path, error } => {
6966
write!(f, "Metadata access error for {}: {}", path.display(), error)
7067
}
68+
HardlinkError::ScanReadDir { path, error } => {
69+
write!(f, "Failed to read directory during scan {}: {}", path.display(), error)
70+
}
7171
}
7272
}
7373
}
@@ -92,18 +92,21 @@ impl From<HardlinkError> for io::Error {
9292
fn from(error: HardlinkError) -> Self {
9393
match error {
9494
HardlinkError::Io(e) => e,
95-
HardlinkError::Scan(msg) => io::Error::other(msg),
9695
HardlinkError::Preservation { source, target } => io::Error::other(format!(
9796
"Failed to preserve hardlink: {} -> {}",
9897
source.display(),
9998
target.display()
10099
)),
101-
102100
HardlinkError::Metadata { path, error } => io::Error::other(format!(
103101
"Metadata access error for {}: {}",
104102
path.display(),
105103
error
106104
)),
105+
HardlinkError::ScanReadDir { path, error } => io::Error::other(format!(
106+
"Failed to read directory during scan {}: {}",
107+
path.display(),
108+
error
109+
)),
107110
}
108111
}
109112
}
@@ -234,18 +237,18 @@ impl HardlinkGroupScanner {
234237
}
235238

236239
/// Recursively scan a directory for hardlinked files
237-
fn scan_directory_recursive(&mut self, dir: &Path) -> io::Result<()> {
240+
fn scan_directory_recursive(&mut self, dir: &Path) -> Result<(), HardlinkError> {
238241
use std::os::unix::fs::MetadataExt;
239242

240-
let entries = std::fs::read_dir(dir)?;
243+
let entries = std::fs::read_dir(dir).map_err(|e| HardlinkError::ScanReadDir { path: dir.to_path_buf(), error: e})?;
241244
for entry in entries {
242-
let entry = entry?;
245+
let entry = entry.map_err(|e| HardlinkError::Io(e))?;
243246
let path = entry.path();
244247

245248
if path.is_dir() {
246249
self.scan_directory_recursive(&path)?;
247250
} else {
248-
let metadata = path.metadata()?;
251+
let metadata = path.metadata().map_err(|e| HardlinkError::Metadata { path: path.clone(), error: e})?;
249252
if metadata.nlink() > 1 {
250253
let key = (metadata.dev(), metadata.ino());
251254
self.hardlink_groups.entry(key).or_default().push(path);

0 commit comments

Comments
 (0)