Skip to content

Commit

Permalink
fcntl adding apple F_PREALLOCATE flag. (#2393)
Browse files Browse the repository at this point in the history
  • Loading branch information
devnexen committed Sep 6, 2024
1 parent 99b2ad3 commit 66be012
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog/2393.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add `fcntl`'s `F_PREALLOCATE` constant for Apple targets.
13 changes: 11 additions & 2 deletions src/fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -919,7 +924,11 @@ pub fn fcntl<Fd: std::os::fd::AsFd>(fd: Fd, arg: FcntlArg) -> Result<c_int> {
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)
},
}
};

Expand Down
18 changes: 18 additions & 0 deletions test/test_fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 66be012

Please sign in to comment.