1
1
use std:: fs:: File ;
2
- use std:: os:: unix:: fs:: symlink;
2
+ use std:: os:: unix:: fs:: { symlink, PermissionsExt } ;
3
3
use std:: os:: unix:: prelude:: AsRawFd ;
4
+ use tempfile:: NamedTempFile ;
4
5
5
6
use libc:: { S_IFMT , S_IFLNK } ;
6
7
7
8
use nix:: fcntl;
8
- use nix:: sys:: stat:: { self , stat, fstat, lstat} ;
9
- use nix:: sys:: stat:: FileStat ;
9
+ use nix:: sys:: stat:: * ;
10
10
use nix:: Result ;
11
11
use tempdir:: TempDir ;
12
12
@@ -81,11 +81,11 @@ fn test_fstatat() {
81
81
File :: create ( & filename) . unwrap ( ) ;
82
82
let dirfd = fcntl:: open ( tempdir. path ( ) ,
83
83
fcntl:: OFlag :: empty ( ) ,
84
- stat :: Mode :: empty ( ) ) ;
84
+ Mode :: empty ( ) ) ;
85
85
86
- let result = stat :: fstatat ( dirfd. unwrap ( ) ,
87
- & filename,
88
- fcntl:: AtFlags :: empty ( ) ) ;
86
+ let result = fstatat ( dirfd. unwrap ( ) ,
87
+ & filename,
88
+ fcntl:: AtFlags :: empty ( ) ) ;
89
89
assert_stat_results ( result) ;
90
90
}
91
91
@@ -110,3 +110,30 @@ fn test_stat_fstat_lstat() {
110
110
let fstat_result = fstat ( link. as_raw_fd ( ) ) ;
111
111
assert_stat_results ( fstat_result) ;
112
112
}
113
+
114
+ fn assert_mode ( f : & NamedTempFile , mode : u32 ) {
115
+ assert_eq ! ( f. metadata( ) . unwrap( ) . permissions( ) . mode( ) ,
116
+ mode) ;
117
+ }
118
+
119
+ #[ test]
120
+ fn test_chmod ( ) {
121
+ let tempfile = NamedTempFile :: new ( ) . unwrap ( ) ;
122
+ chmod ( tempfile. path ( ) ,
123
+ Mode :: from_bits ( 0o755 ) . unwrap ( ) ) . unwrap ( ) ;
124
+ assert_mode ( & tempfile, 0o755 ) ;
125
+
126
+ fchmod ( tempfile. as_raw_fd ( ) ,
127
+ Mode :: from_bits ( 0o644 ) . unwrap ( ) ) . unwrap ( ) ;
128
+ assert_mode ( & tempfile, 0o644 ) ;
129
+
130
+ let parent_dir = tempfile. path ( ) . parent ( ) . unwrap ( ) ;
131
+ let dirfd = fcntl:: open ( parent_dir,
132
+ fcntl:: OFlag :: empty ( ) ,
133
+ Mode :: empty ( ) ) . unwrap ( ) ;
134
+ fchmodat ( dirfd,
135
+ tempfile. path ( ) . file_name ( ) . unwrap ( ) ,
136
+ Mode :: from_bits ( 0o600 ) . unwrap ( ) ,
137
+ fcntl:: AtFlags :: empty ( ) ) . unwrap ( ) ;
138
+ assert_mode ( & tempfile, 0o600 ) ;
139
+ }
0 commit comments