From 12e1f5a8dc60b864eecc37dbd9805248a510fc69 Mon Sep 17 00:00:00 2001 From: David CARLIER Date: Fri, 13 Sep 2024 01:57:04 +0100 Subject: [PATCH] fcntl adding F_LOG2PHYS* flags. (#2483) to get the current device address, in practice its current offset only. --- changelog/2483.added.md | 1 + src/fcntl.rs | 23 +++++++++++++++++++++++ test/test_fcntl.rs | 22 ++++++++++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 changelog/2483.added.md diff --git a/changelog/2483.added.md b/changelog/2483.added.md new file mode 100644 index 0000000000..8b29b0ec33 --- /dev/null +++ b/changelog/2483.added.md @@ -0,0 +1 @@ +Add `F_LOG2PHYS` and `F_LOG2PHYS_EXT` for Apple target diff --git a/src/fcntl.rs b/src/fcntl.rs index c104246882..7d79c0bb5b 100644 --- a/src/fcntl.rs +++ b/src/fcntl.rs @@ -808,6 +808,17 @@ pub enum FcntlArg<'a> { /// fstore_t field fst_bytesalloc. #[cfg(apple_targets)] F_PREALLOCATE(&'a mut libc::fstore_t), + #[cfg(apple_targets)] + /// Get disk device information. In practice, + /// only the file offset data is set. + F_LOG2PHYS(&'a mut libc::off_t), + #[cfg(apple_targets)] + /// Get disk device information. In practice, + /// only the file offset data is set. + /// The difference with F_LOG2PHYS is the struct passed + /// is used as both IN/OUT as both its l2p_devoffset and + /// l2p_contigbytes can be used for more specific queries. + F_LOG2PHYS_EXT(&'a mut libc::log2phys), // TODO: Rest of flags } @@ -919,6 +930,18 @@ pub fn fcntl(fd: Fd, arg: FcntlArg) -> Result { #[cfg(apple_targets)] F_RDADVISE(rad) => { libc::fcntl(fd, libc::F_RDADVISE, &rad) + }, + #[cfg(apple_targets)] + F_LOG2PHYS(offset) => { + let mut info: libc::log2phys = std::mem::zeroed(); + let res = libc::fcntl(fd, libc::F_LOG2PHYS, &mut info); + let ok_res = Errno::result(res)?; + *offset = info.l2p_devoffset; + return Ok(ok_res) + } + #[cfg(apple_targets)] + F_LOG2PHYS_EXT(info) => { + libc::fcntl(fd, libc::F_LOG2PHYS_EXT, info) } #[cfg(apple_targets)] F_RDAHEAD(on) => { diff --git a/test/test_fcntl.rs b/test/test_fcntl.rs index c273e9acfb..e59eecec8e 100644 --- a/test/test_fcntl.rs +++ b/test/test_fcntl.rs @@ -770,3 +770,25 @@ fn test_f_rdadvise() { assert_eq!(contents.len(), read(&fd, &mut buf).unwrap()); assert_eq!(contents, &buf[0..contents.len()]); } + +#[cfg(apple_targets)] +#[test] +fn test_f_log2phys() { + use nix::fcntl::*; + + const CONTENTS: &[u8] = b"abcd"; + let mut tmp = NamedTempFile::new().unwrap(); + tmp.write_all(CONTENTS).unwrap(); + let mut offset: libc::off_t = 0; + let mut res = fcntl(&tmp, FcntlArg::F_LOG2PHYS(&mut offset)) + .expect("log2phys failed"); + assert_ne!(res, -1); + assert_ne!(offset, 0); + let mut info: libc::log2phys = unsafe { std::mem::zeroed() }; + info.l2p_contigbytes = CONTENTS.len() as _; + info.l2p_devoffset = 3; + res = fcntl(&tmp, FcntlArg::F_LOG2PHYS_EXT(&mut info)) + .expect("log2phys failed"); + assert_ne!(res, -1); + assert_ne!({ info.l2p_devoffset }, 3); +}