1
- use nix:: fcntl:: { openat, open, O_PATH , O_RDONLY } ;
1
+ use nix:: fcntl:: { openat, open, O_PATH , O_RDONLY , readlink , readlinkat } ;
2
2
use nix:: sys:: stat:: Mode ;
3
3
use nix:: unistd:: { close, read} ;
4
+ use tempdir:: TempDir ;
4
5
use tempfile:: NamedTempFile ;
5
6
use std:: io:: prelude:: * ;
7
+ use std:: os:: unix:: fs;
6
8
7
9
#[ test]
8
10
fn test_openat ( ) {
@@ -26,6 +28,25 @@ fn test_openat() {
26
28
close ( dirfd) . unwrap ( ) ;
27
29
}
28
30
31
+ #[ test]
32
+ fn test_readlink ( ) {
33
+ let tempdir = TempDir :: new ( "nix-test_readdir" )
34
+ . unwrap_or_else ( |e| panic ! ( "tempdir failed: {}" , e) ) ;
35
+ let src = tempdir. path ( ) . join ( "a" ) ;
36
+ let dst = tempdir. path ( ) . join ( "b" ) ;
37
+ println ! ( "a: {:?}, b: {:?}" , & src, & dst) ;
38
+ fs:: symlink ( & src. as_path ( ) , & dst. as_path ( ) ) . unwrap ( ) ;
39
+ let dirfd = open ( tempdir. path ( ) ,
40
+ O_PATH ,
41
+ Mode :: empty ( ) ) . unwrap ( ) ;
42
+
43
+ let mut buf = vec ! [ 0 ; src. to_str( ) . unwrap( ) . len( ) ] ;
44
+ assert_eq ! ( readlink( & dst, & mut buf) . unwrap( ) . to_str( ) . unwrap( ) ,
45
+ src. to_str( ) . unwrap( ) ) ;
46
+ assert_eq ! ( readlinkat( dirfd, "b" , & mut buf) . unwrap( ) . to_str( ) . unwrap( ) ,
47
+ src. to_str( ) . unwrap( ) ) ;
48
+ }
49
+
29
50
#[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
30
51
mod linux_android {
31
52
use std:: io:: prelude:: * ;
0 commit comments