From 8c412260d2bb566412ed9e827b82139d45b092eb Mon Sep 17 00:00:00 2001 From: sxyazi Date: Mon, 25 Sep 2023 01:11:57 +0800 Subject: [PATCH] .. --- core/src/tasks/workers/file.rs | 3 ++- shared/src/fns.rs | 31 ++++++++++++++++++++++--------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/core/src/tasks/workers/file.rs b/core/src/tasks/workers/file.rs index d822f919..c1f6ef5e 100644 --- a/core/src/tasks/workers/file.rs +++ b/core/src/tasks/workers/file.rs @@ -131,7 +131,8 @@ impl File { Cow::Borrowed(task.from.as_path()) }; - let src = if task.relative { path_relative_to(&src, &task.to) } else { src }; + let src = + if task.relative { path_relative_to(&src, task.to.parent().unwrap()) } else { src }; match fs::remove_file(&task.to).await { Err(e) if e.kind() != NotFound => Err(e)?, diff --git a/shared/src/fns.rs b/shared/src/fns.rs index 69799077..be1daa28 100644 --- a/shared/src/fns.rs +++ b/shared/src/fns.rs @@ -73,8 +73,7 @@ pub async fn unique_path(mut p: Url) -> Url { // * Unix: The relative format to `root` of `path`. // * Windows: The relative format to `root` of `path`; or `path` itself when // `path` and `root` are both under different disk drives. -pub fn path_relative_to>(path: &Path, root: P) -> Cow<'_, Path> { - let root = root.as_ref(); +pub fn path_relative_to<'a>(path: &'a Path, root: &Path) -> Cow<'a, Path> { assert!(path.is_absolute()); assert!(root.is_absolute()); let mut p_comps = path.components(); @@ -131,14 +130,28 @@ mod tests { #[cfg(unix)] #[test] fn test_path_relative_to() { - assert_path_relate_to_root("/a/b", "/a/b/c", "../"); - assert_path_relate_to_root("/a/b/c", "/a/b", "c"); - assert_path_relate_to_root("/a/b/c", "/a/b/d", "../c"); - assert_path_relate_to_root("/a", "/a/b/c", "../../"); - assert_path_relate_to_root("/a/a/b", "/a/b/b", "../../b/b"); + fn assert(path: &str, root: &str, res: &str) { + assert_eq!(path_relative_to(Path::new(path), Path::new(root)), Cow::Borrowed(Path::new(res))); + } + + assert("/a/b", "/a/b/c", "../"); + assert("/a/b/c", "/a/b", "c"); + assert("/a/b/c", "/a/b/d", "../c"); + assert("/a", "/a/b/c", "../../"); + assert("/a/a/b", "/a/b/b", "../../a/b"); } - fn assert_path_relate_to_root(path: &str, root: &str, res: &str) { - assert_eq!(path_relative_to(Path::new(path), Path::new(root)), Cow::Borrowed(Path::new(res))); + #[cfg(windows)] + #[test] + fn test_path_relative_to() { + fn assert(path: &str, root: &str, res: &str) { + assert_eq!(path_relative_to(Path::new(path), Path::new(root)), Cow::Borrowed(Path::new(res))); + } + + assert("C:\\a\\b", "C:\\a\\b\\c", "..\\"); + assert("C:\\a\\b\\c", "C:\\a\\b", "c"); + assert("C:\\a\\b\\c", "C:\\a\\b\\d", "..\\c"); + assert("C:\\a", "C:\\a\\b\\c", "..\\..\\"); + assert("C:\\a\\a\\b", "C:\\a\\b\\b", "..\\..\\a\\b"); } }