From 8b0dd6cc2123f728d83e24a4c866d597b43dfe6d Mon Sep 17 00:00:00 2001 From: Kiril Videlov Date: Sat, 27 Apr 2024 17:15:08 +0200 Subject: [PATCH] skip tempfile under windows --- crates/gitbutler-core/src/fs.rs | 48 +++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/crates/gitbutler-core/src/fs.rs b/crates/gitbutler-core/src/fs.rs index 43a7d626b..89c82b865 100644 --- a/crates/gitbutler-core/src/fs.rs +++ b/crates/gitbutler-core/src/fs.rs @@ -58,13 +58,21 @@ pub(crate) fn write>( file_path: P, contents: impl AsRef<[u8]>, ) -> anyhow::Result<()> { - let mut temp_file = gix::tempfile::new( - file_path.as_ref().parent().unwrap(), - ContainingDirectory::Exists, - AutoRemove::Tempfile, - )?; - temp_file.write_all(contents.as_ref())?; - Ok(persist_tempfile(temp_file, file_path)?) + #[cfg(windows)] + { + Ok(std::fs::write(file_path, contents)?) + } + + #[cfg(not(windows))] + { + let mut temp_file = gix::tempfile::new( + file_path.as_ref().parent().unwrap(), + ContainingDirectory::Exists, + AutoRemove::Tempfile, + )?; + temp_file.write_all(contents.as_ref())?; + Ok(persist_tempfile(temp_file, file_path)?) + } } /// Write a single file so that the write either fully succeeds, or fully fails, @@ -73,13 +81,25 @@ pub(crate) fn create_dirs_then_write>( file_path: P, contents: impl AsRef<[u8]>, ) -> std::io::Result<()> { - let mut temp_file = gix::tempfile::new( - file_path.as_ref().parent().unwrap(), - ContainingDirectory::CreateAllRaceProof(Retries::default()), - AutoRemove::Tempfile, - )?; - temp_file.write_all(contents.as_ref())?; - persist_tempfile(temp_file, file_path) + #[cfg(windows)] + { + let dir = file_path.as_ref().parent().unwrap(); + if !dir.exists() { + std::fs::create_dir_all(dir)?; + } + std::fs::write(file_path, contents) + } + + #[cfg(not(windows))] + { + let mut temp_file = gix::tempfile::new( + file_path.as_ref().parent().unwrap(), + ContainingDirectory::CreateAllRaceProof(Retries::default()), + AutoRemove::Tempfile, + )?; + temp_file.write_all(contents.as_ref())?; + persist_tempfile(temp_file, file_path) + } } fn persist_tempfile(