|
|
@@ -732,27 +732,39 @@ pub fn par_whole_scan(id: &str, time_point: &str, config: &Config) -> anyhow::Re
|
|
|
|
|
|
debug!("Scan {contig}, writing file");
|
|
|
|
|
|
- let tmp_file = format!("{out_file}.tmp.{}", std::process::id());
|
|
|
-
|
|
|
{
|
|
|
- let mut file = get_gz_writer(&tmp_file, true)
|
|
|
- .with_context(|| format!("failed to open temp BGZF file: {tmp_file}"))?;
|
|
|
+ let temp_file = tempfile::NamedTempFile::new_in(&config.tmp_dir)
|
|
|
+ .with_context(|| format!("failed to create temp file in {out_dir:?}"))?;
|
|
|
|
|
|
- for bin in bins {
|
|
|
- writeln!(file, "{}", bin.to_tsv_row())
|
|
|
- .with_context(|| format!("failed writing {contig} row to {tmp_file}"))?;
|
|
|
- }
|
|
|
+ let temp_path = temp_file.into_temp_path();
|
|
|
|
|
|
- file.flush()
|
|
|
- .with_context(|| format!("failed flushing BGZF writer: {tmp_file}"))?;
|
|
|
+ {
|
|
|
+ let temp_path_str = temp_path
|
|
|
+ .to_str()
|
|
|
+ .with_context(|| format!("temp path not valid UTF-8: {temp_path:?}"))?;
|
|
|
|
|
|
- file.close().with_context(|| {
|
|
|
- format!("failed closing/finalizing BGZF writer: {tmp_file}")
|
|
|
- })?;
|
|
|
- }
|
|
|
+ let mut writer = get_gz_writer(temp_path_str, true)
|
|
|
+ .with_context(|| format!("failed to open BGZF file: {temp_path_str}"))?;
|
|
|
+
|
|
|
+ for bin in &bins {
|
|
|
+ writeln!(writer, "{}", bin.to_tsv_row()).with_context(|| {
|
|
|
+ format!("failed writing {contig} row to {temp_path_str}")
|
|
|
+ })?;
|
|
|
+ }
|
|
|
|
|
|
- std::fs::rename(&tmp_file, &out_file)
|
|
|
- .with_context(|| format!("failed atomic rename {tmp_file} -> {out_file}"))?;
|
|
|
+ writer
|
|
|
+ .flush()
|
|
|
+ .with_context(|| format!("failed flushing BGZF writer: {temp_path_str}"))?;
|
|
|
+
|
|
|
+ writer
|
|
|
+ .close()
|
|
|
+ .with_context(|| format!("failed closing BGZF writer: {temp_path_str}"))?;
|
|
|
+ }
|
|
|
+
|
|
|
+ temp_path
|
|
|
+ .persist(&out_file)
|
|
|
+ .with_context(|| format!("failed atomic rename to {out_file}"))?;
|
|
|
+ }
|
|
|
|
|
|
Ok(())
|
|
|
})?;
|