Make delete more safer

This commit is contained in:
sxyazi 2025-01-11 11:49:25 +08:00
parent 41487dac43
commit 941436dd06
No known key found for this signature in database
2 changed files with 44 additions and 21 deletions

View file

@ -1,6 +1,6 @@
use anyhow::{Result, bail};
use anyhow::{Context, Result, bail};
use tokio::fs;
use yazi_fs::maybe_exists;
use yazi_fs::{maybe_exists, ok_or_not_found, remove_dir_clean};
use yazi_macro::outln;
use super::Dependency;
@ -24,7 +24,45 @@ Please manually delete it from: {}",
);
}
fs::remove_dir_all(&dir).await?;
let files = if self.is_flavor {
&["flavor.toml", "tmtheme.xml", "README.md", "preview.png", "LICENSE", "LICENSE-tmtheme"][..]
} else {
&["main.lua", "README.md", "LICENSE"][..]
};
for p in files.iter().map(|&f| dir.join(f)) {
ok_or_not_found(fs::remove_file(&p).await)
.with_context(|| format!("failed to delete `{}`", p.display()))?;
}
self.delete_assets().await?;
if ok_or_not_found(fs::remove_dir(&dir).await).is_ok() {
outln!("Done!")?;
} else {
outln!(
"Done!
For safety, user data has been preserved, please manually delete them within: {}",
dir.display()
)?;
}
Ok(())
}
pub(super) async fn delete_assets(&self) -> Result<()> {
let assets = self.target().join("assets");
match fs::read_dir(&assets).await {
Ok(mut it) => {
while let Some(entry) = it.next_entry().await? {
fs::remove_file(entry.path())
.await
.with_context(|| format!("failed to remove `{}`", entry.path().display()))?;
}
}
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
Err(e) => Err(e).context(format!("failed to read `{}`", assets.display()))?,
};
remove_dir_clean(&assets).await;
Ok(())
}
}

View file

@ -2,7 +2,7 @@ use std::path::PathBuf;
use anyhow::{Context, Result, bail};
use tokio::fs;
use yazi_fs::{copy_and_seal, maybe_exists, remove_dir_clean};
use yazi_fs::{copy_and_seal, maybe_exists};
use yazi_macro::outln;
use super::Dependency;
@ -49,6 +49,7 @@ Please manually delete it from `{}` and re-run the command.",
.with_context(|| format!("failed to copy `{}` to `{}`", from.display(), to.display()))?;
}
self.delete_assets().await?;
Self::deploy_assets(from.join("assets"), to.join("assets")).await?;
outln!("Done!")?;
@ -56,21 +57,6 @@ Please manually delete it from `{}` and re-run the command.",
}
async fn deploy_assets(from: PathBuf, to: PathBuf) -> Result<()> {
use std::io::ErrorKind::NotFound;
match fs::read_dir(&to).await {
Ok(mut it) => {
while let Some(entry) = it.next_entry().await? {
fs::remove_file(entry.path())
.await
.with_context(|| format!("failed to remove `{}`", entry.path().display()))?;
}
}
Err(e) if e.kind() == NotFound => {}
Err(e) => Err(e).context(format!("failed to read `{}`", to.display()))?,
};
remove_dir_clean(&to).await;
match fs::read_dir(&from).await {
Ok(mut it) => {
fs::create_dir_all(&to).await?;
@ -81,10 +67,9 @@ Please manually delete it from `{}` and re-run the command.",
})?;
}
}
Err(e) if e.kind() == NotFound => {}
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
Err(e) => Err(e).context(format!("failed to read `{}`", from.display()))?,
}
Ok(())
}
}