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