Thomas преди 4 дни
родител
ревизия
9183f25a61
променени са 3 файла, в които са добавени 55 реда и са изтрити 22 реда
  1. 26 6
      Cargo.lock
  2. 1 0
      Cargo.toml
  3. 28 16
      src/scan/scan.rs

+ 26 - 6
Cargo.lock

@@ -1000,6 +1000,12 @@ version = "0.1.9"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a"
 
+[[package]]
+name = "fastrand"
+version = "2.4.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9f1f227452a390804cdb637b74a86990f2a7d7ba4b7d5693aac9b4dd6defd8d6"
+
 [[package]]
 name = "filetime"
 version = "0.2.27"
@@ -1518,9 +1524,9 @@ dependencies = [
 
 [[package]]
 name = "libc"
-version = "0.2.178"
+version = "0.2.186"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "37c93d8daa9d8a012fd8ab92f088405fb202ea0b6ab73ee2482ae66af4f42091"
+checksum = "68ab91017fe16c622486840e4c83c9a37afeff978bd239b5293d61ece587de66"
 
 [[package]]
 name = "libloading"
@@ -1589,9 +1595,9 @@ checksum = "bfae20f6b19ad527b550c223fddc3077a547fc70cda94b9b566575423fd303ee"
 
 [[package]]
 name = "linux-raw-sys"
-version = "0.11.0"
+version = "0.12.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "df1d3c3b53da64cf5760482273a98e575c651a67eec7f77df96b5b642de8f039"
+checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53"
 
 [[package]]
 name = "litemap"
@@ -1987,6 +1993,7 @@ dependencies = [
  "serde",
  "serde_json",
  "tar",
+ "tempfile",
  "thiserror 2.0.17",
  "toml 0.9.10+spec-1.1.0",
  "tracing",
@@ -2472,9 +2479,9 @@ dependencies = [
 
 [[package]]
 name = "rustix"
-version = "1.1.2"
+version = "1.1.4"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "cd15f8a2c5551a84d56efdc1cd049089e409ac19a3072d5037a17fd70719ff3e"
+checksum = "b6fe4565b9518b83ef4f91bb47ce29620ca828bd32cb7e408f0062e9930ba190"
 dependencies = [
  "bitflags",
  "errno",
@@ -2779,6 +2786,19 @@ dependencies = [
  "xattr",
 ]
 
+[[package]]
+name = "tempfile"
+version = "3.27.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd"
+dependencies = [
+ "fastrand",
+ "getrandom 0.3.4",
+ "once_cell",
+ "rustix",
+ "windows-sys 0.61.2",
+]
+
 [[package]]
 name = "thiserror"
 version = "1.0.69"

+ 1 - 0
Cargo.toml

@@ -55,6 +55,7 @@ triple_accel = "0.4.0"
 chainfile = "0.3.0"
 omics = "0.2.0"
 filetime = "0.2.27"
+tempfile = "3.27.0"
 
 [profile.dev]
 opt-level = 0

+ 28 - 16
src/scan/scan.rs

@@ -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(())
         })?;