This commit is contained in:
sxyazi 2024-06-24 00:51:21 +08:00
parent e263a0d477
commit 3ad259e1b9
No known key found for this signature in database
3 changed files with 16 additions and 20 deletions

View file

@ -6,7 +6,7 @@ use tokio::{fs::{self, OpenOptions}, io::{stdin, AsyncReadExt, AsyncWriteExt}};
use yazi_config::{OPEN, PREVIEW}; use yazi_config::{OPEN, PREVIEW};
use yazi_dds::Pubsub; use yazi_dds::Pubsub;
use yazi_proxy::{AppProxy, TasksProxy, HIDER, WATCHER}; 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; use crate::manager::Manager;
@ -84,24 +84,15 @@ impl Manager {
for (o, n) in todo { for (o, n) in todo {
let (old, new) = (root.join(&o), root.join(&n)); 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"))); failed.push((o, n, anyhow!("Destination already exists")));
} else { } else if let Err(e) = fs::rename(&old, &new).await {
#[cfg(unix)]
if let Err(e) = fs::rename(&old, &new).await {
failed.push((o, n, e.into())); failed.push((o, n, e.into()));
} else if let Ok(f) = File::from(new.into()).await { } else if let Ok(f) = File::from(new.into()).await {
succeeded.insert(Url::from(old), f); succeeded.insert(Url::from(old), f);
} else { } else {
failed.push((o, n, anyhow!("Failed to retrieve file info"))); 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")));
}
}
} }
if !succeeded.is_empty() { if !succeeded.is_empty() {

View file

@ -5,7 +5,7 @@ use tokio::fs;
use yazi_config::popup::InputCfg; use yazi_config::popup::InputCfg;
use yazi_dds::Pubsub; use yazi_dds::Pubsub;
use yazi_proxy::{InputProxy, TabProxy, WATCHER}; 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; use crate::manager::Manager;
@ -62,7 +62,7 @@ impl Manager {
} }
let new = hovered.parent().unwrap().join(name); 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(); Self::rename_do(tab, hovered, Url::from(new)).await.ok();
return; return;
} }

View file

@ -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<Path>, b: impl AsRef<Path>) -> bool {
_paths_to_same_file(a.as_ref(), b.as_ref()).await.unwrap_or(false)
}
#[cfg(unix)] #[cfg(unix)]
pub async fn paths_to_same_file(a: &Path, b: &Path) -> io::Result<bool> { async fn _paths_to_same_file(a: &Path, b: &Path) -> io::Result<bool> {
use std::os::unix::fs::MetadataExt; use std::os::unix::fs::MetadataExt;
let (a_, b_) = (fs::symlink_metadata(a).await?, fs::symlink_metadata(b).await?); 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<bool> {
} }
#[cfg(windows)] #[cfg(windows)]
pub async fn paths_to_same_file(a: &Path, b: &Path) -> std::io::Result<bool> { async fn _paths_to_same_file(a: &Path, b: &Path) -> std::io::Result<bool> {
use std::os::windows::{ffi::OsStringExt, io::AsRawHandle}; 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}}; use windows_sys::Win32::{Foundation::{HANDLE, MAX_PATH}, Storage::FileSystem::{GetFinalPathNameByHandleW, FILE_FLAG_BACKUP_SEMANTICS, FILE_FLAG_OPEN_REPARSE_POINT, VOLUME_NAME_DOS}};