Skip to content

Commit 3586a8c

Browse files
authored
Unrolled build for #150918
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 08f833a + 6878e73 commit 3586a8c

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
@@ -352,8 +352,24 @@ impl File {
352352
unsupported()
353353
}
354354

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

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

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

0 commit comments

Comments
 (0)