fix: prevent pasting a directory into itself (#925)

Co-authored-by: sxyazi <sxyazi@gmail.com>
This commit is contained in:
Rolv Apneseth 2024-04-20 03:15:57 +00:00 committed by GitHub
parent 669373a030
commit a0b4ee6e6e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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) && 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());
@ -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 {
@ -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(())
}
}