Skip to content

Commit

Permalink
Rollup merge of rust-lang#98211 - devnexen:get_path_freebsd, r=Mark-S…
Browse files Browse the repository at this point in the history
…imulacrum

Implement `fs::get_path` for FreeBSD.

Using `F_KINFO` fcntl flag, the kf_structsize field
needs to be set beforehand for that effect.
  • Loading branch information
Dylan-DPC authored Jul 25, 2022
2 parents 5509e42 + 5803492 commit 6e28b0e
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions library/std/src/sys/unix/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,19 @@ impl fmt::Debug for File {
Some(PathBuf::from(OsString::from_vec(buf)))
}

#[cfg(target_os = "freebsd")]
fn get_path(fd: c_int) -> Option<PathBuf> {
let info = Box::<libc::kinfo_file>::new_zeroed();
let mut info = unsafe { info.assume_init() };
info.kf_structsize = mem::size_of::<libc::kinfo_file>() as libc::c_int;
let n = unsafe { libc::fcntl(fd, libc::F_KINFO, &mut *info) };
if n == -1 {
return None;
}
let buf = unsafe { CStr::from_ptr(info.kf_path.as_mut_ptr()).to_bytes().to_vec() };
Some(PathBuf::from(OsString::from_vec(buf)))
}

#[cfg(target_os = "vxworks")]
fn get_path(fd: c_int) -> Option<PathBuf> {
let mut buf = vec![0; libc::PATH_MAX as usize];
Expand All @@ -1142,6 +1155,7 @@ impl fmt::Debug for File {
target_os = "linux",
target_os = "macos",
target_os = "vxworks",
target_os = "freebsd",
target_os = "netbsd"
)))]
fn get_path(_fd: c_int) -> Option<PathBuf> {
Expand Down

0 comments on commit 6e28b0e

Please sign in to comment.