Skip to content

Commit 10978ae

Browse files
authored
Merge pull request #898 from nicholasbishop/bishop-try-exists
uefi: Change try_exists to return FileSystemResult<bool>
2 parents 9e1840d + 0f186cf commit 10978ae

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- `uefi::proto::shim` is now available on 32-bit x86 targets.
1212
- `Parity` and `StopBits` are now a newtype-enums instead of Rust enums. Their
1313
members now have upper-case names.
14+
- `FileSystem::try_exists` now returns `FileSystemResult<bool>`.
1415

1516
## uefi-macros - [Unreleased]
1617

uefi-test-runner/src/fs/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ pub fn test(sfs: ScopedProtocol<SimpleFileSystem>) -> Result<(), fs::Error> {
6262
// test remove dir all
6363
fs.remove_dir_all(cstr16!("foo_dir\\1"))?;
6464
// file should not be available after remove all
65-
let err = fs.try_exists(cstr16!("foo_dir\\1"));
66-
assert!(err.is_err());
65+
assert!(!fs.try_exists(cstr16!("foo_dir\\1"))?);
6766

6867
Ok(())
6968
}

uefi/src/fs/file_system/fs.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,22 @@ impl<'a> FileSystem<'a> {
2727
Self(proto)
2828
}
2929

30-
/// Tests if the underlying file exists. If this returns `Ok`, the file
31-
/// exists.
32-
pub fn try_exists(&mut self, path: impl AsRef<Path>) -> FileSystemResult<()> {
33-
self.metadata(path).map(|_| ())
30+
/// Returns `Ok(true)` if the path points at an existing file.
31+
///
32+
/// If the file does not exist, `Ok(false)` is returned. If it cannot be
33+
/// determined whether the file exists or not, an error is returned.
34+
pub fn try_exists(&mut self, path: impl AsRef<Path>) -> FileSystemResult<bool> {
35+
match self.open(path.as_ref(), UefiFileMode::Read, false) {
36+
Ok(_) => Ok(true),
37+
Err(Error::Io(err)) => {
38+
if err.uefi_error.status() == Status::NOT_FOUND {
39+
Ok(false)
40+
} else {
41+
Err(Error::Io(err))
42+
}
43+
}
44+
Err(err) => Err(err),
45+
}
3446
}
3547

3648
/// Copies the contents of one file to another. Creates the destination file
@@ -69,7 +81,7 @@ impl<'a> FileSystem<'a> {
6981
dirs_to_create.reverse();
7082

7183
for parent in dirs_to_create {
72-
if self.try_exists(&parent).is_err() {
84+
if !self.try_exists(&parent)? {
7385
self.create_dir(parent)?;
7486
}
7587
}
@@ -260,7 +272,7 @@ impl<'a> FileSystem<'a> {
260272

261273
// since there is no .truncate() in UEFI, we delete the file first it it
262274
// exists.
263-
if self.try_exists(path).is_ok() {
275+
if self.try_exists(path)? {
264276
self.remove_file(path)?;
265277
}
266278

0 commit comments

Comments
 (0)