Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 078e6a5

Browse files
committed
"is_regular_file" for FileHandle via File trait.
1 parent 9047806 commit 078e6a5

File tree

4 files changed

+54
-10
lines changed

4 files changed

+54
-10
lines changed

‎CHANGELOG.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
Now you can compare everything that is `AsRef<str>` (such as `String` and `str`
1515
from the standard library) to uefi strings. Please head to the documentation of
1616
`EqStrUntilNul` to find out limitations and further information.
17+
- the `File` trait now knows the methods `is_regular_file` and `is_directory`.
18+
Developers profit from this on the struct `FileHandle`, for example.
1719

1820
### Changed
1921

@@ -39,7 +41,7 @@
3941

4042
- The `no_panic_handler` feature has been replaced with an additive
4143
`panic_handler` feature. The new feature is enabled by default.
42-
44+
4345
## uefi - 0.16.1
4446

4547
### Added
@@ -55,7 +57,7 @@
5557
function pointers. This prevents potential invalid pointer access.
5658
- Fixed an incorrect pointer cast in the `Rng` protocol that could cause
5759
undefined behavior.
58-
60+
5961
### Changed
6062

6163
- Relaxed the version requirements for the `bitflags` and `log`

‎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(mutself) -> 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)

AltStyle によって変換されたページ (->オリジナル) /