Skip to content

Commit 66be012

Browse files
authored
fcntl adding apple F_PREALLOCATE flag. (#2393)
1 parent 99b2ad3 commit 66be012

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
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: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,12 @@ pub enum FcntlArg<'a> {
802802
F_RDADVISE(libc::radvisory),
803803
/// Turn read ahead off/on
804804
#[cfg(apple_targets)]
805-
F_RDAHEAD(bool)
805+
F_RDAHEAD(bool),
806+
/// Pre-allocate storage with different policies on fd.
807+
/// Note that we want a mutable reference for the OUT
808+
/// fstore_t field fst_bytesalloc.
809+
#[cfg(apple_targets)]
810+
F_PREALLOCATE(&'a mut libc::fstore_t),
806811
// TODO: Rest of flags
807812
}
808813

@@ -919,7 +924,11 @@ pub fn fcntl<Fd: std::os::fd::AsFd>(fd: Fd, arg: FcntlArg) -> Result<c_int> {
919924
F_RDAHEAD(on) => {
920925
let val = if on { 1 } else { 0 };
921926
libc::fcntl(fd, libc::F_RDAHEAD, val)
922-
}
927+
},
928+
#[cfg(apple_targets)]
929+
F_PREALLOCATE(st) => {
930+
libc::fcntl(fd, libc::F_PREALLOCATE, st)
931+
},
923932
}
924933
};
925934

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 = fcntl(tmp, FcntlArg::F_PREALLOCATE(&mut st))
596+
.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)