MCPcopy Create free account
hub / github.com/cloud-hypervisor/cloud-hypervisor / pread_exact

Function pread_exact

block/src/qcow_common.rs:26–47  ·  view source on GitHub ↗

Read exactly the requested bytes at offset, looping on short reads.

(fd: RawFd, buf: &mut [u8], offset: u64)

Source from the content-addressed store, hash-verified

24
25/// Read exactly the requested bytes at offset, looping on short reads.
26pub 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`.
50pub fn pread_alloc(fd: RawFd, offset: u64, len: usize) -> io::Result<Vec<u8>> {

Callers 11

scatter_read_syncMethod · 0.85
read_vectoredMethod · 0.85
pread_allocFunction · 0.85
aligned_preadFunction · 0.85
aligned_pwriteFunction · 0.85
read_atMethod · 0.85
read_clustersMethod · 0.85

Calls 2

as_mut_ptrMethod · 0.80
lenMethod · 0.45