tools/fs: add scan_subdir helper
This filters the results of read_subdir with a regex. Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
parent
b4d5787de9
commit
a25f863afd
@ -35,3 +35,29 @@ pub fn read_subdir<P: ?Sized + nix::NixPath>(dirfd: RawFd, path: &P) -> Result<R
|
||||
});
|
||||
Ok(ReadDir { iter })
|
||||
}
|
||||
|
||||
/// 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<impl Iterator<Item = Result<nix::dir::Entry, Error>> + '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))),
|
||||
}
|
||||
}))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user