From c735a6b358a97149c7ab1dde9044f176108d683d Mon Sep 17 00:00:00 2001 From: sxyazi Date: Tue, 11 Feb 2025 01:46:52 +0800 Subject: [PATCH] fix: unable to delete sealed files on Windows due to platform differences --- yazi-cli/src/main.rs | 2 +- yazi-cli/src/package/delete.rs | 6 +++--- yazi-cli/src/package/package.rs | 6 +++--- yazi-fs/src/fns.rs | 13 ++++++++++++- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/yazi-cli/src/main.rs b/yazi-cli/src/main.rs index ebb69f549..9e6029b8a 100644 --- a/yazi-cli/src/main.rs +++ b/yazi-cli/src/main.rs @@ -19,7 +19,7 @@ async fn main() -> ExitCode { } } } - errln!("{:#}", e).ok(); + errln!("{e:#}").ok(); ExitCode::FAILURE } } diff --git a/yazi-cli/src/package/delete.rs b/yazi-cli/src/package/delete.rs index f9f2e9aeb..76bbe2cf7 100644 --- a/yazi-cli/src/package/delete.rs +++ b/yazi-cli/src/package/delete.rs @@ -1,6 +1,6 @@ use anyhow::{Context, Result, bail}; use tokio::fs; -use yazi_fs::{maybe_exists, ok_or_not_found, remove_dir_clean}; +use yazi_fs::{maybe_exists, ok_or_not_found, remove_dir_clean, remove_sealed}; use yazi_macro::outln; use super::Dependency; @@ -30,7 +30,7 @@ Please manually delete it from: {}", &["main.lua", "README.md", "LICENSE"][..] }; for p in files.iter().map(|&f| dir.join(f)) { - ok_or_not_found(fs::remove_file(&p).await) + ok_or_not_found(remove_sealed(&p).await) .with_context(|| format!("failed to delete `{}`", p.display()))?; } @@ -53,7 +53,7 @@ For safety, user data has been preserved, please manually delete them within: {} match fs::read_dir(&assets).await { Ok(mut it) => { while let Some(entry) = it.next_entry().await? { - fs::remove_file(entry.path()) + remove_sealed(&entry.path()) .await .with_context(|| format!("failed to remove `{}`", entry.path().display()))?; } diff --git a/yazi-cli/src/package/package.rs b/yazi-cli/src/package/package.rs index d85fa3a10..994ec66ab 100644 --- a/yazi-cli/src/package/package.rs +++ b/yazi-cli/src/package/package.rs @@ -3,7 +3,7 @@ use std::{path::{Path, PathBuf}, str::FromStr}; use anyhow::{Context, Result, bail}; use serde::{Deserialize, Deserializer, Serialize, Serializer}; use tokio::fs; -use yazi_fs::{Xdg, ok_or_not_found, unique_name}; +use yazi_fs::{Xdg, ok_or_not_found, remove_sealed, unique_name}; use yazi_macro::outln; use super::Dependency; @@ -134,7 +134,7 @@ impl Package { if d.hash.is_empty() { d.hash = d.hash().await?; } - fs::remove_file(&tracker).await.ok(); + remove_sealed(&tracker).await.ok(); } } for d in &mut self.flavors { @@ -156,7 +156,7 @@ impl Package { if d.hash.is_empty() { d.hash = d.hash().await?; } - fs::remove_file(&tracker).await.ok(); + remove_sealed(&tracker).await.ok(); } } diff --git a/yazi-fs/src/fns.rs b/yazi-fs/src/fns.rs index ca799a79a..45206338c 100644 --- a/yazi-fs/src/fns.rs +++ b/yazi-fs/src/fns.rs @@ -85,7 +85,7 @@ async fn _paths_to_same_file(a: &Path, b: &Path) -> std::io::Result { pub async fn copy_and_seal(from: &Path, to: &Path) -> io::Result<()> { let b = fs::read(from).await?; - ok_or_not_found(fs::remove_file(to).await)?; + ok_or_not_found(remove_sealed(to).await)?; let mut file = fs::OpenOptions::new().create_new(true).write(true).truncate(true).open(to).await?; @@ -98,6 +98,17 @@ pub async fn copy_and_seal(from: &Path, to: &Path) -> io::Result<()> { Ok(()) } +pub async fn remove_sealed(p: &Path) -> io::Result<()> { + #[cfg(windows)] + { + let mut perm = fs::metadata(p).await?.permissions(); + perm.set_readonly(false); + fs::set_permissions(p, perm).await?; + } + + fs::remove_file(p).await +} + pub async fn realname(p: &Path) -> Option { let name = p.file_name()?; if p == fs::canonicalize(p).await.ok()? {