Skip to content

Commit 0526601

Browse files
phip1611nicholasbishop
authored andcommitted
"is_regular_file" for FileHandle via File trait.
1 parent 4cba77a commit 0526601

File tree

4 files changed

+52
-8
lines changed

4 files changed

+52
-8
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
### Fixed
3232

3333
- Corrected the name of `BlockIOMedia::is_media_preset` to `is_media_present`.
34+
- The `File` trait now knows the methods `is_regular_file` and `is_directory`.
35+
Developers profit from this on the struct `FileHandle`, for example.
3436

3537
### Changed
3638

src/proto/media/file/dir.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,12 @@ impl File for Directory {
6767
fn handle(&mut self) -> &mut FileHandle {
6868
self.0.handle()
6969
}
70+
71+
fn is_regular_file(&self) -> Result<bool> {
72+
Ok(false)
73+
}
74+
75+
fn is_directory(&self) -> Result<bool> {
76+
Ok(true)
77+
}
7078
}

src/proto/media/file/mod.rs

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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

285311
impl Drop for FileHandle {

src/proto/media/file/regular.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,12 @@ impl File for RegularFile {
104104
fn handle(&mut self) -> &mut FileHandle {
105105
&mut self.0
106106
}
107+
108+
fn is_regular_file(&self) -> Result<bool> {
109+
Ok(true)
110+
}
111+
112+
fn is_directory(&self) -> Result<bool> {
113+
Ok(false)
114+
}
107115
}

0 commit comments

Comments
 (0)