use gix::tempfile in place of the tempfile.

This allows to unify handling of filewrites in more places.
This commit is contained in:
Sebastian Thiel 2024-04-24 11:26:07 +02:00 committed by Kiril Videlov
parent 813ba8f2d6
commit 6033a62a94
3 changed files with 24 additions and 12 deletions

View File

@ -8,6 +8,7 @@ publish = false
[dev-dependencies] [dev-dependencies]
once_cell = "1.19" once_cell = "1.19"
pretty_assertions = "1.4" pretty_assertions = "1.4"
tempfile = "3.10"
gitbutler-testsupport.workspace = true gitbutler-testsupport.workspace = true
[dependencies] [dependencies]
@ -52,7 +53,6 @@ urlencoding = "2.1.3"
uuid.workspace = true uuid.workspace = true
walkdir = "2.5.0" walkdir = "2.5.0"
zip = "0.6.5" zip = "0.6.5"
tempfile = "3.10"
gitbutler-git.workspace = true gitbutler-git.workspace = true
[features] [features]

View File

@ -54,11 +54,7 @@ impl Storage {
AutoRemove::Tempfile, AutoRemove::Tempfile,
)?; )?;
tempfile.write_all(content.as_bytes())?; tempfile.write_all(content.as_bytes())?;
match tempfile.persist(file_path) { persist_tempfile(tempfile, file_path)
Ok(Some(_opened_file)) => Ok(()),
Ok(None) => unreachable!("BUG: a signal has caused the tempfile to be removed, but we didn't install a handler"),
Err(err) => Err(err.error)
}
} }
/// Delete the file or directory at `rela_path`. /// Delete the file or directory at `rela_path`.
@ -84,3 +80,16 @@ impl Storage {
Ok(()) Ok(())
} }
} }
pub(crate) fn persist_tempfile(
tempfile: gix::tempfile::Handle<gix::tempfile::handle::Writable>,
to_path: impl AsRef<Path>,
) -> std::io::Result<()> {
match tempfile.persist(to_path) {
Ok(Some(_opened_file)) => Ok(()),
Ok(None) => unreachable!(
"BUG: a signal has caused the tempfile to be removed, but we didn't install a handler"
),
Err(err) => Err(err.error),
}
}

View File

@ -1,3 +1,4 @@
use gix::tempfile::{AutoRemove, ContainingDirectory};
use std::{ use std::{
collections::HashMap, collections::HashMap,
fs::File, fs::File,
@ -5,6 +6,7 @@ use std::{
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
use crate::storage;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use super::{target::Target, Branch}; use super::{target::Target, Branch};
@ -151,10 +153,11 @@ impl VirtualBranchesHandle {
fn write<P: AsRef<Path>>(file_path: P, virtual_branches: &VirtualBranches) -> anyhow::Result<()> { fn write<P: AsRef<Path>>(file_path: P, virtual_branches: &VirtualBranches) -> anyhow::Result<()> {
let contents = toml::to_string(&virtual_branches)?; let contents = toml::to_string(&virtual_branches)?;
let temp_file = tempfile::NamedTempFile::new_in(file_path.as_ref().parent().unwrap())?; let mut temp_file = gix::tempfile::new(
let (mut file, temp_path) = temp_file.keep()?; file_path.as_ref().parent().unwrap(),
file.write_all(contents.as_bytes())?; ContainingDirectory::Exists,
drop(file); AutoRemove::Tempfile,
std::fs::rename(temp_path, file_path.as_ref())?; )?;
Ok(()) temp_file.write_all(contents.as_bytes())?;
Ok(storage::persist_tempfile(temp_file, file_path)?)
} }