Write all bytes to fd at offset, looping on short writes.
(fd: RawFd, buf: &[u8], offset: u64)
| 75 | |
| 76 | /// Write all bytes to fd at offset, looping on short writes. |
| 77 | pub fn pwrite_all(fd: RawFd, buf: &[u8], offset: u64) -> io::Result<()> { |
| 78 | let mut total = 0usize; |
| 79 | while total < buf.len() { |
| 80 | // SAFETY: buf and fd are valid for the lifetime of the call. |
| 81 | let ret = unsafe { |
| 82 | libc::pwrite64( |
| 83 | fd, |
| 84 | buf[total..].as_ptr().cast(), |
| 85 | buf.len() - total, |
| 86 | (offset + total as u64) as libc::off_t, |
| 87 | ) |
| 88 | }; |
| 89 | if ret < 0 { |
| 90 | return Err(io::Error::last_os_error()); |
| 91 | } |
| 92 | if ret == 0 { |
| 93 | return Err(io::Error::other("pwrite64 wrote 0 bytes")); |
| 94 | } |
| 95 | total += ret as usize; |
| 96 | } |
| 97 | Ok(()) |
| 98 | } |
| 99 | |
| 100 | /// RAII wrapper for an aligned heap buffer required by O_DIRECT. |
| 101 | pub struct AlignedBuf { |
no test coverage detected