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
148
149
150
151
152
153
154
155
156
157
//! # filepath
//!
//! `filepath` contains an extension trait for `std::fs::File` providing a `path` method.
//!

#[cfg(target_os="macos")]
extern crate libc;
#[cfg(windows)]
extern crate winapi;

#[cfg(target_os="linux")]
use std::path::Path;
use std::path::PathBuf;
use std::io;
use std::fs::File;
#[cfg(windows)]
use std::ptr;

#[cfg(target_os="linux")]
use std::fs::read_link;

#[cfg(unix)]
use std::os::unix::io::AsRawFd;
#[cfg(windows)]
use std::os::windows::io::AsRawHandle;

#[cfg(any(target_os="macos", windows))]
use std::ffi::OsString;

#[cfg(target_os="macos")]
use std::os::unix::ffi::OsStringExt;
#[cfg(windows)]
use std::os::windows::prelude::*;

#[cfg(windows)]
use winapi::um::fileapi;

#[cfg(target_os="macos")]
const F_GETPATH : i32 = 50;

/// An extension trait for `std::fs::File` providing a `path` method.
pub trait FilePath {
    /// Returns the path of this file.
    ///
    /// The path might be wrong for example after moving a file.
    ///
    /// # Platform-specific behavior
    /// This function currently uses `/proc/self/fd/` on Linux, `fcntl` with `F_GETPATH` on macOS
    /// and `GetFinalPathNameByHandle` on Windows.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use std::fs::File;
    /// use std::io;
    /// use filepath::FilePath;
    ///
    /// fn main() -> io::Result<()> {
    ///     let file = File::open("some_file")?;
    ///     let path = file.path()?;
    ///     Ok(())
    /// }
    /// ```
    fn path(&self) -> io::Result<PathBuf>;
}

impl FilePath for File {
    #[cfg(target_os="linux")]
    fn path(&self) -> io::Result<PathBuf> {
        let fd = self.as_raw_fd();
        let path = Path::new("/proc/self/fd/").join(fd.to_string());
        read_link(path)
    }

    #[cfg(target_os="macos")]
    fn path(&self) -> io::Result<PathBuf> {
        let fd = self.as_raw_fd();
        let mut path = vec![0; libc::PATH_MAX as usize + 1];

        unsafe {
            if libc::fcntl(fd, F_GETPATH, path.as_mut_ptr()) < 0 {
                return Err(io::Error::last_os_error());
            }
        }

        path.retain(|&c| c != 0);
        Ok(PathBuf::from(OsString::from_vec(path)))
    }

    #[cfg(windows)]
    fn path(&self) -> io::Result<PathBuf> {
        // Call with null to get the required size.
        let len = unsafe {
            fileapi::GetFinalPathNameByHandleW(self.as_raw_handle(), ptr::null_mut(), 0, 0)
        };
        if len == 0 {
            return Err(io::Error::last_os_error());
        }

        let mut path = Vec::with_capacity(len as usize);
        let len2 = unsafe {
            fileapi::GetFinalPathNameByHandleW(self.as_raw_handle(), path.as_mut_ptr(), len, 0)
        };
        // Handle unlikely case that path length changed between those two calls.
        if len2 == 0 || len2 >= len {
            return Err(io::Error::last_os_error());
        }
        unsafe {
            path.set_len(len2 as usize);
        }

        // Turn the \\?\UNC\ network path prefix into \\.
        let prefix = ['\\' as _, '\\' as _, '?' as _, '\\' as _, 'U' as _, 'N' as _, 'C' as _,
                      '\\' as _];
        if path.starts_with(&prefix) {
            let mut network_path: Vec<u16> = vec!['\\' as u16, '\\' as u16];
            network_path.extend_from_slice(&path[prefix.len() ..]);
            return Ok(PathBuf::from(OsString::from_wide(&network_path)));
        }

        // Remove the \\?\ prefix.
        let prefix = ['\\' as _, '\\' as _, '?' as _, '\\' as _];
        if path.starts_with(&prefix) {
            return Ok(PathBuf::from(OsString::from_wide(&path[prefix.len() ..])));
        }

        Ok(PathBuf::from(OsString::from_wide(&path)))
    }
}

#[cfg(test)]
mod tests {
    use std::io::prelude::*;
    use std::fs::{remove_file, File};
    use FilePath;

    #[test]
    fn simple() {
        let file = File::create("foobar").unwrap();
        assert_eq!(file.path().unwrap().file_name().unwrap(), "foobar");
        remove_file("foobar").unwrap();
    }

    #[test]
    fn roundtrip() {
        let mut file = File::create("bar").unwrap();
        file.write(b"abc").unwrap();
        file.flush().unwrap();

        let mut file2 = File::open(file.path().unwrap()).unwrap();
        let mut buffer = String::new();
        file2.read_to_string(&mut buffer).unwrap();

        assert_eq!(buffer, "abc");
        remove_file("bar").unwrap();
    }
}