Skip to content

Commit

Permalink
nix::fcntl add FcntlArg::F_READAHEAD for freebsd.
Browse files Browse the repository at this point in the history
Set/clear the amount of read ahead for the file descriptor.
  • Loading branch information
devnexen committed Dec 24, 2024
1 parent 5ef78a8 commit ad7bd99
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/2569.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added `FcntlArg::F_READAHEAD` for FreeBSD target
9 changes: 9 additions & 0 deletions src/fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,11 @@ pub enum FcntlArg<'a> {
/// Both descriptors must reference regular files in the same volume.
#[cfg(apple_targets)]
F_TRANSFEREXTENTS(RawFd),
/// Set or clear the read ahead (pre-fetch) amount for sequential access or
/// disable it with 0 or to system default for any value < 0.
/// It manages how the kernel caches file data.
#[cfg(target_os = "freebsd")]
F_READAHEAD(c_int),
// TODO: Rest of flags
}

Expand Down Expand Up @@ -962,6 +967,10 @@ pub fn fcntl<Fd: std::os::fd::AsFd>(fd: Fd, arg: FcntlArg) -> Result<c_int> {
F_TRANSFEREXTENTS(rawfd) => {
libc::fcntl(fd, libc::F_TRANSFEREXTENTS, rawfd)
},
#[cfg(target_os = "freebsd")]
F_READAHEAD(val) => {
libc::fcntl(fd, libc::F_READAHEAD, val)
},
}
};

Expand Down
13 changes: 13 additions & 0 deletions test/test_fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -805,3 +805,16 @@ fn test_f_transferextents() {
.expect("transferextents failed");
assert_ne!(res, -1);
}

#[cfg(target_os = "freebsd")]
#[test]
fn test_f_readahead() {
use nix::fcntl::*;

let tmp = NamedTempFile::new().unwrap();
let mut res = fcntl(&tmp, FcntlArg::F_READAHEAD(1_000_000))
.expect("read ahead failed");
assert_ne!(res, -1);
res = fcntl(&tmp, FcntlArg::F_READAHEAD(-1024)).expect("read ahead failed");
assert_ne!(res, -1);
}

0 comments on commit ad7bd99

Please sign in to comment.