Skip to content

Commit 90a3d90

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

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
@@ -808,6 +808,16 @@ pub enum FcntlArg<'a> {
808808
/// fstore_t field fst_bytesalloc.
809809
#[cfg(apple_targets)]
810810
F_PREALLOCATE(&'a mut libc::fstore_t),
811+
#[cfg(apple_targets)]
812+
/// Get disk device information. In practice,
813+
/// only the file offset data is set.
814+
F_LOG2PHYS(&'a mut libc::off_t),
815+
#[cfg(apple_targets)]
816+
/// Get disk device information. In practice,
817+
/// only the file offset data is set.
818+
/// difference with F_LOG2PHYS is the struct passed
819+
/// is used as both IN/OUT.
820+
F_LOG2PHYS_EXT(&'a mut libc::off_t),
811821
// TODO: Rest of flags
812822
}
813823

@@ -919,6 +929,23 @@ pub fn fcntl<Fd: std::os::fd::AsFd>(fd: Fd, arg: FcntlArg) -> Result<c_int> {
919929
#[cfg(apple_targets)]
920930
F_RDADVISE(rad) => {
921931
libc::fcntl(fd, libc::F_RDADVISE, &rad)
932+
},
933+
#[cfg(apple_targets)]
934+
F_LOG2PHYS(offset) => {
935+
let mut info: libc::log2phys = std::mem::zeroed();
936+
let res = libc::fcntl(fd, libc::F_LOG2PHYS, &mut info);
937+
let ok_res = Errno::result(res)?;
938+
*offset = info.l2p_devoffset;
939+
return Ok(ok_res)
940+
}
941+
#[cfg(apple_targets)]
942+
F_LOG2PHYS_EXT(offset) => {
943+
let mut info: libc::log2phys = std::mem::zeroed();
944+
info.l2p_devoffset = *offset;
945+
let res = libc::fcntl(fd, libc::F_LOG2PHYS_EXT, &mut info);
946+
let ok_res = Errno::result(res)?;
947+
*offset = info.l2p_devoffset;
948+
return Ok(ok_res)
922949
}
923950
#[cfg(apple_targets)]
924951
F_RDAHEAD(on) => {

test/test_fcntl.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,3 +770,23 @@ fn test_f_rdadvise() {
770770
assert_eq!(contents.len(), read(&fd, &mut buf).unwrap());
771771
assert_eq!(contents, &buf[0..contents.len()]);
772772
}
773+
774+
#[cfg(apple_targets)]
775+
#[test]
776+
fn test_f_log2phys() {
777+
use nix::fcntl::*;
778+
779+
const CONTENTS: &[u8] = b"abcd";
780+
let mut tmp = NamedTempFile::new().unwrap();
781+
tmp.write_all(CONTENTS).unwrap();
782+
let mut offset: libc::off_t = 0;
783+
let mut res = fcntl(&tmp, FcntlArg::F_LOG2PHYS(&mut offset))
784+
.expect("log2phys failed");
785+
assert_ne!(res, -1);
786+
assert_ne!(offset, 0);
787+
offset = 3;
788+
res = fcntl(&tmp, FcntlArg::F_LOG2PHYS_EXT(&mut offset))
789+
.expect("log2phys failed");
790+
assert_ne!(res, -1);
791+
assert_ne!(offset, 3);
792+
}

0 commit comments

Comments
 (0)