1 // Copyright 2022 The ChromiumOS Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 use std::ffi::c_void; 6 use std::io; 7 8 pub use winapi::um::winioctl::FSCTL_SET_SPARSE; 9 10 use crate::descriptor::AsRawDescriptor; 11 12 /// Marks the given file as sparse. Required if we want hole punching to be performant. 13 /// (If a file is not marked as sparse, a hole punch will just write zeros.) 14 /// # Safety 15 /// handle *must* be File. We accept all AsRawDescriptors for convenience. set_sparse_file<T: AsRawDescriptor>(handle: &T) -> io::Result<()>16pub fn set_sparse_file<T: AsRawDescriptor>(handle: &T) -> io::Result<()> { 17 // Safe because we check the return value and handle is guaranteed to be a 18 // valid file handle by the caller. 19 let result = unsafe { 20 super::super::ioctl::ioctl_with_ptr( 21 handle, 22 FSCTL_SET_SPARSE, 23 std::ptr::null_mut() as *mut c_void, 24 ) 25 }; 26 if result != 0 { 27 return Err(io::Error::from_raw_os_error(result)); 28 } 29 Ok(()) 30 } 31