|
1 |
| -use super::{File, FileAttribute, FileInfo, FileMode, FileProtocolInfo, FromUefi}; |
| 1 | +use super::{File, FileHandle, FileInfo, FromUefi, RegularFile}; |
2 | 2 | use crate::data_types::Align;
|
3 | 3 | use crate::prelude::*;
|
4 | 4 | use crate::Result;
|
5 | 5 | use core::ffi::c_void;
|
6 | 6 |
|
7 |
| -/// `File` wrapper for handling directories |
| 7 | +/// A `FileHandle` that is also a directory. |
8 | 8 | ///
|
9 |
| -/// The `File` abstraction can handle directories, but does so in a very roundabout way. |
10 |
| -/// A dedicated abstraction for directory handling is therefore desirable. |
11 |
| -pub struct Directory<'file>(File<'file>); |
| 9 | +/// Use `File::into_type` or `Directory::new` to create a `Directory`. In |
| 10 | +/// addition to supporting the normal `File` operations, `Directory` |
| 11 | +/// supports iterating over its contained files. |
| 12 | +#[repr(transparent)] |
| 13 | +pub struct Directory(RegularFile); |
12 | 14 |
|
13 |
| -impl<'file> Directory<'file> { |
14 |
| - /// Wrap a File handle into a Directory |
15 |
| - /// |
16 |
| - /// You should have made sure that the file is indeed a directory beforehand, using |
17 |
| - /// `file.get_info<FileInfo>(...)`. We cannot do it for you because this requires an unbounded |
18 |
| - /// amount of memory and we refrain from calling the UEFI allocator implicitly. |
19 |
| - pub unsafe fn from_file(file: File<'file>) -> Self { |
20 |
| - Directory(file) |
21 |
| - } |
22 |
| - |
23 |
| - /// Try to open a file relative to this directory. |
24 |
| - /// |
25 |
| - /// This simply forwards to the underlying `File::open` implementation |
26 |
| - pub fn open( |
27 |
| - &mut self, |
28 |
| - filename: &str, |
29 |
| - open_mode: FileMode, |
30 |
| - attributes: FileAttribute, |
31 |
| - ) -> Result<File> { |
32 |
| - self.0.open(filename, open_mode, attributes) |
33 |
| - } |
34 |
| - |
35 |
| - /// Close this directory handle. Same as dropping this structure. |
36 |
| - pub fn close(self) {} |
37 |
| - |
38 |
| - /// Closes and deletes this directory |
39 |
| - /// |
40 |
| - /// This simply forwards to the underlying `File::delete` implementation |
41 |
| - pub fn delete(self) -> Result { |
42 |
| - self.0.delete() |
| 15 | +impl Directory { |
| 16 | + /// Coverts a `FileHandle` into a `Directory` without checking the file type. |
| 17 | + /// # Safety |
| 18 | + /// This function should only be called on files which ARE directories, |
| 19 | + /// doing otherwise is unsafe. |
| 20 | + pub unsafe fn new(handle: FileHandle) -> Self { |
| 21 | + Self(RegularFile::new(handle)) |
43 | 22 | }
|
44 | 23 |
|
45 | 24 | /// Read the next directory entry
|
@@ -82,28 +61,11 @@ impl<'file> Directory<'file> {
|
82 | 61 | pub fn reset_entry_readout(&mut self) -> Result {
|
83 | 62 | self.0.set_position(0)
|
84 | 63 | }
|
| 64 | +} |
85 | 65 |
|
86 |
| - /// Queries some information about a directory |
87 |
| - /// |
88 |
| - /// This simply forwards to the underlying `File::get_info` implementation |
89 |
| - pub fn get_info<'buf, Info: FileProtocolInfo + ?Sized>( |
90 |
| - &mut self, |
91 |
| - buffer: &'buf mut [u8], |
92 |
| - ) -> Result<&'buf mut Info, Option<usize>> { |
93 |
| - self.0.get_info::<Info>(buffer) |
94 |
| - } |
95 |
| - |
96 |
| - /// Sets some information about a directory |
97 |
| - /// |
98 |
| - /// This simply forwards to the underlying `File::set_info` implementation |
99 |
| - pub fn set_info<Info: FileProtocolInfo + ?Sized>(&mut self, info: &Info) -> Result { |
100 |
| - self.0.set_info(info) |
101 |
| - } |
102 |
| - |
103 |
| - /// Flushes all modified data associated with the directory to the device |
104 |
| - /// |
105 |
| - /// This simply forwards to the underlying `File::flush` implementation |
106 |
| - pub fn flush(&mut self) -> Result { |
107 |
| - self.0.flush() |
| 66 | +impl File for Directory { |
| 67 | + #[inline] |
| 68 | + fn handle(&mut self) -> &mut FileHandle { |
| 69 | + self.0.handle() |
108 | 70 | }
|
109 | 71 | }
|
0 commit comments