Skip to content

Commit 5803492

Browse files
committed
Implement fs::get_path for FreeBSD.
Using `F_KINFO` fcntl flag, the kf_structsize field needs to be set beforehand for that effect.
1 parent 2a990f7 commit 5803492

File tree

1 file changed

+14
-0
lines changed
  • library/std/src/sys/unix

1 file changed

+14
-0
lines changed

library/std/src/sys/unix/fs.rs

+14
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,19 @@ impl fmt::Debug for File {
11261126
Some(PathBuf::from(OsString::from_vec(buf)))
11271127
}
11281128

1129+
#[cfg(target_os = "freebsd")]
1130+
fn get_path(fd: c_int) -> Option<PathBuf> {
1131+
let info = Box::<libc::kinfo_file>::new_zeroed();
1132+
let mut info = unsafe { info.assume_init() };
1133+
info.kf_structsize = mem::size_of::<libc::kinfo_file>() as libc::c_int;
1134+
let n = unsafe { libc::fcntl(fd, libc::F_KINFO, &mut *info) };
1135+
if n == -1 {
1136+
return None;
1137+
}
1138+
let buf = unsafe { CStr::from_ptr(info.kf_path.as_mut_ptr()).to_bytes().to_vec() };
1139+
Some(PathBuf::from(OsString::from_vec(buf)))
1140+
}
1141+
11291142
#[cfg(target_os = "vxworks")]
11301143
fn get_path(fd: c_int) -> Option<PathBuf> {
11311144
let mut buf = vec![0; libc::PATH_MAX as usize];
@@ -1142,6 +1155,7 @@ impl fmt::Debug for File {
11421155
target_os = "linux",
11431156
target_os = "macos",
11441157
target_os = "vxworks",
1158+
target_os = "freebsd",
11451159
target_os = "netbsd"
11461160
)))]
11471161
fn get_path(_fd: c_int) -> Option<PathBuf> {

0 commit comments

Comments
 (0)