diff --git a/rust/Cargo.lock b/rust/Cargo.lock --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -169,6 +169,7 @@ "rand_pcg 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "rayon 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "regex 1.3.4 (registry+https://github.com/rust-lang/crates.io-index)", + "same-file 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", "tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "twox-hash 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -489,6 +490,14 @@ ] [[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "scopeguard" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -570,6 +579,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] +name = "winapi-util" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -631,6 +648,7 @@ "checksum regex-syntax 0.6.14 (registry+https://github.com/rust-lang/crates.io-index)" = "b28dfe3fe9badec5dbf0a79a9cccad2cfc2ab5484bdb3e44cbd1ae8b3ba2be06" "checksum remove_dir_all 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "4a83fa3702a688b9359eccba92d153ac33fd2e8462f9e0e3fdf155239ea7792e" "checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" +"checksum same-file 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" "checksum scopeguard 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b42e15e59b18a828bbf5c58ea01debb36b9b096346de35d941dcb89009f24a0d" "checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" "checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" @@ -642,4 +660,5 @@ "checksum wasi 0.9.0+wasi-snapshot-preview1 (registry+https://github.com/rust-lang/crates.io-index)" = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" "checksum winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6" "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +"checksum winapi-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "4ccfbf554c6ad11084fb7517daca16cfdcaccbdadba4fc336f032a8b12c2ad80" "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/rust/hg-core/Cargo.toml b/rust/hg-core/Cargo.toml --- a/rust/hg-core/Cargo.toml +++ b/rust/hg-core/Cargo.toml @@ -18,7 +18,8 @@ rayon = "1.3.0" regex = "1.1.0" twox-hash = "1.5.0" +same-file = "1.0.6" [dev-dependencies] tempfile = "3.1.0" pretty_assertions = "0.6.1" \ No newline at end of file diff --git a/rust/hg-core/src/utils/files.rs b/rust/hg-core/src/utils/files.rs --- a/rust/hg-core/src/utils/files.rs +++ b/rust/hg-core/src/utils/files.rs @@ -9,13 +9,18 @@ //! Functions for fiddling with files. -use crate::utils::hg_path::{HgPath, HgPathBuf}; - -use crate::utils::replace_slice; +use crate::utils::{ + hg_path::{path_to_hg_path_buf, HgPath, HgPathBuf, HgPathError}, + path_auditor::PathAuditor, + replace_slice, +}; use lazy_static::lazy_static; +use same_file::is_same_file; +use std::borrow::ToOwned; use std::fs::Metadata; use std::iter::FusedIterator; -use std::path::Path; +use std::ops::Deref; +use std::path::{Path, PathBuf}; pub fn get_path_from_bytes(bytes: &[u8]) -> &Path { let os_str; @@ -189,9 +194,66 @@ } } +/// Returns the canonical path of `name`, given `cwd` and `root` +pub fn canonical_path( + root: impl AsRef, + cwd: impl AsRef, + name: impl AsRef, +) -> Result { + // TODO add missing normalization for other platforms + let root = root.as_ref(); + let cwd = cwd.as_ref(); + let name = name.as_ref(); + + let name = if !name.is_absolute() { + root.join(&cwd).join(&name) + } else { + name.to_owned() + }; + let mut auditor = PathAuditor::new(&root); + if name != root && name.starts_with(&root) { + let name = name.strip_prefix(&root).unwrap(); + auditor.audit_path(path_to_hg_path_buf(name)?)?; + return Ok(name.to_owned()); + } else if name == root { + return Ok("".into()); + } else { + // Determine whether `name' is in the hierarchy at or beneath `root', + // by iterating name=name.parent() until it returns `None` (can't + // check name == '/', because that doesn't work on windows). + let mut name = name.deref(); + let original_name = name.to_owned(); + loop { + let same = is_same_file(&name, &root).unwrap_or(false); + if same { + if name == original_name { + // `name` was actually the same as root (maybe a symlink) + return Ok("".into()); + } + // `name` is a symlink to root, so `original_name` is under + // root + let rel_path = original_name.strip_prefix(&name).unwrap(); + auditor.audit_path(path_to_hg_path_buf(&rel_path)?)?; + return Ok(rel_path.to_owned()); + } + name = match name.parent() { + None => break, + Some(p) => p, + }; + } + // TODO hint to the user about using --cwd + // Bubble up the responsibility to Python for now + Err(HgPathError::NotUnderRoot { + path: original_name.to_owned(), + root: root.to_owned(), + }) + } +} + #[cfg(test)] mod tests { use super::*; + use pretty_assertions::assert_eq; #[test] fn find_dirs_some() { @@ -235,4 +297,88 @@ assert_eq!(dirs.next(), None); assert_eq!(dirs.next(), None); } + + #[test] + fn test_canonical_path() { + let root = Path::new("/repo"); + let cwd = Path::new("/dir"); + let name = Path::new("filename"); + assert_eq!( + canonical_path(root, cwd, name), + Err(HgPathError::NotUnderRoot { + path: PathBuf::from("/dir/filename"), + root: root.to_path_buf() + }) + ); + + let root = Path::new("/repo"); + let cwd = Path::new("/"); + let name = Path::new("filename"); + assert_eq!( + canonical_path(root, cwd, name), + Err(HgPathError::NotUnderRoot { + path: PathBuf::from("/filename"), + root: root.to_path_buf() + }) + ); + + let root = Path::new("/repo"); + let cwd = Path::new("/"); + let name = Path::new("repo/filename"); + assert_eq!( + canonical_path(root, cwd, name), + Ok(PathBuf::from("filename")) + ); + + let root = Path::new("/repo"); + let cwd = Path::new("/repo"); + let name = Path::new("filename"); + assert_eq!( + canonical_path(root, cwd, name), + Ok(PathBuf::from("filename")) + ); + + let root = Path::new("/repo"); + let cwd = Path::new("/repo/subdir"); + let name = Path::new("filename"); + assert_eq!( + canonical_path(root, cwd, name), + Ok(PathBuf::from("subdir/filename")) + ); + } + + #[test] + fn test_canonical_path_not_rooted() { + use std::fs::create_dir; + use tempfile::tempdir; + + let base_dir = tempdir().unwrap(); + let base_dir_path = base_dir.path(); + let beneath_repo = base_dir_path.join("a"); + let root = base_dir_path.join("a/b"); + let out_of_repo = base_dir_path.join("c"); + let under_repo_symlink = out_of_repo.join("d"); + + create_dir(&beneath_repo).unwrap(); + create_dir(&root).unwrap(); + + // TODO make portable + std::os::unix::fs::symlink(&root, &out_of_repo).unwrap(); + + assert_eq!( + canonical_path(&root, Path::new(""), out_of_repo), + Ok(PathBuf::from("")) + ); + assert_eq!( + canonical_path(&root, Path::new(""), &beneath_repo), + Err(HgPathError::NotUnderRoot { + path: beneath_repo.to_owned(), + root: root.to_owned() + }) + ); + assert_eq!( + canonical_path(&root, Path::new(""), &under_repo_symlink), + Ok(PathBuf::from("d")) + ); + } }