From 7308dbc7ad987b4aa1a13b6cd922aad65e61a15c Mon Sep 17 00:00:00 2001 From: Dezhi Wu Date: Sun, 21 Jul 2024 18:57:48 +0800 Subject: [PATCH] fish_indent: Prevent overwriting file with identical content Fixes #10616 --- src/bin/fish_indent.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/bin/fish_indent.rs b/src/bin/fish_indent.rs index 0e8c195ef..d37a8803f 100644 --- a/src/bin/fish_indent.rs +++ b/src/bin/fish_indent.rs @@ -7,6 +7,7 @@ #![allow(clippy::uninlined_format_args)] use std::ffi::{CString, OsStr}; +use std::fs::OpenOptions; use std::io::{stdin, Read, Write}; use std::os::unix::ffi::OsStrExt; use std::sync::atomic::Ordering; @@ -971,9 +972,15 @@ fn throwing_main() -> i32 { colored_output = no_colorize(&output_wtext); } OutputType::File => { - match std::fs::File::create(OsStr::from_bytes(&wcs2string(output_location))) { + match OpenOptions::new() + .write(true) + .open(OsStr::from_bytes(&wcs2string(output_location))) + { Ok(mut file) => { - let _ = file.write_all(&wcs2string(&output_wtext)); + // If the output is the same as the input, don't write it. + if output_wtext != src { + let _ = file.write_all(&wcs2string(&output_wtext)); + } } Err(err) => { eprintf!(