@@ -214,6 +214,18 @@ pub trait File: Sized {
214214 // global allocator.
215215 unsafe { Ok ( Box :: from_raw ( info) ) }
216216 }
217+
218+ /// Returns if the underlying file is a regular file.
219+ /// The result is an error if the underlying file was already closed or deleted.
220+ ///
221+ /// UEFI file system protocol only knows "regular files" and "directories".
222+ fn is_regular_file ( & self ) -> Result < bool > ;
223+
224+ /// Returns if the underlying file is a directory.
225+ /// The result is an error if the underlying file was already closed or deleted.
226+ ///
227+ /// UEFI file system protocol only knows "regular files" and "directories".
228+ fn is_directory ( & self ) -> Result < bool > ;
217229}
218230
219231// Internal File helper methods to access the funciton pointer table.
@@ -241,16 +253,14 @@ impl FileHandle {
241253 }
242254
243255 /// Converts `File` into a more specific subtype based on if it is a
244- /// directory or not. It does this via a call to `get_position` .
245- pub fn into_type ( mut self ) -> Result < FileType > {
256+ /// directory or not. Wrapper around [Self::is_regular_file] .
257+ pub fn into_type ( self ) -> Result < FileType > {
246258 use FileType :: * ;
247259
248- // get_position fails with EFI_UNSUPPORTED on directories
249- let mut pos = 0 ;
250- match ( self . imp ( ) . get_position ) ( self . imp ( ) , & mut pos) {
251- Status :: SUCCESS => unsafe { Ok ( Regular ( RegularFile :: new ( self ) ) ) } ,
252- Status :: UNSUPPORTED => unsafe { Ok ( Dir ( Directory :: new ( self ) ) ) } ,
253- s => Err ( s. into ( ) ) ,
260+ if self . is_regular_file ( ) ? {
261+ unsafe { Ok ( Regular ( RegularFile :: new ( self ) ) ) }
262+ } else {
263+ unsafe { Ok ( Dir ( Directory :: new ( self ) ) ) }
254264 }
255265 }
256266
@@ -280,6 +290,22 @@ impl File for FileHandle {
280290 fn handle ( & mut self ) -> & mut FileHandle {
281291 self
282292 }
293+
294+ fn is_regular_file ( & self ) -> Result < bool > {
295+ let this = unsafe { self . 0 . as_mut ( ) . unwrap ( ) } ;
296+
297+ // get_position fails with EFI_UNSUPPORTED on directories
298+ let mut pos = 0 ;
299+ match ( this. get_position ) ( this, & mut pos) {
300+ Status :: SUCCESS => Ok ( true ) ,
301+ Status :: UNSUPPORTED => Ok ( false ) ,
302+ s => Err ( s. into ( ) ) ,
303+ }
304+ }
305+
306+ fn is_directory ( & self ) -> Result < bool > {
307+ self . is_regular_file ( ) . map ( |b| !b)
308+ }
283309}
284310
285311impl Drop for FileHandle {
0 commit comments