Skip to content

Commit

Permalink
Avoid using temporary files when overwriting formatted files to prese…
Browse files Browse the repository at this point in the history
…rve file permissions
  • Loading branch information
sile committed Jul 31, 2024
1 parent d6d0c56 commit 5bd84bb
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 69 deletions.
60 changes: 0 additions & 60 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 1 addition & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -456,12 +455,6 @@ fn validate_formatted_text<P: AsRef<Path>>(
}

fn overwrite<P: AsRef<Path>>(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(())
}

0 comments on commit 5bd84bb

Please sign in to comment.