feat: prioritize symbolic links when cutting remote files (#3412)

This commit is contained in:
三咲雅 misaki masa 2025-12-07 18:00:49 +08:00 committed by GitHub
parent d2e9e72684
commit 9ae8b90d23
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 87 additions and 21 deletions

View file

@ -0,0 +1,4 @@
#[derive(Clone, Copy, Debug)]
pub struct Capabilities {
pub symlink: bool,
}

View file

@ -3,7 +3,7 @@ use std::{io, path::Path, sync::Arc};
use tokio::sync::mpsc;
use yazi_shared::{path::{AsPath, PathBufDyn}, scheme::SchemeKind, strand::AsStrand, url::{Url, UrlBuf, UrlCow}};
use crate::{cha::Cha, path::absolute_url, provider::{Attrs, Provider}};
use crate::{cha::Cha, path::absolute_url, provider::{Attrs, Capabilities, Provider}};
#[derive(Clone)]
pub struct Local<'a> {
@ -25,6 +25,9 @@ impl<'a> Provider for Local<'a> {
tokio::fs::canonicalize(self.path).await.map(Into::into)
}
#[inline]
fn capabilities(&self) -> Capabilities { Capabilities { symlink: true } }
async fn casefold(&self) -> io::Result<UrlBuf> {
super::casefold(self.path).await.map(Into::into)
}

View file

@ -1,3 +1,3 @@
yazi_macro::mod_pub!(local);
yazi_macro::mod_flat!(attrs traits);
yazi_macro::mod_flat!(attrs capabilities traits);

View file

@ -4,7 +4,7 @@ use tokio::{io::{AsyncRead, AsyncSeek, AsyncWrite, AsyncWriteExt}, sync::mpsc};
use yazi_macro::ok_or_not_found;
use yazi_shared::{path::{AsPath, PathBufDyn}, strand::{AsStrand, StrandCow}, url::{AsUrl, Url, UrlBuf}};
use crate::{cha::{Cha, ChaType}, provider::Attrs};
use crate::{cha::{Cha, ChaType}, provider::{Attrs, Capabilities}};
pub trait Provider: Sized {
type File: AsyncRead + AsyncSeek + AsyncWrite + Unpin;
@ -17,6 +17,8 @@ pub trait Provider: Sized {
fn canonicalize(&self) -> impl Future<Output = io::Result<UrlBuf>>;
fn capabilities(&self) -> Capabilities;
fn casefold(&self) -> impl Future<Output = io::Result<UrlBuf>>;
fn copy<P>(&self, to: P, attrs: Attrs) -> impl Future<Output = io::Result<u64>>

View file

@ -53,7 +53,7 @@ impl File {
}
pub(crate) async fn copy_do(&self, mut task: FileInCopy) -> Result<(), FileOutCopyDo> {
ok_or_not_found!(provider::remove_file(&task.to).await); // FIXME: integrate into copy_with_progress
ok_or_not_found!(provider::remove_file(&task.to).await);
let mut it = provider::copy_with_progress(&task.from, &task.to, task.cha.unwrap()).await?;
while let Some(res) = it.recv().await {
@ -87,20 +87,30 @@ impl File {
return Ok(self.ops.out(id, FileOutCut::Succ));
}
let (mut links, mut files) = (vec![], vec![]);
let reorder = task.follow && provider::capabilities(&task.from).await?.symlink;
super::traverse::<FileOutCut, _, _, _, _, _>(
task,
async |dir| match provider::create_dir(dir).await {
Err(e) if e.kind() != AlreadyExists => Err(e)?,
_ => Ok(()),
},
async |task, cha| {
Ok(if cha.is_orphan() || (cha.is_link() && !task.follow) {
self.ops.out(id, FileOutCut::New(0));
|task, cha| {
let nofollow = cha.is_orphan() || (cha.is_link() && !task.follow);
self.ops.out(id, FileOutCut::New(if nofollow { 0 } else { cha.len }));
if nofollow {
self.queue(task.into_link(), NORMAL);
} else {
self.ops.out(id, FileOutCut::New(cha.len));
self.queue(task, LOW);
})
match (cha.is_link(), reorder) {
(_, false) => self.queue(task, LOW),
(true, true) => links.push(task),
(false, true) => files.push(task),
}
};
async { Ok(()) }
},
|err| {
self.ops.out(id, FileOutCut::Deform(err));
@ -108,11 +118,26 @@ impl File {
)
.await?;
if !links.is_empty() {
let len = links.len();
let (tx, mut rx) = mpsc::channel(len);
for task in links {
self.queue(task.with_drop(&tx), LOW);
}
for _ in 0..len {
rx.recv().await;
}
}
for task in files {
self.queue(task, LOW);
}
Ok(self.ops.out(id, FileOutCut::Succ))
}
pub(crate) async fn cut_do(&self, mut task: FileInCut) -> Result<(), FileOutCutDo> {
ok_or_not_found!(provider::remove_file(&task.to).await); // FIXME: integrate into copy_with_progress
ok_or_not_found!(provider::remove_file(&task.to).await);
let mut it = provider::copy_with_progress(&task.from, &task.to, task.cha.unwrap()).await?;
while let Some(res) = it.recv().await {

View file

@ -1,5 +1,6 @@
use std::path::PathBuf;
use std::{mem, path::PathBuf};
use tokio::sync::mpsc;
use yazi_fs::cha::Cha;
use yazi_shared::{Id, url::UrlBuf};
@ -37,20 +38,34 @@ pub(crate) struct FileInCut {
pub(crate) cha: Option<Cha>,
pub(crate) follow: bool,
pub(crate) retry: u8,
pub(crate) drop: Option<mpsc::Sender<()>>,
}
impl Drop for FileInCut {
fn drop(&mut self) {
if let Some(tx) = self.drop.take() {
tx.try_send(()).ok();
}
}
}
impl FileInCut {
pub(super) fn into_link(self) -> FileInLink {
pub(super) fn into_link(mut self) -> FileInLink {
FileInLink {
id: self.id,
from: self.from,
to: self.to,
from: mem::take(&mut self.from),
to: mem::take(&mut self.to),
cha: self.cha,
resolve: true,
relative: false,
delete: true,
}
}
pub(super) fn with_drop(mut self, drop: &mpsc::Sender<()>) -> Self {
self.drop = Some(drop.clone());
self
}
}
// --- Link

View file

@ -61,6 +61,7 @@ impl Traverse for FileInCut {
cha: Some(cha),
follow: self.follow,
retry: self.retry,
drop: self.drop.clone(),
}
}
@ -137,14 +138,14 @@ impl Traverse for FileInUpload {
pub(super) async fn traverse<R, T, D, FC, FR, E>(
mut task: T,
on_dir: D,
on_file: FC,
mut on_file: FC,
on_error: E,
) -> Result<(), R>
where
R: Debug + From<io::Error>,
T: Traverse,
D: AsyncFn(Url) -> Result<(), R>,
FC: Fn(T, Cha) -> FR,
FC: FnMut(T, Cha) -> FR,
FR: Future<Output = Result<(), R>>,
E: Fn(String),
{

View file

@ -102,7 +102,7 @@ impl Scheduler {
if !force {
to = unique_name(to, must_be_dir(&from)).await?;
}
file.cut(FileInCut { id, from, to, cha: None, follow, retry: 0 }).await
file.cut(FileInCut { id, from, to, cha: None, follow, retry: 0, drop: None }).await
});
}

View file

@ -1,7 +1,7 @@
use std::io;
use tokio::sync::mpsc;
use yazi_fs::{cha::Cha, provider::{Attrs, Provider, local::Local}};
use yazi_fs::{cha::Cha, provider::{Attrs, Capabilities, Provider, local::Local}};
use yazi_shared::{path::PathBufDyn, strand::AsStrand, url::{AsUrl, UrlBuf, UrlCow}};
use super::{Providers, ReadDir, RwFile};
@ -32,6 +32,13 @@ where
Providers::new(url.as_url()).await?.canonicalize().await
}
pub async fn capabilities<U>(url: U) -> io::Result<Capabilities>
where
U: AsUrl,
{
Ok(Providers::new(url.as_url()).await?.capabilities())
}
pub async fn casefold<U>(url: U) -> io::Result<UrlBuf>
where
U: AsUrl,

View file

@ -1,7 +1,7 @@
use std::io;
use tokio::sync::mpsc;
use yazi_fs::{cha::Cha, provider::{Attrs, Provider}};
use yazi_fs::{cha::Cha, provider::{Attrs, Capabilities, Provider}};
use yazi_shared::{path::{AsPath, PathBufDyn}, strand::AsStrand, url::{Url, UrlBuf, UrlCow}};
#[derive(Clone)]
@ -31,6 +31,13 @@ impl<'a> Provider for Providers<'a> {
}
}
fn capabilities(&self) -> Capabilities {
match self {
Self::Local(p) => p.capabilities(),
Self::Sftp(p) => p.capabilities(),
}
}
async fn casefold(&self) -> io::Result<UrlBuf> {
match self {
Self::Local(p) => p.casefold().await,

View file

@ -2,7 +2,7 @@ use std::{io, sync::Arc};
use tokio::{io::{AsyncWriteExt, BufReader, BufWriter}, sync::mpsc::Receiver};
use yazi_config::vfs::{ServiceSftp, Vfs};
use yazi_fs::{CWD, provider::{DirReader, FileHolder, Provider}};
use yazi_fs::{CWD, provider::{Capabilities, DirReader, FileHolder, Provider}};
use yazi_sftp::fs::{Attrs, Flags};
use yazi_shared::{loc::LocBuf, path::{AsPath, PathBufDyn}, pool::InternStr, scheme::SchemeKind, strand::AsStrand, url::{Url, UrlBuf, UrlCow, UrlLike}};
@ -43,6 +43,8 @@ impl<'a> Provider for Sftp<'a> {
})
}
fn capabilities(&self) -> Capabilities { Capabilities { symlink: true } }
async fn casefold(&self) -> io::Result<UrlBuf> {
let Some((parent, name)) = self.url.parent().zip(self.url.name()) else {
return Ok(self.url.to_owned());