From 3ad259e1b92c725708224cd9abb4f87568e3880d Mon Sep 17 00:00:00 2001 From: sxyazi Date: Mon, 24 Jun 2024 00:51:21 +0800 Subject: [PATCH] .. --- yazi-core/src/manager/commands/bulk_rename.rs | 23 ++++++------------- yazi-core/src/manager/commands/rename.rs | 4 ++-- yazi-shared/src/fs/fns.rs | 9 ++++++-- 3 files changed, 16 insertions(+), 20 deletions(-) diff --git a/yazi-core/src/manager/commands/bulk_rename.rs b/yazi-core/src/manager/commands/bulk_rename.rs index e79c0e26..e10b48df 100644 --- a/yazi-core/src/manager/commands/bulk_rename.rs +++ b/yazi-core/src/manager/commands/bulk_rename.rs @@ -6,7 +6,7 @@ use tokio::{fs::{self, OpenOptions}, io::{stdin, AsyncReadExt, AsyncWriteExt}}; use yazi_config::{OPEN, PREVIEW}; use yazi_dds::Pubsub; use yazi_proxy::{AppProxy, TasksProxy, HIDER, WATCHER}; -use yazi_shared::{fs::{max_common_root, maybe_exists, File, FilesOp, Url}, terminal_clear}; +use yazi_shared::{fs::{max_common_root, maybe_exists, paths_to_same_file, File, FilesOp, Url}, terminal_clear}; use crate::manager::Manager; @@ -84,23 +84,14 @@ impl Manager { for (o, n) in todo { let (old, new) = (root.join(&o), root.join(&n)); - if maybe_exists(&new).await { + if maybe_exists(&new).await && !paths_to_same_file(&old, &new).await { failed.push((o, n, anyhow!("Destination already exists"))); + } else if let Err(e) = fs::rename(&old, &new).await { + failed.push((o, n, e.into())); + } else if let Ok(f) = File::from(new.into()).await { + succeeded.insert(Url::from(old), f); } else { - #[cfg(unix)] - if let Err(e) = fs::rename(&old, &new).await { - failed.push((o, n, e.into())); - } else if let Ok(f) = File::from(new.into()).await { - succeeded.insert(Url::from(old), f); - } else { - failed.push((o, n, anyhow!("Failed to retrieve file info"))); - } - #[cfg(windows)] - if let Ok(f) = File::from(new.into()).await { - succeeded.insert(Url::from(old), f); - } else { - failed.push((o, n, anyhow!("Failed to retrieve file info"))); - } + failed.push((o, n, anyhow!("Failed to retrieve file info"))); } } diff --git a/yazi-core/src/manager/commands/rename.rs b/yazi-core/src/manager/commands/rename.rs index 6b13878d..b8cfc4dd 100644 --- a/yazi-core/src/manager/commands/rename.rs +++ b/yazi-core/src/manager/commands/rename.rs @@ -5,7 +5,7 @@ use tokio::fs; use yazi_config::popup::InputCfg; use yazi_dds::Pubsub; use yazi_proxy::{InputProxy, TabProxy, WATCHER}; -use yazi_shared::{event::Cmd, fs::{maybe_exists, ok_or_not_found, symlink_realpath, File, FilesOp, Url}}; +use yazi_shared::{event::Cmd, fs::{maybe_exists, ok_or_not_found, paths_to_same_file, symlink_realpath, File, FilesOp, Url}}; use crate::manager::Manager; @@ -62,7 +62,7 @@ impl Manager { } let new = hovered.parent().unwrap().join(name); - if opt.force || !maybe_exists(&new).await { + if opt.force || !maybe_exists(&new).await || paths_to_same_file(&hovered, &new).await { Self::rename_do(tab, hovered, Url::from(new)).await.ok(); return; } diff --git a/yazi-shared/src/fs/fns.rs b/yazi-shared/src/fs/fns.rs index ee7a2bf8..ffb2ef30 100644 --- a/yazi-shared/src/fs/fns.rs +++ b/yazi-shared/src/fs/fns.rs @@ -23,8 +23,13 @@ pub fn ok_or_not_found(result: io::Result<()>) -> io::Result<()> { } } +#[inline] +pub async fn paths_to_same_file(a: impl AsRef, b: impl AsRef) -> bool { + _paths_to_same_file(a.as_ref(), b.as_ref()).await.unwrap_or(false) +} + #[cfg(unix)] -pub async fn paths_to_same_file(a: &Path, b: &Path) -> io::Result { +async fn _paths_to_same_file(a: &Path, b: &Path) -> io::Result { use std::os::unix::fs::MetadataExt; let (a_, b_) = (fs::symlink_metadata(a).await?, fs::symlink_metadata(b).await?); @@ -36,7 +41,7 @@ pub async fn paths_to_same_file(a: &Path, b: &Path) -> io::Result { } #[cfg(windows)] -pub async fn paths_to_same_file(a: &Path, b: &Path) -> std::io::Result { +async fn _paths_to_same_file(a: &Path, b: &Path) -> std::io::Result { use std::os::windows::{ffi::OsStringExt, io::AsRawHandle}; use windows_sys::Win32::{Foundation::{HANDLE, MAX_PATH}, Storage::FileSystem::{GetFinalPathNameByHandleW, FILE_FLAG_BACKUP_SEMANTICS, FILE_FLAG_OPEN_REPARSE_POINT, VOLUME_NAME_DOS}};