From 4814c1971d9f2f91666caa943286d297ce1b2e3d Mon Sep 17 00:00:00 2001 From: nym21 Date: Sat, 26 Jul 2025 23:35:23 +0200 Subject: [PATCH] vecs: add linux punch hole impl --- crates/brk_vecs/src/file/mod.rs | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/crates/brk_vecs/src/file/mod.rs b/crates/brk_vecs/src/file/mod.rs index d43eb9785..7570169f3 100644 --- a/crates/brk_vecs/src/file/mod.rs +++ b/crates/brk_vecs/src/file/mod.rs @@ -475,15 +475,15 @@ impl File { fn punch_hole(&self, start: u64, length: u64) -> Result<()> { let file = self.file.write(); - Self::punch_hole_macos(&file, start, length) + Self::punch_hole_impl(&file, start, length) } fn punch_hole_(file: &fs::File, start: u64, length: u64) -> Result<()> { - Self::punch_hole_macos(file, start, length) + Self::punch_hole_impl(file, start, length) } #[cfg(target_os = "macos")] - fn punch_hole_macos(file: &fs::File, start: u64, length: u64) -> Result<()> { + fn punch_hole_impl(file: &fs::File, start: u64, length: u64) -> Result<()> { let fpunchhole = FPunchhole { fp_flags: 0, reserved: 0, @@ -506,6 +506,32 @@ impl File { Ok(()) } + + #[cfg(target_os = "linux")] + fn punch_hole_impl(file: &fs::File, start: u64, length: u64) -> Result<()> { + let result = unsafe { + libc::fallocate( + file.as_raw_fd(), + libc::FALLOC_FL_PUNCH_HOLE | libc::FALLOC_FL_KEEP_SIZE, + start as libc::off_t, + length as libc::off_t, + ) + }; + + if result == -1 { + let err = std::io::Error::last_os_error(); + return Err(Error::String(format!("Failed to punch hole: {err}"))); + } + + Ok(()) + } + + #[cfg(not(any(target_os = "macos", target_os = "linux")))] + fn punch_hole_impl(_file: &fs::File, _start: u64, _length: u64) -> Result<()> { + Err(Error::String( + "Hole punching not supported on this platform".to_string(), + )) + } } #[repr(C)]