From a25f863afdce9f38bd450c2de90dd18abae56e0a Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Tue, 12 Feb 2019 14:20:16 +0100 Subject: [PATCH] tools/fs: add scan_subdir helper This filters the results of read_subdir with a regex. Signed-off-by: Wolfgang Bumiller --- src/tools/fs.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/tools/fs.rs b/src/tools/fs.rs index 48abe436..45f9bb06 100644 --- a/src/tools/fs.rs +++ b/src/tools/fs.rs @@ -35,3 +35,29 @@ pub fn read_subdir(dirfd: RawFd, path: &P) -> Result( + dirfd: RawFd, + path: &P, + regex: &'a regex::Regex, +) -> Result> + 'a, Error> { + Ok(read_subdir(dirfd, path)?.filter_map(move |entry| { + match entry { + Ok(entry) => match entry.file_name().to_str() { + Ok(name) => { + if regex.is_match(name) { + Some(Ok(entry)) + } else { + None // Skip values not matching the regex + } + } + // Skip values which aren't valid utf-8 + Err(_) => None, + }, + // Propagate errors through + Err(e) => Some(Err(Error::from(e))), + } + })) +}