Show More
@@ -169,6 +169,7 b' dependencies = [' | |||
|
169 | 169 | "rand_pcg 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", |
|
170 | 170 | "rayon 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", |
|
171 | 171 | "regex 1.3.4 (registry+https://github.com/rust-lang/crates.io-index)", |
|
172 | "same-file 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", | |
|
172 | 173 | "tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)", |
|
173 | 174 | "twox-hash 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)", |
|
174 | 175 | ] |
@@ -489,6 +490,14 b' dependencies = [' | |||
|
489 | 490 | ] |
|
490 | 491 | |
|
491 | 492 | [[package]] |
|
493 | name = "same-file" | |
|
494 | version = "1.0.6" | |
|
495 | source = "registry+https://github.com/rust-lang/crates.io-index" | |
|
496 | dependencies = [ | |
|
497 | "winapi-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", | |
|
498 | ] | |
|
499 | ||
|
500 | [[package]] | |
|
492 | 501 | name = "scopeguard" |
|
493 | 502 | version = "1.0.0" |
|
494 | 503 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -570,6 +579,14 b' version = "0.4.0"' | |||
|
570 | 579 | source = "registry+https://github.com/rust-lang/crates.io-index" |
|
571 | 580 | |
|
572 | 581 | [[package]] |
|
582 | name = "winapi-util" | |
|
583 | version = "0.1.3" | |
|
584 | source = "registry+https://github.com/rust-lang/crates.io-index" | |
|
585 | dependencies = [ | |
|
586 | "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", | |
|
587 | ] | |
|
588 | ||
|
589 | [[package]] | |
|
573 | 590 | name = "winapi-x86_64-pc-windows-gnu" |
|
574 | 591 | version = "0.4.0" |
|
575 | 592 | source = "registry+https://github.com/rust-lang/crates.io-index" |
@@ -631,6 +648,7 b' source = "registry+https://github.com/ru' | |||
|
631 | 648 | "checksum regex-syntax 0.6.14 (registry+https://github.com/rust-lang/crates.io-index)" = "b28dfe3fe9badec5dbf0a79a9cccad2cfc2ab5484bdb3e44cbd1ae8b3ba2be06" |
|
632 | 649 | "checksum remove_dir_all 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "4a83fa3702a688b9359eccba92d153ac33fd2e8462f9e0e3fdf155239ea7792e" |
|
633 | 650 | "checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" |
|
651 | "checksum same-file 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" | |
|
634 | 652 | "checksum scopeguard 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b42e15e59b18a828bbf5c58ea01debb36b9b096346de35d941dcb89009f24a0d" |
|
635 | 653 | "checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" |
|
636 | 654 | "checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" |
@@ -642,4 +660,5 b' source = "registry+https://github.com/ru' | |||
|
642 | 660 | "checksum wasi 0.9.0+wasi-snapshot-preview1 (registry+https://github.com/rust-lang/crates.io-index)" = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" |
|
643 | 661 | "checksum winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6" |
|
644 | 662 | "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" |
|
663 | "checksum winapi-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "4ccfbf554c6ad11084fb7517daca16cfdcaccbdadba4fc336f032a8b12c2ad80" | |
|
645 | 664 | "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" |
@@ -18,6 +18,7 b' rand_pcg = "0.1.1"' | |||
|
18 | 18 | rayon = "1.3.0" |
|
19 | 19 | regex = "1.1.0" |
|
20 | 20 | twox-hash = "1.5.0" |
|
21 | same-file = "1.0.6" | |
|
21 | 22 | |
|
22 | 23 | [dev-dependencies] |
|
23 | 24 | tempfile = "3.1.0" |
@@ -9,13 +9,18 b'' | |||
|
9 | 9 | |
|
10 | 10 | //! Functions for fiddling with files. |
|
11 | 11 | |
|
12 | use crate::utils::hg_path::{HgPath, HgPathBuf}; | |
|
13 | ||
|
14 | use crate::utils::replace_slice; | |
|
12 | use crate::utils::{ | |
|
13 | hg_path::{path_to_hg_path_buf, HgPath, HgPathBuf, HgPathError}, | |
|
14 | path_auditor::PathAuditor, | |
|
15 | replace_slice, | |
|
16 | }; | |
|
15 | 17 | use lazy_static::lazy_static; |
|
18 | use same_file::is_same_file; | |
|
19 | use std::borrow::ToOwned; | |
|
16 | 20 | use std::fs::Metadata; |
|
17 | 21 | use std::iter::FusedIterator; |
|
18 |
use std:: |
|
|
22 | use std::ops::Deref; | |
|
23 | use std::path::{Path, PathBuf}; | |
|
19 | 24 | |
|
20 | 25 | pub fn get_path_from_bytes(bytes: &[u8]) -> &Path { |
|
21 | 26 | let os_str; |
@@ -189,9 +194,66 b' impl HgMetadata {' | |||
|
189 | 194 | } |
|
190 | 195 | } |
|
191 | 196 | |
|
197 | /// Returns the canonical path of `name`, given `cwd` and `root` | |
|
198 | pub fn canonical_path( | |
|
199 | root: impl AsRef<Path>, | |
|
200 | cwd: impl AsRef<Path>, | |
|
201 | name: impl AsRef<Path>, | |
|
202 | ) -> Result<PathBuf, HgPathError> { | |
|
203 | // TODO add missing normalization for other platforms | |
|
204 | let root = root.as_ref(); | |
|
205 | let cwd = cwd.as_ref(); | |
|
206 | let name = name.as_ref(); | |
|
207 | ||
|
208 | let name = if !name.is_absolute() { | |
|
209 | root.join(&cwd).join(&name) | |
|
210 | } else { | |
|
211 | name.to_owned() | |
|
212 | }; | |
|
213 | let mut auditor = PathAuditor::new(&root); | |
|
214 | if name != root && name.starts_with(&root) { | |
|
215 | let name = name.strip_prefix(&root).unwrap(); | |
|
216 | auditor.audit_path(path_to_hg_path_buf(name)?)?; | |
|
217 | return Ok(name.to_owned()); | |
|
218 | } else if name == root { | |
|
219 | return Ok("".into()); | |
|
220 | } else { | |
|
221 | // Determine whether `name' is in the hierarchy at or beneath `root', | |
|
222 | // by iterating name=name.parent() until it returns `None` (can't | |
|
223 | // check name == '/', because that doesn't work on windows). | |
|
224 | let mut name = name.deref(); | |
|
225 | let original_name = name.to_owned(); | |
|
226 | loop { | |
|
227 | let same = is_same_file(&name, &root).unwrap_or(false); | |
|
228 | if same { | |
|
229 | if name == original_name { | |
|
230 | // `name` was actually the same as root (maybe a symlink) | |
|
231 | return Ok("".into()); | |
|
232 | } | |
|
233 | // `name` is a symlink to root, so `original_name` is under | |
|
234 | // root | |
|
235 | let rel_path = original_name.strip_prefix(&name).unwrap(); | |
|
236 | auditor.audit_path(path_to_hg_path_buf(&rel_path)?)?; | |
|
237 | return Ok(rel_path.to_owned()); | |
|
238 | } | |
|
239 | name = match name.parent() { | |
|
240 | None => break, | |
|
241 | Some(p) => p, | |
|
242 | }; | |
|
243 | } | |
|
244 | // TODO hint to the user about using --cwd | |
|
245 | // Bubble up the responsibility to Python for now | |
|
246 | Err(HgPathError::NotUnderRoot { | |
|
247 | path: original_name.to_owned(), | |
|
248 | root: root.to_owned(), | |
|
249 | }) | |
|
250 | } | |
|
251 | } | |
|
252 | ||
|
192 | 253 | #[cfg(test)] |
|
193 | 254 | mod tests { |
|
194 | 255 | use super::*; |
|
256 | use pretty_assertions::assert_eq; | |
|
195 | 257 | |
|
196 | 258 | #[test] |
|
197 | 259 | fn find_dirs_some() { |
@@ -235,4 +297,88 b' mod tests {' | |||
|
235 | 297 | assert_eq!(dirs.next(), None); |
|
236 | 298 | assert_eq!(dirs.next(), None); |
|
237 | 299 | } |
|
300 | ||
|
301 | #[test] | |
|
302 | fn test_canonical_path() { | |
|
303 | let root = Path::new("/repo"); | |
|
304 | let cwd = Path::new("/dir"); | |
|
305 | let name = Path::new("filename"); | |
|
306 | assert_eq!( | |
|
307 | canonical_path(root, cwd, name), | |
|
308 | Err(HgPathError::NotUnderRoot { | |
|
309 | path: PathBuf::from("/dir/filename"), | |
|
310 | root: root.to_path_buf() | |
|
311 | }) | |
|
312 | ); | |
|
313 | ||
|
314 | let root = Path::new("/repo"); | |
|
315 | let cwd = Path::new("/"); | |
|
316 | let name = Path::new("filename"); | |
|
317 | assert_eq!( | |
|
318 | canonical_path(root, cwd, name), | |
|
319 | Err(HgPathError::NotUnderRoot { | |
|
320 | path: PathBuf::from("/filename"), | |
|
321 | root: root.to_path_buf() | |
|
322 | }) | |
|
323 | ); | |
|
324 | ||
|
325 | let root = Path::new("/repo"); | |
|
326 | let cwd = Path::new("/"); | |
|
327 | let name = Path::new("repo/filename"); | |
|
328 | assert_eq!( | |
|
329 | canonical_path(root, cwd, name), | |
|
330 | Ok(PathBuf::from("filename")) | |
|
331 | ); | |
|
332 | ||
|
333 | let root = Path::new("/repo"); | |
|
334 | let cwd = Path::new("/repo"); | |
|
335 | let name = Path::new("filename"); | |
|
336 | assert_eq!( | |
|
337 | canonical_path(root, cwd, name), | |
|
338 | Ok(PathBuf::from("filename")) | |
|
339 | ); | |
|
340 | ||
|
341 | let root = Path::new("/repo"); | |
|
342 | let cwd = Path::new("/repo/subdir"); | |
|
343 | let name = Path::new("filename"); | |
|
344 | assert_eq!( | |
|
345 | canonical_path(root, cwd, name), | |
|
346 | Ok(PathBuf::from("subdir/filename")) | |
|
347 | ); | |
|
348 | } | |
|
349 | ||
|
350 | #[test] | |
|
351 | fn test_canonical_path_not_rooted() { | |
|
352 | use std::fs::create_dir; | |
|
353 | use tempfile::tempdir; | |
|
354 | ||
|
355 | let base_dir = tempdir().unwrap(); | |
|
356 | let base_dir_path = base_dir.path(); | |
|
357 | let beneath_repo = base_dir_path.join("a"); | |
|
358 | let root = base_dir_path.join("a/b"); | |
|
359 | let out_of_repo = base_dir_path.join("c"); | |
|
360 | let under_repo_symlink = out_of_repo.join("d"); | |
|
361 | ||
|
362 | create_dir(&beneath_repo).unwrap(); | |
|
363 | create_dir(&root).unwrap(); | |
|
364 | ||
|
365 | // TODO make portable | |
|
366 | std::os::unix::fs::symlink(&root, &out_of_repo).unwrap(); | |
|
367 | ||
|
368 | assert_eq!( | |
|
369 | canonical_path(&root, Path::new(""), out_of_repo), | |
|
370 | Ok(PathBuf::from("")) | |
|
371 | ); | |
|
372 | assert_eq!( | |
|
373 | canonical_path(&root, Path::new(""), &beneath_repo), | |
|
374 | Err(HgPathError::NotUnderRoot { | |
|
375 | path: beneath_repo.to_owned(), | |
|
376 | root: root.to_owned() | |
|
377 | }) | |
|
378 | ); | |
|
379 | assert_eq!( | |
|
380 | canonical_path(&root, Path::new(""), &under_repo_symlink), | |
|
381 | Ok(PathBuf::from("d")) | |
|
382 | ); | |
|
383 | } | |
|
238 | 384 | } |
General Comments 0
You need to be logged in to leave comments.
Login now