Skip to content

Commit 24ef18c

Browse files
committed
fs: add new FileSystemError type
1 parent 05fe57e commit 24ef18c

File tree

3 files changed

+96
-1
lines changed

3 files changed

+96
-1
lines changed

uefi/src/fs/file_system/error.rs

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

uefi/src/fs/file_system/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
mod error;
12
mod fs;
23

4+
pub use error::*;
35
pub use fs::*;

uefi/src/fs/path/pathbuf.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use core::fmt::{Display, Formatter};
77
/// [`CString16`] strings and [`SEPARATOR`] as separator.
88
///
99
/// `/` is replaced by [`SEPARATOR`] on the fly.
10-
#[derive(Debug, Default, Eq, PartialOrd, Ord)]
10+
#[derive(Clone, Debug, Default, Eq, PartialOrd, Ord)]
1111
pub struct PathBuf(CString16);
1212

1313
impl PathBuf {

0 commit comments

Comments
 (0)