std::unix::fs::get_path: using fcntl codepath for netbsd instead. · model-checking/verify-rust-std@31e0022 (original) (raw)

Original file line number Diff line number Diff line change
@@ -1481,29 +1481,33 @@ impl FromRawFd for File {
1481 1481
1482 1482 impl fmt::Debug for File {
1483 1483 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1484 -#[cfg(any(
1485 - target_os = "linux",
1486 - target_os = "netbsd",
1487 - target_os = "illumos",
1488 - target_os = "solaris"
1489 - ))]
1484 +#[cfg(any(target_os = "linux", target_os = "illumos", target_os = "solaris"))]
1490 1485 fn get_path(fd: c_int) -> Option<PathBuf> {
1491 1486 let mut p = PathBuf::from("/proc/self/fd");
1492 1487 p.push(&fd.to_string());
1493 1488 readlink(&p).ok()
1494 1489 }
1495 1490
1496 -#[cfg(target_vendor = "apple")]
1491 +#[cfg(any(target_vendor = "apple", target_os = "netbsd"))]
1497 1492 fn get_path(fd: c_int) -> Option<PathBuf> {
1498 1493 // FIXME: The use of PATH_MAX is generally not encouraged, but it
1499 -// is inevitable in this case because Apple targets define `fcntl`
1494 +// is inevitable in this case because Apple targets and NetBSD define `fcntl`
1500 1495 // with `F_GETPATH` in terms of `MAXPATHLEN`, and there are no
1501 1496 // alternatives. If a better method is invented, it should be used
1502 1497 // instead.
1503 1498 let mut buf = vec![0; libc::PATH_MAX as usize];
1504 1499 let n = unsafe { libc::fcntl(fd, libc::F_GETPATH, buf.as_ptr()) };
1505 1500 if n == -1 {
1506 -return None;
1501 + cfg_if::cfg_if! {
1502 +if #[cfg(target_os = "netbsd")] {
1503 +// fallback to procfs as last resort
1504 +let mut p = PathBuf::from("/proc/self/fd");
1505 + p.push(&fd.to_string());
1506 +return readlink(&p).ok();
1507 +} else {
1508 +return None;
1509 +}
1510 +}
1507 1511 }
1508 1512 let l = buf.iter().position(|&c
1509 1513 buf.truncate(l as usize);