aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / vendor / uefi / src / fs / file_system / error.rs
| 1 | // SPDX-License-Identifier: MIT OR Apache-2.0 |
| 2 | |
| 3 | use crate::fs::{PathBuf, PathError}; |
| 4 | use alloc::string::FromUtf8Error; |
| 5 | use core::error; |
| 6 | use core::fmt::{self, Debug, Display, Formatter}; |
| 7 | |
| 8 | /// All errors that can happen when working with the [`FileSystem`]. |
| 9 | /// |
| 10 | /// [`FileSystem`]: super::FileSystem |
| 11 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 12 | pub enum Error { |
| 13 | /// IO (low-level UEFI-errors) errors. See [`IoError`]. |
| 14 | Io(IoError), |
| 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 | impl Display for Error { |
| 22 | fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { |
| 23 | match self { |
| 24 | Self::Io(_) => write!(f, "IO error"), |
| 25 | Self::Path(_) => write!(f, "path error"), |
| 26 | Self::Utf8Encoding(_) => write!(f, "UTF-8 encoding error"), |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | /// UEFI-error with context when working with the underlying UEFI file protocol. |
| 32 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 33 | pub struct IoError { |
| 34 | /// The path that led to the error. |
| 35 | pub path: PathBuf, |
| 36 | /// The context in which the path was used. |
| 37 | pub context: IoErrorContext, |
| 38 | /// The underlying UEFI error. |
| 39 | pub uefi_error: crate::Error, |
| 40 | } |
| 41 | |
| 42 | impl Display for IoError { |
| 43 | fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { |
| 44 | write!(f, "IO error for path {}: {}", self.path, self.context) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /// Enum that further specifies the context in that an [`Error`] occurred. |
| 49 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 50 | pub enum IoErrorContext { |
| 51 | /// Can't delete the directory. |
| 52 | CantDeleteDirectory, |
| 53 | /// Can't delete the file. |
| 54 | CantDeleteFile, |
| 55 | /// Error flushing file. |
| 56 | FlushFailure, |
| 57 | /// Can't open the root directory of the underlying volume. |
| 58 | CantOpenVolume, |
| 59 | /// Error while reading the metadata of the file. |
| 60 | Metadata, |
| 61 | /// Could not open the given path. One possible reason is that the file does |
| 62 | /// not exist. |
| 63 | OpenError, |
| 64 | /// Error reading file. |
| 65 | ReadFailure, |
| 66 | /// Error writing bytes. |
| 67 | WriteFailure, |
| 68 | /// The path exists but does not correspond to a directory when a directory |
| 69 | /// was expected. |
| 70 | NotADirectory, |
| 71 | /// The path exists but does not correspond to a file when a file was |
| 72 | /// expected. |
| 73 | NotAFile, |
| 74 | } |
| 75 | |
| 76 | impl Display for IoErrorContext { |
| 77 | fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { |
| 78 | let s = match self { |
| 79 | Self::CantDeleteDirectory => "failed to delete directory", |
| 80 | Self::CantDeleteFile => "failed to delete file", |
| 81 | Self::FlushFailure => "failed to flush file", |
| 82 | Self::CantOpenVolume => "failed to open volume", |
| 83 | Self::Metadata => "failed to read metadata", |
| 84 | Self::OpenError => "failed to open file", |
| 85 | Self::ReadFailure => "failed to read file", |
| 86 | Self::WriteFailure => "failed to write file", |
| 87 | Self::NotADirectory => "expected a directory", |
| 88 | Self::NotAFile => "expected a file", |
| 89 | }; |
| 90 | write!(f, "{s}") |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | impl From<PathError> for Error { |
| 95 | fn from(value: PathError) -> Self { |
| 96 | Self::Path(value) |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | impl error::Error for Error { |
| 101 | fn source(&self) -> Option<&(dyn error::Error + 'static)> { |
| 102 | match self { |
| 103 | Self::Io(err) => Some(err), |
| 104 | Self::Path(err) => Some(err), |
| 105 | Self::Utf8Encoding(err) => Some(err), |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | impl error::Error for IoError { |
| 111 | fn source(&self) -> Option<&(dyn error::Error + 'static)> { |
| 112 | Some(&self.uefi_error) |
| 113 | } |
| 114 | } |