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.
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.
11 use std::path::{self, Path, PathBuf};
12 use std::ffi::OsString;
16 // Unfortunately, on windows, it looks like msvcrt.dll is silently translating
17 // verbatim paths under the hood to non-verbatim paths! This manifests itself as
18 // gcc looking like it cannot accept paths of the form `\\?\C:\...`, but the
19 // real bug seems to lie in msvcrt.dll.
21 // Verbatim paths are generally pretty rare, but the implementation of
22 // `fs::canonicalize` currently generates paths of this form, meaning that we're
23 // going to be passing quite a few of these down to gcc, so we need to deal with
26 // For now we just strip the "verbatim prefix" of `\\?\` from the path. This
27 // will probably lose information in some cases, but there's not a whole lot
28 // more we can do with a buggy msvcrt...
30 // For some more information, see this comment:
31 // https://github.com/rust-lang/rust/issues/25505#issuecomment-102876737
32 pub fn fix_windows_verbatim_for_gcc(p: &Path) -> PathBuf {
34 return p.to_path_buf();
36 let mut components = p.components();
37 let prefix = match components.next() {
38 Some(path::Component::Prefix(p)) => p,
39 _ => return p.to_path_buf(),
42 path::Prefix::VerbatimDisk(disk) => {
43 let mut base = OsString::from(format!("{}:", disk as char));
44 base.push(components.as_path());
47 path::Prefix::VerbatimUNC(server, share) => {
48 let mut base = OsString::from(r"\\");
52 base.push(components.as_path());
64 /// Copy `p` into `q`, preferring to use hard-linking if possible. If
65 /// `q` already exists, it is removed first.
66 /// The result indicates which of the two operations has been performed.
67 pub fn link_or_copy<P: AsRef<Path>, Q: AsRef<Path>>(p: P, q: Q) -> io::Result<LinkOrCopy> {
74 match fs::hard_link(p, q) {
75 Ok(()) => Ok(LinkOrCopy::Link),
77 match fs::copy(p, q) {
78 Ok(_) => Ok(LinkOrCopy::Copy),
86 pub enum RenameOrCopyRemove {
91 /// Rename `p` into `q`, preferring to use `rename` if possible.
92 /// If `rename` fails (rename may fail for reasons such as crossing
93 /// filesystem), fallback to copy & remove
94 pub fn rename_or_copy_remove<P: AsRef<Path>, Q: AsRef<Path>>(p: P,
96 -> io::Result<RenameOrCopyRemove> {
99 match fs::rename(p, q) {
100 Ok(()) => Ok(RenameOrCopyRemove::Rename),
102 match fs::copy(p, q) {
105 Ok(RenameOrCopyRemove::CopyRemove)
113 // Like std::fs::create_dir_all, except handles concurrent calls among multiple
114 // threads or processes.
115 pub fn create_dir_racy(path: &Path) -> io::Result<()> {
116 match fs::create_dir(path) {
117 Ok(()) => return Ok(()),
118 Err(ref e) if e.kind() == io::ErrorKind::AlreadyExists => return Ok(()),
119 Err(ref e) if e.kind() == io::ErrorKind::NotFound => {}
120 Err(e) => return Err(e),
122 match path.parent() {
123 Some(p) => try!(create_dir_racy(p)),
124 None => return Err(io::Error::new(io::ErrorKind::Other, "failed to create whole tree")),
126 match fs::create_dir(path) {
128 Err(ref e) if e.kind() == io::ErrorKind::AlreadyExists => Ok(()),