Skip to content

Commit 8bd739d

Browse files
committed
fs_proto: improved doc of read function and better return type
1 parent 35ddd85 commit 8bd739d

File tree

3 files changed

+44
-27
lines changed

3 files changed

+44
-27
lines changed

src/proto/media/file/dir.rs

+11-17
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use super::{File, FileHandle, FileInfo, FromUefi, RegularFile};
22
use crate::data_types::Align;
33
use crate::Result;
44
use core::ffi::c_void;
5+
use uefi::Status;
56

67
/// A `FileHandle` that is also a directory.
78
///
@@ -20,35 +21,28 @@ impl Directory {
2021
Self(RegularFile::new(handle))
2122
}
2223

23-
/// Read the next directory entry
24+
/// Read the next directory entry.
2425
///
25-
/// Try to read the next directory entry into `buffer`. If the buffer is too small, report the
26-
/// required buffer size as part of the error. If there are no more directory entries, return
27-
/// an empty optional.
28-
///
29-
/// The input buffer must be correctly aligned for a `FileInfo`. You can query the required
30-
/// alignment through the `Align` trait (`<FileInfo as Align>::alignment()`).
26+
/// Under the hood, this wraps [`super::RegularFile::read`]. Please refer to it's documentation.
3127
///
3228
/// # Arguments
3329
/// * `buffer` The target buffer of the read operation
3430
///
35-
/// # Errors
36-
/// * `uefi::Status::NO_MEDIA` The device has no media
37-
/// * `uefi::Status::DEVICE_ERROR` The device reported an error, the file was deleted,
38-
/// or the end of the file was reached before the `read()`.
39-
/// * `uefi::Status::VOLUME_CORRUPTED` The filesystem structures are corrupted
40-
/// * `uefi::Status::BUFFER_TOO_SMALL` The buffer is too small to hold a directory entry,
41-
/// the required buffer size is provided into the error.
31+
/// # Return Value
32+
/// This either returns some file info or nothing (if the directory contains no more entries)
33+
/// success case or a tuple of the UEFI status and the required buffer size, if the buffer was
34+
/// too small.
4235
pub fn read_entry<'buf>(
4336
&mut self,
4437
buffer: &'buf mut [u8],
45-
) -> Result<Option<&'buf mut FileInfo>, Option<usize>> {
38+
) -> Result<Option<&'buf mut FileInfo>, (Status, Option<usize>)> {
4639
// Make sure that the storage is properly aligned
4740
FileInfo::assert_aligned(buffer);
4841

4942
// Read the directory entry into the aligned storage
50-
self.0.read(buffer).map(|size| {
51-
if size != 0 {
43+
self.0.read(buffer).map(|read_bytes| {
44+
// 0 signalizes that the last directory entry was read
45+
if read_bytes != 0 {
5246
unsafe { Some(FileInfo::from_uefi(buffer.as_mut_ptr().cast::<c_void>())) }
5347
} else {
5448
None

src/proto/media/file/regular.rs

+32-9
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,54 @@ impl RegularFile {
2121
Self(handle)
2222
}
2323

24-
/// Read data from file
24+
/// Read data from a file, hence, a regular file or a directory.
2525
///
26-
/// Try to read as much as possible into `buffer`. Returns the number of bytes that were
27-
/// actually read.
26+
/// # Read from Regular Files
27+
/// If `self` is not a directory, the function reads the requested number of bytes from the file
28+
/// at the file’s current position and returns them in `buffer`. If the read goes beyond the end
29+
/// of the file, the read length is truncated to the end of the file. The file’s current
30+
/// position is increased by the number of bytes returned.
31+
///
32+
/// # Read from Directory
33+
/// If `self` is a directory, the function reads the directory entry at the file’s current
34+
/// position and returns the entry in `buffer`. If the `buffer` is not large enough to hold the
35+
/// current directory entry, then `EFI_BUFFER_TOO_SMALL` is returned and the current file
36+
/// position is not updated. `buffer_size` is set to be the size of the buffer needed to read
37+
/// the entry. On success, the current position is updated to the next directory entry. If there
38+
/// are no more directory entries, the read returns a zero-length buffer. `EFI_FILE_INFO` is the
39+
/// structure returned as the directory entry.
2840
///
2941
/// # Arguments
3042
/// * `buffer` The target buffer of the read operation
3143
///
32-
/// # Errors
44+
/// # Return Value
45+
/// This either returns the size of read bytes in the success case or a tuple of the UEFI
46+
/// status and the required buffer size, if the buffer was too small. The status code can
47+
/// signalize one of the following errors:
48+
///
49+
/// ## Status Error Codes
3350
/// * `uefi::Status::NO_MEDIA` The device has no media
3451
/// * `uefi::Status::DEVICE_ERROR` The device reported an error, the file was deleted,
3552
/// or the end of the file was reached before the `read()`.
3653
/// * `uefi::Status::VOLUME_CORRUPTED` The filesystem structures are corrupted
3754
/// * `uefi::Status::BUFFER_TOO_SMALL` The buffer is too small to hold a directory entry,
3855
/// and the required buffer size is provided as output.
39-
pub fn read(&mut self, buffer: &mut [u8]) -> Result<usize, Option<usize>> {
56+
pub fn read(&mut self, buffer: &mut [u8]) -> Result<usize, (Status, Option<usize>)> {
4057
let mut buffer_size = buffer.len();
41-
unsafe { (self.imp().read)(self.imp(), &mut buffer_size, buffer.as_mut_ptr()) }.into_with(
58+
let status =
59+
unsafe { (self.imp().read)(self.imp(), &mut buffer_size, buffer.as_mut_ptr()) };
60+
61+
// Now, buffer_size is updated. It either contains the number of read bytes or the number
62+
// of required bytes to read the whole entry.
63+
status.into_with(
4264
|| buffer_size,
43-
|s| {
44-
if s == Status::BUFFER_TOO_SMALL {
65+
|s: Status| {
66+
let required_buffer_size = if s == Status::BUFFER_TOO_SMALL {
4567
Some(buffer_size)
4668
} else {
4769
None
48-
}
70+
};
71+
(status, required_buffer_size)
4972
},
5073
)
5174
}

uefi-test-runner/src/proto/media/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub fn test(bt: &BootServices) {
4545
}
4646
Err(error) => {
4747
// Buffer is not big enough, allocate a bigger one and try again.
48-
let min_size = error.data().unwrap();
48+
let min_size = error.data().1.unwrap();
4949
buffer.resize(min_size, 0);
5050
continue;
5151
}

0 commit comments

Comments
 (0)