rustc_fs_util/
lib.rs

1use std::ffi::{CString, OsStr};
2use std::path::{Path, PathBuf, absolute};
3use std::{env, fs, io};
4
5use tempfile::TempDir;
6
7// Unfortunately, on windows, it looks like msvcrt.dll is silently translating
8// verbatim paths under the hood to non-verbatim paths! This manifests itself as
9// gcc looking like it cannot accept paths of the form `\\?\C:\...`, but the
10// real bug seems to lie in msvcrt.dll.
11//
12// Verbatim paths are generally pretty rare, but the implementation of
13// `fs::canonicalize` currently generates paths of this form, meaning that we're
14// going to be passing quite a few of these down to gcc, so we need to deal with
15// this case.
16//
17// For now we just strip the "verbatim prefix" of `\\?\` from the path. This
18// will probably lose information in some cases, but there's not a whole lot
19// more we can do with a buggy msvcrt...
20//
21// For some more information, see this comment:
22//   https://github.com/rust-lang/rust/issues/25505#issuecomment-102876737
23#[cfg(windows)]
24pub fn fix_windows_verbatim_for_gcc(p: &Path) -> PathBuf {
25    use std::ffi::OsString;
26    use std::path;
27    let mut components = p.components();
28    let prefix = match components.next() {
29        Some(path::Component::Prefix(p)) => p,
30        _ => return p.to_path_buf(),
31    };
32    match prefix.kind() {
33        path::Prefix::VerbatimDisk(disk) => {
34            let mut base = OsString::from(format!("{}:", disk as char));
35            base.push(components.as_path());
36            PathBuf::from(base)
37        }
38        path::Prefix::VerbatimUNC(server, share) => {
39            let mut base = OsString::from(r"\\");
40            base.push(server);
41            base.push(r"\");
42            base.push(share);
43            base.push(components.as_path());
44            PathBuf::from(base)
45        }
46        _ => p.to_path_buf(),
47    }
48}
49
50#[cfg(not(windows))]
51pub fn fix_windows_verbatim_for_gcc(p: &Path) -> PathBuf {
52    p.to_path_buf()
53}
54
55pub enum LinkOrCopy {
56    Link,
57    Copy,
58}
59
60/// Copies `p` into `q`, preferring to use hard-linking if possible.
61/// The result indicates which of the two operations has been performed.
62pub fn link_or_copy<P: AsRef<Path>, Q: AsRef<Path>>(p: P, q: Q) -> io::Result<LinkOrCopy> {
63    // Creating a hard-link will fail if the destination path already exists. We could defensively
64    // call remove_file in this function, but that pessimizes callers who can avoid such calls.
65    // Incremental compilation calls this function a lot, and is able to avoid calls that
66    // would fail the first hard_link attempt.
67
68    let p = p.as_ref();
69    let q = q.as_ref();
70
71    let err = match fs::hard_link(p, q) {
72        Ok(()) => return Ok(LinkOrCopy::Link),
73        Err(err) => err,
74    };
75
76    if err.kind() == io::ErrorKind::AlreadyExists {
77        fs::remove_file(q)?;
78        if fs::hard_link(p, q).is_ok() {
79            return Ok(LinkOrCopy::Link);
80        }
81    }
82
83    // Hard linking failed, fall back to copying.
84    fs::copy(p, q).map(|_| LinkOrCopy::Copy)
85}
86
87#[cfg(any(unix, all(target_os = "wasi", target_env = "p1")))]
88pub fn path_to_c_string(p: &Path) -> CString {
89    use std::ffi::OsStr;
90    #[cfg(unix)]
91    use std::os::unix::ffi::OsStrExt;
92    #[cfg(all(target_os = "wasi", target_env = "p1"))]
93    use std::os::wasi::ffi::OsStrExt;
94
95    let p: &OsStr = p.as_ref();
96    CString::new(p.as_bytes()).unwrap()
97}
98#[cfg(windows)]
99pub fn path_to_c_string(p: &Path) -> CString {
100    CString::new(p.to_str().unwrap()).unwrap()
101}
102
103#[inline]
104pub fn try_canonicalize<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
105    fs::canonicalize(&path).or_else(|_| absolute(&path))
106}
107
108pub struct TempDirBuilder<'a, 'b> {
109    builder: tempfile::Builder<'a, 'b>,
110}
111
112impl<'a, 'b> TempDirBuilder<'a, 'b> {
113    pub fn new() -> Self {
114        Self { builder: tempfile::Builder::new() }
115    }
116
117    pub fn prefix<S: AsRef<OsStr> + ?Sized>(&mut self, prefix: &'a S) -> &mut Self {
118        self.builder.prefix(prefix);
119        self
120    }
121
122    pub fn suffix<S: AsRef<OsStr> + ?Sized>(&mut self, suffix: &'b S) -> &mut Self {
123        self.builder.suffix(suffix);
124        self
125    }
126
127    pub fn tempdir_in<P: AsRef<Path>>(&self, dir: P) -> io::Result<TempDir> {
128        let dir = dir.as_ref();
129        // On Windows in CI, we had been getting fairly frequent "Access is denied"
130        // errors when creating temporary directories.
131        // So this implements a simple retry with backoff loop.
132        #[cfg(windows)]
133        for wait in 1..11 {
134            match self.builder.tempdir_in(dir) {
135                Err(e) if e.kind() == io::ErrorKind::PermissionDenied => {}
136                t => return t,
137            }
138            std::thread::sleep(std::time::Duration::from_millis(1 << wait));
139        }
140        self.builder.tempdir_in(dir)
141    }
142
143    pub fn tempdir(&self) -> io::Result<TempDir> {
144        self.tempdir_in(env::temp_dir())
145    }
146}