Flush the aligned buffer to the file.
(&mut self)
| 386 | |
| 387 | /// Flush the aligned buffer to the file. |
| 388 | fn flush_buffer(&mut self) -> Result<()> { |
| 389 | if self.buffer.is_empty() { |
| 390 | return Ok(()); |
| 391 | } |
| 392 | |
| 393 | let data = if self.config.use_direct_io { |
| 394 | // O_DIRECT requires aligned I/O size. |
| 395 | self.buffer.as_aligned_slice() |
| 396 | } else { |
| 397 | // Without O_DIRECT, write only the actual data. |
| 398 | self.buffer.as_slice() |
| 399 | }; |
| 400 | |
| 401 | // Use pwrite to write at the exact offset, retrying on short writes. |
| 402 | #[cfg(unix)] |
| 403 | { |
| 404 | use std::os::unix::io::AsRawFd; |
| 405 | let fd = self.file.as_raw_fd(); |
| 406 | let mut remaining = data; |
| 407 | let mut write_offset = self.file_offset; |
| 408 | while !remaining.is_empty() { |
| 409 | let written = unsafe { |
| 410 | libc::pwrite( |
| 411 | fd, |
| 412 | remaining.as_ptr() as *const libc::c_void, |
| 413 | remaining.len(), |
| 414 | write_offset as libc::off_t, |
| 415 | ) |
| 416 | }; |
| 417 | if written < 0 { |
| 418 | return Err(WalError::Io(std::io::Error::last_os_error())); |
| 419 | } |
| 420 | let n = written as usize; |
| 421 | remaining = &remaining[n..]; |
| 422 | write_offset += n as u64; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | self.file_offset += data.len() as u64; |
| 427 | self.buffer.clear(); |
| 428 | Ok(()) |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | #[cfg(test)] |