Skip to content

Commit a8f91a2

Browse files
committed
fs: naming adjustments and error simplification
1 parent 1bbcd91 commit a8f91a2

File tree

5 files changed

+62
-55
lines changed

5 files changed

+62
-55
lines changed

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

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

1110
/// Tests functionality from the `uefi::fs` module. This test relies on a
1211
/// working File System Protocol, which is tested at a dedicated place.
@@ -27,7 +26,7 @@ pub fn test(sfs: ScopedProtocol<SimpleFileSystem>) -> Result<(), fs::Error> {
2726

2827
// test copy from non-existent file: does the error type work as expected?
2928
let err = fs.copy(cstr16!("not_found"), cstr16!("abc"));
30-
let expected_err = uefi::Error::IO(IOError {
29+
let expected_err = fs::Error::Io(IoError {
3130
path: PathBuf::from(cstr16!("not_found")),
3231
context: FileSystemIOErrorContext::OpenError,
3332
uefi_error: uefi::Error::new(Status::NOT_FOUND, ()),

uefi/src/fs/file_system/error.rs

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ use derive_more::Display;
88
/// [`FileSystem`]: super::FileSystem
99
#[derive(Debug, Clone, Display, PartialEq, Eq)]
1010
pub enum Error {
11-
/// Logical errors. See [`LogicError`].
12-
Logic(LogicError),
13-
/// IO (low-level UEFI-errors) errors. See [`IOError`].
14-
IO(IOError),
11+
/// IO (low-level UEFI-errors) errors. See [`IoError`].
12+
Io(IoError),
1513
/// Path-related errors. See [`PathError`].
1614
Path(PathError),
1715
/// Can't parse file content as UTF-8. See [`FromUtf8Error`].
@@ -20,8 +18,8 @@ pub enum Error {
2018

2119
/// UEFI-error with context when working with the underlying UEFI file protocol.
2220
#[derive(Debug, Clone, Display, PartialEq, Eq)]
23-
#[display(fmt = "FileSystemIOError({},{})", context, path)]
24-
pub struct IOError {
21+
#[display(fmt = "IoError({},{})", context, path)]
22+
pub struct IoError {
2523
/// The path that led to the error.
2624
pub path: PathBuf,
2725
/// The context in which the path was used.
@@ -30,17 +28,6 @@ pub struct IOError {
3028
pub uefi_error: crate::Error,
3129
}
3230

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-
4431
/// Enum that further specifies the context in that a [`Error`]
4532
/// occurred.
4633
#[derive(Debug, Clone, Display, PartialEq, Eq)]
@@ -62,6 +49,12 @@ pub enum FileSystemIOErrorContext {
6249
ReadFailure,
6350
/// Error writing bytes.
6451
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,
6558
}
6659

6760
impl From<PathError> for Error {
@@ -74,7 +67,7 @@ impl From<PathError> for Error {
7467
impl core::error::Error for Error {
7568
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
7669
match self {
77-
Error::IO(err) => Some(err),
70+
Error::Io(err) => Some(err),
7871
Error::Path(err) => Some(err),
7972
Error::Utf8Encoding(err) => Some(err),
8073
Error::Logic(err) => Some(err),
@@ -83,11 +76,8 @@ impl core::error::Error for Error {
8376
}
8477

8578
#[cfg(feature = "unstable")]
86-
impl core::error::Error for IOError {
79+
impl core::error::Error for IoError {
8780
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
8881
Some(&self.uefi_error)
8982
}
9083
}
91-
92-
#[cfg(feature = "unstable")]
93-
impl core::error::Error for LogicError {}

uefi/src/fs/file_system/fs.rs

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! Module for [`FileSystem`].
22
3-
use crate::fs::*;
4-
use crate::fs::{Path, PathBuf, UefiDirectoryIter, SEPARATOR_STR};
3+
use crate::fs::{Path, PathBuf, UefiDirectoryIter, SEPARATOR_STR, *};
54
use crate::table::boot::ScopedProtocol;
5+
use crate::Status;
66
use alloc::boxed::Box;
77
use alloc::string::String;
88
use alloc::vec;
@@ -13,7 +13,7 @@ use core::ops::Deref;
1313
use log::debug;
1414

1515
/// Return type for public [`FileSystem`] operations.
16-
pub type FileSystemResult<T> = Result<T, FileSystemError>;
16+
pub type FileSystemResult<T> = Result<T, Error>;
1717

1818
/// High-level file-system abstraction for UEFI volumes with an API that is
1919
/// close to `std::fs`. It acts as convenient accessor around the
@@ -83,7 +83,7 @@ impl<'a> FileSystem<'a> {
8383
let path = path.as_ref();
8484
let mut file = self.open(path, UefiFileMode::Read, false)?;
8585
file.get_boxed_info().map_err(|err| {
86-
FileSystemError::IO(FileSystemIOError {
86+
Error::Io(IoError {
8787
path: path.to_path_buf(),
8888
context: FileSystemIOErrorContext::Metadata,
8989
uefi_error: err,
@@ -98,11 +98,16 @@ impl<'a> FileSystem<'a> {
9898
let mut file = self
9999
.open(path, UefiFileMode::Read, false)?
100100
.into_regular_file()
101-
.ok_or(FileSystemError::Logic(LogicError::NotAFile(
102-
path.to_path_buf(),
103-
)))?;
101+
.ok_or(Error::Io(IoError {
102+
path: path.to_path_buf(),
103+
context: FileSystemIOErrorContext::NotAFile,
104+
// We do not have a real UEFI error here as we have a logical
105+
// problem.
106+
uefi_error: Status::INVALID_PARAMETER.into(),
107+
}))?;
108+
104109
let info = file.get_boxed_info::<UefiFileInfo>().map_err(|err| {
105-
FileSystemError::IO(FileSystemIOError {
110+
Error::Io(IoError {
106111
path: path.to_path_buf(),
107112
context: FileSystemIOErrorContext::Metadata,
108113
uefi_error: err,
@@ -111,7 +116,7 @@ impl<'a> FileSystem<'a> {
111116

112117
let mut vec = vec![0; info.file_size() as usize];
113118
let read_bytes = file.read(vec.as_mut_slice()).map_err(|err| {
114-
FileSystemError::IO(FileSystemIOError {
119+
Error::Io(IoError {
115120
path: path.to_path_buf(),
116121
context: FileSystemIOErrorContext::ReadFailure,
117122
uefi_error: err.to_err_without_payload(),
@@ -132,15 +137,19 @@ impl<'a> FileSystem<'a> {
132137
let dir = self
133138
.open(path, UefiFileMode::Read, false)?
134139
.into_directory()
135-
.ok_or(FileSystemError::Logic(LogicError::NotADirectory(
136-
path.to_path_buf(),
137-
)))?;
140+
.ok_or(Error::Io(IoError {
141+
path: path.to_path_buf(),
142+
context: FileSystemIOErrorContext::NotADirectory,
143+
// We do not have a real UEFI error here as we have a logical
144+
// problem.
145+
uefi_error: Status::INVALID_PARAMETER.into(),
146+
}))?;
138147
Ok(UefiDirectoryIter::new(dir))
139148
}
140149

141150
/// Read the entire contents of a file into a Rust string.
142151
pub fn read_to_string(&mut self, path: impl AsRef<Path>) -> FileSystemResult<String> {
143-
String::from_utf8(self.read(path)?).map_err(FileSystemError::Utf8Encoding)
152+
String::from_utf8(self.read(path)?).map_err(Error::Utf8Encoding)
144153
}
145154

146155
/// Removes an empty directory.
@@ -154,15 +163,21 @@ impl<'a> FileSystem<'a> {
154163

155164
match file {
156165
UefiFileType::Dir(dir) => dir.delete().map_err(|err| {
157-
FileSystemError::IO(FileSystemIOError {
166+
Error::Io(IoError {
158167
path: path.to_path_buf(),
159168
context: FileSystemIOErrorContext::CantDeleteDirectory,
160169
uefi_error: err,
161170
})
162171
}),
163-
UefiFileType::Regular(_) => Err(FileSystemError::Logic(LogicError::NotADirectory(
164-
path.to_path_buf(),
165-
))),
172+
UefiFileType::Regular(_) => {
173+
Err(Error::Io(IoError {
174+
path: path.to_path_buf(),
175+
context: FileSystemIOErrorContext::NotADirectory,
176+
// We do not have a real UEFI error here as we have a logical
177+
// problem.
178+
uefi_error: Status::INVALID_PARAMETER.into(),
179+
}))
180+
}
166181
}
167182
}
168183

@@ -183,15 +198,19 @@ impl<'a> FileSystem<'a> {
183198

184199
match file {
185200
UefiFileType::Regular(file) => file.delete().map_err(|err| {
186-
FileSystemError::IO(FileSystemIOError {
201+
Error::Io(IoError {
187202
path: path.to_path_buf(),
188203
context: FileSystemIOErrorContext::CantDeleteFile,
189204
uefi_error: err,
190205
})
191206
}),
192-
UefiFileType::Dir(_) => Err(FileSystemError::Logic(LogicError::NotAFile(
193-
path.to_path_buf(),
194-
))),
207+
UefiFileType::Dir(_) => Err(Error::Io(IoError {
208+
path: path.to_path_buf(),
209+
context: FileSystemIOErrorContext::NotAFile,
210+
// We do not have a real UEFI error here as we have a logical
211+
// problem.
212+
uefi_error: Status::INVALID_PARAMETER.into(),
213+
})),
195214
}
196215
}
197216

@@ -228,14 +247,14 @@ impl<'a> FileSystem<'a> {
228247
.unwrap();
229248

230249
handle.write(content.as_ref()).map_err(|err| {
231-
FileSystemError::IO(FileSystemIOError {
250+
Error::Io(IoError {
232251
path: path.to_path_buf(),
233252
context: FileSystemIOErrorContext::WriteFailure,
234253
uefi_error: err.to_err_without_payload(),
235254
})
236255
})?;
237256
handle.flush().map_err(|err| {
238-
FileSystemError::IO(FileSystemIOError {
257+
Error::Io(IoError {
239258
path: path.to_path_buf(),
240259
context: FileSystemIOErrorContext::FlushFailure,
241260
uefi_error: err,
@@ -247,7 +266,7 @@ impl<'a> FileSystem<'a> {
247266
/// Opens a fresh handle to the root directory of the volume.
248267
fn open_root(&mut self) -> FileSystemResult<UefiDirectoryHandle> {
249268
self.0.open_volume().map_err(|err| {
250-
FileSystemError::IO(FileSystemIOError {
269+
Error::Io(IoError {
251270
path: {
252271
let mut path = PathBuf::new();
253272
path.push(SEPARATOR_STR);
@@ -283,7 +302,7 @@ impl<'a> FileSystem<'a> {
283302
.open(path.to_cstr16(), mode, attr)
284303
.map_err(|err| {
285304
log::trace!("Can't open file {path}: {err:?}");
286-
FileSystemError::IO(FileSystemIOError {
305+
Error::Io(IoError {
287306
path: path.to_path_buf(),
288307
context: FileSystemIOErrorContext::OpenError,
289308
uefi_error: err,

uefi/src/fs/path/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ mod validation;
2020
pub use path::{Components, Path};
2121
pub use pathbuf::PathBuf;
2222

23-
use uefi::data_types::chars::NUL_16;
24-
use uefi::{CStr16, Char16};
23+
use crate::data_types::chars::NUL_16;
24+
use crate::{CStr16, Char16};
2525
pub(super) use validation::validate_path;
2626
pub use validation::PathError;
2727

uefi/src/fs/path/path.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
#![allow(clippy::module_inception)]
33

44
use crate::fs::path::{PathBuf, SEPARATOR};
5-
use crate::CStr16;
5+
use crate::{CStr16, CString16};
66
use core::fmt::{Display, Formatter};
7-
use uefi::CString16;
87

98
/// A path similar to the `Path` of the standard library, but based on
109
/// [`CStr16`] strings and [`SEPARATOR`] as separator.

0 commit comments

Comments
 (0)