@@ -329,7 +329,20 @@ mod tests {
329
329
/// Resolve symlinks in all the directories leading up to a file, but
330
330
/// not the file itself; this allows sudo to specify a precise policy with
331
331
/// tools like busybox or pgrep (which is a symlink to pgrep on systems)
332
+ /// This function will check for existence.
332
333
pub fn canonicalize < P : AsRef < Path > > ( path : P ) -> io:: Result < PathBuf > {
334
+ let reconstructed_path = canonicalize_newfile ( path) ?;
335
+
336
+ // access the object to generate the regular error if it does not exist
337
+ let _ = fs:: metadata ( & reconstructed_path) ?;
338
+
339
+ Ok ( reconstructed_path)
340
+ }
341
+
342
+ /// Resolve symlinks in all the directories leading up to a file, but
343
+ /// not the file itself; this allows us to keep symlinks as is, and will
344
+ /// also work on non-existing files.
345
+ pub fn canonicalize_newfile < P : AsRef < Path > > ( path : P ) -> io:: Result < PathBuf > {
333
346
let path = path. as_ref ( ) ;
334
347
let Some ( parent) = path. parent ( ) else {
335
348
// path is "/" or a prefix
@@ -344,9 +357,6 @@ pub fn canonicalize<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
344
357
canon_path
345
358
} ;
346
359
347
- // access the object to generate the regular error if it does not exist
348
- let _ = fs:: metadata ( & reconstructed_path) ?;
349
-
350
360
Ok ( reconstructed_path)
351
361
}
352
362
@@ -358,7 +368,7 @@ mod test {
358
368
#[ test]
359
369
fn canonicalization ( ) {
360
370
assert_eq ! ( canonicalize( "/" ) . unwrap( ) , Path :: new( "/" ) ) ;
361
- assert_eq ! ( canonicalize( "" ) . unwrap ( ) , Path :: new ( "" ) ) ;
371
+ assert ! ( canonicalize( "" ) . is_err ( ) ) ;
362
372
if cfg ! ( any( target_os = "linux" , target_os = "macos" ) ) {
363
373
// this test REQUIRES /usr/bin/unxz to be a symlink for /usr/bin/xz
364
374
assert_eq ! (
0 commit comments