1
1
//! Module for [`FileSystem`].
2
2
3
- use crate :: fs:: * ;
4
- use crate :: fs:: { Path , PathBuf , UefiDirectoryIter , SEPARATOR_STR } ;
3
+ use crate :: fs:: { Path , PathBuf , UefiDirectoryIter , SEPARATOR_STR , * } ;
5
4
use crate :: table:: boot:: ScopedProtocol ;
5
+ use crate :: Status ;
6
6
use alloc:: boxed:: Box ;
7
7
use alloc:: string:: String ;
8
8
use alloc:: vec;
@@ -13,7 +13,7 @@ use core::ops::Deref;
13
13
use log:: debug;
14
14
15
15
/// Return type for public [`FileSystem`] operations.
16
- pub type FileSystemResult < T > = Result < T , FileSystemError > ;
16
+ pub type FileSystemResult < T > = Result < T , Error > ;
17
17
18
18
/// High-level file-system abstraction for UEFI volumes with an API that is
19
19
/// close to `std::fs`. It acts as convenient accessor around the
@@ -83,7 +83,7 @@ impl<'a> FileSystem<'a> {
83
83
let path = path. as_ref ( ) ;
84
84
let mut file = self . open ( path, UefiFileMode :: Read , false ) ?;
85
85
file. get_boxed_info ( ) . map_err ( |err| {
86
- FileSystemError :: IO ( FileSystemIOError {
86
+ Error :: Io ( IoError {
87
87
path : path. to_path_buf ( ) ,
88
88
context : FileSystemIOErrorContext :: Metadata ,
89
89
uefi_error : err,
@@ -98,11 +98,16 @@ impl<'a> FileSystem<'a> {
98
98
let mut file = self
99
99
. open ( path, UefiFileMode :: Read , false ) ?
100
100
. 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
+
104
109
let info = file. get_boxed_info :: < UefiFileInfo > ( ) . map_err ( |err| {
105
- FileSystemError :: IO ( FileSystemIOError {
110
+ Error :: Io ( IoError {
106
111
path : path. to_path_buf ( ) ,
107
112
context : FileSystemIOErrorContext :: Metadata ,
108
113
uefi_error : err,
@@ -111,7 +116,7 @@ impl<'a> FileSystem<'a> {
111
116
112
117
let mut vec = vec ! [ 0 ; info. file_size( ) as usize ] ;
113
118
let read_bytes = file. read ( vec. as_mut_slice ( ) ) . map_err ( |err| {
114
- FileSystemError :: IO ( FileSystemIOError {
119
+ Error :: Io ( IoError {
115
120
path : path. to_path_buf ( ) ,
116
121
context : FileSystemIOErrorContext :: ReadFailure ,
117
122
uefi_error : err. to_err_without_payload ( ) ,
@@ -132,15 +137,19 @@ impl<'a> FileSystem<'a> {
132
137
let dir = self
133
138
. open ( path, UefiFileMode :: Read , false ) ?
134
139
. 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
+ } ) ) ?;
138
147
Ok ( UefiDirectoryIter :: new ( dir) )
139
148
}
140
149
141
150
/// Read the entire contents of a file into a Rust string.
142
151
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 )
144
153
}
145
154
146
155
/// Removes an empty directory.
@@ -154,15 +163,21 @@ impl<'a> FileSystem<'a> {
154
163
155
164
match file {
156
165
UefiFileType :: Dir ( dir) => dir. delete ( ) . map_err ( |err| {
157
- FileSystemError :: IO ( FileSystemIOError {
166
+ Error :: Io ( IoError {
158
167
path : path. to_path_buf ( ) ,
159
168
context : FileSystemIOErrorContext :: CantDeleteDirectory ,
160
169
uefi_error : err,
161
170
} )
162
171
} ) ,
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
+ }
166
181
}
167
182
}
168
183
@@ -183,15 +198,19 @@ impl<'a> FileSystem<'a> {
183
198
184
199
match file {
185
200
UefiFileType :: Regular ( file) => file. delete ( ) . map_err ( |err| {
186
- FileSystemError :: IO ( FileSystemIOError {
201
+ Error :: Io ( IoError {
187
202
path : path. to_path_buf ( ) ,
188
203
context : FileSystemIOErrorContext :: CantDeleteFile ,
189
204
uefi_error : err,
190
205
} )
191
206
} ) ,
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
+ } ) ) ,
195
214
}
196
215
}
197
216
@@ -228,14 +247,14 @@ impl<'a> FileSystem<'a> {
228
247
. unwrap ( ) ;
229
248
230
249
handle. write ( content. as_ref ( ) ) . map_err ( |err| {
231
- FileSystemError :: IO ( FileSystemIOError {
250
+ Error :: Io ( IoError {
232
251
path : path. to_path_buf ( ) ,
233
252
context : FileSystemIOErrorContext :: WriteFailure ,
234
253
uefi_error : err. to_err_without_payload ( ) ,
235
254
} )
236
255
} ) ?;
237
256
handle. flush ( ) . map_err ( |err| {
238
- FileSystemError :: IO ( FileSystemIOError {
257
+ Error :: Io ( IoError {
239
258
path : path. to_path_buf ( ) ,
240
259
context : FileSystemIOErrorContext :: FlushFailure ,
241
260
uefi_error : err,
@@ -247,7 +266,7 @@ impl<'a> FileSystem<'a> {
247
266
/// Opens a fresh handle to the root directory of the volume.
248
267
fn open_root ( & mut self ) -> FileSystemResult < UefiDirectoryHandle > {
249
268
self . 0 . open_volume ( ) . map_err ( |err| {
250
- FileSystemError :: IO ( FileSystemIOError {
269
+ Error :: Io ( IoError {
251
270
path : {
252
271
let mut path = PathBuf :: new ( ) ;
253
272
path. push ( SEPARATOR_STR ) ;
@@ -283,7 +302,7 @@ impl<'a> FileSystem<'a> {
283
302
. open ( path. to_cstr16 ( ) , mode, attr)
284
303
. map_err ( |err| {
285
304
log:: trace!( "Can't open file {path}: {err:?}" ) ;
286
- FileSystemError :: IO ( FileSystemIOError {
305
+ Error :: Io ( IoError {
287
306
path : path. to_path_buf ( ) ,
288
307
context : FileSystemIOErrorContext :: OpenError ,
289
308
uefi_error : err,
0 commit comments