]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_fs_util/src/lib.rs
Inline and remove `early_lint_node`.
[rust.git] / compiler / rustc_fs_util / src / lib.rs
1 #![deny(rustc::untranslatable_diagnostic)]
2 #![deny(rustc::diagnostic_outside_of_impl)]
3
4 use std::ffi::CString;
5 use std::fs;
6 use std::io;
7 use std::path::{Path, PathBuf};
8
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.
13 //
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
17 // this case.
18 //
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...
22 //
23 // For some more information, see this comment:
24 //   https://github.com/rust-lang/rust/issues/25505#issuecomment-102876737
25 #[cfg(windows)]
26 pub fn fix_windows_verbatim_for_gcc(p: &Path) -> PathBuf {
27     use std::ffi::OsString;
28     use std::path;
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(),
33     };
34     match prefix.kind() {
35         path::Prefix::VerbatimDisk(disk) => {
36             let mut base = OsString::from(format!("{}:", disk as char));
37             base.push(components.as_path());
38             PathBuf::from(base)
39         }
40         path::Prefix::VerbatimUNC(server, share) => {
41             let mut base = OsString::from(r"\\");
42             base.push(server);
43             base.push(r"\");
44             base.push(share);
45             base.push(components.as_path());
46             PathBuf::from(base)
47         }
48         _ => p.to_path_buf(),
49     }
50 }
51
52 #[cfg(not(windows))]
53 pub fn fix_windows_verbatim_for_gcc(p: &Path) -> PathBuf {
54     p.to_path_buf()
55 }
56
57 pub enum LinkOrCopy {
58     Link,
59     Copy,
60 }
61
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> {
66     let p = p.as_ref();
67     let q = q.as_ref();
68     match fs::remove_file(&q) {
69         Ok(()) => (),
70         Err(err) if err.kind() == io::ErrorKind::NotFound => (),
71         Err(err) => return Err(err),
72     }
73
74     match fs::hard_link(p, q) {
75         Ok(()) => Ok(LinkOrCopy::Link),
76         Err(_) => match fs::copy(p, q) {
77             Ok(_) => Ok(LinkOrCopy::Copy),
78             Err(e) => Err(e),
79         },
80     }
81 }
82
83 #[cfg(unix)]
84 pub fn path_to_c_string(p: &Path) -> CString {
85     use std::ffi::OsStr;
86     use std::os::unix::ffi::OsStrExt;
87     let p: &OsStr = p.as_ref();
88     CString::new(p.as_bytes()).unwrap()
89 }
90 #[cfg(windows)]
91 pub fn path_to_c_string(p: &Path) -> CString {
92     CString::new(p.to_str().unwrap()).unwrap()
93 }