diff --git a/rust/src/openat_utils.rs b/rust/src/openat_utils.rs index c167dea7..e9ec46c4 100644 --- a/rust/src/openat_utils.rs +++ b/rust/src/openat_utils.rs @@ -26,6 +26,10 @@ pub(crate) trait OpenatDirExt { // and Rust has an elegant way to map that with Option<>. Every other // error I usually just want to propagate back up. fn open_file_optional(&self, p: P) -> io::Result>; + + // On modern filesystems the directory entry contains the type; if available, + // return it. Otherwise invoke stat(). + fn get_file_type(&self, e: &openat::Entry) -> io::Result; } impl OpenatDirExt for openat::Dir { @@ -41,5 +45,13 @@ impl OpenatDirExt for openat::Dir { } } } + + fn get_file_type(&self, e: &openat::Entry) -> io::Result { + if let Some(ftype) = e.simple_type() { + Ok(ftype) + } else { + Ok(self.metadata(e.file_name())?.simple_type()) + } + } }