aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / vendor / uefi / src / proto / media / file / dir.rs
| 1 | // SPDX-License-Identifier: MIT OR Apache-2.0 |
| 2 | |
| 3 | use super::{File, FileHandle, FileInfo, FromUefi, RegularFile}; |
| 4 | use crate::data_types::Align; |
| 5 | use crate::{Error, Result}; |
| 6 | #[cfg(feature = "alloc")] |
| 7 | use {crate::mem::make_boxed, alloc::boxed::Box}; |
| 8 | |
| 9 | /// A `FileHandle` that is also a directory. |
| 10 | /// |
| 11 | /// Use `File::into_type` or `Directory::new` to create a `Directory`. In |
| 12 | /// addition to supporting the normal `File` operations, `Directory` |
| 13 | /// supports iterating over its contained files. |
| 14 | #[repr(transparent)] |
| 15 | #[derive(Debug)] |
| 16 | pub struct Directory(RegularFile); |
| 17 | |
| 18 | impl Directory { |
| 19 | /// Coverts a `FileHandle` into a `Directory` without checking the file type. |
| 20 | /// # Safety |
| 21 | /// This function should only be called on files which ARE directories, |
| 22 | /// doing otherwise is unsafe. |
| 23 | #[must_use] |
| 24 | pub const unsafe fn new(handle: FileHandle) -> Self { |
| 25 | // SAFETY: The memory is valid. |
| 26 | Self(unsafe { RegularFile::new(handle) }) |
| 27 | } |
| 28 | |
| 29 | /// Read the next directory entry. |
| 30 | /// |
| 31 | /// Try to read the next directory entry into `buffer`. If the buffer is too small, report the |
| 32 | /// required buffer size as part of the error. If there are no more directory entries, return |
| 33 | /// an empty optional. |
| 34 | /// |
| 35 | /// The input buffer must be correctly aligned for a `FileInfo`. You can query the required |
| 36 | /// alignment through the `Align` trait (`<FileInfo as Align>::alignment()`). |
| 37 | /// |
| 38 | /// # Arguments |
| 39 | /// * `buffer` The target buffer of the read operation |
| 40 | /// |
| 41 | /// # Errors |
| 42 | /// |
| 43 | /// All errors come from calls to [`RegularFile::read`] or |
| 44 | /// [`FromUefi::from_uefi`]. Like in [`File::get_info`], the required buffer |
| 45 | /// size includes the trailing padding of `FileInfo`. |
| 46 | pub fn read_entry<'buf>( |
| 47 | &mut self, |
| 48 | buffer: &'buf mut [u8], |
| 49 | ) -> Result<Option<&'buf mut FileInfo>, Option<usize>> { |
| 50 | // Make sure that the storage is properly aligned |
| 51 | FileInfo::assert_aligned(buffer); |
| 52 | |
| 53 | // Read the directory entry into the aligned storage |
| 54 | let read_bytes = self.0.read_unchunked(buffer).map_err(|err| { |
| 55 | let (status, required_size) = err.split(); |
| 56 | Error::new(status, required_size.map(FileInfo::round_up_to_alignment)) |
| 57 | })?; |
| 58 | |
| 59 | // 0 read bytes signals that the last directory entry was read |
| 60 | if read_bytes == 0 { |
| 61 | return Ok(None); |
| 62 | } |
| 63 | FileInfo::from_uefi(buffer, read_bytes).map(Some) |
| 64 | } |
| 65 | |
| 66 | /// Wrapper around [`Self::read_entry`] that returns an owned copy of the data. It has the same |
| 67 | /// implications and requirements. On failure, the payload of `Err` is `()ยด. |
| 68 | #[cfg(feature = "alloc")] |
| 69 | pub fn read_entry_boxed(&mut self) -> Result<Option<Box<FileInfo>>> { |
| 70 | let read_entry_res = self.read_entry(&mut []); |
| 71 | |
| 72 | // If no more entries are available, return early. |
| 73 | if read_entry_res == Ok(None) { |
| 74 | return Ok(None); |
| 75 | } |
| 76 | |
| 77 | let fetch_data_fn = |buf| { |
| 78 | self.read_entry(buf) |
| 79 | // this is safe, as above, we checked that there are more entries |
| 80 | .map(|maybe_info: Option<&mut FileInfo>| { |
| 81 | maybe_info.expect("Should have more entries") |
| 82 | }) |
| 83 | }; |
| 84 | let file_info = make_boxed::<FileInfo, _>(fetch_data_fn)?; |
| 85 | Ok(Some(file_info)) |
| 86 | } |
| 87 | |
| 88 | /// Start over the process of enumerating directory entries |
| 89 | /// |
| 90 | /// # Errors |
| 91 | /// |
| 92 | /// All errors come from calls to [`RegularFile::set_position`]. |
| 93 | pub fn reset_entry_readout(&mut self) -> Result { |
| 94 | self.0.set_position(0) |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | impl File for Directory { |
| 99 | #[inline] |
| 100 | fn handle(&mut self) -> &mut FileHandle { |
| 101 | self.0.handle() |
| 102 | } |
| 103 | |
| 104 | fn is_regular_file(&self) -> Result<bool> { |
| 105 | Ok(false) |
| 106 | } |
| 107 | |
| 108 | fn is_directory(&self) -> Result<bool> { |
| 109 | Ok(true) |
| 110 | } |
| 111 | } |