Preallocate disk space for a disk image file. Uses `fallocate()` to allocate all disk space upfront, ensuring storage availability and reducing fragmentation. Allocating all blocks upfront is more likely to place them contiguously than allocating on demand during random writes.
(file: &File, path: P)
| 423 | /// more likely to place them contiguously than allocating on demand during |
| 424 | /// random writes. |
| 425 | pub fn preallocate_disk<P: AsRef<Path>>(file: &File, path: P) { |
| 426 | let size = match file.metadata() { |
| 427 | Ok(m) => m.len(), |
| 428 | Err(e) => { |
| 429 | warn!("Failed to get metadata for {:?}: {}", path.as_ref(), e); |
| 430 | return; |
| 431 | } |
| 432 | }; |
| 433 | |
| 434 | if size == 0 { |
| 435 | return; |
| 436 | } |
| 437 | |
| 438 | // SAFETY: FFI call with valid file descriptor and size |
| 439 | let ret = unsafe { libc::fallocate(file.as_raw_fd(), 0, 0, size as libc::off_t) }; |
| 440 | |
| 441 | if ret != 0 { |
| 442 | warn!( |
| 443 | "Failed to preallocate disk space for {:?}: {}", |
| 444 | path.as_ref(), |
| 445 | io::Error::last_os_error() |
| 446 | ); |
| 447 | } else { |
| 448 | debug!( |
| 449 | "Preallocated {size} bytes for disk image {:?}", |
| 450 | path.as_ref() |
| 451 | ); |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | pub trait AsyncAdaptor { |
| 456 | fn read_vectored_sync( |