From 5bd84bbea432f3b3f4727aebc282d45cd6bfe5b7 Mon Sep 17 00:00:00 2001 From: Takeru Ohta Date: Thu, 1 Aug 2024 08:34:04 +0900 Subject: [PATCH] Avoid using temporary files when overwriting formatted files to preserve file permissions --- Cargo.lock | 60 ----------------------------------------------------- Cargo.toml | 1 - src/main.rs | 9 +------- 3 files changed, 1 insertion(+), 69 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e790c6c..22417f3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -72,12 +72,6 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" -[[package]] -name = "bitflags" -version = "2.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" - [[package]] name = "bstr" version = "1.10.0" @@ -89,12 +83,6 @@ dependencies = [ "serde", ] -[[package]] -name = "cfg-if" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" - [[package]] name = "clap" version = "4.5.11" @@ -204,7 +192,6 @@ dependencies = [ "regex", "similar", "similar-asserts", - "tempfile", ] [[package]] @@ -281,22 +268,6 @@ dependencies = [ "thiserror", ] -[[package]] -name = "errno" -version = "0.3.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "534c5cf6194dfab3db3242765c03bbe257cf92f22b38f6bc0c58d59108a820ba" -dependencies = [ - "libc", - "windows-sys 0.52.0", -] - -[[package]] -name = "fastrand" -version = "2.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9fc0510504f03c51ada170672ac806f1f105a88aa97a5281117e1ddc3368e51a" - [[package]] name = "globset" version = "0.4.14" @@ -362,12 +333,6 @@ version = "0.2.155" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" -[[package]] -name = "linux-raw-sys" -version = "0.4.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" - [[package]] name = "log" version = "0.4.22" @@ -523,19 +488,6 @@ version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b" -[[package]] -name = "rustix" -version = "0.38.34" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70dc5ec042f7a43c4a73241207cecc9873a06d45debb38b329f8541d85c2730f" -dependencies = [ - "bitflags", - "errno", - "libc", - "linux-raw-sys", - "windows-sys 0.52.0", -] - [[package]] name = "same-file" version = "1.0.6" @@ -602,18 +554,6 @@ dependencies = [ "unicode-ident", ] -[[package]] -name = "tempfile" -version = "3.10.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85b77fafb263dd9d05cbeac119526425676db3784113aa9295c88498cbf8bff1" -dependencies = [ - "cfg-if", - "fastrand", - "rustix", - "windows-sys 0.52.0", -] - [[package]] name = "thiserror" version = "1.0.63" diff --git a/Cargo.toml b/Cargo.toml index 4d4629e..e95c01f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,7 +19,6 @@ env_logger = "0.11" log = "0.4" rayon = "1" similar = { version= "2", features = ["inline"] } -tempfile = "3" regex = "1.6.0" colored = "2.1.0" ignore = "0.4.22" diff --git a/src/main.rs b/src/main.rs index 2a9492c..0003593 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,6 @@ use env_logger::Env; use rayon::iter::{IntoParallelIterator as _, ParallelIterator}; use regex::Regex; use std::io::Read as _; -use std::io::Write as _; use std::path::{Path, PathBuf}; /// Erlang Code Formatter. @@ -456,12 +455,6 @@ fn validate_formatted_text>( } fn overwrite>(path: P, text: &str) -> anyhow::Result<()> { - let dir = path - .as_ref() - .parent() - .ok_or_else(|| anyhow::anyhow!("failed to get parent dir: {:?}", path.as_ref()))?; - let mut temp = tempfile::NamedTempFile::new_in(dir)?; - temp.write_all(text.as_bytes())?; - temp.persist(path)?; + std::fs::write(path, text)?; Ok(()) }