From e849d48db6fc1880a965d543492069e00f2ba111 Mon Sep 17 00:00:00 2001 From: meteorgan Date: Sun, 1 Dec 2024 00:07:49 +0800 Subject: [PATCH] fix: don't move directory itself --- bin/oli/src/commands/mv.rs | 19 +++++++++++++++---- bin/oli/tests/mv.rs | 4 +++- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/bin/oli/src/commands/mv.rs b/bin/oli/src/commands/mv.rs index 81095208c30f..3f5fa093a010 100644 --- a/bin/oli/src/commands/mv.rs +++ b/bin/oli/src/commands/mv.rs @@ -44,13 +44,18 @@ impl MoveCmd { let (dst_op, dst_path) = cfg.parse_location(&self.destination)?; let src_meta = src_op.stat(&src_path).await?; - if !self.recursive { + if !self.recursive || src_meta.is_file() { if src_meta.is_dir() { return Err(Error::msg("can not move a directory in non-recursive mode")); } let mut actual_dst_path = dst_path.clone(); - if dst_path.is_empty() || dst_path.ends_with("/") { + if let Ok(meta) = dst_op.stat(&dst_path).await { + if meta.is_dir() && !dst_path.ends_with("/") { + actual_dst_path.push('/'); + } + } + if actual_dst_path.is_empty() || actual_dst_path.ends_with("/") { let file_name = src_path.rsplit_once("/").unwrap_or(("", &src_path)).1; actual_dst_path.push_str(file_name); } @@ -74,13 +79,18 @@ impl MoveCmd { let mut lst = src_op.lister_with(&src_path).recursive(true).await?; while let Some(entry) = lst.try_next().await? { let path = entry.path(); + if path == src_path { + continue; + } + let suffix = path.strip_prefix(prefix).expect("invalid path"); let depath = dst_root.join(suffix); - println!("Moving: {}", src_path); + println!("Moving: {}", path); let meta = entry.metadata(); if meta.is_dir() { dst_op.create_dir(&depath.to_string_lossy()).await?; + src_op.delete(path).await?; continue; } @@ -93,8 +103,9 @@ impl MoveCmd { path_metadata.content_length(), ) .await?; + + src_op.delete(path).await?; } - src_op.remove_all(&src_path).await?; Ok(()) } diff --git a/bin/oli/tests/mv.rs b/bin/oli/tests/mv.rs index d28913924dab..926a937a684e 100644 --- a/bin/oli/tests/mv.rs +++ b/bin/oli/tests/mv.rs @@ -102,7 +102,9 @@ async fn test_mv_with_recursive() -> Result<()> { assert_eq!(dst_file2_content, file2_content); assert!(fs::exists(dst_path.path().join("empty_dir/"))?); - assert!(!fs::exists(&src_path)?); + // src_path is empty now + let mut src_data = fs::read_dir(&src_path)?; + assert!(src_data.next().is_none()); Ok(()) }