Skip to content

Commit b815ba1

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

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/fcntl.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
use crate::errno::Errno;
2+
#[cfg(target_os = "freebsd")]
3+
use core::slice;
24
use libc::{self, c_int, c_uint, size_t, ssize_t};
35
#[cfg(any(
46
target_os = "netbsd",
57
target_os = "macos",
68
target_os = "ios",
79
target_os = "dragonfly",
10+
target_os = "freebsd",
811
))]
912
use std::ffi::CStr;
1013
use std::ffi::OsString;
@@ -18,6 +21,7 @@ use std::os::unix::io::RawFd;
1821
target_os = "macos",
1922
target_os = "ios",
2023
target_os = "dragonfly",
24+
target_os = "freebsd",
2125
))]
2226
use std::path::PathBuf;
2327
#[cfg(any(
@@ -505,6 +509,8 @@ pub enum FcntlArg<'a> {
505509
F_SETPIPE_SZ(c_int),
506510
#[cfg(any(target_os = "netbsd", target_os = "dragonfly", target_os = "macos", target_os = "ios"))]
507511
F_GETPATH(&'a mut PathBuf),
512+
#[cfg(target_os = "freebsd")]
513+
F_KINFO(&'a mut PathBuf),
508514
// TODO: Rest of flags
509515
}
510516

@@ -574,6 +580,18 @@ pub fn fcntl(fd: RawFd, arg: FcntlArg) -> Result<c_int> {
574580
*path = PathBuf::from(OsString::from(optr.to_str().unwrap()));
575581
return Ok(ok_res)
576582
},
583+
#[cfg(target_os = "freebsd")]
584+
F_KINFO(path) => {
585+
let mut info: libc::kinfo_file = std::mem::zeroed();
586+
info.kf_structsize = std::mem::size_of::<libc::kinfo_file>() as i32;
587+
let res = libc::fcntl(fd, libc::F_KINFO, &mut info);
588+
let ok_res = Errno::result(res)?;
589+
let p = info.kf_path;
590+
let u8_slice = slice::from_raw_parts(p.as_ptr().cast(), p.len());
591+
let optr = CStr::from_bytes_until_nul(u8_slice).unwrap();
592+
*path = PathBuf::from(OsString::from(optr.to_str().unwrap()));
593+
return Ok(ok_res)
594+
},
577595
}
578596
};
579597

test/test_fcntl.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,3 +584,18 @@ 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 tmp2 = File::open(tmp.path()).unwrap();
596+
let fd = tmp2.as_raw_fd();
597+
let mut path = PathBuf::new();
598+
let res = fcntl(fd, FcntlArg::F_KINFO(&mut path)).expect("get path failed");
599+
assert_ne!(res, -1);
600+
assert_eq!(path, tmp.path());
601+
}

0 commit comments

Comments
 (0)