//! File system helper utilities. use std::borrow::{Borrow, BorrowMut}; use std::ops::{Deref, DerefMut}; use std::os::unix::io::{AsRawFd, RawFd}; use failure::*; use nix::dir; use nix::dir::Dir; use regex::Regex; use crate::tools::borrow::Tied; /// This wraps nix::dir::Entry with the parent directory's file descriptor. pub struct ReadDirEntry { entry: dir::Entry, parent_fd: RawFd, } impl Into for ReadDirEntry { fn into(self) -> dir::Entry { self.entry } } impl Deref for ReadDirEntry { type Target = dir::Entry; fn deref(&self) -> &Self::Target { &self.entry } } impl DerefMut for ReadDirEntry { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.entry } } impl AsRef for ReadDirEntry { fn as_ref(&self) -> &dir::Entry { &self.entry } } impl AsMut for ReadDirEntry { fn as_mut(&mut self) -> &mut dir::Entry { &mut self.entry } } impl Borrow for ReadDirEntry { fn borrow(&self) -> &dir::Entry { &self.entry } } impl BorrowMut for ReadDirEntry { fn borrow_mut(&mut self) -> &mut dir::Entry { &mut self.entry } } impl ReadDirEntry { #[inline] pub fn parent_fd(&self) -> RawFd { self.parent_fd } pub unsafe fn file_name_utf8_unchecked(&self) -> &str { std::str::from_utf8_unchecked(self.file_name().to_bytes()) } } // Since Tied implements Deref to U, a Tied already implements Iterator. // This is simply a wrapper with a shorter type name mapping nix::Error to failure::Error. /// Wrapper over a pair of `nix::dir::Dir` and `nix::dir::Iter`, returned by `read_subdir()`. pub struct ReadDir { iter: Tied>>, dir_fd: RawFd, } impl Iterator for ReadDir { type Item = Result; fn next(&mut self) -> Option { self.iter.next().map(|res| { res.map(|entry| ReadDirEntry { entry, parent_fd: self.dir_fd }) .map_err(|e| Error::from(e)) }) } } /// Create an iterator over sub directory entries. /// This uses `openat` on `dirfd`, so `path` can be relative to that or an absolute path. pub fn read_subdir(dirfd: RawFd, path: &P) -> Result { use nix::fcntl::OFlag; use nix::sys::stat::Mode; let dir = Dir::openat(dirfd, path, OFlag::O_RDONLY, Mode::empty())?; let fd = dir.as_raw_fd(); let iter = Tied::new(dir, |dir| { Box::new(unsafe { (*dir).iter() }) as Box>> }); Ok(ReadDir { iter, dir_fd: fd }) } /// Scan through a directory with a regular expression. This is simply a shortcut filtering the /// results of `read_subdir`. Non-UTF8 comaptible file names are silently ignored. pub fn scan_subdir<'a, P: ?Sized + nix::NixPath>( dirfd: RawFd, path: &P, regex: &'a regex::Regex, ) -> Result> + 'a, Error> { Ok(read_subdir(dirfd, path)?.filter_file_name_regex(regex)) } /// Helper trait to provide a combinators for directory entry iterators. pub trait FileIterOps where Self: Sized + Iterator>, T: Borrow, E: Into + Send + Sync, { /// Filter by file type. This is more convenient than using the `filter` method alone as this /// also includes error handling and handling of files without a type (via an error). fn filter_file_type(self, ty: dir::Type) -> FileTypeFilter { FileTypeFilter { inner: self, ty } } /// Filter by file name. Note that file names which aren't valid utf-8 will be treated as if /// they do not match the pattern. fn filter_file_name_regex<'a>(self, regex: &'a Regex) -> FileNameRegexFilter<'a, Self, T, E> { FileNameRegexFilter { inner: self, regex } } } impl FileIterOps for I where I: Iterator>, T: Borrow, E: Into + Send + Sync, { } /// This filters files from its inner iterator by a file type. Files with no type produce an error. pub struct FileTypeFilter where I: Iterator>, T: Borrow, E: Into + Send + Sync, { inner: I, ty: nix::dir::Type, } impl Iterator for FileTypeFilter where I: Iterator>, T: Borrow, E: Into + Send + Sync, { type Item = Result; fn next(&mut self) -> Option { loop { let item = self.inner.next()?.map_err(|e| e.into()); match item { Ok(ref entry) => match entry.borrow().file_type() { Some(ty) => { if ty == self.ty { return Some(item); } else { continue; } } None => return Some(Err(format_err!("unable to detect file type"))), }, Err(_) => return Some(item), } } } } /// This filters files by name via a Regex. Files whose file name aren't valid utf-8 are skipped /// silently. pub struct FileNameRegexFilter<'a, I, T, E> where I: Iterator>, T: Borrow, { inner: I, regex: &'a Regex, } impl Iterator for FileNameRegexFilter<'_, I, T, E> where I: Iterator>, T: Borrow, { type Item = Result; fn next(&mut self) -> Option { loop { let item = self.inner.next()?; match item { Ok(ref entry) => { if let Ok(name) = entry.borrow().file_name().to_str() { if self.regex.is_match(name) { return Some(item); } } // file did not match regex or isn't valid utf-8 continue; }, Err(_) => return Some(item), } } } }