Skip to content

fcntl adding F_LOG2PHYS* flags. #2483

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}