Skip to content

Commit

Permalink
fcntl update following up with F_GETPATH but with FreeBSD's F_KINFO f…
Browse files Browse the repository at this point in the history
…lag support instead
  • Loading branch information
devnexen committed Oct 3, 2023
1 parent 68c230b commit 070f7d9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use libc::{self, c_int, c_uint, size_t, ssize_t};
target_os = "macos",
target_os = "ios",
target_os = "dragonfly",
target_os = "freebsd",
))]
use std::ffi::CStr;
use std::ffi::OsString;
Expand All @@ -18,6 +19,7 @@ use std::os::unix::io::RawFd;
target_os = "macos",
target_os = "ios",
target_os = "dragonfly",
target_os = "freebsd",
))]
use std::path::PathBuf;
#[cfg(any(
Expand Down Expand Up @@ -505,6 +507,8 @@ pub enum FcntlArg<'a> {
F_SETPIPE_SZ(c_int),
#[cfg(any(target_os = "netbsd", target_os = "dragonfly", target_os = "macos", target_os = "ios"))]
F_GETPATH(&'a mut PathBuf),
#[cfg(target_os = "freebsd")]
F_KINFO(&'a mut PathBuf),
// TODO: Rest of flags
}

Expand Down Expand Up @@ -574,6 +578,18 @@ pub fn fcntl(fd: RawFd, arg: FcntlArg) -> Result<c_int> {
*path = PathBuf::from(OsString::from(optr.to_str().unwrap()));
return Ok(ok_res)
},
#[cfg(target_os = "freebsd")]
F_KINFO(path) => {
let mut info: libc::kinfo_file = std::mem::zeroed();
info.kf_structsize = std::mem::size_of::<libc::kinfo_file>() as i32;
let res = libc::fcntl(fd, libc::F_KINFO, &mut info);
let ok_res = Errno::result(res)?;
let sl = info.kf_path.to_vec();
let vec: &[u8] = &sl.iter().map(|&c| c as u8).collect::<Vec<u8>>()[..];
let optr = CStr::from_bytes_until_nul(&vec).unwrap();
*path = PathBuf::from(OsString::from(optr.to_str().unwrap()));
return Ok(ok_res)
},
}
};

Expand Down
14 changes: 14 additions & 0 deletions test/test_fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -584,3 +584,17 @@ fn test_f_get_path() {
tmp.path().canonicalize().unwrap()
);
}

#[cfg(target_os = "freebsd")]
#[test]
fn test_f_kinfo() {
use nix::fcntl::*;
use std::{os::unix::io::AsRawFd, path::PathBuf};

let tmp = NamedTempFile::new().unwrap();
let fd = tmp.as_raw_fd();
let mut path = PathBuf::new();
let res = fcntl(fd, FcntlArg::F_KINFO(&mut path)).expect("get path failed");
assert_ne!(res, -1);
assert_eq!(path.as_path(), tmp.path());
}

0 comments on commit 070f7d9

Please sign in to comment.