@@ -72,14 +72,12 @@ pub fn create_file(path: &Path) -> Result<File> {
72
72
73
73
/// Removes all the content of a directory but not the directory itself
74
74
pub fn remove_dir_content ( dir : & Path ) -> Result < ( ) > {
75
- for item in fs:: read_dir ( dir) ? {
76
- if let Ok ( item) = item {
77
- let item = item. path ( ) ;
78
- if item. is_dir ( ) {
79
- fs:: remove_dir_all ( item) ?;
80
- } else {
81
- fs:: remove_file ( item) ?;
82
- }
75
+ for item in fs:: read_dir ( dir) ?. flatten ( ) {
76
+ let item = item. path ( ) ;
77
+ if item. is_dir ( ) {
78
+ fs:: remove_dir_all ( item) ?;
79
+ } else {
80
+ fs:: remove_file ( item) ?;
83
81
}
84
82
}
85
83
Ok ( ( ) )
@@ -108,72 +106,41 @@ pub fn copy_files_except_ext(
108
106
}
109
107
110
108
for entry in fs:: read_dir ( from) ? {
111
- let entry = entry?;
109
+ let entry = entry?. path ( ) ;
112
110
let metadata = entry
113
- . path ( )
114
111
. metadata ( )
115
- . with_context ( || format ! ( "Failed to read {:?}" , entry. path( ) ) ) ?;
112
+ . with_context ( || format ! ( "Failed to read {entry:?}" ) ) ?;
113
+
114
+ let entry_file_name = entry. file_name ( ) . unwrap ( ) ;
115
+ let target_file_path = to. join ( entry_file_name) ;
116
116
117
117
// If the entry is a dir and the recursive option is enabled, call itself
118
118
if metadata. is_dir ( ) && recursive {
119
- if entry. path ( ) == to. to_path_buf ( ) {
119
+ if entry == to. as_os_str ( ) {
120
120
continue ;
121
121
}
122
122
123
123
if let Some ( avoid) = avoid_dir {
124
- if entry. path ( ) == * avoid {
124
+ if entry == * avoid {
125
125
continue ;
126
126
}
127
127
}
128
128
129
129
// check if output dir already exists
130
- if !to . join ( entry . file_name ( ) ) . exists ( ) {
131
- fs:: create_dir ( & to . join ( entry . file_name ( ) ) ) ?;
130
+ if !target_file_path . exists ( ) {
131
+ fs:: create_dir ( & target_file_path ) ?;
132
132
}
133
133
134
- copy_files_except_ext (
135
- & from. join ( entry. file_name ( ) ) ,
136
- & to. join ( entry. file_name ( ) ) ,
137
- true ,
138
- avoid_dir,
139
- ext_blacklist,
140
- ) ?;
134
+ copy_files_except_ext ( & entry, & target_file_path, true , avoid_dir, ext_blacklist) ?;
141
135
} else if metadata. is_file ( ) {
142
136
// Check if it is in the blacklist
143
- if let Some ( ext) = entry. path ( ) . extension ( ) {
137
+ if let Some ( ext) = entry. extension ( ) {
144
138
if ext_blacklist. contains ( & ext. to_str ( ) . unwrap ( ) ) {
145
139
continue ;
146
140
}
147
141
}
148
- debug ! (
149
- "creating path for file: {:?}" ,
150
- & to. join(
151
- entry
152
- . path( )
153
- . file_name( )
154
- . expect( "a file should have a file name..." )
155
- )
156
- ) ;
157
-
158
- debug ! (
159
- "Copying {:?} to {:?}" ,
160
- entry. path( ) ,
161
- & to. join(
162
- entry
163
- . path( )
164
- . file_name( )
165
- . expect( "a file should have a file name..." )
166
- )
167
- ) ;
168
- copy (
169
- entry. path ( ) ,
170
- & to. join (
171
- entry
172
- . path ( )
173
- . file_name ( )
174
- . expect ( "a file should have a file name..." ) ,
175
- ) ,
176
- ) ?;
142
+ debug ! ( "Copying {entry:?} to {target_file_path:?}" ) ;
143
+ copy ( & entry, & target_file_path) ?;
177
144
}
178
145
}
179
146
Ok ( ( ) )
0 commit comments