about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <476013+matthiaskrgr@users.noreply.github.com>2025-03-10 09:32:13 +0100
committerGitHub <noreply@github.com>2025-03-10 09:32:13 +0100
commit2f1908df61469fad9b09507d2b8e1585438700d3 (patch)
treedddd9afd2a4fa8e1822efff24362b1137a0c0545
parentcbde8b9dcfb2017b1265da4a92cec640b0065d6d (diff)
parent1abaacd375bf74b3dc01fb578d8064771e327805 (diff)
downloadrust-2f1908df61469fad9b09507d2b8e1585438700d3.tar.gz
rust-2f1908df61469fad9b09507d2b8e1585438700d3.zip
Rollup merge of #138074 - thaliaarchi:hermit-seek, r=ChrisDenton
Support `File::seek` for Hermit

`lseek` was added in `hermit-abi` in commit [87dd201](https://github.com/hermit-os/hermit-rs/commit/87dd201a14ac7661e1a4b761273e24d750496286) (add missing interface for lseek, 2024-07-15), which was just released in version 0.5.0.

cc ``@mkroening,`` ``@stlankes``

Fixes https://github.com/hermit-os/hermit-rs/issues/652
-rw-r--r--library/Cargo.lock4
-rw-r--r--library/std/Cargo.toml2
-rw-r--r--library/std/src/sys/fs/hermit.rs6
-rw-r--r--library/std/src/sys/pal/hermit/fd.rs21
4 files changed, 25 insertions, 8 deletions
diff --git a/library/Cargo.lock b/library/Cargo.lock
index 405c69d9568..8f174f28472 100644
--- a/library/Cargo.lock
+++ b/library/Cargo.lock
@@ -146,9 +146,9 @@ dependencies = [
 
 [[package]]
 name = "hermit-abi"
-version = "0.4.0"
+version = "0.5.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "fbf6a919d6cf397374f7dfeeea91d974c7c0a7221d0d0f4f20d859d329e53fcc"
+checksum = "fbd780fe5cc30f81464441920d82ac8740e2e46b29a6fad543ddd075229ce37e"
 dependencies = [
  "compiler_builtins",
  "rustc-std-workspace-alloc",
diff --git a/library/std/Cargo.toml b/library/std/Cargo.toml
index 0ec167c2d16..f7379c413f1 100644
--- a/library/std/Cargo.toml
+++ b/library/std/Cargo.toml
@@ -73,7 +73,7 @@ fortanix-sgx-abi = { version = "0.5.0", features = [
 ], public = true }
 
 [target.'cfg(target_os = "hermit")'.dependencies]
-hermit-abi = { version = "0.4.0", features = [
+hermit-abi = { version = "0.5.0", features = [
     'rustc-dep-of-std',
 ], public = true }
 
diff --git a/library/std/src/sys/fs/hermit.rs b/library/std/src/sys/fs/hermit.rs
index e9339ff261c..1191e335daa 100644
--- a/library/std/src/sys/fs/hermit.rs
+++ b/library/std/src/sys/fs/hermit.rs
@@ -417,12 +417,12 @@ impl File {
         Ok(())
     }
 
-    pub fn seek(&self, _pos: SeekFrom) -> io::Result<u64> {
-        Err(Error::from_raw_os_error(22))
+    pub fn seek(&self, pos: SeekFrom) -> io::Result<u64> {
+        self.0.seek(pos)
     }
 
     pub fn tell(&self) -> io::Result<u64> {
-        self.seek(SeekFrom::Current(0))
+        self.0.tell()
     }
 
     pub fn duplicate(&self) -> io::Result<File> {
diff --git a/library/std/src/sys/pal/hermit/fd.rs b/library/std/src/sys/pal/hermit/fd.rs
index 79fc13bd4a8..3d6b99cd77b 100644
--- a/library/std/src/sys/pal/hermit/fd.rs
+++ b/library/std/src/sys/pal/hermit/fd.rs
@@ -2,8 +2,8 @@
 
 use super::hermit_abi;
 use crate::cmp;
-use crate::io::{self, IoSlice, IoSliceMut, Read};
-use crate::os::hermit::io::{FromRawFd, OwnedFd, RawFd, *};
+use crate::io::{self, IoSlice, IoSliceMut, Read, SeekFrom};
+use crate::os::hermit::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
 use crate::sys::{cvt, unsupported};
 use crate::sys_common::{AsInner, FromInner, IntoInner};
 
@@ -66,9 +66,26 @@ impl FileDesc {
         true
     }
 
+    pub fn seek(&self, pos: SeekFrom) -> io::Result<u64> {
+        let (whence, pos) = match pos {
+            // Casting to `i64` is fine, too large values will end up as
+            // negative which will cause an error in `lseek`.
+            SeekFrom::Start(off) => (hermit_abi::SEEK_SET, off as i64),
+            SeekFrom::End(off) => (hermit_abi::SEEK_END, off),
+            SeekFrom::Current(off) => (hermit_abi::SEEK_CUR, off),
+        };
+        let n = cvt(unsafe { hermit_abi::lseek(self.as_raw_fd(), pos as isize, whence) })?;
+        Ok(n as u64)
+    }
+
+    pub fn tell(&self) -> io::Result<u64> {
+        self.seek(SeekFrom::Current(0))
+    }
+
     pub fn duplicate(&self) -> io::Result<FileDesc> {
         self.duplicate_path(&[])
     }
+
     pub fn duplicate_path(&self, _path: &[u8]) -> io::Result<FileDesc> {
         unsupported()
     }