]> git.lizzy.rs Git - rust.git/blob - src/librustc_fs_util/lib.rs
Auto merge of #54720 - davidtwco:issue-51191, r=nikomatsakis
[rust.git] / src / librustc_fs_util / lib.rs
1 // Copyright 2018 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
11 use std::path::{Path, PathBuf};
12 use std::ffi::CString;
13 use std::fs;
14 use std::io;
15
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.
20 //
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
24 // this case.
25 //
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...
29 //
30 // For some more information, see this comment:
31 //   https://github.com/rust-lang/rust/issues/25505#issuecomment-102876737
32 #[cfg(windows)]
33 pub fn fix_windows_verbatim_for_gcc(p: &Path) -> PathBuf {
34     use std::path;
35     use std::ffi::OsString;
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(),
40     };
41     match prefix.kind() {
42         path::Prefix::VerbatimDisk(disk) => {
43             let mut base = OsString::from(format!("{}:", disk as char));
44             base.push(components.as_path());
45             PathBuf::from(base)
46         }
47         path::Prefix::VerbatimUNC(server, share) => {
48             let mut base = OsString::from(r"\\");
49             base.push(server);
50             base.push(r"\");
51             base.push(share);
52             base.push(components.as_path());
53             PathBuf::from(base)
54         }
55         _ => p.to_path_buf(),
56     }
57 }
58
59 #[cfg(not(windows))]
60 pub fn fix_windows_verbatim_for_gcc(p: &Path) -> PathBuf {
61     p.to_path_buf()
62 }
63
64 pub enum LinkOrCopy {
65     Link,
66     Copy,
67 }
68
69 /// Copy `p` into `q`, preferring to use hard-linking if possible. If
70 /// `q` already exists, it is removed first.
71 /// The result indicates which of the two operations has been performed.
72 pub fn link_or_copy<P: AsRef<Path>, Q: AsRef<Path>>(p: P, q: Q) -> io::Result<LinkOrCopy> {
73     let p = p.as_ref();
74     let q = q.as_ref();
75     if q.exists() {
76         fs::remove_file(&q)?;
77     }
78
79     match fs::hard_link(p, q) {
80         Ok(()) => Ok(LinkOrCopy::Link),
81         Err(_) => {
82             match fs::copy(p, q) {
83                 Ok(_) => Ok(LinkOrCopy::Copy),
84                 Err(e) => Err(e),
85             }
86         }
87     }
88 }
89
90 #[derive(Debug)]
91 pub enum RenameOrCopyRemove {
92     Rename,
93     CopyRemove,
94 }
95
96 /// Rename `p` into `q`, preferring to use `rename` if possible.
97 /// If `rename` fails (rename may fail for reasons such as crossing
98 /// filesystem), fallback to copy & remove
99 pub fn rename_or_copy_remove<P: AsRef<Path>, Q: AsRef<Path>>(p: P,
100                                                              q: Q)
101                                                              -> io::Result<RenameOrCopyRemove> {
102     let p = p.as_ref();
103     let q = q.as_ref();
104     match fs::rename(p, q) {
105         Ok(()) => Ok(RenameOrCopyRemove::Rename),
106         Err(_) => {
107             match fs::copy(p, q) {
108                 Ok(_) => {
109                     fs::remove_file(p)?;
110                     Ok(RenameOrCopyRemove::CopyRemove)
111                 }
112                 Err(e) => Err(e),
113             }
114         }
115     }
116 }
117
118 #[cfg(unix)]
119 pub fn path2cstr(p: &Path) -> CString {
120     use std::os::unix::prelude::*;
121     use std::ffi::OsStr;
122     let p: &OsStr = p.as_ref();
123     CString::new(p.as_bytes()).unwrap()
124 }
125 #[cfg(windows)]
126 pub fn path2cstr(p: &Path) -> CString {
127     CString::new(p.to_str().unwrap()).unwrap()
128 }