1
- use nix:: sys:: stat:: stat;
1
+ use nix:: sys:: stat:: { stat, fstat } ;
2
2
3
- #[ test]
4
- fn test_stat ( ) {
5
- use nix:: fcntl:: open;
6
- use nix:: unistd:: { close, unlink} ;
7
- use nix:: fcntl:: O_CREAT ;
8
- use nix:: sys:: stat:: S_IWUSR ;
9
-
10
- let filename = b"target/foo.txt" ;
11
- let fd_res = open ( filename, O_CREAT , S_IWUSR ) . unwrap ( ) ;
12
- close ( fd_res) . unwrap ( ) ; //.ok().unwrap()); // close right here. We use the file only for the stat test
13
- let stat_result = stat ( filename) ;
3
+ use nix:: fcntl:: open;
4
+ use nix:: unistd:: { close, unlink} ;
5
+ use nix:: fcntl:: O_CREAT ;
6
+ use nix:: sys:: stat:: { FileStat , S_IWUSR } ;
7
+ use nix:: NixResult ;
14
8
9
+ fn assert_stat_results ( stat_result : NixResult < FileStat > ) {
15
10
match stat_result {
16
11
Ok ( stats) => {
17
12
assert ! ( stats. st_dev > 0 ) ; // must be positive integer, exact number machine dependent
@@ -27,5 +22,20 @@ fn test_stat() {
27
22
}
28
23
Err ( _) => panic ! ( "stat call failed" ) // if stats system call fails, something is seriously wrong on that machine
29
24
}
25
+ }
26
+
27
+
28
+ #[ test]
29
+ fn test_stat_and_fstat ( ) {
30
+ let filename = b"target/foo.txt" ;
31
+ let fd = open ( filename, O_CREAT , S_IWUSR ) . unwrap ( ) ; // create empty file
32
+
33
+ let stat_result = stat ( filename) ;
34
+ assert_stat_results ( stat_result) ;
35
+
36
+ let fstat_result = fstat ( fd) ;
37
+ assert_stat_results ( fstat_result) ;
38
+
39
+ close ( fd) . unwrap ( ) ;
30
40
unlink ( filename) . unwrap ( ) ;
31
41
}
0 commit comments