From d1eedbbd965e2c485354e409fc12d172b35b026d Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Mon, 25 Feb 2019 01:00:26 +0000 Subject: [PATCH] rust/openat: Add helper to retrieve file type Will be used by sysusers code. Closes: #1764 Approved by: jlebon --- rust/src/openat_utils.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) 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()) + } + } }