Skip to content

Commit c925887

Browse files
committed
fcntl adding F_LOG2PHYS* flags.
to get the current device address, in practice its current offset only.
1 parent e599223 commit c925887

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

src/fcntl.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,16 @@ pub enum FcntlArg<'a> {
800800
/// Issue an advisory read async with no copy to user
801801
#[cfg(apple_targets)]
802802
F_RDADVISE(libc::radvisory),
803+
#[cfg(apple_targets)]
804+
/// Get disk device information. In practice,
805+
/// only the file offset data is set.
806+
F_LOG2PHYS(&'a mut libc::off_t),
807+
#[cfg(apple_targets)]
808+
/// Get disk device information. In practice,
809+
/// only the file offset data is set.
810+
/// difference with F_LOG2PHYS is the struct passed
811+
/// is used as both IN/OUT.
812+
F_LOG2PHYS_EXT(&'a mut libc::off_t),
803813
// TODO: Rest of flags
804814
}
805815

@@ -911,6 +921,23 @@ pub fn fcntl<Fd: std::os::fd::AsFd>(fd: Fd, arg: FcntlArg) -> Result<c_int> {
911921
#[cfg(apple_targets)]
912922
F_RDADVISE(rad) => {
913923
libc::fcntl(fd, libc::F_RDADVISE, &rad)
924+
},
925+
#[cfg(apple_targets)]
926+
F_LOG2PHYS(offset) => {
927+
let mut info: libc::log2phys = std::mem::zeroed();
928+
let res = libc::fcntl(fd, libc::F_LOG2PHYS, &mut info);
929+
let ok_res = Errno::result(res)?;
930+
*offset = info.l2p_devoffset;
931+
return Ok(ok_res)
932+
}
933+
#[cfg(apple_targets)]
934+
F_LOG2PHYS_EXT(offset) => {
935+
let mut info: libc::log2phys = std::mem::zeroed();
936+
info.l2p_devoffset = *offset;
937+
let res = libc::fcntl(fd, libc::F_LOG2PHYS_EXT, &mut info);
938+
let ok_res = Errno::result(res)?;
939+
*offset = info.l2p_devoffset;
940+
return Ok(ok_res)
914941
}
915942
}
916943
};

test/test_fcntl.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,3 +752,23 @@ fn test_f_rdadvise() {
752752
assert_eq!(contents.len(), read(&fd, &mut buf).unwrap());
753753
assert_eq!(contents, &buf[0..contents.len()]);
754754
}
755+
756+
#[cfg(apple_targets)]
757+
#[test]
758+
fn test_f_log2phys() {
759+
use nix::fcntl::*;
760+
761+
const CONTENTS: &[u8] = b"abcd";
762+
let mut tmp = NamedTempFile::new().unwrap();
763+
tmp.write_all(CONTENTS).unwrap();
764+
let mut offset: libc::off_t = 0;
765+
let mut res = fcntl(&tmp, FcntlArg::F_LOG2PHYS(&mut offset))
766+
.expect("log2phys failed");
767+
assert_ne!(res, -1);
768+
assert_ne!(offset, 0);
769+
offset = 3;
770+
res = fcntl(&tmp, FcntlArg::F_LOG2PHYS_EXT(&mut offset))
771+
.expect("log2phys failed");
772+
assert_ne!(res, -1);
773+
assert_ne!(offset, 3);
774+
}

0 commit comments

Comments
 (0)