From 1db416150d4895b72b62881ec533b17e98a20e54 Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Wed, 13 Feb 2019 14:02:27 +0100 Subject: [PATCH] tools/fs: let ReadDir iterator yield ReadDirEntry Signed-off-by: Wolfgang Bumiller --- src/tools/fs.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/tools/fs.rs b/src/tools/fs.rs index 8ab33477..447583b9 100644 --- a/src/tools/fs.rs +++ b/src/tools/fs.rs @@ -2,7 +2,7 @@ use std::borrow::{Borrow, BorrowMut}; use std::ops::{Deref, DerefMut}; -use std::os::unix::io::RawFd; +use std::os::unix::io::{AsRawFd, RawFd}; use failure::Error; use nix::dir; @@ -72,13 +72,17 @@ impl ReadDirEntry { /// 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; + type Item = Result; fn next(&mut self) -> Option { - self.iter.next().map(|res| res.map_err(|e| Error::from(e))) + self.iter.next().map(|res| { + res.map(|entry| ReadDirEntry { entry, parent_fd: self.dir_fd }) + .map_err(|e| Error::from(e)) + }) } } @@ -89,10 +93,11 @@ pub fn read_subdir(dirfd: RawFd, path: &P) -> Result>> }); - Ok(ReadDir { iter }) + Ok(ReadDir { iter, dir_fd: fd }) } /// Scan through a directory with a regular expression. This is simply a shortcut filtering the