1 #![deny(rustc::untranslatable_diagnostic)]
2 #![deny(rustc::diagnostic_outside_of_impl)]
7 use std::path::{Path, PathBuf};
9 // Unfortunately, on windows, it looks like msvcrt.dll is silently translating
10 // verbatim paths under the hood to non-verbatim paths! This manifests itself as
11 // gcc looking like it cannot accept paths of the form `\\?\C:\...`, but the
12 // real bug seems to lie in msvcrt.dll.
14 // Verbatim paths are generally pretty rare, but the implementation of
15 // `fs::canonicalize` currently generates paths of this form, meaning that we're
16 // going to be passing quite a few of these down to gcc, so we need to deal with
19 // For now we just strip the "verbatim prefix" of `\\?\` from the path. This
20 // will probably lose information in some cases, but there's not a whole lot
21 // more we can do with a buggy msvcrt...
23 // For some more information, see this comment:
24 // https://github.com/rust-lang/rust/issues/25505#issuecomment-102876737
26 pub fn fix_windows_verbatim_for_gcc(p: &Path) -> PathBuf {
27 use std::ffi::OsString;
29 let mut components = p.components();
30 let prefix = match components.next() {
31 Some(path::Component::Prefix(p)) => p,
32 _ => return p.to_path_buf(),
35 path::Prefix::VerbatimDisk(disk) => {
36 let mut base = OsString::from(format!("{}:", disk as char));
37 base.push(components.as_path());
40 path::Prefix::VerbatimUNC(server, share) => {
41 let mut base = OsString::from(r"\\");
45 base.push(components.as_path());
53 pub fn fix_windows_verbatim_for_gcc(p: &Path) -> PathBuf {
62 /// Copies `p` into `q`, preferring to use hard-linking if possible. If
63 /// `q` already exists, it is removed first.
64 /// The result indicates which of the two operations has been performed.
65 pub fn link_or_copy<P: AsRef<Path>, Q: AsRef<Path>>(p: P, q: Q) -> io::Result<LinkOrCopy> {
68 match fs::remove_file(&q) {
70 Err(err) if err.kind() == io::ErrorKind::NotFound => (),
71 Err(err) => return Err(err),
74 match fs::hard_link(p, q) {
75 Ok(()) => Ok(LinkOrCopy::Link),
76 Err(_) => match fs::copy(p, q) {
77 Ok(_) => Ok(LinkOrCopy::Copy),
84 pub fn path_to_c_string(p: &Path) -> CString {
86 use std::os::unix::ffi::OsStrExt;
87 let p: &OsStr = p.as_ref();
88 CString::new(p.as_bytes()).unwrap()
91 pub fn path_to_c_string(p: &Path) -> CString {
92 CString::new(p.to_str().unwrap()).unwrap()