@@ -45,18 +45,15 @@ pub type HardlinkResult<T> = Result<T, HardlinkError>;
45
45
#[ derive( Debug ) ]
46
46
pub enum HardlinkError {
47
47
Io ( io:: Error ) ,
48
- Scan ( String ) ,
49
48
Preservation { source : PathBuf , target : PathBuf } ,
50
49
Metadata { path : PathBuf , error : io:: Error } ,
50
+ ScanReadDir { path : PathBuf , error : io:: Error } ,
51
51
}
52
52
53
53
impl std:: fmt:: Display for HardlinkError {
54
54
fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
55
55
match self {
56
56
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
- }
60
57
HardlinkError :: Preservation { source, target } => {
61
58
write ! (
62
59
f,
@@ -68,6 +65,9 @@ impl std::fmt::Display for HardlinkError {
68
65
HardlinkError :: Metadata { path, error } => {
69
66
write ! ( f, "Metadata access error for {}: {}" , path. display( ) , error)
70
67
}
68
+ HardlinkError :: ScanReadDir { path, error } => {
69
+ write ! ( f, "Failed to read directory during scan {}: {}" , path. display( ) , error)
70
+ }
71
71
}
72
72
}
73
73
}
@@ -92,18 +92,21 @@ impl From<HardlinkError> for io::Error {
92
92
fn from ( error : HardlinkError ) -> Self {
93
93
match error {
94
94
HardlinkError :: Io ( e) => e,
95
- HardlinkError :: Scan ( msg) => io:: Error :: other ( msg) ,
96
95
HardlinkError :: Preservation { source, target } => io:: Error :: other ( format ! (
97
96
"Failed to preserve hardlink: {} -> {}" ,
98
97
source. display( ) ,
99
98
target. display( )
100
99
) ) ,
101
-
102
100
HardlinkError :: Metadata { path, error } => io:: Error :: other ( format ! (
103
101
"Metadata access error for {}: {}" ,
104
102
path. display( ) ,
105
103
error
106
104
) ) ,
105
+ HardlinkError :: ScanReadDir { path, error } => io:: Error :: other ( format ! (
106
+ "Failed to read directory during scan {}: {}" ,
107
+ path. display( ) ,
108
+ error
109
+ ) ) ,
107
110
}
108
111
}
109
112
}
@@ -234,18 +237,18 @@ impl HardlinkGroupScanner {
234
237
}
235
238
236
239
/// 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 > {
238
241
use std:: os:: unix:: fs:: MetadataExt ;
239
242
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 } ) ?;
241
244
for entry in entries {
242
- let entry = entry?;
245
+ let entry = entry. map_err ( |e| HardlinkError :: Io ( e ) ) ?;
243
246
let path = entry. path ( ) ;
244
247
245
248
if path. is_dir ( ) {
246
249
self . scan_directory_recursive ( & path) ?;
247
250
} else {
248
- let metadata = path. metadata ( ) ?;
251
+ let metadata = path. metadata ( ) . map_err ( |e| HardlinkError :: Metadata { path : path . clone ( ) , error : e } ) ?;
249
252
if metadata. nlink ( ) > 1 {
250
253
let key = ( metadata. dev ( ) , metadata. ino ( ) ) ;
251
254
self . hardlink_groups . entry ( key) . or_default ( ) . push ( path) ;
0 commit comments