1
+ //! FFI for statvfs functions
2
+ //!
3
+ //! See the `vfs::Statvfs` struct for some rusty wrappers
4
+
1
5
use { Result , NixPath , from_ffi} ;
2
6
use errno:: Errno ;
3
7
use std:: os:: unix:: io:: AsRawFd ;
4
8
5
9
pub mod vfs {
10
+ //! Structs related to the `statvfs` and `fstatvfs` functions
11
+ //!
12
+ //! The `Statvfs` struct has some wrappers methods around the `statvfs` and
13
+ //! `fstatvfs` calls.
14
+
6
15
use libc:: { c_ulong, c_int} ;
16
+ use std:: os:: unix:: io:: AsRawFd ;
17
+ use { Result , NixPath } ;
18
+
19
+ use super :: { statvfs, fstatvfs} ;
7
20
8
21
bitflags ! (
22
+ /// Mount Flags
9
23
#[ repr( C ) ]
10
24
#[ derive( Default ) ]
11
25
flags FsFlags : c_ulong {
26
+ /// Read Only
12
27
const RDONLY = 1 ,
28
+ /// Do not allow the set-uid bits to have an effect
13
29
const NOSUID = 2 ,
30
+ /// Do not interpret character or block-special devices
14
31
const NODEV = 4 ,
32
+ /// Do not allow execution of binaries on the filesystem
15
33
const NOEXEC = 8 ,
34
+ /// All IO should be done synchronously
16
35
const SYNCHRONOUS = 16 ,
36
+ /// Allow mandatory locks on the filesystem
17
37
const MANDLOCK = 64 ,
18
38
const WRITE = 128 ,
19
39
const APPEND = 256 ,
20
40
const IMMUTABLE = 512 ,
41
+ /// Do not update access times on files
21
42
const NOATIME = 1024 ,
43
+ /// Do not update access times on files
22
44
const NODIRATIME = 2048 ,
45
+ /// Update access time relative to modify/change time
23
46
const RELATIME = 4096 ,
24
47
}
25
48
) ;
26
49
50
+ /// The posix statvfs struct
51
+ ///
52
+ /// http://linux.die.net/man/2/statvfs
27
53
#[ repr( C ) ]
28
54
#[ derive( Debug , Copy , Clone ) ]
29
55
pub struct Statvfs {
56
+ /// Filesystem block size. This is the value that will lead to
57
+ /// most efficient use of the filesystem
30
58
pub f_bsize : c_ulong ,
59
+ /// Fragment Size -- actual minimum unit of allocation on this
60
+ /// filesystem
31
61
pub f_frsize : c_ulong ,
62
+ /// Total number of blocks on the filesystem
32
63
pub f_blocks : u64 ,
64
+ /// Number of unused blocks on the filesystem, including those
65
+ /// reserved for root
33
66
pub f_bfree : u64 ,
67
+ /// Number of blocks available to non-root users
34
68
pub f_bavail : u64 ,
69
+ /// Total number of inodes available on the filesystem
35
70
pub f_files : u64 ,
71
+ /// Number of inodes available on the filesystem
36
72
pub f_ffree : u64 ,
73
+ /// Number of inodes available to non-root users
37
74
pub f_favail : u64 ,
75
+ /// File System ID
38
76
pub f_fsid : c_ulong ,
77
+ /// Mount Flags
39
78
pub f_flag : FsFlags ,
79
+ /// Maximum filename length
40
80
pub f_namemax : c_ulong ,
81
+ /// Reserved extra space, OS-dependent
41
82
f_spare : [ c_int ; 6 ] ,
42
83
}
84
+
85
+ impl Statvfs {
86
+ /// Create a new `Statvfs` object and fill it with information about
87
+ /// the mount that contains `path`
88
+ pub fn for_path < P : ?Sized + NixPath > ( path : & P ) -> Result < Statvfs > {
89
+ let mut stat = Statvfs :: default ( ) ;
90
+ let res = statvfs ( path, & mut stat) ;
91
+ res. map ( |_| stat)
92
+ }
93
+
94
+ /// Replace information in this struct with information about `path`
95
+ pub fn update_with_path < P : ?Sized + NixPath > ( & mut self , path : & P ) -> Result < ( ) > {
96
+ statvfs ( path, self )
97
+ }
98
+
99
+ /// Create a new `Statvfs` object and fill it with information from fd
100
+ pub fn for_fd < T : AsRawFd > ( fd : & T ) -> Result < Statvfs > {
101
+ let mut stat = Statvfs :: default ( ) ;
102
+ let res = fstatvfs ( fd, & mut stat) ;
103
+ res. map ( |_| stat)
104
+ }
105
+
106
+ /// Replace information in this struct with information about `fd`
107
+ pub fn update_with_fd < T : AsRawFd > ( & mut self , fd : & T ) -> Result < ( ) > {
108
+ fstatvfs ( fd, self )
109
+ }
110
+ }
111
+
112
+ impl Default for Statvfs {
113
+ /// Create a statvfs object initialized to all zeros
114
+ fn default ( ) -> Self {
115
+ Statvfs {
116
+ f_bsize : 0 ,
117
+ f_frsize : 0 ,
118
+ f_blocks : 0 ,
119
+ f_bfree : 0 ,
120
+ f_bavail : 0 ,
121
+ f_files : 0 ,
122
+ f_ffree : 0 ,
123
+ f_favail : 0 ,
124
+ f_fsid : 0 ,
125
+ f_flag : FsFlags :: default ( ) ,
126
+ f_namemax : 0 ,
127
+ f_spare : [ 0 , 0 , 0 , 0 , 0 , 0 ] ,
128
+ }
129
+ }
130
+ }
43
131
}
44
132
45
133
mod ffi {
@@ -52,6 +140,7 @@ mod ffi {
52
140
}
53
141
}
54
142
143
+ /// Fill an existing `Statvfs` object with information about the `path`
55
144
pub fn statvfs < P : ?Sized + NixPath > ( path : & P , stat : & mut vfs:: Statvfs ) -> Result < ( ) > {
56
145
unsafe {
57
146
Errno :: clear ( ) ;
@@ -62,9 +151,29 @@ pub fn statvfs<P: ?Sized + NixPath>(path: &P, stat: &mut vfs::Statvfs) -> Result
62
151
}
63
152
}
64
153
154
+ /// Fill an existing `Statvfs` object with information about `fd`
65
155
pub fn fstatvfs < T : AsRawFd > ( fd : & T , stat : & mut vfs:: Statvfs ) -> Result < ( ) > {
66
156
unsafe {
67
157
Errno :: clear ( ) ;
68
158
from_ffi ( ffi:: fstatvfs ( fd. as_raw_fd ( ) , stat) )
69
159
}
70
160
}
161
+
162
+ #[ cfg( test) ]
163
+ mod test {
164
+ use std:: fs:: File ;
165
+ use sys:: statvfs:: * ;
166
+
167
+ #[ test]
168
+ fn statvfs_call ( ) {
169
+ let mut stat = vfs:: Statvfs :: default ( ) ;
170
+ statvfs ( "/" . as_bytes ( ) , & mut stat) . unwrap ( )
171
+ }
172
+
173
+ #[ test]
174
+ fn fstatvfs_call ( ) {
175
+ let mut stat = vfs:: Statvfs :: default ( ) ;
176
+ let root = File :: open ( "/" ) . unwrap ( ) ;
177
+ fstatvfs ( & root, & mut stat) . unwrap ( )
178
+ }
179
+ }
0 commit comments