Merge pull request #3620 from gitbutlerapp/add-windows-fs-write-support

skip tempfile under windows
This commit is contained in:
Kiril Videlov 2024-04-27 18:39:59 +02:00 committed by GitHub
commit 470f89de01
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -58,6 +58,13 @@ pub(crate) fn write<P: AsRef<Path>>(
file_path: P, file_path: P,
contents: impl AsRef<[u8]>, contents: impl AsRef<[u8]>,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
#[cfg(windows)]
{
Ok(std::fs::write(file_path, contents)?)
}
#[cfg(not(windows))]
{
let mut temp_file = gix::tempfile::new( let mut temp_file = gix::tempfile::new(
file_path.as_ref().parent().unwrap(), file_path.as_ref().parent().unwrap(),
ContainingDirectory::Exists, ContainingDirectory::Exists,
@ -65,6 +72,7 @@ pub(crate) fn write<P: AsRef<Path>>(
)?; )?;
temp_file.write_all(contents.as_ref())?; temp_file.write_all(contents.as_ref())?;
Ok(persist_tempfile(temp_file, file_path)?) Ok(persist_tempfile(temp_file, file_path)?)
}
} }
/// Write a single file so that the write either fully succeeds, or fully fails, /// Write a single file so that the write either fully succeeds, or fully fails,
@ -73,6 +81,17 @@ pub(crate) fn create_dirs_then_write<P: AsRef<Path>>(
file_path: P, file_path: P,
contents: impl AsRef<[u8]>, contents: impl AsRef<[u8]>,
) -> std::io::Result<()> { ) -> std::io::Result<()> {
#[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( let mut temp_file = gix::tempfile::new(
file_path.as_ref().parent().unwrap(), file_path.as_ref().parent().unwrap(),
ContainingDirectory::CreateAllRaceProof(Retries::default()), ContainingDirectory::CreateAllRaceProof(Retries::default()),
@ -80,6 +99,7 @@ pub(crate) fn create_dirs_then_write<P: AsRef<Path>>(
)?; )?;
temp_file.write_all(contents.as_ref())?; temp_file.write_all(contents.as_ref())?;
persist_tempfile(temp_file, file_path) persist_tempfile(temp_file, file_path)
}
} }
fn persist_tempfile( fn persist_tempfile(