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 23, 2024
1 parent 5ef78a8 commit f4c08e4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,10 @@ 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 amount for sequential access or
/// disable it with 0 or to system default for any value < 0.
#[cfg(target_os = "freebsd")]
F_READAHEAD(c_int),
// TODO: Rest of flags
}

Expand Down Expand Up @@ -962,6 +966,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
19 changes: 19 additions & 0 deletions test/test_fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -805,3 +805,22 @@ 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();
// With TMPDIR set with UFS, the vnode name is not entered
// into the name cache thus path is always empty.
// Therefore, we reopen the tempfile a second time for the test
// to pass.
let tmp2 = File::open(tmp.path()).unwrap();
let mut res = fcntl(&tmp2, FcntlArg::F_READAHEAD(1_000_000))
.expect("read ahead failed");
assert_ne!(res, -1);
res =
fcntl(&tmp2, FcntlArg::F_READAHEAD(-1024)).expect("read ahead failed");
assert_ne!(res, -1);
}

0 comments on commit f4c08e4

Please sign in to comment.