Read exactly the requested bytes at offset, looping on short reads.
(fd: RawFd, buf: &mut [u8], offset: u64)
| 24 | |
| 25 | /// Read exactly the requested bytes at offset, looping on short reads. |
| 26 | pub fn pread_exact(fd: RawFd, buf: &mut [u8], offset: u64) -> io::Result<()> { |
| 27 | let mut total = 0usize; |
| 28 | while total < buf.len() { |
| 29 | // SAFETY: buf and fd are valid for the lifetime of the call. |
| 30 | let ret = unsafe { |
| 31 | libc::pread64( |
| 32 | fd, |
| 33 | buf[total..].as_mut_ptr().cast(), |
| 34 | buf.len() - total, |
| 35 | (offset + total as u64) as libc::off_t, |
| 36 | ) |
| 37 | }; |
| 38 | if ret < 0 { |
| 39 | return Err(io::Error::last_os_error()); |
| 40 | } |
| 41 | if ret == 0 { |
| 42 | return Err(io::Error::from(io::ErrorKind::UnexpectedEof)); |
| 43 | } |
| 44 | total += ret as usize; |
| 45 | } |
| 46 | Ok(()) |
| 47 | } |
| 48 | |
| 49 | /// Allocate a buffer and pread exactly `len` bytes at `offset`. |
| 50 | pub fn pread_alloc(fd: RawFd, offset: u64, len: usize) -> io::Result<Vec<u8>> { |