Skip to content

Commit 2562a3a

Browse files
authored
Rollup merge of #150918 - uefi-fs-seek, r=jhpratt
std: sys: fs: uefi: Implement File::seek - Tested using OVMF on QEMU. @rustbot label +O-UEFI
2 parents 37890eb + 6878e73 commit 2562a3a

1 file changed

Lines changed: 24 additions & 2 deletions

File tree

library/std/src/sys/fs/uefi.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,24 @@ impl File {
353353
Ok(())
354354
}
355355

356-
pub fn seek(&self, _pos: SeekFrom) -> io::Result<u64> {
357-
unsupported()
356+
pub fn seek(&self, pos: SeekFrom) -> io::Result<u64> {
357+
const NEG_OFF_ERR: io::Error =
358+
io::const_error!(io::ErrorKind::InvalidInput, "cannot seek to negative offset.");
359+
360+
let off = match pos {
361+
SeekFrom::Start(p) => p,
362+
SeekFrom::End(p) => {
363+
// Seeking to position 0xFFFFFFFFFFFFFFFF causes the current position to be set to the end of the file.
364+
if p == 0 {
365+
0xFFFFFFFFFFFFFFFF
366+
} else {
367+
self.file_attr()?.size().checked_add_signed(p).ok_or(NEG_OFF_ERR)?
368+
}
369+
}
370+
SeekFrom::Current(p) => self.tell()?.checked_add_signed(p).ok_or(NEG_OFF_ERR)?,
371+
};
372+
373+
self.0.set_position(off).map(|_| off)
358374
}
359375

360376
pub fn size(&self) -> Option<io::Result<u64>> {
@@ -775,6 +791,12 @@ mod uefi_fs {
775791
if r.is_error() { Err(io::Error::from_raw_os_error(r.as_usize())) } else { Ok(pos) }
776792
}
777793

794+
pub(crate) fn set_position(&self, pos: u64) -> io::Result<()> {
795+
let file_ptr = self.protocol.as_ptr();
796+
let r = unsafe { ((*file_ptr).set_position)(file_ptr, pos) };
797+
if r.is_error() { Err(io::Error::from_raw_os_error(r.as_usize())) } else { Ok(()) }
798+
}
799+
778800
pub(crate) fn delete(self) -> io::Result<()> {
779801
let file_ptr = self.protocol.as_ptr();
780802
let r = unsafe { ((*file_ptr).delete)(file_ptr) };

0 commit comments

Comments
 (0)