From: Elias Rohrer Date: Tue, 22 Aug 2023 12:13:55 +0000 (+0200) Subject: f Reintroduce per-write tmp files X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=5c6090d387c2193c3d029484c2d68808a16253f8;p=rust-lightning f Reintroduce per-write tmp files --- diff --git a/lightning-persister/src/fs_store.rs b/lightning-persister/src/fs_store.rs index c29f0fb05..4ef8fc598 100644 --- a/lightning-persister/src/fs_store.rs +++ b/lightning-persister/src/fs_store.rs @@ -6,6 +6,7 @@ use std::collections::HashMap; use std::fs; use std::io::{BufReader, Read, Write}; use std::path::{Path, PathBuf}; +use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::{Arc, Mutex, RwLock}; #[cfg(not(target_os = "windows"))] @@ -33,6 +34,7 @@ fn path_to_windows_str>(path: T) -> Vec { /// A [`KVStore`] implementation that writes to and reads from the file system. pub struct FilesystemStore { data_dir: PathBuf, + tmp_file_counter: AtomicUsize, locks: Mutex>>>, } @@ -40,7 +42,8 @@ impl FilesystemStore { /// Constructs a new [`FilesystemStore`]. pub fn new(data_dir: PathBuf) -> Self { let locks = Mutex::new(HashMap::new()); - Self { data_dir, locks } + let tmp_file_counter = AtomicUsize::new(0); + Self { data_dir, tmp_file_counter, locks } } /// Returns the data directory. @@ -119,7 +122,8 @@ impl KVStore for FilesystemStore { // The way to atomically write a file on Unix platforms is: // open(tmpname), write(tmpfile), fsync(tmpfile), close(tmpfile), rename(), fsync(dir) let mut tmp_file_path = dest_file_path.clone(); - tmp_file_path.set_extension("tmp"); + let tmp_file_ext = format!("{}.tmp", self.tmp_file_counter.fetch_add(1, Ordering::AcqRel)); + tmp_file_path.set_extension(tmp_file_ext); { let mut tmp_file = fs::File::create(&tmp_file_path)?;