Skip to content

Unix: Add read_buf_at and read_buf_exact_at to FileExt #122887

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

Closed
wants to merge 1 commit into from
Closed
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
61 changes: 59 additions & 2 deletions library/std/src/os/unix/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ pub trait FileExt {
match self.read_at(buf, offset) {
Ok(0) => break,
Ok(n) => {
let tmp = buf;
buf = &mut tmp[n..];
buf = &mut buf[n..];
offset += n as u64;
}
Err(ref e) if e.is_interrupted() => {}
Expand All @@ -134,6 +133,61 @@ pub trait FileExt {
}
}

/// Reads a number of bytes starting from a given offset.
///
/// This is equivalent to the [`read_at`](FileExt::read_at) method, except
/// that it is passed a [`BorrowedCursor`] rather than `[u8]` to allow use
/// with uninitialized buffers. The new data will be appended to any
/// existing contents of `buf`.
///
/// The offset is relative to the start of the file and thus independent
/// from the current cursor.
///
/// The current file cursor is not affected by this function.
///
/// Note that similar to [`File::read_buf`], it is not an error to return with a
/// short read.
///
/// [`File::read_buf`]: fs::File::read_buf
/// [`BorrowedCursor`]: io::BorrowedCursor
#[unstable(feature = "read_buf", issue = "78485")]
fn read_buf_at(&self, mut cursor: io::BorrowedCursor<'_>, offset: u64) -> io::Result<()> {
let n = self.read_at(cursor.ensure_init().init_mut(), offset)?;
cursor.advance(n);
Ok(())
}

/// Reads the exact number of bytes required to fill `buf` from the given offset.
///
/// The offset is relative to the start of the file and thus independent
/// from the current cursor.
///
/// The current file cursor is not affected by this function.
#[unstable(feature = "read_buf", issue = "78485")]
fn read_buf_exact_at(
&self,
mut cursor: io::BorrowedCursor<'_>,
mut offset: u64,
) -> io::Result<()> {
while cursor.capacity() > 0 {
let prev_written = cursor.written();
match self.read_buf_at(cursor.reborrow(), offset) {
Ok(()) => offset += (cursor.written() - prev_written) as u64,
Err(e) if e.is_interrupted() => continue,
Err(e) => return Err(e),
}

if cursor.written() == prev_written {
return Err(io::const_io_error!(
io::ErrorKind::UnexpectedEof,
"failed to fill whole buffer"
));
}
}

Ok(())
}

/// Writes a number of bytes starting from a given offset.
///
/// Returns the number of bytes written.
Expand Down Expand Up @@ -271,6 +325,9 @@ impl FileExt for fs::File {
fn read_at(&self, buf: &mut [u8], offset: u64) -> io::Result<usize> {
self.as_inner().read_at(buf, offset)
}
fn read_buf_at(&self, cursor: io::BorrowedCursor<'_>, offset: u64) -> io::Result<()> {
self.as_inner().read_buf_at(cursor, offset)
}
fn read_vectored_at(&self, bufs: &mut [io::IoSliceMut<'_>], offset: u64) -> io::Result<usize> {
self.as_inner().read_vectored_at(bufs, offset)
}
Expand Down
30 changes: 30 additions & 0 deletions library/std/src/sys/pal/unix/fd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,36 @@ impl FileDesc {
Ok(())
}

pub fn read_buf_at(&self, mut cursor: BorrowedCursor<'_>, offset: u64) -> io::Result<()> {
#[cfg(not(any(
all(target_os = "linux", not(target_env = "musl")),
target_os = "android",
target_os = "hurd"
)))]
use libc::pread as pread64;
#[cfg(any(
all(target_os = "linux", not(target_env = "musl")),
target_os = "android",
target_os = "hurd"
))]
use libc::pread64;

let ret = cvt(unsafe {
pread64(
self.as_raw_fd(),
cursor.as_mut().as_mut_ptr() as *mut libc::c_void,
cmp::min(cursor.capacity(), READ_LIMIT),
offset as off64_t,
)
})?;

// Safety: `ret` bytes were written to the initialized portion of the buffer
unsafe {
cursor.advance_unchecked(ret as usize);
}
Ok(())
}

#[cfg(any(
target_os = "emscripten",
target_os = "freebsd",
Expand Down
4 changes: 4 additions & 0 deletions library/std/src/sys/pal/unix/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1247,6 +1247,10 @@ impl File {
self.0.read_buf(cursor)
}

pub fn read_buf_at(&self, cursor: BorrowedCursor<'_>, offset: u64) -> io::Result<()> {
self.0.read_buf_at(cursor, offset)
}

pub fn read_vectored_at(&self, bufs: &mut [IoSliceMut<'_>], offset: u64) -> io::Result<usize> {
self.0.read_vectored_at(bufs, offset)
}
Expand Down
Loading