Skip to content

Commit 5f76d8b

Browse files
committed
f Reintroduce per-write tmp files
1 parent 93351e6 commit 5f76d8b

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

lightning-persister/src/fs_store.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::collections::HashMap;
66
use std::fs;
77
use std::io::{BufReader, Read, Write};
88
use std::path::{Path, PathBuf};
9+
use std::sync::atomic::{AtomicUsize, Ordering};
910
use std::sync::{Arc, Mutex, RwLock};
1011

1112
#[cfg(not(target_os = "windows"))]
@@ -33,14 +34,16 @@ fn path_to_windows_str<T: AsRef<OsStr>>(path: T) -> Vec<u16> {
3334
/// A [`KVStore`] implementation that writes to and reads from the file system.
3435
pub struct FilesystemStore {
3536
data_dir: PathBuf,
37+
tmp_file_counter: AtomicUsize,
3638
locks: Mutex<HashMap<(String, String), Arc<RwLock<()>>>>,
3739
}
3840

3941
impl FilesystemStore {
4042
/// Constructs a new [`FilesystemStore`].
4143
pub fn new(data_dir: PathBuf) -> Self {
4244
let locks = Mutex::new(HashMap::new());
43-
Self { data_dir, locks }
45+
let tmp_file_counter = AtomicUsize::new(0);
46+
Self { data_dir, tmp_file_counter, locks }
4447
}
4548

4649
/// Returns the data directory.
@@ -119,7 +122,8 @@ impl KVStore for FilesystemStore {
119122
// The way to atomically write a file on Unix platforms is:
120123
// open(tmpname), write(tmpfile), fsync(tmpfile), close(tmpfile), rename(), fsync(dir)
121124
let mut tmp_file_path = dest_file_path.clone();
122-
tmp_file_path.set_extension("tmp");
125+
let tmp_file_ext = format!("{}.tmp", self.tmp_file_counter.fetch_add(1, Ordering::AcqRel));
126+
tmp_file_path.set_extension(tmp_file_ext);
123127

124128
{
125129
let mut tmp_file = fs::File::create(&tmp_file_path)?;

0 commit comments

Comments
 (0)