]> git.lizzy.rs Git - rust.git/blob - src/librustc_fs_util/lib.rs
Rollup merge of #63083 - matthewjasper:parameter-hygiene, r=petrochenkov
[rust.git] / src / librustc_fs_util / lib.rs
1 use std::path::{Path, PathBuf};
2 use std::ffi::CString;
3 use std::fs;
4 use std::io;
5
6 // Unfortunately, on windows, it looks like msvcrt.dll is silently translating
7 // verbatim paths under the hood to non-verbatim paths! This manifests itself as
8 // gcc looking like it cannot accept paths of the form `\\?\C:\...`, but the
9 // real bug seems to lie in msvcrt.dll.
10 //
11 // Verbatim paths are generally pretty rare, but the implementation of
12 // `fs::canonicalize` currently generates paths of this form, meaning that we're
13 // going to be passing quite a few of these down to gcc, so we need to deal with
14 // this case.
15 //
16 // For now we just strip the "verbatim prefix" of `\\?\` from the path. This
17 // will probably lose information in some cases, but there's not a whole lot
18 // more we can do with a buggy msvcrt...
19 //
20 // For some more information, see this comment:
21 //   https://github.com/rust-lang/rust/issues/25505#issuecomment-102876737
22 #[cfg(windows)]
23 pub fn fix_windows_verbatim_for_gcc(p: &Path) -> PathBuf {
24     use std::path;
25     use std::ffi::OsString;
26     let mut components = p.components();
27     let prefix = match components.next() {
28         Some(path::Component::Prefix(p)) => p,
29         _ => return p.to_path_buf(),
30     };
31     match prefix.kind() {
32         path::Prefix::VerbatimDisk(disk) => {
33             let mut base = OsString::from(format!("{}:", disk as char));
34             base.push(components.as_path());
35             PathBuf::from(base)
36         }
37         path::Prefix::VerbatimUNC(server, share) => {
38             let mut base = OsString::from(r"\\");
39             base.push(server);
40             base.push(r"\");
41             base.push(share);
42             base.push(components.as_path());
43             PathBuf::from(base)
44         }
45         _ => p.to_path_buf(),
46     }
47 }
48
49 #[cfg(not(windows))]
50 pub fn fix_windows_verbatim_for_gcc(p: &Path) -> PathBuf {
51     p.to_path_buf()
52 }
53
54 pub enum LinkOrCopy {
55     Link,
56     Copy,
57 }
58
59 /// Copies `p` into `q`, preferring to use hard-linking if possible. If
60 /// `q` already exists, it is removed first.
61 /// The result indicates which of the two operations has been performed.
62 pub fn link_or_copy<P: AsRef<Path>, Q: AsRef<Path>>(p: P, q: Q) -> io::Result<LinkOrCopy> {
63     let p = p.as_ref();
64     let q = q.as_ref();
65     if q.exists() {
66         fs::remove_file(&q)?;
67     }
68
69     match fs::hard_link(p, q) {
70         Ok(()) => Ok(LinkOrCopy::Link),
71         Err(_) => {
72             match fs::copy(p, q) {
73                 Ok(_) => Ok(LinkOrCopy::Copy),
74                 Err(e) => Err(e),
75             }
76         }
77     }
78 }
79
80 #[derive(Debug)]
81 pub enum RenameOrCopyRemove {
82     Rename,
83     CopyRemove,
84 }
85
86 /// Rename `p` into `q`, preferring to use `rename` if possible.
87 /// If `rename` fails (rename may fail for reasons such as crossing
88 /// filesystem), fallback to copy & remove
89 pub fn rename_or_copy_remove<P: AsRef<Path>, Q: AsRef<Path>>(p: P,
90                                                              q: Q)
91                                                              -> io::Result<RenameOrCopyRemove> {
92     let p = p.as_ref();
93     let q = q.as_ref();
94     match fs::rename(p, q) {
95         Ok(()) => Ok(RenameOrCopyRemove::Rename),
96         Err(_) => {
97             match fs::copy(p, q) {
98                 Ok(_) => {
99                     fs::remove_file(p)?;
100                     Ok(RenameOrCopyRemove::CopyRemove)
101                 }
102                 Err(e) => Err(e),
103             }
104         }
105     }
106 }
107
108 #[cfg(unix)]
109 pub fn path_to_c_string(p: &Path) -> CString {
110     use std::os::unix::ffi::OsStrExt;
111     use std::ffi::OsStr;
112     let p: &OsStr = p.as_ref();
113     CString::new(p.as_bytes()).unwrap()
114 }
115 #[cfg(windows)]
116 pub fn path_to_c_string(p: &Path) -> CString {
117     CString::new(p.to_str().unwrap()).unwrap()
118 }