Skip to content

Commit f1d9beb

Browse files
authored
fcntl: adding macOs F_RDADVISE flag (#2480)
* fcntl: adding macOs F_RDADVISE flag Hints to prefetch blocks of the file into memory when possible. Can be useful to process large files. * add changelog entry * testing
1 parent 07540bb commit f1d9beb

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

changelog/2113.added.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Add socket option `IPV6_PKTINFO` for BSDs/Linux/Android, also `IPV6_RECVPKTINFO` for DragonFlyBSD
1+
Add socket option `IPV6_PKTINFO` for BSDs/Linux/Android, also `IPV6_RECVPKTINFO` for DragonFlyBSD

changelog/2480.added.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add fcntl constant `F_RDADVISE` for Apple target

src/fcntl.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,9 @@ pub enum FcntlArg<'a> {
797797
/// Return the full path without firmlinks of the fd.
798798
#[cfg(apple_targets)]
799799
F_GETPATH_NOFIRMLINK(&'a mut PathBuf),
800+
/// Issue an advisory read async with no copy to user
801+
#[cfg(apple_targets)]
802+
F_RDADVISE(libc::radvisory),
800803
// TODO: Rest of flags
801804
}
802805

@@ -905,6 +908,10 @@ pub fn fcntl<Fd: std::os::fd::AsFd>(fd: Fd, arg: FcntlArg) -> Result<c_int> {
905908
*path = PathBuf::from(OsString::from(optr.to_str().unwrap()));
906909
return Ok(ok_res)
907910
},
911+
#[cfg(apple_targets)]
912+
F_RDADVISE(rad) => {
913+
libc::fcntl(fd, libc::F_RDADVISE, &rad)
914+
}
908915
}
909916
};
910917

test/test_fcntl.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,3 +732,23 @@ mod test_flock {
732732
.expect_err("Should not have been able to lock the file");
733733
}
734734
}
735+
736+
#[cfg(apple_targets)]
737+
#[test]
738+
fn test_f_rdadvise() {
739+
use nix::fcntl::*;
740+
741+
let contents = vec![1; 1024];
742+
let mut buf = [0; 1024];
743+
let mut tmp = NamedTempFile::new().unwrap();
744+
tmp.write_all(&contents).unwrap();
745+
let fd = open(tmp.path(), OFlag::empty(), Mode::empty()).unwrap();
746+
let rad = libc::radvisory {
747+
ra_offset: 0,
748+
ra_count: contents.len() as _,
749+
};
750+
let res = fcntl(&tmp, FcntlArg::F_RDADVISE(rad)).expect("rdadivse failed");
751+
assert_ne!(res, -1);
752+
assert_eq!(contents.len(), read(&fd, &mut buf).unwrap());
753+
assert_eq!(contents, &buf[0..contents.len()]);
754+
}

0 commit comments

Comments
 (0)