@@ -7,8 +7,9 @@ use crate::os::unix::net::{UnixDatagram, UnixListener, UnixStream};
7
7
use cap_primitives:: fs:: set_permissions;
8
8
use cap_primitives:: fs:: {
9
9
canonicalize, copy, create_dir, hard_link, open, open_ambient_dir, open_dir, open_parent_dir,
10
- read_base_dir, read_dir, read_link, remove_dir, remove_dir_all, remove_file, remove_open_dir,
11
- remove_open_dir_all, rename, stat, DirOptions , FollowSymlinks , Permissions ,
10
+ read_base_dir, read_dir, read_link, read_link_contents, remove_dir, remove_dir_all,
11
+ remove_file, remove_open_dir, remove_open_dir_all, rename, stat, DirOptions , FollowSymlinks ,
12
+ Permissions ,
12
13
} ;
13
14
use cap_primitives:: AmbientAuthority ;
14
15
use io_lifetimes:: AsFilelike ;
@@ -21,7 +22,7 @@ use std::path::{Path, PathBuf};
21
22
use std:: { fmt, fs} ;
22
23
#[ cfg( not( windows) ) ]
23
24
use {
24
- cap_primitives:: fs:: symlink,
25
+ cap_primitives:: fs:: { symlink, symlink_contents } ,
25
26
io_extras:: os:: rustix:: { AsRawFd , FromRawFd , IntoRawFd , RawFd } ,
26
27
} ;
27
28
#[ cfg( windows) ]
@@ -284,12 +285,22 @@ impl Dir {
284
285
/// Reads a symbolic link, returning the file that the link points to.
285
286
///
286
287
/// This corresponds to [`std::fs::read_link`], but only accesses paths
287
- /// relative to `self`.
288
+ /// relative to `self`. Unlike [`read_link_contents`], this method considers it an error if
289
+ /// the link's target is an absolute path.
288
290
#[ inline]
289
291
pub fn read_link < P : AsRef < Path > > ( & self , path : P ) -> io:: Result < PathBuf > {
290
292
read_link ( & self . std_file , path. as_ref ( ) )
291
293
}
292
294
295
+ /// Reads a symbolic link, returning the file that the link points to.
296
+ ///
297
+ /// This corresponds to [`std::fs::read_link`]. but only accesses paths
298
+ /// relative to `self`.
299
+ #[ inline]
300
+ pub fn read_link_contents < P : AsRef < Path > > ( & self , path : P ) -> io:: Result < PathBuf > {
301
+ read_link_contents ( & self . std_file , path. as_ref ( ) )
302
+ }
303
+
293
304
/// Read the entire contents of a file into a string.
294
305
///
295
306
/// This corresponds to [`std::fs::read_to_string`], but only accesses
@@ -411,13 +422,38 @@ impl Dir {
411
422
/// This corresponds to [`std::os::unix::fs::symlink`], but only accesses
412
423
/// paths relative to `self`.
413
424
///
425
+ /// Unlike [`symlink_contents`] this method will return an error if `original` is an absolute
426
+ /// path.
427
+ ///
414
428
/// [`std::os::unix::fs::symlink`]: https://doc.rust-lang.org/std/os/unix/fs/fn.symlink.html
415
429
#[ cfg( not( windows) ) ]
416
430
#[ inline]
417
431
pub fn symlink < P : AsRef < Path > , Q : AsRef < Path > > ( & self , original : P , link : Q ) -> io:: Result < ( ) > {
418
432
symlink ( original. as_ref ( ) , & self . std_file , link. as_ref ( ) )
419
433
}
420
434
435
+ /// Creates a new symbolic link on a filesystem.
436
+ ///
437
+ /// The `original` argument provides the target of the symlink. The `link`
438
+ /// argument provides the name of the created symlink.
439
+ ///
440
+ /// Despite the argument ordering, `original` is not resolved relative to
441
+ /// `self` here. `link` is resolved relative to `self`, and `original` is
442
+ /// not resolved within this function.
443
+ ///
444
+ /// The `link` path is resolved when the symlink is dereferenced, relative
445
+ /// to the directory that contains it.
446
+ ///
447
+ /// This corresponds to [`std::os::unix::fs::symlink`], but only accesses
448
+ /// paths relative to `self`.
449
+ ///
450
+ /// [`std::os::unix::fs::symlink`]: https://doc.rust-lang.org/std/os/unix/fs/fn.symlink.html
451
+ #[ cfg( not( windows) ) ]
452
+ #[ inline]
453
+ pub fn symlink_contents < P : AsRef < Path > , Q : AsRef < Path > > ( & self , original : P , link : Q ) -> io:: Result < ( ) > {
454
+ symlink_contents ( original. as_ref ( ) , & self . std_file , link. as_ref ( ) )
455
+ }
456
+
421
457
/// Creates a new file symbolic link on a filesystem.
422
458
///
423
459
/// The `original` argument provides the target of the symlink. The `link`
0 commit comments