rust/openat: Add helper to retrieve file type

Will be used by sysusers code.

Closes: #1764
Approved by: jlebon
This commit is contained in:
Colin Walters 2019-02-25 01:00:26 +00:00 committed by Atomic Bot
parent 579f017fc2
commit d1eedbbd96

View File

@ -26,6 +26,10 @@ pub(crate) trait OpenatDirExt {
// and Rust has an elegant way to map that with Option<>. Every other // and Rust has an elegant way to map that with Option<>. Every other
// error I usually just want to propagate back up. // error I usually just want to propagate back up.
fn open_file_optional<P: openat::AsPath>(&self, p: P) -> io::Result<Option<fs::File>>; fn open_file_optional<P: openat::AsPath>(&self, p: P) -> io::Result<Option<fs::File>>;
// 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<openat::SimpleType>;
} }
impl OpenatDirExt for openat::Dir { impl OpenatDirExt for openat::Dir {
@ -41,5 +45,13 @@ impl OpenatDirExt for openat::Dir {
} }
} }
} }
fn get_file_type(&self, e: &openat::Entry) -> io::Result<openat::SimpleType> {
if let Some(ftype) = e.simple_type() {
Ok(ftype)
} else {
Ok(self.metadata(e.file_name())?.simple_type())
}
}
} }