Skip to content

Commit 23cfb4b

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

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>> {
@@ -756,6 +772,12 @@ mod uefi_fs {
756772
if r.is_error() { Err(io::Error::from_raw_os_error(r.as_usize())) } else { Ok(pos) }
757773
}
758774

775+
pub(crate) fn set_position(&self, pos: u64) -> io::Result<()> {
776+
let file_ptr = self.protocol.as_ptr();
777+
let r = unsafe { ((*file_ptr).set_position)(file_ptr, pos) };
778+
if r.is_error() { Err(io::Error::from_raw_os_error(r.as_usize())) } else { Ok(()) }
779+
}
780+
759781
pub(crate) fn delete(self) -> io::Result<()> {
760782
let file_ptr = self.protocol.as_ptr();
761783
let r = unsafe { ((*file_ptr).delete)(file_ptr) };

0 commit comments

Comments
 (0)