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