fast_rustc_ap_rustc_fs_util/
lib.rs1use std::ffi::CString;
2use std::fs;
3use std::io;
4use std::path::{Path, PathBuf};
5
6#[cfg(windows)]
23pub fn fix_windows_verbatim_for_gcc(p: &Path) -> PathBuf {
24 use std::ffi::OsString;
25 use std::path;
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))]
50pub fn fix_windows_verbatim_for_gcc(p: &Path) -> PathBuf {
51 p.to_path_buf()
52}
53
54pub enum LinkOrCopy {
55 Link,
56 Copy,
57}
58
59pub 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(_) => match fs::copy(p, q) {
72 Ok(_) => Ok(LinkOrCopy::Copy),
73 Err(e) => Err(e),
74 },
75 }
76}
77
78#[derive(Debug)]
79pub enum RenameOrCopyRemove {
80 Rename,
81 CopyRemove,
82}
83
84pub fn rename_or_copy_remove<P: AsRef<Path>, Q: AsRef<Path>>(
88 p: P,
89 q: Q,
90) -> io::Result<RenameOrCopyRemove> {
91 let p = p.as_ref();
92 let q = q.as_ref();
93 match fs::rename(p, q) {
94 Ok(()) => Ok(RenameOrCopyRemove::Rename),
95 Err(_) => match fs::copy(p, q) {
96 Ok(_) => {
97 fs::remove_file(p)?;
98 Ok(RenameOrCopyRemove::CopyRemove)
99 }
100 Err(e) => Err(e),
101 },
102 }
103}
104
105#[cfg(unix)]
106pub fn path_to_c_string(p: &Path) -> CString {
107 use std::ffi::OsStr;
108 use std::os::unix::ffi::OsStrExt;
109 let p: &OsStr = p.as_ref();
110 CString::new(p.as_bytes()).unwrap()
111}
112#[cfg(windows)]
113pub fn path_to_c_string(p: &Path) -> CString {
114 CString::new(p.to_str().unwrap()).unwrap()
115}