10
10
//! The mtab file
11
11
//! https://www.gnu.org/software/libc/manual/html_node/mtab.html
12
12
13
- use libc:: { endmntent, getmntent, setmntent, FILE } ;
14
13
use std:: ffi:: { CStr , CString } ;
15
14
use std:: io;
16
15
use std:: sync:: Mutex ;
@@ -20,16 +19,18 @@ const _PATH_MOUNTED: &CStr = c"/etc/mtab";
20
19
/// The mtab (contraction of mounted file systems table) file
21
20
/// is a system information file, commonly found on Unix-like systems
22
21
pub struct MountTable {
23
- inner : * mut FILE ,
22
+ inner : * mut libc :: FILE ,
24
23
}
25
24
26
25
/// Structure describing a mount table entry
26
+ ///
27
+ /// Wrapper of [`libc::mntent`]
27
28
#[ derive( Debug , PartialEq ) ]
28
- pub struct MountTableEntity {
29
- /// Device or server for filesystem
30
- pub fsname : CString ,
29
+ pub struct MountEntity {
31
30
/// Directory mounted on
32
31
pub dir : CString ,
32
+ /// Device or server for filesystem
33
+ pub fsname : CString ,
33
34
/// Type of filesystem: ufs, nfs, etc
34
35
pub fstype : CString ,
35
36
/// Comma-separated options for fs
@@ -41,15 +42,16 @@ pub struct MountTableEntity {
41
42
}
42
43
43
44
impl MountTable {
44
- pub fn try_new ( ) -> Result < Self , io:: Error > {
45
+ /// Open system mtab file
46
+ pub fn open_system ( ) -> Result < Self , io:: Error > {
45
47
Self :: open ( _PATH_MOUNTED, c"r" )
46
48
}
47
49
48
50
/// Open mtab file
49
51
fn open ( filename : & CStr , mode : & CStr ) -> Result < Self , io:: Error > {
50
52
// Preliminary: | MT-Safe | AS-Unsafe heap lock | AC-Unsafe mem fd lock
51
53
// https://www.gnu.org/software/libc/manual/html_node/POSIX-Safety-Concepts.html
52
- let inner = unsafe { setmntent ( filename. as_ptr ( ) , mode. as_ptr ( ) ) } ;
54
+ let inner = unsafe { libc :: setmntent ( filename. as_ptr ( ) , mode. as_ptr ( ) ) } ;
53
55
if inner. is_null ( ) {
54
56
return Err ( io:: Error :: last_os_error ( ) ) ;
55
57
}
@@ -58,29 +60,29 @@ impl MountTable {
58
60
}
59
61
60
62
impl Iterator for MountTable {
61
- type Item = MountTableEntity ;
63
+ type Item = MountEntity ;
62
64
63
65
fn next ( & mut self ) -> Option < Self :: Item > {
64
66
static THREAD_UNSAFE_FUNCTION_MUTEX : Mutex < ( ) > = Mutex :: new ( ( ) ) ;
65
67
let _lock = THREAD_UNSAFE_FUNCTION_MUTEX . lock ( ) . unwrap ( ) ;
66
68
67
69
// Preliminary: | MT-Unsafe race:mntentbuf locale | AS-Unsafe corrupt heap init | AC-Unsafe init corrupt lock mem
68
70
// https://www.gnu.org/software/libc/manual/html_node/POSIX-Safety-Concepts.html
69
- let me = unsafe { getmntent ( self . inner ) } ;
71
+ let me = unsafe { libc :: getmntent ( self . inner ) } ;
70
72
if me. is_null ( ) {
71
73
return None ;
72
74
}
73
75
74
- unsafe {
75
- Some ( MountTableEntity {
76
- fsname : CStr :: from_ptr ( ( * me) . mnt_fsname ) . into ( ) ,
76
+ Some ( unsafe {
77
+ MountEntity {
77
78
dir : CStr :: from_ptr ( ( * me) . mnt_dir ) . into ( ) ,
79
+ fsname : CStr :: from_ptr ( ( * me) . mnt_fsname ) . into ( ) ,
78
80
fstype : CStr :: from_ptr ( ( * me) . mnt_type ) . into ( ) ,
79
81
opts : CStr :: from_ptr ( ( * me) . mnt_opts ) . into ( ) ,
80
82
freq : ( * me) . mnt_freq ,
81
83
passno : ( * me) . mnt_passno ,
82
- } )
83
- }
84
+ }
85
+ } )
84
86
}
85
87
}
86
88
@@ -89,7 +91,7 @@ impl Drop for MountTable {
89
91
fn drop ( & mut self ) {
90
92
// Preliminary: | MT-Safe | AS-Unsafe heap lock | AC-Unsafe lock mem fd
91
93
// https://www.gnu.org/software/libc/manual/html_node/POSIX-Safety-Concepts.html
92
- let _rc = unsafe { endmntent ( self . inner ) } ;
94
+ let _rc = unsafe { libc :: endmntent ( self . inner ) } ;
93
95
}
94
96
}
95
97
@@ -104,8 +106,8 @@ mod tests {
104
106
}
105
107
106
108
#[ test]
107
- fn test_open_not_found ( ) {
108
- let mtab = MountTable :: open ( c"/tmp/not_found " , c"r" ) ;
109
+ fn test_open_not_exist ( ) {
110
+ let mtab = MountTable :: open ( c"tests/not_exist " , c"r" ) ;
109
111
let mtab = mtab. err ( ) . unwrap ( ) ;
110
112
assert_eq ! ( mtab. kind( ) , std:: io:: ErrorKind :: NotFound ) ;
111
113
}
@@ -117,9 +119,9 @@ mod tests {
117
119
assert_eq ! ( vec. len( ) , 2 ) ;
118
120
assert_eq ! (
119
121
vec[ 0 ] ,
120
- MountTableEntity {
121
- fsname: CString :: new( "/dev/sdb1" ) . unwrap( ) ,
122
+ MountEntity {
122
123
dir: CString :: new( "/" ) . unwrap( ) ,
124
+ fsname: CString :: new( "/dev/sdb1" ) . unwrap( ) ,
123
125
fstype: CString :: new( "ext3" ) . unwrap( ) ,
124
126
opts: CString :: new( "rw,relatime,errors=remount-ro" ) . unwrap( ) ,
125
127
freq: 0 ,
@@ -128,14 +130,23 @@ mod tests {
128
130
) ;
129
131
assert_eq ! (
130
132
vec[ 1 ] ,
131
- MountTableEntity {
132
- fsname: CString :: new( "proc" ) . unwrap( ) ,
133
+ MountEntity {
133
134
dir: CString :: new( "/proc" ) . unwrap( ) ,
135
+ fsname: CString :: new( "proc" ) . unwrap( ) ,
134
136
fstype: CString :: new( "proc" ) . unwrap( ) ,
135
137
opts: CString :: new( "rw,noexec,nosuid,nodev" ) . unwrap( ) ,
136
138
freq: 0 ,
137
139
passno: 0 ,
138
140
}
139
141
) ;
140
142
}
143
+
144
+ #[ test]
145
+ fn test_open_system ( ) {
146
+ let mtab = MountTable :: open_system ( ) ;
147
+ assert ! ( mtab. is_ok( ) ) ;
148
+ for i in mtab. unwrap ( ) {
149
+ let _ = i. dir ;
150
+ }
151
+ }
141
152
}
0 commit comments