Skip to content

Commit f127399

Browse files
committed
fs protocol: improved doc of read function
1 parent 35ddd85 commit f127399

File tree

3 files changed

+48
-24
lines changed

3 files changed

+48
-24
lines changed

src/proto/media/file/dir.rs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,17 @@ impl Directory {
2020
Self(RegularFile::new(handle))
2121
}
2222

23-
/// Read the next directory entry
23+
/// Read the next directory entry.
2424
///
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()`).
25+
/// Under the hood, this wraps [`super::RegularFile::read`]. Please refer to it's documentation.
3126
///
3227
/// # Arguments
3328
/// * `buffer` The target buffer of the read operation
3429
///
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.
30+
/// # Return Value
31+
/// This method returns a UEFI [`Result`]. In the success case, this method returns an `Option`
32+
/// with the file info (`None` if the directory contains no more entries) or, in the error case,
33+
/// the required buffer size, if the provided buffer was too small.
4234
pub fn read_entry<'buf>(
4335
&mut self,
4436
buffer: &'buf mut [u8],
@@ -47,8 +39,9 @@ impl Directory {
4739
FileInfo::assert_aligned(buffer);
4840

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

src/proto/media/file/regular.rs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,31 @@ 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.
2839
///
2940
/// # Arguments
3041
/// * `buffer` The target buffer of the read operation
3142
///
32-
/// # Errors
43+
/// # Return Value
44+
/// This method returns a UEFI [`Result`]. In the success case, this method returns the number
45+
/// of read bytes (0 is EOF) or, in the error case, with the required buffer size, if the
46+
/// provided buffer was too small.
47+
///
48+
/// ## Status Error Codes
3349
/// * `uefi::Status::NO_MEDIA` The device has no media
3450
/// * `uefi::Status::DEVICE_ERROR` The device reported an error, the file was deleted,
3551
/// or the end of the file was reached before the `read()`.
@@ -38,10 +54,16 @@ impl RegularFile {
3854
/// and the required buffer size is provided as output.
3955
pub fn read(&mut self, buffer: &mut [u8]) -> Result<usize, Option<usize>> {
4056
let mut buffer_size = buffer.len();
41-
unsafe { (self.imp().read)(self.imp(), &mut buffer_size, buffer.as_mut_ptr()) }.into_with(
57+
let status =
58+
unsafe { (self.imp().read)(self.imp(), &mut buffer_size, buffer.as_mut_ptr()) };
59+
60+
// Now, buffer_size is updated. It either contains the number of read bytes or the number
61+
// of required bytes to read the whole entry.
62+
status.into_with(
4263
|| buffer_size,
43-
|s| {
64+
|s: Status| {
4465
if s == Status::BUFFER_TOO_SMALL {
66+
// This was updated to the required buffer size.
4567
Some(buffer_size)
4668
} else {
4769
None

src/result/status.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ impl Status {
123123
}
124124

125125
/// Converts this status code into a result with a given value.
126+
///
127+
/// The status representing the specific error code is embedded into the error value of
128+
/// the [`Result`].
126129
#[inline]
127130
pub fn into_with_val<T>(self, val: impl FnOnce() -> T) -> Result<T, ()> {
128131
if self.is_success() {
@@ -132,7 +135,10 @@ impl Status {
132135
}
133136
}
134137

135-
/// Converts this status code into a result with a given error payload
138+
/// Converts this status code into a result with a given error payload.
139+
///
140+
/// The status representing the specific error code is embedded into the error value of
141+
/// the [`Result`].
136142
#[inline]
137143
pub fn into_with_err<ErrData: Debug>(
138144
self,
@@ -145,7 +151,10 @@ impl Status {
145151
}
146152
}
147153

148-
/// Convert this status code into a result with a given value and error payload
154+
/// Convert this status code into a result with a given value and error payload.
155+
///
156+
/// The status representing the specific error code is embedded into the error value of
157+
/// the [`Result`].
149158
#[inline]
150159
pub fn into_with<T, ErrData: Debug>(
151160
self,

0 commit comments

Comments
 (0)