Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fcntl adding apple F_PREALLOCATE flag. #2393

Merged
merged 1 commit into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we shoud document the reason why a mutable reference is needed here? i.e., there is a OUT field in the associated sturcutre🤔

/// 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