Skip to content

Commit c924b1b

Browse files
committed
fcntl adding apple F_PREALLOCATE flag.
1 parent e599223 commit c924b1b

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

changelog/2393.added.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add `fcntl`'s `F_PREALLOCATE` constant for Apple targets.

src/fcntl.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,11 @@ pub enum FcntlArg<'a> {
800800
/// Issue an advisory read async with no copy to user
801801
#[cfg(apple_targets)]
802802
F_RDADVISE(libc::radvisory),
803+
/// Pre-allocate storage with different policies on fd.
804+
/// Note that we want a mutable reference for the OUT
805+
/// fstore_t field fst_bytesalloc.
806+
#[cfg(apple_targets)]
807+
F_PREALLOCATE(&'a mut libc::fstore_t),
803808
// TODO: Rest of flags
804809
}
805810

@@ -912,6 +917,10 @@ pub fn fcntl<Fd: std::os::fd::AsFd>(fd: Fd, arg: FcntlArg) -> Result<c_int> {
912917
F_RDADVISE(rad) => {
913918
libc::fcntl(fd, libc::F_RDADVISE, &rad)
914919
}
920+
#[cfg(apple_targets)]
921+
F_PREALLOCATE(st) => {
922+
libc::fcntl(fd, libc::F_PREALLOCATE, st)
923+
},
915924
}
916925
};
917926

test/test_fcntl.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,24 @@ fn test_f_get_path() {
581581
);
582582
}
583583

584+
#[cfg(apple_targets)]
585+
#[test]
586+
fn test_f_preallocate() {
587+
use nix::fcntl::*;
588+
589+
let tmp = NamedTempFile::new().unwrap();
590+
let mut st: libc::fstore_t = unsafe { std::mem::zeroed() };
591+
592+
st.fst_flags = libc::F_ALLOCATECONTIG as libc::c_uint;
593+
st.fst_posmode = libc::F_PEOFPOSMODE;
594+
st.fst_length = 1024;
595+
let res =
596+
fcntl(tmp, FcntlArg::F_PREALLOCATE(&mut st)).expect("preallocation failed");
597+
598+
assert_eq!(res, 0);
599+
assert!(st.fst_bytesalloc > 0);
600+
}
601+
584602
#[cfg(apple_targets)]
585603
#[test]
586604
fn test_f_get_path_nofirmlink() {

0 commit comments

Comments
 (0)