Skip to content

Commit 8090630

Browse files
josephlrGabrielMajeri
authored andcommitted
Refactor File types to have common functionality use traits (#86)
* Refactor file types to use traits * Remove lifetime bounds
1 parent 134e21b commit 8090630

File tree

5 files changed

+238
-201
lines changed

5 files changed

+238
-201
lines changed

src/proto/media/file/dir.rs

Lines changed: 19 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,24 @@
1-
use super::{File, FileAttribute, FileInfo, FileMode, FileProtocolInfo, FromUefi};
1+
use super::{File, FileHandle, FileInfo, FromUefi, RegularFile};
22
use crate::data_types::Align;
33
use crate::prelude::*;
44
use crate::Result;
55
use core::ffi::c_void;
66

7-
/// `File` wrapper for handling directories
7+
/// A `FileHandle` that is also a directory.
88
///
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);
1214

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))
4322
}
4423

4524
/// Read the next directory entry
@@ -82,28 +61,11 @@ impl<'file> Directory<'file> {
8261
pub fn reset_entry_readout(&mut self) -> Result {
8362
self.0.set_position(0)
8463
}
64+
}
8565

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()
10870
}
10971
}

src/proto/media/file/info.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use core::mem;
99
use core::slice;
1010

1111
/// Common trait for data structures that can be used with
12-
/// `File::set_info()` or `File::set_info()`.
12+
/// `File::set_info()` or `File::get_info()`.
1313
///
1414
/// The long-winded name is needed because "FileInfo" is already taken by UEFI.
1515
pub trait FileProtocolInfo: Align + Identify + FromUefi {}

0 commit comments

Comments
 (0)