diff --git a/proxmox-sys/src/linux/procfs/mountinfo.rs b/proxmox-sys/src/linux/procfs/mountinfo.rs index 0f5325e2..9bc19d66 100644 --- a/proxmox-sys/src/linux/procfs/mountinfo.rs +++ b/proxmox-sys/src/linux/procfs/mountinfo.rs @@ -165,8 +165,12 @@ pub struct MountInfo { entries: Vec, } +/// An iterator over entries in a `MountInfo`. pub type Iter<'a> = std::slice::Iter<'a, Entry>; +/// An iterator over mutable entries in a `MountInfo`. +pub type IterMut<'a> = std::slice::IterMut<'a, Entry>; + impl MountInfo { /// Read the current mount point information. pub fn read() -> Result { @@ -195,6 +199,55 @@ impl MountInfo { pub fn iter(&self) -> Iter { self.entries.iter() } + + /// Check if there exists a mount point for a specific path. + /// + /// FIXME: Do we need to verify that mount points don't get "hidden" by other higher level + /// mount points? For this we'd need to implement mountpoint-tree iteration first, the info for + /// which we have available in the `Entry` struct! + pub fn path_is_mounted

(&self, path: &P) -> bool + where + PathBuf: PartialEq

, + { + self.iter().any(|entry| entry.mount_point == *path) + } + + /// Check whether there exists a mount point for a specified source. + pub fn source_is_mounted(&self, source: &T) -> bool + where + OsString: PartialEq, + { + self.iter() + .filter_map(|entry| entry.mount_source.as_ref()) + .any(|s| *s == *source) + } +} + +impl IntoIterator for MountInfo { + type Item = Entry; + type IntoIter = std::vec::IntoIter; + + fn into_iter(self) -> Self::IntoIter { + self.entries.into_iter() + } +} + +impl<'a> IntoIterator for &'a MountInfo { + type Item = &'a Entry; + type IntoIter = Iter<'a>; + + fn into_iter(self) -> Self::IntoIter { + (&self.entries).into_iter() + } +} + +impl<'a> IntoIterator for &'a mut MountInfo { + type Item = &'a mut Entry; + type IntoIter = IterMut<'a>; + + fn into_iter(self) -> Self::IntoIter { + (&mut self.entries).into_iter() + } } #[test]