From af9a875512c4e0b712f32c12d274e84fc83e9930 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E9=9B=85=20=C2=B7=20Misaki=20Masa?= Date: Sat, 25 Jan 2025 21:10:05 +0800 Subject: [PATCH] feat: support `package.toml` as a symlink (#2245) --- yazi-cli/src/package/package.rs | 10 +++++----- yazi-fs/src/fns.rs | 13 +++++-------- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/yazi-cli/src/package/package.rs b/yazi-cli/src/package/package.rs index 670dbab6..a23e1a98 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, create_and_seal, ok_or_not_found, unique_name}; +use yazi_fs::{Xdg, ok_or_not_found, unique_name}; use yazi_macro::outln; use super::Dependency; @@ -41,7 +41,7 @@ impl Package { } let s = toml::to_string_pretty(self)?; - create_and_seal(&Self::toml(), s.as_bytes()).await.context("Failed to write package.toml") + fs::write(Self::toml(), s).await.context("Failed to write package.toml") } pub(crate) async fn delete(&mut self, use_: &str) -> Result<()> { @@ -57,7 +57,7 @@ impl Package { } let s = toml::to_string_pretty(self)?; - create_and_seal(&Self::toml(), s.as_bytes()).await.context("Failed to write package.toml") + fs::write(Self::toml(), s).await.context("Failed to write package.toml") } pub(crate) async fn install(&mut self, upgrade: bool) -> Result<()> { @@ -77,7 +77,7 @@ impl Package { } let s = toml::to_string_pretty(self)?; - create_and_seal(&Self::toml(), s.as_bytes()).await.context("Failed to write package.toml") + fs::write(Self::toml(), s).await.context("Failed to write package.toml") } pub(crate) fn print(&self) -> Result<()> { @@ -179,7 +179,7 @@ impl Package { } let s = toml::to_string_pretty(self)?; - create_and_seal(&Self::toml(), s.as_bytes()).await.context("Failed to write package.toml") + fs::write(Self::toml(), s).await.context("Failed to write package.toml") } #[inline] diff --git a/yazi-fs/src/fns.rs b/yazi-fs/src/fns.rs index 21026eb7..ca799a79 100644 --- a/yazi-fs/src/fns.rs +++ b/yazi-fs/src/fns.rs @@ -83,16 +83,13 @@ async fn _paths_to_same_file(a: &Path, b: &Path) -> std::io::Result { Ok(final_name(a).await? == final_name(b).await?) } -#[inline] pub async fn copy_and_seal(from: &Path, to: &Path) -> io::Result<()> { - create_and_seal(to, &fs::read(from).await?).await -} + let b = fs::read(from).await?; + ok_or_not_found(fs::remove_file(to).await)?; -pub async fn create_and_seal(p: &Path, b: &[u8]) -> io::Result<()> { - ok_or_not_found(fs::remove_file(p).await)?; - - let mut file = fs::OpenOptions::new().create_new(true).write(true).truncate(true).open(p).await?; - file.write_all(b).await?; + let mut file = + fs::OpenOptions::new().create_new(true).write(true).truncate(true).open(to).await?; + file.write_all(&b).await?; let mut perm = file.metadata().await?.permissions(); perm.set_readonly(true);