Skip to content

Commit 31e0022

Browse files
committed
std::unix::fs::get_path: using fcntl codepath for netbsd instead.
on netbsd, procfs is not as central as on linux/solaris thus can be perfectly not mounted. Thus using fcntl with F_GETPATH, the kernel deals with MAXPATHLEN internally too.
1 parent 6ef46b3 commit 31e0022

File tree

1 file changed

+13
-9
lines changed
  • std/src/sys/pal/unix

1 file changed

+13
-9
lines changed

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

+13-9
Original file line numberDiff line numberDiff line change
@@ -1481,29 +1481,33 @@ impl FromRawFd for File {
14811481

14821482
impl fmt::Debug for File {
14831483
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"))]
14901485
fn get_path(fd: c_int) -> Option<PathBuf> {
14911486
let mut p = PathBuf::from("/proc/self/fd");
14921487
p.push(&fd.to_string());
14931488
readlink(&p).ok()
14941489
}
14951490

1496-
#[cfg(target_vendor = "apple")]
1491+
#[cfg(any(target_vendor = "apple", target_os = "netbsd"))]
14971492
fn get_path(fd: c_int) -> Option<PathBuf> {
14981493
// 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`
15001495
// with `F_GETPATH` in terms of `MAXPATHLEN`, and there are no
15011496
// alternatives. If a better method is invented, it should be used
15021497
// instead.
15031498
let mut buf = vec![0; libc::PATH_MAX as usize];
15041499
let n = unsafe { libc::fcntl(fd, libc::F_GETPATH, buf.as_ptr()) };
15051500
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+
}
15071511
}
15081512
let l = buf.iter().position(|&c| c == 0).unwrap();
15091513
buf.truncate(l as usize);

0 commit comments

Comments
 (0)