Skip to content

Commit 624f23c

Browse files
committed
re-restructure get_path
1 parent 6759de5 commit 624f23c

File tree

1 file changed

+4
-77
lines changed

1 file changed

+4
-77
lines changed

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

Lines changed: 4 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -492,13 +492,14 @@ fn get_path_from_fd(fd: c_int) -> Option<PathBuf> {
492492
let mut buf = vec![0; libc::PATH_MAX as usize];
493493
let n = unsafe { libc::fcntl(fd, libc::F_GETPATH, buf.as_ptr()) };
494494
if n == -1 {
495-
cfg_if::cfg_if! {
496-
if #[cfg(target_os = "netbsd")] {
495+
cfg_select! {
496+
target_os = "netbsd" => {
497497
// fallback to procfs as last resort
498498
let mut p = PathBuf::from("/proc/self/fd");
499499
p.push(&fd.to_string());
500500
return run_path_with_cstr(&p, &readlink).ok()
501-
} else {
501+
}
502+
_ => {
502503
return None;
503504
}
504505
}
@@ -2162,80 +2163,6 @@ impl FromRawFd for File {
21622163

21632164
impl fmt::Debug for File {
21642165
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2165-
#[cfg(any(target_os = "linux", target_os = "illumos", target_os = "solaris"))]
2166-
fn get_path(fd: c_int) -> Option<PathBuf> {
2167-
let mut p = PathBuf::from("/proc/self/fd");
2168-
p.push(&fd.to_string());
2169-
run_path_with_cstr(&p, &readlink).ok()
2170-
}
2171-
2172-
#[cfg(any(target_vendor = "apple", target_os = "netbsd"))]
2173-
fn get_path(fd: c_int) -> Option<PathBuf> {
2174-
// FIXME: The use of PATH_MAX is generally not encouraged, but it
2175-
// is inevitable in this case because Apple targets and NetBSD define `fcntl`
2176-
// with `F_GETPATH` in terms of `MAXPATHLEN`, and there are no
2177-
// alternatives. If a better method is invented, it should be used
2178-
// instead.
2179-
let mut buf = vec![0; libc::PATH_MAX as usize];
2180-
let n = unsafe { libc::fcntl(fd, libc::F_GETPATH, buf.as_ptr()) };
2181-
if n == -1 {
2182-
cfg_select! {
2183-
target_os = "netbsd" => {
2184-
// fallback to procfs as last resort
2185-
let mut p = PathBuf::from("/proc/self/fd");
2186-
p.push(&fd.to_string());
2187-
return run_path_with_cstr(&p, &readlink).ok()
2188-
}
2189-
_ => {
2190-
return None;
2191-
}
2192-
}
2193-
}
2194-
let l = buf.iter().position(|&c| c == 0).unwrap();
2195-
buf.truncate(l as usize);
2196-
buf.shrink_to_fit();
2197-
Some(PathBuf::from(OsString::from_vec(buf)))
2198-
}
2199-
2200-
#[cfg(target_os = "freebsd")]
2201-
fn get_path(fd: c_int) -> Option<PathBuf> {
2202-
let info = Box::<libc::kinfo_file>::new_zeroed();
2203-
let mut info = unsafe { info.assume_init() };
2204-
info.kf_structsize = size_of::<libc::kinfo_file>() as libc::c_int;
2205-
let n = unsafe { libc::fcntl(fd, libc::F_KINFO, &mut *info) };
2206-
if n == -1 {
2207-
return None;
2208-
}
2209-
let buf = unsafe { CStr::from_ptr(info.kf_path.as_mut_ptr()).to_bytes().to_vec() };
2210-
Some(PathBuf::from(OsString::from_vec(buf)))
2211-
}
2212-
2213-
#[cfg(target_os = "vxworks")]
2214-
fn get_path(fd: c_int) -> Option<PathBuf> {
2215-
let mut buf = vec![0; libc::PATH_MAX as usize];
2216-
let n = unsafe { libc::ioctl(fd, libc::FIOGETNAME, buf.as_ptr()) };
2217-
if n == -1 {
2218-
return None;
2219-
}
2220-
let l = buf.iter().position(|&c| c == 0).unwrap();
2221-
buf.truncate(l as usize);
2222-
Some(PathBuf::from(OsString::from_vec(buf)))
2223-
}
2224-
2225-
#[cfg(not(any(
2226-
target_os = "linux",
2227-
target_os = "vxworks",
2228-
target_os = "freebsd",
2229-
target_os = "netbsd",
2230-
target_os = "illumos",
2231-
target_os = "solaris",
2232-
target_vendor = "apple",
2233-
)))]
2234-
fn get_path(_fd: c_int) -> Option<PathBuf> {
2235-
// FIXME(#24570): implement this for other Unix platforms
2236-
None
2237-
}
2238-
22392166
fn get_mode(fd: c_int) -> Option<(bool, bool)> {
22402167
let mode = unsafe { libc::fcntl(fd, libc::F_GETFL) };
22412168
if mode == -1 {

0 commit comments

Comments
 (0)