From 58034926024af6637dab04493c715acf349088e4 Mon Sep 17 00:00:00 2001 From: David CARLIER Date: Fri, 17 Jun 2022 20:31:59 +0100 Subject: [PATCH] Implement `fs::get_path` for FreeBSD. Using `F_KINFO` fcntl flag, the kf_structsize field needs to be set beforehand for that effect. --- library/std/src/sys/unix/fs.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/library/std/src/sys/unix/fs.rs b/library/std/src/sys/unix/fs.rs index 7c8824694408c..ebadc23877d46 100644 --- a/library/std/src/sys/unix/fs.rs +++ b/library/std/src/sys/unix/fs.rs @@ -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 { + let info = Box::::new_zeroed(); + let mut info = unsafe { info.assume_init() }; + info.kf_structsize = mem::size_of::() 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 { let mut buf = vec![0; libc::PATH_MAX as usize]; @@ -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 {