summaryrefslogtreecommitdiffstats
path: root/vendor/redox_syscall/src/scheme/seek.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:02:58 +0000
commit698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch)
tree173a775858bd501c378080a10dca74132f05bc50 /vendor/redox_syscall/src/scheme/seek.rs
parentInitial commit. (diff)
downloadrustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz
rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/redox_syscall/src/scheme/seek.rs')
-rw-r--r--vendor/redox_syscall/src/scheme/seek.rs33
1 files changed, 33 insertions, 0 deletions
diff --git a/vendor/redox_syscall/src/scheme/seek.rs b/vendor/redox_syscall/src/scheme/seek.rs
new file mode 100644
index 000000000..09a45186a
--- /dev/null
+++ b/vendor/redox_syscall/src/scheme/seek.rs
@@ -0,0 +1,33 @@
+use core::cmp;
+use core::convert::TryFrom;
+use crate::error::*;
+use crate::flag::*;
+
+/// Helper for seek calls
+/// In most cases it's easier to use a usize to track the offset and buffer size internally,
+/// but the seek interface uses isize. This wrapper ensures EOVERFLOW errors are returned
+/// as appropriate if the value in the usize can't fit in the isize.
+pub fn calc_seek_offset_usize(cur_offset: usize, pos: isize, whence: usize, buf_len: usize) -> Result<isize> {
+ let cur_offset = isize::try_from(cur_offset).or_else(|_| Err(Error::new(EOVERFLOW)))?;
+ let buf_len = isize::try_from(buf_len).or_else(|_| Err(Error::new(EOVERFLOW)))?;
+ calc_seek_offset_isize(cur_offset, pos, whence, buf_len)
+}
+
+/// Helper for seek calls
+/// Result is guaranteed to be positive.
+/// EOVERFLOW returned if the arguments would cause an overflow.
+/// EINVAL returned if the new offset is out of bounds.
+pub fn calc_seek_offset_isize(cur_offset: isize, pos: isize, whence: usize, buf_len: isize) -> Result<isize> {
+ let new_offset = match whence {
+ SEEK_CUR => pos.checked_add(cur_offset),
+ SEEK_END => pos.checked_add(buf_len),
+ SEEK_SET => Some(pos),
+ _ => None,
+ };
+
+ match new_offset {
+ Some(new_offset) if new_offset < 0 => Err(Error::new(EINVAL)),
+ Some(new_offset) => Ok(cmp::min(new_offset, buf_len)),
+ None => Err(Error::new(EOVERFLOW))
+ }
+} \ No newline at end of file