]> git.proxmox.com Git - proxmox-backup.git/commitdiff
tools/fs: let ReadDir iterator yield ReadDirEntry
authorWolfgang Bumiller <w.bumiller@proxmox.com>
Wed, 13 Feb 2019 13:02:27 +0000 (14:02 +0100)
committerWolfgang Bumiller <w.bumiller@proxmox.com>
Thu, 14 Feb 2019 09:09:59 +0000 (10:09 +0100)
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
src/tools/fs.rs

index 8ab3347745e00d94fad65d8900f97ed40f59f63a..447583b909c5ed69a339cb2e61eaa3368b317139 100644 (file)
@@ -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, Iterator<Item = nix::Result<dir::Entry>>>,
+    dir_fd: RawFd,
 }
 
 impl Iterator for ReadDir {
-    type Item = Result<dir::Entry, Error>;
+    type Item = Result<ReadDirEntry, Error>;
 
     fn next(&mut self) -> Option<Self::Item> {
-        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<P: ?Sized + nix::NixPath>(dirfd: RawFd, path: &P) -> Result<R
     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<Iterator<Item = nix::Result<dir::Entry>>>
     });
-    Ok(ReadDir { iter })
+    Ok(ReadDir { iter, dir_fd: fd })
 }
 
 /// Scan through a directory with a regular expression. This is simply a shortcut filtering the