Skip to content

Commit 023b08d

Browse files
phip1611nicholasbishop
authored andcommitted
fs: add new Error type + naming adjustments
1 parent 1b01ca0 commit 023b08d

File tree

8 files changed

+195
-119
lines changed

8 files changed

+195
-119
lines changed

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
33
use alloc::string::{String, ToString};
44
use alloc::vec::Vec;
5-
use uefi::cstr16;
6-
use uefi::fs::{FileSystem, FileSystemError, PathBuf};
5+
use uefi::fs::{FileSystem, FileSystemIOErrorContext, IoError, PathBuf};
76
use uefi::proto::media::fs::SimpleFileSystem;
87
use uefi::table::boot::ScopedProtocol;
8+
use uefi::{cstr16, fs, Status};
99

1010
/// Tests functionality from the `uefi::fs` module. This test relies on a
1111
/// working File System Protocol, which is tested at a dedicated place.
12-
pub fn test(sfs: ScopedProtocol<SimpleFileSystem>) -> Result<(), FileSystemError> {
12+
pub fn test(sfs: ScopedProtocol<SimpleFileSystem>) -> Result<(), fs::Error> {
1313
let mut fs = FileSystem::new(sfs);
1414

1515
// test create dir
@@ -24,9 +24,14 @@ pub fn test(sfs: ScopedProtocol<SimpleFileSystem>) -> Result<(), FileSystemError
2424
let read = String::from_utf8(read).expect("Should be valid utf8");
2525
assert_eq!(read.as_str(), data_to_write);
2626

27-
// test copy from non-existent file
27+
// test copy from non-existent file: does the error type work as expected?
2828
let err = fs.copy(cstr16!("not_found"), cstr16!("abc"));
29-
assert!(matches!(err, Err(FileSystemError::OpenError { .. })));
29+
let expected_err = fs::Error::Io(IoError {
30+
path: PathBuf::from(cstr16!("not_found")),
31+
context: FileSystemIOErrorContext::OpenError,
32+
uefi_error: uefi::Error::new(Status::NOT_FOUND, ()),
33+
});
34+
assert_eq!(err, Err(expected_err));
3035

3136
// test rename file + path buf replaces / with \
3237
fs.rename(
@@ -35,7 +40,7 @@ pub fn test(sfs: ScopedProtocol<SimpleFileSystem>) -> Result<(), FileSystemError
3540
)?;
3641
// file should not be available after rename
3742
let err = fs.read(cstr16!("foo_dir\\foo_cpy"));
38-
assert!(matches!(err, Err(FileSystemError::OpenError { .. })));
43+
assert!(err.is_err());
3944

4045
// test read dir on a sub dir
4146
let entries = fs

uefi/src/fs/file_system/error.rs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
use crate::fs::{PathBuf, PathError};
2+
use alloc::string::FromUtf8Error;
3+
use core::fmt::Debug;
4+
use derive_more::Display;
5+
6+
/// All errors that can happen when working with the [`FileSystem`].
7+
///
8+
/// [`FileSystem`]: super::FileSystem
9+
#[derive(Debug, Clone, Display, PartialEq, Eq)]
10+
pub enum Error {
11+
/// IO (low-level UEFI-errors) errors. See [`IoError`].
12+
Io(IoError),
13+
/// Path-related errors. See [`PathError`].
14+
Path(PathError),
15+
/// Can't parse file content as UTF-8. See [`FromUtf8Error`].
16+
Utf8Encoding(FromUtf8Error),
17+
}
18+
19+
/// UEFI-error with context when working with the underlying UEFI file protocol.
20+
#[derive(Debug, Clone, Display, PartialEq, Eq)]
21+
#[display(fmt = "IoError({},{})", context, path)]
22+
pub struct IoError {
23+
/// The path that led to the error.
24+
pub path: PathBuf,
25+
/// The context in which the path was used.
26+
pub context: FileSystemIOErrorContext,
27+
/// The underlying UEFI error.
28+
pub uefi_error: crate::Error,
29+
}
30+
31+
/// Enum that further specifies the context in that a [`Error`]
32+
/// occurred.
33+
#[derive(Debug, Clone, Display, PartialEq, Eq)]
34+
pub enum FileSystemIOErrorContext {
35+
/// Can't delete the directory.
36+
CantDeleteDirectory,
37+
/// Can't delete the file.
38+
CantDeleteFile,
39+
/// Error flushing file.
40+
FlushFailure,
41+
/// Can't open the root directory of the underlying volume.
42+
CantOpenVolume,
43+
/// Error while reading the metadata of the file.
44+
Metadata,
45+
/// Could not open the given path. One possible reason is that the file does
46+
/// not exist.
47+
OpenError,
48+
/// Error reading file.
49+
ReadFailure,
50+
/// Error writing bytes.
51+
WriteFailure,
52+
/// The path exists but does not correspond to a directory when a directory
53+
/// was expected.
54+
NotADirectory,
55+
/// The path exists but does not correspond to a file when a file was
56+
/// expected.
57+
NotAFile,
58+
}
59+
60+
impl From<PathError> for Error {
61+
fn from(value: PathError) -> Self {
62+
Self::Path(value)
63+
}
64+
}
65+
66+
#[cfg(feature = "unstable")]
67+
impl core::error::Error for Error {
68+
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
69+
match self {
70+
Error::Io(err) => Some(err),
71+
Error::Path(err) => Some(err),
72+
Error::Utf8Encoding(err) => Some(err),
73+
}
74+
}
75+
}
76+
77+
#[cfg(feature = "unstable")]
78+
impl core::error::Error for IoError {
79+
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
80+
Some(&self.uefi_error)
81+
}
82+
}

0 commit comments

Comments
 (0)