Skip to content

Commit

Permalink
fcntl adding F_LOG2PHYS* flags. (nix-rust#2483)
Browse files Browse the repository at this point in the history
to get the current device address, in practice its current offset only.
  • Loading branch information
devnexen authored Sep 13, 2024
1 parent e5ac667 commit 12e1f5a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/2483.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add `F_LOG2PHYS` and `F_LOG2PHYS_EXT` for Apple target
23 changes: 23 additions & 0 deletions src/fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -919,6 +930,18 @@ pub fn fcntl<Fd: std::os::fd::AsFd>(fd: Fd, arg: FcntlArg) -> Result<c_int> {
#[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) => {
Expand Down
22 changes: 22 additions & 0 deletions test/test_fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit 12e1f5a

Please sign in to comment.