Skip to content

Commit 070f7d9

Browse files
committed
fcntl update following up with F_GETPATH but with FreeBSD's F_KINFO flag support instead
1 parent 68c230b commit 070f7d9

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/fcntl.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use libc::{self, c_int, c_uint, size_t, ssize_t};
55
target_os = "macos",
66
target_os = "ios",
77
target_os = "dragonfly",
8+
target_os = "freebsd",
89
))]
910
use std::ffi::CStr;
1011
use std::ffi::OsString;
@@ -18,6 +19,7 @@ use std::os::unix::io::RawFd;
1819
target_os = "macos",
1920
target_os = "ios",
2021
target_os = "dragonfly",
22+
target_os = "freebsd",
2123
))]
2224
use std::path::PathBuf;
2325
#[cfg(any(
@@ -505,6 +507,8 @@ pub enum FcntlArg<'a> {
505507
F_SETPIPE_SZ(c_int),
506508
#[cfg(any(target_os = "netbsd", target_os = "dragonfly", target_os = "macos", target_os = "ios"))]
507509
F_GETPATH(&'a mut PathBuf),
510+
#[cfg(target_os = "freebsd")]
511+
F_KINFO(&'a mut PathBuf),
508512
// TODO: Rest of flags
509513
}
510514

@@ -574,6 +578,18 @@ pub fn fcntl(fd: RawFd, arg: FcntlArg) -> Result<c_int> {
574578
*path = PathBuf::from(OsString::from(optr.to_str().unwrap()));
575579
return Ok(ok_res)
576580
},
581+
#[cfg(target_os = "freebsd")]
582+
F_KINFO(path) => {
583+
let mut info: libc::kinfo_file = std::mem::zeroed();
584+
info.kf_structsize = std::mem::size_of::<libc::kinfo_file>() as i32;
585+
let res = libc::fcntl(fd, libc::F_KINFO, &mut info);
586+
let ok_res = Errno::result(res)?;
587+
let sl = info.kf_path.to_vec();
588+
let vec: &[u8] = &sl.iter().map(|&c| c as u8).collect::<Vec<u8>>()[..];
589+
let optr = CStr::from_bytes_until_nul(&vec).unwrap();
590+
*path = PathBuf::from(OsString::from(optr.to_str().unwrap()));
591+
return Ok(ok_res)
592+
},
577593
}
578594
};
579595

test/test_fcntl.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,3 +584,17 @@ fn test_f_get_path() {
584584
tmp.path().canonicalize().unwrap()
585585
);
586586
}
587+
588+
#[cfg(target_os = "freebsd")]
589+
#[test]
590+
fn test_f_kinfo() {
591+
use nix::fcntl::*;
592+
use std::{os::unix::io::AsRawFd, path::PathBuf};
593+
594+
let tmp = NamedTempFile::new().unwrap();
595+
let fd = tmp.as_raw_fd();
596+
let mut path = PathBuf::new();
597+
let res = fcntl(fd, FcntlArg::F_KINFO(&mut path)).expect("get path failed");
598+
assert_ne!(res, -1);
599+
assert_eq!(path.as_path(), tmp.path());
600+
}

0 commit comments

Comments
 (0)