From 66be0128ba9b984e0402a57ded0a038b4efb8e7b Mon Sep 17 00:00:00 2001 From: David CARLIER Date: Fri, 6 Sep 2024 08:00:50 +0100 Subject: [PATCH] fcntl adding apple F_PREALLOCATE flag. (#2393) --- changelog/2393.added.md | 1 + src/fcntl.rs | 13 +++++++++++-- test/test_fcntl.rs | 18 ++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 changelog/2393.added.md diff --git a/changelog/2393.added.md b/changelog/2393.added.md new file mode 100644 index 0000000000..d0c2dd6288 --- /dev/null +++ b/changelog/2393.added.md @@ -0,0 +1 @@ +Add `fcntl`'s `F_PREALLOCATE` constant for Apple targets. diff --git a/src/fcntl.rs b/src/fcntl.rs index 7272a950c7..c104246882 100644 --- a/src/fcntl.rs +++ b/src/fcntl.rs @@ -802,7 +802,12 @@ pub enum FcntlArg<'a> { F_RDADVISE(libc::radvisory), /// Turn read ahead off/on #[cfg(apple_targets)] - F_RDAHEAD(bool) + F_RDAHEAD(bool), + /// Pre-allocate storage with different policies on fd. + /// Note that we want a mutable reference for the OUT + /// fstore_t field fst_bytesalloc. + #[cfg(apple_targets)] + F_PREALLOCATE(&'a mut libc::fstore_t), // TODO: Rest of flags } @@ -919,7 +924,11 @@ pub fn fcntl(fd: Fd, arg: FcntlArg) -> Result { F_RDAHEAD(on) => { let val = if on { 1 } else { 0 }; libc::fcntl(fd, libc::F_RDAHEAD, val) - } + }, + #[cfg(apple_targets)] + F_PREALLOCATE(st) => { + libc::fcntl(fd, libc::F_PREALLOCATE, st) + }, } }; diff --git a/test/test_fcntl.rs b/test/test_fcntl.rs index f8eddd3cde..c273e9acfb 100644 --- a/test/test_fcntl.rs +++ b/test/test_fcntl.rs @@ -581,6 +581,24 @@ fn test_f_get_path() { ); } +#[cfg(apple_targets)] +#[test] +fn test_f_preallocate() { + use nix::fcntl::*; + + let tmp = NamedTempFile::new().unwrap(); + let mut st: libc::fstore_t = unsafe { std::mem::zeroed() }; + + st.fst_flags = libc::F_ALLOCATECONTIG as libc::c_uint; + st.fst_posmode = libc::F_PEOFPOSMODE; + st.fst_length = 1024; + let res = fcntl(tmp, FcntlArg::F_PREALLOCATE(&mut st)) + .expect("preallocation failed"); + + assert_eq!(res, 0); + assert!(st.fst_bytesalloc > 0); +} + #[cfg(apple_targets)] #[test] fn test_f_get_path_nofirmlink() {