Skip to content

Commit b8d4eac

Browse files
authored
Merge pull request #307 from fox0/mntent
fs/mntent.rs: 100% test coverage
2 parents 9f64d39 + 6d014f8 commit b8d4eac

File tree

2 files changed

+33
-22
lines changed

2 files changed

+33
-22
lines changed

fs/df.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ fn read_mount_info() -> io::Result<MountList> {
323323
fn read_mount_info() -> io::Result<MountList> {
324324
let mut info = MountList::new();
325325

326-
let mounts = MountTable::try_new()?;
326+
let mounts = MountTable::open_system()?;
327327
for mount in mounts {
328328
unsafe {
329329
let mut buf: libc::statfs = std::mem::zeroed();

fs/mntent.rs

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
//! The mtab file
1111
//! https://www.gnu.org/software/libc/manual/html_node/mtab.html
1212
13-
use libc::{endmntent, getmntent, setmntent, FILE};
1413
use std::ffi::{CStr, CString};
1514
use std::io;
1615
use std::sync::Mutex;
@@ -20,16 +19,18 @@ const _PATH_MOUNTED: &CStr = c"/etc/mtab";
2019
/// The mtab (contraction of mounted file systems table) file
2120
/// is a system information file, commonly found on Unix-like systems
2221
pub struct MountTable {
23-
inner: *mut FILE,
22+
inner: *mut libc::FILE,
2423
}
2524

2625
/// Structure describing a mount table entry
26+
///
27+
/// Wrapper of [`libc::mntent`]
2728
#[derive(Debug, PartialEq)]
28-
pub struct MountTableEntity {
29-
/// Device or server for filesystem
30-
pub fsname: CString,
29+
pub struct MountEntity {
3130
/// Directory mounted on
3231
pub dir: CString,
32+
/// Device or server for filesystem
33+
pub fsname: CString,
3334
/// Type of filesystem: ufs, nfs, etc
3435
pub fstype: CString,
3536
/// Comma-separated options for fs
@@ -41,15 +42,16 @@ pub struct MountTableEntity {
4142
}
4243

4344
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> {
4547
Self::open(_PATH_MOUNTED, c"r")
4648
}
4749

4850
/// Open mtab file
4951
fn open(filename: &CStr, mode: &CStr) -> Result<Self, io::Error> {
5052
// Preliminary: | MT-Safe | AS-Unsafe heap lock | AC-Unsafe mem fd lock
5153
// 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()) };
5355
if inner.is_null() {
5456
return Err(io::Error::last_os_error());
5557
}
@@ -58,29 +60,29 @@ impl MountTable {
5860
}
5961

6062
impl Iterator for MountTable {
61-
type Item = MountTableEntity;
63+
type Item = MountEntity;
6264

6365
fn next(&mut self) -> Option<Self::Item> {
6466
static THREAD_UNSAFE_FUNCTION_MUTEX: Mutex<()> = Mutex::new(());
6567
let _lock = THREAD_UNSAFE_FUNCTION_MUTEX.lock().unwrap();
6668

6769
// Preliminary: | MT-Unsafe race:mntentbuf locale | AS-Unsafe corrupt heap init | AC-Unsafe init corrupt lock mem
6870
// 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) };
7072
if me.is_null() {
7173
return None;
7274
}
7375

74-
unsafe {
75-
Some(MountTableEntity {
76-
fsname: CStr::from_ptr((*me).mnt_fsname).into(),
76+
Some(unsafe {
77+
MountEntity {
7778
dir: CStr::from_ptr((*me).mnt_dir).into(),
79+
fsname: CStr::from_ptr((*me).mnt_fsname).into(),
7880
fstype: CStr::from_ptr((*me).mnt_type).into(),
7981
opts: CStr::from_ptr((*me).mnt_opts).into(),
8082
freq: (*me).mnt_freq,
8183
passno: (*me).mnt_passno,
82-
})
83-
}
84+
}
85+
})
8486
}
8587
}
8688

@@ -89,7 +91,7 @@ impl Drop for MountTable {
8991
fn drop(&mut self) {
9092
// Preliminary: | MT-Safe | AS-Unsafe heap lock | AC-Unsafe lock mem fd
9193
// 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) };
9395
}
9496
}
9597

@@ -104,8 +106,8 @@ mod tests {
104106
}
105107

106108
#[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");
109111
let mtab = mtab.err().unwrap();
110112
assert_eq!(mtab.kind(), std::io::ErrorKind::NotFound);
111113
}
@@ -117,9 +119,9 @@ mod tests {
117119
assert_eq!(vec.len(), 2);
118120
assert_eq!(
119121
vec[0],
120-
MountTableEntity {
121-
fsname: CString::new("/dev/sdb1").unwrap(),
122+
MountEntity {
122123
dir: CString::new("/").unwrap(),
124+
fsname: CString::new("/dev/sdb1").unwrap(),
123125
fstype: CString::new("ext3").unwrap(),
124126
opts: CString::new("rw,relatime,errors=remount-ro").unwrap(),
125127
freq: 0,
@@ -128,14 +130,23 @@ mod tests {
128130
);
129131
assert_eq!(
130132
vec[1],
131-
MountTableEntity {
132-
fsname: CString::new("proc").unwrap(),
133+
MountEntity {
133134
dir: CString::new("/proc").unwrap(),
135+
fsname: CString::new("proc").unwrap(),
134136
fstype: CString::new("proc").unwrap(),
135137
opts: CString::new("rw,noexec,nosuid,nodev").unwrap(),
136138
freq: 0,
137139
passno: 0,
138140
}
139141
);
140142
}
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+
}
141152
}

0 commit comments

Comments
 (0)