Skip to content

Commit 12e1f5a

Browse files
authored
fcntl adding F_LOG2PHYS* flags. (#2483)
to get the current device address, in practice its current offset only.
1 parent e5ac667 commit 12e1f5a

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

changelog/2483.added.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add `F_LOG2PHYS` and `F_LOG2PHYS_EXT` for Apple target

src/fcntl.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,17 @@ 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+
/// The difference with F_LOG2PHYS is the struct passed
819+
/// is used as both IN/OUT as both its l2p_devoffset and
820+
/// l2p_contigbytes can be used for more specific queries.
821+
F_LOG2PHYS_EXT(&'a mut libc::log2phys),
811822
// TODO: Rest of flags
812823
}
813824

@@ -919,6 +930,18 @@ pub fn fcntl<Fd: std::os::fd::AsFd>(fd: Fd, arg: FcntlArg) -> Result<c_int> {
919930
#[cfg(apple_targets)]
920931
F_RDADVISE(rad) => {
921932
libc::fcntl(fd, libc::F_RDADVISE, &rad)
933+
},
934+
#[cfg(apple_targets)]
935+
F_LOG2PHYS(offset) => {
936+
let mut info: libc::log2phys = std::mem::zeroed();
937+
let res = libc::fcntl(fd, libc::F_LOG2PHYS, &mut info);
938+
let ok_res = Errno::result(res)?;
939+
*offset = info.l2p_devoffset;
940+
return Ok(ok_res)
941+
}
942+
#[cfg(apple_targets)]
943+
F_LOG2PHYS_EXT(info) => {
944+
libc::fcntl(fd, libc::F_LOG2PHYS_EXT, info)
922945
}
923946
#[cfg(apple_targets)]
924947
F_RDAHEAD(on) => {

test/test_fcntl.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,3 +770,25 @@ 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+
let mut info: libc::log2phys = unsafe { std::mem::zeroed() };
788+
info.l2p_contigbytes = CONTENTS.len() as _;
789+
info.l2p_devoffset = 3;
790+
res = fcntl(&tmp, FcntlArg::F_LOG2PHYS_EXT(&mut info))
791+
.expect("log2phys failed");
792+
assert_ne!(res, -1);
793+
assert_ne!({ info.l2p_devoffset }, 3);
794+
}

0 commit comments

Comments
 (0)