1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//! Get filename from a raw file descriptor
//!
//! # Example
//!
//! ```
//! use filename::file_name;
//!
//! let f = tempfile::tempfile().unwrap();
//!
//! println!("tempfile @ {:?}", file_name(&f).unwrap());
//! ```
use std::io;
use std::path::PathBuf;

/// OS-specific extensions to extract file name.
pub trait Filename {
    /// Returns the file name of an underlying object, if there is one.
    fn file_name(&self) -> io::Result<PathBuf>;
}

/// Returns the file name of an underlying object, if there is one.
#[cfg(unix)]
pub fn file_name<T>(fd: &T) -> io::Result<PathBuf>
where
    T: std::os::unix::io::AsRawFd,
{
    fd.file_name()
}

/// Returns the file name of an underlying object, if there is one.
#[cfg(windows)]
pub fn file_name<T>(fd: &T) -> io::Result<PathBuf>
where
    T: std::os::windows::io::AsRawHandle,
{
    fd.file_name()
}

#[cfg(unix)]
mod unix {
    use std::os::unix::io::AsRawFd;

    use super::*;

    #[cfg(any(target_os = "linux", target_os = "android"))]
    impl<T> Filename for T
    where
        T: AsRawFd,
    {
        fn file_name(&self) -> io::Result<PathBuf> {
            use std::path::Path;

            Path::new(&format!("/proc/self/fd/{}", self.as_raw_fd())).read_link()
        }
    }

    #[cfg(any(target_os = "macos", target_os = "ios"))]
    impl<T> Filename for T
    where
        T: AsRawFd,
    {
        fn file_name(&self) -> io::Result<PathBuf> {
            use std::ffi::OsStr;
            use std::os::unix::ffi::OsStrExt;

            const MAXPATHLEN: usize = 1024;

            let mut buf = [0; MAXPATHLEN];
            let ret = unsafe { libc::fcntl(self.as_raw_fd(), libc::F_GETPATH, &mut buf) };

            if ret == -1 {
                Err(io::Error::last_os_error())
            } else {
                let end = buf.iter().position(|&b| b == 0).unwrap_or(buf.len());
                Ok(PathBuf::from(OsStr::from_bytes(&buf[..end])))
            }
        }
    }
}

#[cfg(windows)]
mod win {
    use std::os::windows::io::AsRawHandle;

    use super::*;

    impl<T> Filename for T
    where
        T: AsRawHandle,
    {
        fn file_name(&self) -> io::Result<PathBuf> {
            use std::ffi::OsString;
            use std::mem;
            use std::os::windows::ffi::OsStringExt;
            use std::ptr::NonNull;
            use std::slice;

            use winapi::{
                shared::minwindef::FALSE,
                um::{
                    fileapi::FILE_NAME_INFO, minwinbase::FileNameInfo,
                    winbase::GetFileInformationByHandleEx, winnt::WCHAR,
                },
            };

            let mut buf = [0u8; 4096];
            let ret = unsafe {
                GetFileInformationByHandleEx(
                    self.as_raw_handle(),
                    FileNameInfo,
                    buf.as_mut_ptr() as *mut _,
                    buf.len() as u32,
                )
            };

            if ret == FALSE {
                Err(io::Error::last_os_error())
            } else {
                unsafe {
                    let info = NonNull::new_unchecked(buf.as_mut_ptr()).cast::<FILE_NAME_INFO>();
                    let info = info.as_ref();
                    let filename = slice::from_raw_parts(
                        info.FileName.as_ptr(),
                        (info.FileNameLength as usize) / mem::size_of::<WCHAR>(),
                    );

                    Ok(PathBuf::from(OsString::from_wide(filename)))
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::Filename;

    #[test]
    fn tmpfile() {
        let f = tempfile::NamedTempFile::new().unwrap();

        assert_eq!(
            f.path().canonicalize().unwrap(),
            f.as_file().file_name().unwrap().canonicalize().unwrap()
        );
    }
}