Skip to content

Commit

Permalink
fix: unable to delete sealed files on Windows due to platform differe…
Browse files Browse the repository at this point in the history
…nces (#2319)
  • Loading branch information
sxyazi authored Feb 10, 2025
1 parent d75a5d6 commit 4986231
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion yazi-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ async fn main() -> ExitCode {
}
}
}
errln!("{:#}", e).ok();
errln!("{e:#}").ok();
ExitCode::FAILURE
}
}
Expand Down
6 changes: 3 additions & 3 deletions yazi-cli/src/package/delete.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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()))?;
}

Expand All @@ -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()))?;
}
Expand Down
6 changes: 3 additions & 3 deletions yazi-cli/src/package/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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();
}
}

Expand Down
13 changes: 12 additions & 1 deletion yazi-fs/src/fns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ async fn _paths_to_same_file(a: &Path, b: &Path) -> std::io::Result<bool> {

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?;
Expand All @@ -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<OsString> {
let name = p.file_name()?;
if p == fs::canonicalize(p).await.ok()? {
Expand Down

0 comments on commit 4986231

Please sign in to comment.