]> git.proxmox.com Git - rustc.git/blame - src/librustc/util/fs.rs
Imported Upstream version 1.2.0+dfsg1
[rustc.git] / src / librustc / util / fs.rs
CommitLineData
d9579d0f
AL
1// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2// file at the top-level directory of this distribution and at
3// http://rust-lang.org/COPYRIGHT.
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10
11use std::path::{self, Path, PathBuf};
12use std::ffi::OsString;
13
62682a34
SL
14// Unfortunately, on windows, it looks like msvcrt.dll is silently translating
15// verbatim paths under the hood to non-verbatim paths! This manifests itself as
16// gcc looking like it cannot accept paths of the form `\\?\C:\...`, but the
17// real bug seems to lie in msvcrt.dll.
18//
19// Verbatim paths are generally pretty rare, but the implementation of
20// `fs::canonicalize` currently generates paths of this form, meaning that we're
21// going to be passing quite a few of these down to gcc, so we need to deal with
22// this case.
d9579d0f
AL
23//
24// For now we just strip the "verbatim prefix" of `\\?\` from the path. This
25// will probably lose information in some cases, but there's not a whole lot
62682a34
SL
26// more we can do with a buggy msvcrt...
27//
28// For some more information, see this comment:
29// https://github.com/rust-lang/rust/issues/25505#issuecomment-102876737
d9579d0f
AL
30pub fn fix_windows_verbatim_for_gcc(p: &Path) -> PathBuf {
31 if !cfg!(windows) {
32 return p.to_path_buf()
33 }
34 let mut components = p.components();
35 let prefix = match components.next() {
36 Some(path::Component::Prefix(p)) => p,
37 _ => return p.to_path_buf(),
38 };
62682a34
SL
39 match prefix.kind() {
40 path::Prefix::VerbatimDisk(disk) => {
41 let mut base = OsString::from(format!("{}:", disk as char));
42 base.push(components.as_path());
43 PathBuf::from(base)
44 }
45 path::Prefix::VerbatimUNC(server, share) => {
46 let mut base = OsString::from(r"\\");
47 base.push(server);
48 base.push(r"\");
49 base.push(share);
50 base.push(components.as_path());
51 PathBuf::from(base)
52 }
53 _ => p.to_path_buf(),
54 }
d9579d0f 55}