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