Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the offsets for SeekFrom's Hole and Data unsigned. #1266

Merged
merged 2 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/backend/libc/fs/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1293,9 +1293,17 @@ pub(crate) fn seek(fd: BorrowedFd<'_>, pos: SeekFrom) -> io::Result<u64> {
SeekFrom::End(offset) => (c::SEEK_END, offset),
SeekFrom::Current(offset) => (c::SEEK_CUR, offset),
#[cfg(any(apple, freebsdlike, linux_kernel, solarish))]
SeekFrom::Data(offset) => (c::SEEK_DATA, offset),
SeekFrom::Data(pos) => {
let pos: u64 = pos;
// Silently cast; we'll get `EINVAL` if the value is negative.
(c::SEEK_DATA, pos as i64)
}
#[cfg(any(apple, freebsdlike, linux_kernel, solarish))]
SeekFrom::Hole(offset) => (c::SEEK_HOLE, offset),
SeekFrom::Hole(pos) => {
let pos: u64 = pos;
// Silently cast; we'll get `EINVAL` if the value is negative.
(c::SEEK_HOLE, pos as i64)
}
};

// ESP-IDF and Vita don't support 64-bit offsets.
Expand Down
12 changes: 10 additions & 2 deletions src/backend/linux_raw/fs/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,16 @@ pub(crate) fn seek(fd: BorrowedFd<'_>, pos: SeekFrom) -> io::Result<u64> {
}
SeekFrom::End(offset) => (SEEK_END, offset),
SeekFrom::Current(offset) => (SEEK_CUR, offset),
SeekFrom::Data(offset) => (SEEK_DATA, offset),
SeekFrom::Hole(offset) => (SEEK_HOLE, offset),
SeekFrom::Data(pos) => {
let pos: u64 = pos;
// Silently cast; we'll get `EINVAL` if the value is negative.
(SEEK_DATA, pos as i64)
}
SeekFrom::Hole(pos) => {
let pos: u64 = pos;
// Silently cast; we'll get `EINVAL` if the value is negative.
(SEEK_HOLE, pos as i64)
}
};
_seek(fd, offset, whence)
}
Expand Down
4 changes: 2 additions & 2 deletions src/fs/seek_from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ pub enum SeekFrom {
///
/// [`Errno::NXIO`]: crate::io::Errno::NXIO
#[cfg(any(apple, freebsdlike, linux_kernel, solarish))]
Data(i64),
Data(u64),

/// Sets the offset to the current position plus the specified number of
/// bytes, plus the distance to the next byte which is in a hole.
///
/// If there is no hole past the offset, it will be set to the end of the
/// file i.e. there is an implicit hole at the end of any file.
#[cfg(any(apple, freebsdlike, linux_kernel, solarish))]
Hole(i64),
Hole(u64),
}
11 changes: 4 additions & 7 deletions tests/fs/seek.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,15 @@ fn test_seek_holes() {
assert_eq!(seek(&file, SeekFrom::Start(0)), Ok(0));
assert_eq!(seek(&file, SeekFrom::Current(0)), Ok(0));
assert_eq!(seek(&file, SeekFrom::Hole(0)), Ok(hole_size));
assert_eq!(seek(&file, SeekFrom::Hole(hole_size as i64)), Ok(hole_size));
assert_eq!(seek(&file, SeekFrom::Hole(hole_size)), Ok(hole_size));
assert_eq!(
seek(&file, SeekFrom::Hole(hole_size as i64 * 2)),
seek(&file, SeekFrom::Hole(hole_size * 2)),
Ok(hole_size * 2 + 6)
);
assert_eq!(seek(&file, SeekFrom::Data(0)), Ok(0));
assert_eq!(seek(&file, SeekFrom::Data(hole_size)), Ok(hole_size * 2));
assert_eq!(
seek(&file, SeekFrom::Data(hole_size as i64)),
Ok(hole_size * 2)
);
assert_eq!(
seek(&file, SeekFrom::Data(hole_size as i64 * 2)),
seek(&file, SeekFrom::Data(hole_size * 2)),
Ok(hole_size * 2)
);
}
Expand Down
Loading