1
+ //! This module provides the `File` structure, representing an opaque handle to a
2
+ //! directory / file, as well as providing functions for opening new files.
3
+ //!
4
+ //! Usually a file system implementation will return a "root" file, representing
5
+ //! `/` on that volume, and with that file it is possible to enumerate and open
6
+ //! all the other files on that volume.
7
+
1
8
use bitflags:: bitflags;
9
+ use core:: mem;
2
10
use crate :: { Result , Status } ;
3
11
use ucs2;
4
12
5
- /// A file represents an abstraction of some contiguous block of data residing on a volume.
13
+ /// A file represents an abstraction of some contiguous block of data residing
14
+ /// on a volume.
15
+ ///
16
+ /// Dropping this structure will result in the file handle being closed.
6
17
///
7
18
/// Files have names, and a fixed size.
8
19
pub struct File < ' a > {
@@ -59,19 +70,19 @@ impl<'a> File<'a> {
59
70
}
60
71
}
61
72
62
- /// Close this file handle
63
- ///
64
- /// This MUST be called when you are done with the file
65
- pub fn close ( self ) -> Result < ( ) > {
66
- ( self . inner . close ) ( self . inner ) . into ( )
67
- }
73
+ /// Close this file handle. Same as dropping this structure.
74
+ pub fn close ( self ) { }
68
75
69
76
/// Closes and deletes this file
70
77
///
71
78
/// # Errors
72
79
/// * `uefi::Status::WARN_DELETE_FAILURE` The file was closed, but deletion failed
73
80
pub fn delete ( self ) -> Result < ( ) > {
74
- ( self . inner . delete ) ( self . inner ) . into ( )
81
+ let result = ( self . inner . delete ) ( self . inner ) . into ( ) ;
82
+
83
+ mem:: forget ( self ) ;
84
+
85
+ result
75
86
}
76
87
77
88
/// Read data from file
@@ -148,6 +159,14 @@ impl<'a> File<'a> {
148
159
}
149
160
}
150
161
162
+ impl < ' a > Drop for File < ' a > {
163
+ fn drop ( & mut self ) {
164
+ let result: Result < ( ) > = ( self . inner . close ) ( self . inner ) . into ( ) ;
165
+ // The spec says this always succeeds.
166
+ result. expect ( "Failed to close file" ) ;
167
+ }
168
+ }
169
+
151
170
/// The function pointer table for the File protocol.
152
171
#[ repr( C ) ]
153
172
struct FileImpl {
@@ -173,20 +192,31 @@ struct FileImpl {
173
192
}
174
193
175
194
bitflags ! {
195
+ /// Usage flags describing what is possible to do with the file.
176
196
pub struct FileMode : u64 {
197
+ /// The file can be read from.
177
198
const READ = 1 ;
199
+ /// The file can be written to.
178
200
const WRITE = 1 << 1 ;
201
+ /// The file will be created if not found.
179
202
const CREATE = 1 << 63 ;
180
203
}
181
204
}
182
205
183
206
bitflags ! {
207
+ /// Attributes describing the properties of a file on the file system.
184
208
pub struct FileAttribute : u64 {
209
+ /// File can only be opened in [`FileMode::READ`] mode.
185
210
const READ_ONLY = 1 ;
211
+ /// Hidden file, not normally visible to the user.
186
212
const HIDDEN = 1 << 1 ;
213
+ /// System file, indicates this file is an internal operating system file.
187
214
const SYSTEM = 1 << 2 ;
215
+ /// This file is a directory.
188
216
const DIRECTORY = 1 << 4 ;
217
+ /// This file is compressed.
189
218
const ARCHIVE = 1 << 5 ;
219
+ /// Mask combining all the valid attributes.
190
220
const VALID_ATTR = 0x37 ;
191
221
}
192
222
}
0 commit comments