Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent pasting a directory into itself #925

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions yazi-scheduler/src/scheduler.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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) && to != 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());
Expand Down Expand Up @@ -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) && to != from {
self.new_and_fail(id, "Cannot copy directory into itself").ok();
return;
}

let file = self.file.clone();
_ = self.micro.try_send(
async move {
Expand Down Expand Up @@ -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(())
}
}