diff --git a/yazi-scheduler/src/file/file.rs b/yazi-scheduler/src/file/file.rs index a036faaa..64f9d3f0 100644 --- a/yazi-scheduler/src/file/file.rs +++ b/yazi-scheduler/src/file/file.rs @@ -3,7 +3,7 @@ use std::{borrow::Cow, collections::VecDeque, fs::Metadata, path::{Path, PathBuf use anyhow::{anyhow, Result}; use futures::{future::BoxFuture, FutureExt}; use tokio::{fs, io::{self, ErrorKind::{AlreadyExists, NotFound}}, sync::mpsc}; -use tracing::{debug, warn}; +use tracing::warn; use yazi_config::TASKS; use yazi_shared::fs::{accessible, calculate_size, copy_with_progress, path_relative_to, Url}; @@ -134,15 +134,6 @@ impl File { } pub async fn paste(&self, mut task: FileOpPaste) -> Result<()> { - // Prevent pasting of a directory into itself - if task.from.is_dir() && task.to.as_path().starts_with(task.from.as_path()) { - debug!( - "file_paste: cannot paste directory into itself, skipping {:?} -> {:?}", - task.from, task.to - ); - return self.succ(task.id); - } - if task.cut { match fs::rename(&task.from, &task.to).await { Ok(_) => return self.succ(task.id), diff --git a/yazi-scheduler/src/scheduler.rs b/yazi-scheduler/src/scheduler.rs index a8390e56..f8c9bec4 100644 --- a/yazi-scheduler/src/scheduler.rs +++ b/yazi-scheduler/src/scheduler.rs @@ -1,5 +1,6 @@ use std::{borrow::Cow, ffi::OsString, sync::Arc, time::Duration}; +use anyhow::Result; use futures::{future::BoxFuture, FutureExt}; use parking_lot::Mutex; use tokio::{fs, select, sync::{mpsc::{self, UnboundedReceiver}, oneshot}, task::JoinHandle}; @@ -71,6 +72,11 @@ impl Scheduler { let mut ongoing = self.ongoing.lock(); let id = ongoing.add(TaskKind::User, format!("Cut {:?} to {:?}", from, to)); + if to.starts_with(&from) { + self.new_and_fail(id, "Cannot cut directory into itself").ok(); + return; + } + ongoing.hooks.insert(id, { let ongoing = self.ongoing.clone(); let (from, to) = (from.clone(), to.clone()); @@ -104,6 +110,11 @@ impl Scheduler { let name = format!("Copy {:?} to {:?}", from, to); let id = self.ongoing.lock().add(TaskKind::User, name); + if to.starts_with(&from) { + self.new_and_fail(id, "Cannot copy directory into itself").ok(); + return; + } + let file = self.file.clone(); _ = self.micro.try_send( async move { @@ -399,4 +410,10 @@ impl Scheduler { } }) } + + fn new_and_fail(&self, id: usize, reason: &str) -> Result<()> { + self.prog.send(TaskProg::New(id, 0))?; + self.prog.send(TaskProg::Fail(id, reason.to_owned()))?; + Ok(()) + } }