about summary refs log tree commit diff
path: root/library/std/src/sys
diff options
context:
space:
mode:
authorYuki Okushi <jtitor@2k36.org>2022-07-26 13:12:18 +0900
committerGitHub <noreply@github.com>2022-07-26 13:12:18 +0900
commitd3acd0069d444ee47e8dd2998776d73abd36a6fb (patch)
treeb89759fa28a8988e8512af3d7e0391c1b263ceab /library/std/src/sys
parenta86705942c4cfaaee60f2e7308ca2bca703a710f (diff)
parente39b44a076e37d449db146135e8acb3cab05876b (diff)
downloadrust-d3acd0069d444ee47e8dd2998776d73abd36a6fb.tar.gz
rust-d3acd0069d444ee47e8dd2998776d73abd36a6fb.zip
Rollup merge of #98211 - devnexen:get_path_freebsd, r=Mark-Simulacrum
Implement `fs::get_path` for FreeBSD.

Using `F_KINFO` fcntl flag, the kf_structsize field
needs to be set beforehand for that effect.
Diffstat (limited to 'library/std/src/sys')
-rw-r--r--library/std/src/sys/unix/fs.rs14
1 files changed, 14 insertions, 0 deletions
diff --git a/library/std/src/sys/unix/fs.rs b/library/std/src/sys/unix/fs.rs
index 7c882469440..71522865b7d 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(all(target_os = "freebsd", target_arch = "x86_64"))]
+        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];
@@ -1142,6 +1155,7 @@ impl fmt::Debug for File {
             target_os = "linux",
             target_os = "macos",
             target_os = "vxworks",
+            all(target_os = "freebsd", target_arch = "x86_64"),
             target_os = "netbsd"
         )))]
         fn get_path(_fd: c_int) -> Option<PathBuf> {