diff --git a/src/tools/fs.rs b/src/tools/fs.rs index 45f9bb06..8ab33477 100644 --- a/src/tools/fs.rs +++ b/src/tools/fs.rs @@ -1,5 +1,7 @@ //! File system helper utilities. +use std::borrow::{Borrow, BorrowMut}; +use std::ops::{Deref, DerefMut}; use std::os::unix::io::RawFd; use failure::Error; @@ -8,6 +10,63 @@ use nix::dir::Dir; 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 + } +} + // 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()`.