1use std::ffi::{CString, OsStr};
2use std::path::{Path, PathBuf, absolute};
3use std::{env, fs, io};
4
5use tempfile::TempDir;
6
7#[cfg(windows)]
24pub fn fix_windows_verbatim_for_gcc(p: &Path) -> PathBuf {
25 use std::ffi::OsString;
26 use std::path;
27 let mut components = p.components();
28 let prefix = match components.next() {
29 Some(path::Component::Prefix(p)) => p,
30 _ => return p.to_path_buf(),
31 };
32 match prefix.kind() {
33 path::Prefix::VerbatimDisk(disk) => {
34 let mut base = OsString::from(format!("{}:", disk as char));
35 base.push(components.as_path());
36 PathBuf::from(base)
37 }
38 path::Prefix::VerbatimUNC(server, share) => {
39 let mut base = OsString::from(r"\\");
40 base.push(server);
41 base.push(r"\");
42 base.push(share);
43 base.push(components.as_path());
44 PathBuf::from(base)
45 }
46 _ => p.to_path_buf(),
47 }
48}
49
50#[cfg(not(windows))]
51pub fn fix_windows_verbatim_for_gcc(p: &Path) -> PathBuf {
52 p.to_path_buf()
53}
54
55pub enum LinkOrCopy {
56 Link,
57 Copy,
58}
59
60pub fn link_or_copy<P: AsRef<Path>, Q: AsRef<Path>>(p: P, q: Q) -> io::Result<LinkOrCopy> {
63 let p = p.as_ref();
69 let q = q.as_ref();
70
71 let err = match fs::hard_link(p, q) {
72 Ok(()) => return Ok(LinkOrCopy::Link),
73 Err(err) => err,
74 };
75
76 if err.kind() == io::ErrorKind::AlreadyExists {
77 fs::remove_file(q)?;
78 if fs::hard_link(p, q).is_ok() {
79 return Ok(LinkOrCopy::Link);
80 }
81 }
82
83 fs::copy(p, q).map(|_| LinkOrCopy::Copy)
85}
86
87#[cfg(any(unix, all(target_os = "wasi", target_env = "p1")))]
88pub fn path_to_c_string(p: &Path) -> CString {
89 use std::ffi::OsStr;
90 #[cfg(unix)]
91 use std::os::unix::ffi::OsStrExt;
92 #[cfg(all(target_os = "wasi", target_env = "p1"))]
93 use std::os::wasi::ffi::OsStrExt;
94
95 let p: &OsStr = p.as_ref();
96 CString::new(p.as_bytes()).unwrap()
97}
98#[cfg(windows)]
99pub fn path_to_c_string(p: &Path) -> CString {
100 CString::new(p.to_str().unwrap()).unwrap()
101}
102
103#[inline]
104pub fn try_canonicalize<P: AsRef<Path>>(path: P) -> io::Result<PathBuf> {
105 fs::canonicalize(&path).or_else(|_| absolute(&path))
106}
107
108pub struct TempDirBuilder<'a, 'b> {
109 builder: tempfile::Builder<'a, 'b>,
110}
111
112impl<'a, 'b> TempDirBuilder<'a, 'b> {
113 pub fn new() -> Self {
114 Self { builder: tempfile::Builder::new() }
115 }
116
117 pub fn prefix<S: AsRef<OsStr> + ?Sized>(&mut self, prefix: &'a S) -> &mut Self {
118 self.builder.prefix(prefix);
119 self
120 }
121
122 pub fn suffix<S: AsRef<OsStr> + ?Sized>(&mut self, suffix: &'b S) -> &mut Self {
123 self.builder.suffix(suffix);
124 self
125 }
126
127 pub fn tempdir_in<P: AsRef<Path>>(&self, dir: P) -> io::Result<TempDir> {
128 let dir = dir.as_ref();
129 #[cfg(windows)]
133 for wait in 1..11 {
134 match self.builder.tempdir_in(dir) {
135 Err(e) if e.kind() == io::ErrorKind::PermissionDenied => {}
136 t => return t,
137 }
138 std::thread::sleep(std::time::Duration::from_millis(1 << wait));
139 }
140 self.builder.tempdir_in(dir)
141 }
142
143 pub fn tempdir(&self) -> io::Result<TempDir> {
144 self.tempdir_in(env::temp_dir())
145 }
146}