Show More
@@ -1,305 +1,314 b'' | |||
|
1 | 1 | /* |
|
2 | 2 | * Copyright (c) Facebook, Inc. and its affiliates. |
|
3 | 3 | * |
|
4 | 4 | * This software may be used and distributed according to the terms of the |
|
5 | 5 | * GNU General Public License version 2. |
|
6 | 6 | */ |
|
7 | 7 | |
|
8 | 8 | //! Path-related utilities. |
|
9 | 9 | |
|
10 | 10 | use std::env; |
|
11 | 11 | #[cfg(not(unix))] |
|
12 | 12 | use std::fs::rename; |
|
13 | 13 | use std::fs::{self, remove_file as fs_remove_file}; |
|
14 | 14 | use std::io::{self, ErrorKind}; |
|
15 | 15 | use std::path::{Component, Path, PathBuf}; |
|
16 | 16 | |
|
17 | 17 | use anyhow::Result; |
|
18 | 18 | #[cfg(not(unix))] |
|
19 | 19 | use tempfile::Builder; |
|
20 | 20 | |
|
21 | 21 | /// Normalize a canonicalized Path for display. |
|
22 | 22 | /// |
|
23 | 23 | /// This removes the UNC prefix `\\?\` on Windows. |
|
24 | 24 | pub fn normalize_for_display(path: &str) -> &str { |
|
25 | 25 | if cfg!(windows) && path.starts_with(r"\\?\") { |
|
26 | 26 | &path[4..] |
|
27 | 27 | } else { |
|
28 | 28 | path |
|
29 | 29 | } |
|
30 | 30 | } |
|
31 | 31 | |
|
32 | 32 | /// Similar to [`normalize_for_display`]. But work on bytes. |
|
33 | 33 | pub fn normalize_for_display_bytes(path: &[u8]) -> &[u8] { |
|
34 | 34 | if cfg!(windows) && path.starts_with(br"\\?\") { |
|
35 | 35 | &path[4..] |
|
36 | 36 | } else { |
|
37 | 37 | path |
|
38 | 38 | } |
|
39 | 39 | } |
|
40 | 40 | |
|
41 | 41 | /// Return the absolute and normalized path without accessing the filesystem. |
|
42 | 42 | /// |
|
43 | 43 | /// Unlike [`fs::canonicalize`], do not follow symlinks. |
|
44 | 44 | /// |
|
45 | 45 | /// This function does not access the filesystem. Therefore it can behave |
|
46 | 46 | /// differently from the kernel or other library functions in corner cases. |
|
47 | 47 | /// For example: |
|
48 | 48 | /// |
|
49 | 49 | /// - On some systems with symlink support, `foo/bar/..` and `foo` can be |
|
50 | /// different as seen by the kernel, if `foo/bar` is a symlink. This | |
|
51 |
/// |
|
|
50 | /// different as seen by the kernel, if `foo/bar` is a symlink. This function | |
|
51 | /// always returns `foo` in this case. | |
|
52 | 52 | /// - On Windows, the official normalization rules are much more complicated. |
|
53 | 53 | /// See https://github.com/rust-lang/rust/pull/47363#issuecomment-357069527. |
|
54 | 54 | /// For example, this function cannot translate "drive relative" path like |
|
55 | 55 | /// "X:foo" to an absolute path. |
|
56 | 56 | /// |
|
57 | 57 | /// Return an error if `std::env::current_dir()` fails or if this function |
|
58 | 58 | /// fails to produce an absolute path. |
|
59 | 59 | pub fn absolute(path: impl AsRef<Path>) -> io::Result<PathBuf> { |
|
60 | 60 | let path = path.as_ref(); |
|
61 | 61 | let path = if path.is_absolute() { |
|
62 | 62 | path.to_path_buf() |
|
63 | 63 | } else { |
|
64 | 64 | std::env::current_dir()?.join(path) |
|
65 | 65 | }; |
|
66 | 66 | |
|
67 | 67 | if !path.is_absolute() { |
|
68 | 68 | return Err(io::Error::new( |
|
69 | 69 | io::ErrorKind::Other, |
|
70 | 70 | format!("cannot get absoltue path from {:?}", path), |
|
71 | 71 | )); |
|
72 | 72 | } |
|
73 | 73 | |
|
74 | 74 | let mut result = PathBuf::new(); |
|
75 | 75 | for component in path.components() { |
|
76 | 76 | match component { |
|
77 | Component::Normal(_) | Component::RootDir | Component::Prefix(_) => { | |
|
77 | Component::Normal(_) | |
|
78 | | Component::RootDir | |
|
79 | | Component::Prefix(_) => { | |
|
78 | 80 | result.push(component); |
|
79 | 81 | } |
|
80 | 82 | Component::ParentDir => { |
|
81 | 83 | result.pop(); |
|
82 | 84 | } |
|
83 | 85 | Component::CurDir => (), |
|
84 | 86 | } |
|
85 | 87 | } |
|
86 | 88 | Ok(result) |
|
87 | 89 | } |
|
88 | 90 | |
|
89 | 91 | /// Remove the file pointed by `path`. |
|
90 | 92 | #[cfg(unix)] |
|
91 | 93 | pub fn remove_file<P: AsRef<Path>>(path: P) -> Result<()> { |
|
92 | 94 | fs_remove_file(path)?; |
|
93 | 95 | Ok(()) |
|
94 | 96 | } |
|
95 | 97 | |
|
96 | 98 | /// Remove the file pointed by `path`. |
|
97 | 99 | /// |
|
98 |
/// On Windows, removing a file can fail for various reasons, including if the |
|
|
99 |
/// mapped. This can happen when the repository is accessed |
|
|
100 | /// trying to remove a packfile. To solve this, we can rename the file before trying to remove it. | |
|
100 | /// On Windows, removing a file can fail for various reasons, including if the | |
|
101 | /// file is memory mapped. This can happen when the repository is accessed | |
|
102 | /// concurrently while a background task is trying to remove a packfile. To | |
|
103 | /// solve this, we can rename the file before trying to remove it. | |
|
101 | 104 | /// If the remove operation fails, a future repack will clean it up. |
|
102 | 105 | #[cfg(not(unix))] |
|
103 | 106 | pub fn remove_file<P: AsRef<Path>>(path: P) -> Result<()> { |
|
104 | 107 | let path = path.as_ref(); |
|
105 | 108 | let extension = path |
|
106 | 109 | .extension() |
|
107 | 110 | .and_then(|ext| ext.to_str()) |
|
108 | 111 | .map_or(".to-delete".to_owned(), |ext| ".".to_owned() + ext + "-tmp"); |
|
109 | 112 | |
|
110 | 113 | let dest_path = Builder::new() |
|
111 | 114 | .prefix("") |
|
112 | 115 | .suffix(&extension) |
|
113 | 116 | .rand_bytes(8) |
|
114 | 117 | .tempfile_in(path.parent().unwrap())? |
|
115 | 118 | .into_temp_path(); |
|
116 | 119 | |
|
117 | 120 | rename(path, &dest_path)?; |
|
118 | 121 | |
|
119 |
// Ignore errors when removing the file, it will be cleaned up at a later |
|
|
122 | // Ignore errors when removing the file, it will be cleaned up at a later | |
|
123 | // time. | |
|
120 | 124 | let _ = fs_remove_file(dest_path); |
|
121 | 125 | Ok(()) |
|
122 | 126 | } |
|
123 | 127 | |
|
124 |
/// Create the directory and ignore failures when a directory of the same name |
|
|
128 | /// Create the directory and ignore failures when a directory of the same name | |
|
129 | /// already exists. | |
|
125 | 130 | pub fn create_dir(path: impl AsRef<Path>) -> io::Result<()> { |
|
126 | 131 | match fs::create_dir(path.as_ref()) { |
|
127 | 132 | Ok(()) => Ok(()), |
|
128 | 133 | Err(e) => { |
|
129 | 134 | if e.kind() == ErrorKind::AlreadyExists && path.as_ref().is_dir() { |
|
130 | 135 | Ok(()) |
|
131 | 136 | } else { |
|
132 | 137 | Err(e) |
|
133 | 138 | } |
|
134 | 139 | } |
|
135 | 140 | } |
|
136 | 141 | } |
|
137 | 142 | |
|
138 |
/// Expand the user's home directory and any environment variables references |
|
|
139 | /// the given path. | |
|
143 | /// Expand the user's home directory and any environment variables references | |
|
144 | /// in the given path. | |
|
140 | 145 | /// |
|
141 |
/// This function is designed to emulate the behavior of Mercurial's |
|
|
142 | /// function, which in turn uses Python's `os.path.expand{user,vars}` functions. This | |
|
143 | /// results in behavior that is notably different from the default expansion behavior | |
|
144 | /// of the `shellexpand` crate. In particular: | |
|
146 | /// This function is designed to emulate the behavior of Mercurial's | |
|
147 | /// `util.expandpath` function, which in turn uses Python's | |
|
148 | /// `os.path.expand{user,vars}` functions. This results in behavior that is | |
|
149 | /// notably different from the default expansion behavior of the `shellexpand` | |
|
150 | /// crate. In particular: | |
|
145 | 151 | /// |
|
146 |
/// - If a reference to an environment variable is missing or invalid, the |
|
|
147 |
/// is left unchanged in the resulting path rather than emitting an |
|
|
152 | /// - If a reference to an environment variable is missing or invalid, the | |
|
153 | /// reference is left unchanged in the resulting path rather than emitting an | |
|
154 | /// error. | |
|
148 | 155 | /// |
|
149 | 156 | /// - Home directory expansion explicitly happens after environment variable |
|
150 | 157 | /// expansion, meaning that if an environment variable is expanded into a |
|
151 | 158 | /// string starting with a tilde (`~`), the tilde will be expanded into the |
|
152 | 159 | /// user's home directory. |
|
153 | /// | |
|
154 | 160 | pub fn expand_path(path: impl AsRef<str>) -> PathBuf { |
|
155 | 161 | expand_path_impl(path.as_ref(), |k| env::var(k).ok(), dirs::home_dir) |
|
156 | 162 | } |
|
157 | 163 | |
|
158 |
/// Same as `expand_path` but explicitly takes closures for environment |
|
|
159 | /// and home directory lookup for the sake of testability. | |
|
164 | /// Same as `expand_path` but explicitly takes closures for environment | |
|
165 | /// variable and home directory lookup for the sake of testability. | |
|
160 | 166 | fn expand_path_impl<E, H>(path: &str, getenv: E, homedir: H) -> PathBuf |
|
161 | 167 | where |
|
162 | 168 | E: FnMut(&str) -> Option<String>, |
|
163 | 169 | H: FnOnce() -> Option<PathBuf>, |
|
164 | 170 | { |
|
165 | 171 | // The shellexpand crate does not expand Windows environment variables |
|
166 | 172 | // like `%PROGRAMDATA%`. We'd like to expand them too. So let's do some |
|
167 | 173 | // pre-processing. |
|
168 | 174 | // |
|
169 | 175 | // XXX: Doing this preprocessing has the unfortunate side-effect that |
|
170 | 176 | // if an environment variable fails to expand on Windows, the resulting |
|
171 | 177 | // string will contain a UNIX-style environment variable reference. |
|
172 | 178 | // |
|
173 | 179 | // e.g., "/foo/%MISSING%/bar" will expand to "/foo/${MISSING}/bar" |
|
174 | 180 | // |
|
175 | 181 | // The current approach is good enough for now, but likely needs to |
|
176 | 182 | // be improved later for correctness. |
|
177 | 183 | let path = { |
|
178 | 184 | let mut new_path = String::new(); |
|
179 | 185 | let mut is_starting = true; |
|
180 | 186 | for ch in path.chars() { |
|
181 | 187 | if ch == '%' { |
|
182 | 188 | if is_starting { |
|
183 | 189 | new_path.push_str("${"); |
|
184 | 190 | } else { |
|
185 | 191 | new_path.push('}'); |
|
186 | 192 | } |
|
187 | 193 | is_starting = !is_starting; |
|
188 | 194 | } else if cfg!(windows) && ch == '/' { |
|
189 | 195 | // Only on Windows, change "/" to "\" automatically. |
|
190 | 196 | // This makes sure "%include /foo" works as expected. |
|
191 | 197 | new_path.push('\\') |
|
192 | 198 | } else { |
|
193 | 199 | new_path.push(ch); |
|
194 | 200 | } |
|
195 | 201 | } |
|
196 | 202 | new_path |
|
197 | 203 | }; |
|
198 | 204 | |
|
199 | 205 | let path = shellexpand::env_with_context_no_errors(&path, getenv); |
|
200 | 206 | shellexpand::tilde_with_context(&path, homedir) |
|
201 | 207 | .as_ref() |
|
202 | 208 | .into() |
|
203 | 209 | } |
|
204 | 210 | |
|
205 | 211 | #[cfg(test)] |
|
206 | 212 | mod tests { |
|
207 | 213 | use super::*; |
|
208 | 214 | |
|
209 | 215 | use std::fs::File; |
|
210 | 216 | |
|
211 | 217 | use tempfile::TempDir; |
|
212 | 218 | |
|
213 | 219 | #[cfg(windows)] |
|
214 | 220 | mod windows { |
|
215 | 221 | use super::*; |
|
216 | 222 | |
|
217 | 223 | #[test] |
|
218 | 224 | fn test_absolute_fullpath() { |
|
219 | 225 | assert_eq!(absolute("C:/foo").unwrap(), Path::new("C:\\foo")); |
|
220 | 226 | assert_eq!( |
|
221 | 227 | absolute("x:\\a/b\\./.\\c").unwrap(), |
|
222 | 228 | Path::new("x:\\a\\b\\c") |
|
223 | 229 | ); |
|
224 | 230 | assert_eq!( |
|
225 | 231 | absolute("y:/a/b\\../..\\c\\../d\\./.").unwrap(), |
|
226 | 232 | Path::new("y:\\d") |
|
227 | 233 | ); |
|
228 | 234 | assert_eq!( |
|
229 | 235 | absolute("z:/a/b\\../..\\../..\\..").unwrap(), |
|
230 | 236 | Path::new("z:\\") |
|
231 | 237 | ); |
|
232 | 238 | } |
|
233 | 239 | } |
|
234 | 240 | |
|
235 | 241 | #[cfg(unix)] |
|
236 | 242 | mod unix { |
|
237 | 243 | use super::*; |
|
238 | 244 | |
|
239 | 245 | #[test] |
|
240 | 246 | fn test_absolute_fullpath() { |
|
241 | assert_eq!(absolute("/a/./b\\c/../d/.").unwrap(), Path::new("/a/d")); | |
|
247 | assert_eq!( | |
|
248 | absolute("/a/./b\\c/../d/.").unwrap(), | |
|
249 | Path::new("/a/d") | |
|
250 | ); | |
|
242 | 251 | assert_eq!(absolute("/a/../../../../b").unwrap(), Path::new("/b")); |
|
243 | 252 | assert_eq!(absolute("/../../..").unwrap(), Path::new("/")); |
|
244 | 253 | assert_eq!(absolute("/../../../").unwrap(), Path::new("/")); |
|
245 | 254 | assert_eq!( |
|
246 | 255 | absolute("//foo///bar//baz").unwrap(), |
|
247 | 256 | Path::new("/foo/bar/baz") |
|
248 | 257 | ); |
|
249 | 258 | assert_eq!(absolute("//").unwrap(), Path::new("/")); |
|
250 | 259 | } |
|
251 | 260 | } |
|
252 | 261 | |
|
253 | 262 | #[test] |
|
254 | 263 | fn test_create_dir_non_exist() -> Result<()> { |
|
255 | 264 | let tempdir = TempDir::new()?; |
|
256 | 265 | let mut path = tempdir.path().to_path_buf(); |
|
257 | 266 | path.push("dir"); |
|
258 | 267 | create_dir(&path)?; |
|
259 | 268 | assert!(path.is_dir()); |
|
260 | 269 | Ok(()) |
|
261 | 270 | } |
|
262 | 271 | |
|
263 | 272 | #[test] |
|
264 | 273 | fn test_create_dir_exist() -> Result<()> { |
|
265 | 274 | let tempdir = TempDir::new()?; |
|
266 | 275 | let mut path = tempdir.path().to_path_buf(); |
|
267 | 276 | path.push("dir"); |
|
268 | 277 | create_dir(&path)?; |
|
269 | 278 | assert!(&path.is_dir()); |
|
270 | 279 | create_dir(&path)?; |
|
271 | 280 | assert!(&path.is_dir()); |
|
272 | 281 | Ok(()) |
|
273 | 282 | } |
|
274 | 283 | |
|
275 | 284 | #[test] |
|
276 | 285 | fn test_create_dir_file_exist() -> Result<()> { |
|
277 | 286 | let tempdir = TempDir::new()?; |
|
278 | 287 | let mut path = tempdir.path().to_path_buf(); |
|
279 | 288 | path.push("dir"); |
|
280 | 289 | File::create(&path)?; |
|
281 | 290 | let err = create_dir(&path).unwrap_err(); |
|
282 | 291 | assert_eq!(err.kind(), ErrorKind::AlreadyExists); |
|
283 | 292 | Ok(()) |
|
284 | 293 | } |
|
285 | 294 | |
|
286 | 295 | #[test] |
|
287 | 296 | fn test_path_expansion() { |
|
288 | 297 | fn getenv(key: &str) -> Option<String> { |
|
289 | 298 | match key { |
|
290 | 299 | "foo" => Some("~/a".into()), |
|
291 | 300 | "bar" => Some("b".into()), |
|
292 | 301 | _ => None, |
|
293 | 302 | } |
|
294 | 303 | } |
|
295 | 304 | |
|
296 | 305 | fn homedir() -> Option<PathBuf> { |
|
297 | 306 | Some(PathBuf::from("/home/user")) |
|
298 | 307 | } |
|
299 | 308 | |
|
300 | 309 | let path = "$foo/${bar}/$baz"; |
|
301 | 310 | let expected = PathBuf::from("/home/user/a/b/$baz"); |
|
302 | 311 | |
|
303 | 312 | assert_eq!(expand_path_impl(&path, getenv, homedir), expected); |
|
304 | 313 | } |
|
305 | 314 | } |
General Comments 0
You need to be logged in to leave comments.
Login now