function added to also find mounts by target path

This commit is contained in:
Bruce Leidl 2019-01-29 11:56:56 -05:00
parent 41708c870e
commit 0299d2814f

View File

@ -15,12 +15,17 @@ impl Mount {
/// Returns `true` if `path` matches the source field (first field) /// Returns `true` if `path` matches the source field (first field)
/// of any of the mount lines listed in /proc/mounts /// of any of the mount lines listed in /proc/mounts
/// ///
pub fn is_path_mounted<P: AsRef<Path>>(path: P) -> Result<bool> { pub fn is_source_mounted<P: AsRef<Path>>(path: P) -> Result<bool> {
let path_str = path.as_ref().to_string_lossy(); let path_str = path.as_ref().to_string_lossy();
let mounts = Mount::all_mounts()?; let mounts = Mount::all_mounts()?;
Ok(mounts.into_iter().any(|m| m.source == path_str)) Ok(mounts.into_iter().any(|m| m.source == path_str))
} }
pub fn is_target_mounted<P: AsRef<Path>>(path: P) -> Result<bool> {
let mounts = Mount::all_mounts()?;
Ok(mounts.into_iter().any(|m| m.target == path.as_ref()))
}
pub fn all_mounts() -> Result<Vec<Mount>> { pub fn all_mounts() -> Result<Vec<Mount>> {
let s = fs::read_to_string("/proc/mounts")?; let s = fs::read_to_string("/proc/mounts")?;
Ok(s.lines().flat_map(Mount::parse_mount_line).collect()) Ok(s.lines().flat_map(Mount::parse_mount_line).collect())