@@ -214,6 +214,18 @@ pub trait File: Sized {
214
214
// global allocator.
215
215
unsafe { Ok ( Box :: from_raw ( info) ) }
216
216
}
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 > ;
217
229
}
218
230
219
231
// Internal File helper methods to access the funciton pointer table.
@@ -241,16 +253,14 @@ impl FileHandle {
241
253
}
242
254
243
255
/// 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 > {
246
258
use FileType :: * ;
247
259
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 ) ) ) }
254
264
}
255
265
}
256
266
@@ -280,6 +290,22 @@ impl File for FileHandle {
280
290
fn handle ( & mut self ) -> & mut FileHandle {
281
291
self
282
292
}
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
+ }
283
309
}
284
310
285
311
impl Drop for FileHandle {
0 commit comments