Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs/mntent.rs: 100% test coverage #307

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fs/mntent.rs: 100% test coverage
  • Loading branch information
fox0 committed Oct 10, 2024
commit 6d014f8941b66b848a30099261fe78b0fb53d370
2 changes: 1 addition & 1 deletion fs/df.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ fn read_mount_info() -> io::Result<MountList> {
fn read_mount_info() -> io::Result<MountList> {
let mut info = MountList::new();

let mounts = MountTable::try_new()?;
let mounts = MountTable::open_system()?;
for mount in mounts {
unsafe {
let mut buf: libc::statfs = std::mem::zeroed();
Expand Down
53 changes: 32 additions & 21 deletions fs/mntent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
//! The mtab file
//! https://www.gnu.org/software/libc/manual/html_node/mtab.html

use libc::{endmntent, getmntent, setmntent, FILE};
use std::ffi::{CStr, CString};
use std::io;
use std::sync::Mutex;
Expand All @@ -20,16 +19,18 @@ const _PATH_MOUNTED: &CStr = c"/etc/mtab";
/// The mtab (contraction of mounted file systems table) file
/// is a system information file, commonly found on Unix-like systems
pub struct MountTable {
inner: *mut FILE,
inner: *mut libc::FILE,
}

/// Structure describing a mount table entry
///
/// Wrapper of [`libc::mntent`]
#[derive(Debug, PartialEq)]
pub struct MountTableEntity {
/// Device or server for filesystem
pub fsname: CString,
pub struct MountEntity {
/// Directory mounted on
pub dir: CString,
/// Device or server for filesystem
pub fsname: CString,
/// Type of filesystem: ufs, nfs, etc
pub fstype: CString,
/// Comma-separated options for fs
Expand All @@ -41,15 +42,16 @@ pub struct MountTableEntity {
}

impl MountTable {
pub fn try_new() -> Result<Self, io::Error> {
/// Open system mtab file
pub fn open_system() -> Result<Self, io::Error> {
Self::open(_PATH_MOUNTED, c"r")
}

/// Open mtab file
fn open(filename: &CStr, mode: &CStr) -> Result<Self, io::Error> {
// Preliminary: | MT-Safe | AS-Unsafe heap lock | AC-Unsafe mem fd lock
// https://www.gnu.org/software/libc/manual/html_node/POSIX-Safety-Concepts.html
let inner = unsafe { setmntent(filename.as_ptr(), mode.as_ptr()) };
let inner = unsafe { libc::setmntent(filename.as_ptr(), mode.as_ptr()) };
if inner.is_null() {
return Err(io::Error::last_os_error());
}
Expand All @@ -58,29 +60,29 @@ impl MountTable {
}

impl Iterator for MountTable {
type Item = MountTableEntity;
type Item = MountEntity;

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

// Preliminary: | MT-Unsafe race:mntentbuf locale | AS-Unsafe corrupt heap init | AC-Unsafe init corrupt lock mem
// https://www.gnu.org/software/libc/manual/html_node/POSIX-Safety-Concepts.html
let me = unsafe { getmntent(self.inner) };
let me = unsafe { libc::getmntent(self.inner) };
if me.is_null() {
return None;
}

unsafe {
Some(MountTableEntity {
fsname: CStr::from_ptr((*me).mnt_fsname).into(),
Some(unsafe {
MountEntity {
dir: CStr::from_ptr((*me).mnt_dir).into(),
fsname: CStr::from_ptr((*me).mnt_fsname).into(),
fstype: CStr::from_ptr((*me).mnt_type).into(),
opts: CStr::from_ptr((*me).mnt_opts).into(),
freq: (*me).mnt_freq,
passno: (*me).mnt_passno,
})
}
}
})
}
}

Expand All @@ -89,7 +91,7 @@ impl Drop for MountTable {
fn drop(&mut self) {
// Preliminary: | MT-Safe | AS-Unsafe heap lock | AC-Unsafe lock mem fd
// https://www.gnu.org/software/libc/manual/html_node/POSIX-Safety-Concepts.html
let _rc = unsafe { endmntent(self.inner) };
let _rc = unsafe { libc::endmntent(self.inner) };
}
}

Expand All @@ -104,8 +106,8 @@ mod tests {
}

#[test]
fn test_open_not_found() {
let mtab = MountTable::open(c"/tmp/not_found", c"r");
fn test_open_not_exist() {
let mtab = MountTable::open(c"tests/not_exist", c"r");
let mtab = mtab.err().unwrap();
assert_eq!(mtab.kind(), std::io::ErrorKind::NotFound);
}
Expand All @@ -117,9 +119,9 @@ mod tests {
assert_eq!(vec.len(), 2);
assert_eq!(
vec[0],
MountTableEntity {
fsname: CString::new("/dev/sdb1").unwrap(),
MountEntity {
dir: CString::new("/").unwrap(),
fsname: CString::new("/dev/sdb1").unwrap(),
fstype: CString::new("ext3").unwrap(),
opts: CString::new("rw,relatime,errors=remount-ro").unwrap(),
freq: 0,
Expand All @@ -128,14 +130,23 @@ mod tests {
);
assert_eq!(
vec[1],
MountTableEntity {
fsname: CString::new("proc").unwrap(),
MountEntity {
dir: CString::new("/proc").unwrap(),
fsname: CString::new("proc").unwrap(),
fstype: CString::new("proc").unwrap(),
opts: CString::new("rw,noexec,nosuid,nodev").unwrap(),
freq: 0,
passno: 0,
}
);
}

#[test]
fn test_open_system() {
let mtab = MountTable::open_system();
assert!(mtab.is_ok());
for i in mtab.unwrap() {
let _ = i.dir;
}
}
}