@@ -4,7 +4,7 @@ use anyhow::{Context, Result};
4
4
use filetime:: FileTime ;
5
5
use std:: env;
6
6
use std:: ffi:: { OsStr , OsString } ;
7
- use std:: fs:: { self , File , OpenOptions } ;
7
+ use std:: fs:: { self , File , Metadata , OpenOptions } ;
8
8
use std:: io;
9
9
use std:: io:: prelude:: * ;
10
10
use std:: iter;
@@ -136,6 +136,24 @@ pub fn resolve_executable(exec: &Path) -> Result<PathBuf> {
136
136
}
137
137
}
138
138
139
+ /// Returns metadata for a file (follows symlinks).
140
+ ///
141
+ /// Equivalent to [`std::fs::metadata`] with better error messages.
142
+ pub fn metadata < P : AsRef < Path > > ( path : P ) -> Result < Metadata > {
143
+ let path = path. as_ref ( ) ;
144
+ std:: fs:: metadata ( path)
145
+ . with_context ( || format ! ( "failed to load metadata for path `{}`" , path. display( ) ) )
146
+ }
147
+
148
+ /// Returns metadata for a file without following symlinks.
149
+ ///
150
+ /// Equivalent to [`std::fs::metadata`] with better error messages.
151
+ pub fn symlink_metadata < P : AsRef < Path > > ( path : P ) -> Result < Metadata > {
152
+ let path = path. as_ref ( ) ;
153
+ std:: fs:: symlink_metadata ( path)
154
+ . with_context ( || format ! ( "failed to load metadata for path `{}`" , path. display( ) ) )
155
+ }
156
+
139
157
/// Reads a file to a string.
140
158
///
141
159
/// Equivalent to [`std::fs::read_to_string`] with better error messages.
@@ -216,16 +234,14 @@ pub fn open<P: AsRef<Path>>(path: P) -> Result<File> {
216
234
217
235
/// Returns the last modification time of a file.
218
236
pub fn mtime ( path : & Path ) -> Result < FileTime > {
219
- let meta =
220
- fs:: metadata ( path) . with_context ( || format ! ( "failed to stat `{}`" , path. display( ) ) ) ?;
237
+ let meta = metadata ( path) ?;
221
238
Ok ( FileTime :: from_last_modification_time ( & meta) )
222
239
}
223
240
224
241
/// Returns the maximum mtime of the given path, recursing into
225
242
/// subdirectories, and following symlinks.
226
243
pub fn mtime_recursive ( path : & Path ) -> Result < FileTime > {
227
- let meta =
228
- fs:: metadata ( path) . with_context ( || format ! ( "failed to stat `{}`" , path. display( ) ) ) ?;
244
+ let meta = metadata ( path) ?;
229
245
if !meta. is_dir ( ) {
230
246
return Ok ( FileTime :: from_last_modification_time ( & meta) ) ;
231
247
}
@@ -432,10 +448,7 @@ pub fn remove_dir_all<P: AsRef<Path>>(p: P) -> Result<()> {
432
448
}
433
449
434
450
fn _remove_dir_all ( p : & Path ) -> Result < ( ) > {
435
- if p. symlink_metadata ( )
436
- . with_context ( || format ! ( "could not get metadata for `{}` to remove" , p. display( ) ) ) ?
437
- . is_symlink ( )
438
- {
451
+ if symlink_metadata ( p) ?. is_symlink ( ) {
439
452
return remove_file ( p) ;
440
453
}
441
454
let entries = p
0 commit comments