1use std::ffi::CString;
2use std::path::{Path, PathBuf, absolute};
3use std::{fs, io};
45// Unfortunately, on windows, it looks like msvcrt.dll is silently translating
6// verbatim paths under the hood to non-verbatim paths! This manifests itself as
7// gcc looking like it cannot accept paths of the form `\\?\C:\...`, but the
8// real bug seems to lie in msvcrt.dll.
9//
10// Verbatim paths are generally pretty rare, but the implementation of
11// `fs::canonicalize` currently generates paths of this form, meaning that we're
12// going to be passing quite a few of these down to gcc, so we need to deal with
13// this case.
14//
15// For now we just strip the "verbatim prefix" of `\\?\` from the path. This
16// will probably lose information in some cases, but there's not a whole lot
17// more we can do with a buggy msvcrt...
18//
19// For some more information, see this comment:
20// https://github.com/rust-lang/rust/issues/25505#issuecomment-102876737
21#[cfg(windows)]
22pub fn fix_windows_verbatim_for_gcc(p: &Path) -> PathBuf {
23use std::ffi::OsString;
24use std::path;
25let mut components = p.components();
26let prefix = match components.next() {
27Some(path::Component::Prefix(p)) => p,
28_ => return p.to_path_buf(),
29 };
30match prefix.kind() {
31 path::Prefix::VerbatimDisk(disk) => {
32let mut base = OsString::from(format!("{}:", disk as char));
33 base.push(components.as_path());
34 PathBuf::from(base)
35 }
36 path::Prefix::VerbatimUNC(server, share) => {
37let mut base = OsString::from(r"\\");
38 base.push(server);
39 base.push(r"\");
40 base.push(share);
41 base.push(components.as_path());
42 PathBuf::from(base)
43 }
44_ => p.to_path_buf(),
45 }
46}
4748#[cfg(not(windows))]
49pub fn fix_windows_verbatim_for_gcc(p: &Path) -> PathBuf {
50p.to_path_buf()
51}
5253pub enum LinkOrCopy {
54 Link,
55 Copy,
56}
5758/// Copies `p` into `q`, preferring to use hard-linking if possible.
59/// The result indicates which of the two operations has been performed.
60pub fn link_or_copy<P: AsRef<Path>, Q: AsRef<Path>>(p: P, q: Q) -> io::Result<LinkOrCopy> {
61// Creating a hard-link will fail if the destination path already exists. We could defensively
62 // call remove_file in this function, but that pessimizes callers who can avoid such calls.
63 // Incremental compilation calls this function a lot, and is able to avoid calls that
64 // would fail the first hard_link attempt.
6566let p = p.as_ref();
67let q = q.as_ref();
6869let err = match fs::hard_link(p, q) {
70Ok(()) => return Ok(LinkOrCopy::Link),
71Err(err) => err,
72 };
7374if err.kind() == io::ErrorKind::AlreadyExists {
75fs::remove_file(q)?;
76if fs::hard_link(p, q).is_ok() {
77return Ok(LinkOrCopy::Link);
78 }
79 }
8081// Hard linking failed, fall back to copying.
82fs::copy(p, q).map(|_| LinkOrCopy::Copy)
83}
8485#[cfg(any(unix, all(target_os = "wasi", target_env = "p1")))]
86pub fn path_to_c_string(p: &Path) -> CString {
87use std::ffi::OsStr;
88#[cfg(unix)]
89use std::os::unix::ffi::OsStrExt;
90#[cfg(all(target_os = "wasi", target_env = "p1"))]
91use std::os::wasi::ffi::OsStrExt;
9293let p: &OsStr = p.as_ref();
94CString::new(p.as_bytes()).unwrap()
95}
96#[cfg(windows)]
97pub fn path_to_c_string(p: &Path) -> CString {
98 CString::new(p.to_str().unwrap()).unwrap()
99}
100101#[inline]
102pub fn try_canonicalize<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
103fs::canonicalize(&path).or_else(|_| absolute(&path))
104}