From 78e3ec3df093a0a8d69b122f45ea26f53cc8a038 Mon Sep 17 00:00:00 2001 From: Antioch Peverell Date: Wed, 19 Aug 2020 09:37:22 +0100 Subject: [PATCH] sync_all() on leaf_set and prune_list when using temp file (#3354) --- store/src/leaf_set.rs | 6 ++---- store/src/lib.rs | 15 ++++++++------- store/src/prune_list.rs | 8 +++----- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/store/src/leaf_set.rs b/store/src/leaf_set.rs index c2c4b4ae4..ed5477dc6 100644 --- a/store/src/leaf_set.rs +++ b/store/src/leaf_set.rs @@ -171,10 +171,8 @@ impl LeafSet { self.bitmap.run_optimize(); // Write the updated bitmap file to disk. - save_via_temp_file(&self.path, ".tmp", |w| { - let mut w = BufWriter::new(w); - w.write_all(&self.bitmap.serialize())?; - w.flush() + save_via_temp_file(&self.path, ".tmp", |file| { + file.write_all(&self.bitmap.serialize()) })?; // Make sure our backup in memory is up to date. diff --git a/store/src/lib.rs b/store/src/lib.rs index e9e7a17b1..ac2e0800a 100644 --- a/store/src/lib.rs +++ b/store/src/lib.rs @@ -75,6 +75,7 @@ pub fn u64_to_key(prefix: u8, val: u64) -> Vec { use std::ffi::OsStr; use std::fs::{remove_file, rename, File}; use std::path::Path; + /// Creates temporary file with name created by adding `temp_suffix` to `path`. /// Applies writer function to it and renames temporary file into original specified by `path`. pub fn save_via_temp_file( @@ -83,7 +84,7 @@ pub fn save_via_temp_file( mut writer: F, ) -> Result<(), std::io::Error> where - F: FnMut(Box) -> Result<(), std::io::Error>, + F: FnMut(&mut File) -> Result<(), std::io::Error>, P: AsRef, E: AsRef, { @@ -99,13 +100,13 @@ where remove_file(&temp_path)?; } - let file = File::create(&temp_path)?; - writer(Box::new(file))?; + let mut temp_file = File::create(&temp_path)?; - // Move temporary file into original - if original.exists() { - remove_file(&original)?; - } + // write the new data to the temp file + writer(&mut temp_file)?; + + // force an fsync on the temp file to ensure bytes are on disk + temp_file.sync_all()?; rename(&temp_path, &original)?; diff --git a/store/src/prune_list.rs b/store/src/prune_list.rs index 64e27ce63..122700a1f 100644 --- a/store/src/prune_list.rs +++ b/store/src/prune_list.rs @@ -21,7 +21,7 @@ //! must be shifted the appropriate amount when reading from the hash and data //! files. -use std::io::{self, BufWriter, Write}; +use std::io::{self, Write}; use std::path::{Path, PathBuf}; use croaring::Bitmap; @@ -114,10 +114,8 @@ impl PruneList { // Write the updated bitmap file to disk. if let Some(ref path) = self.path { - save_via_temp_file(path, ".tmp", |w| { - let mut w = BufWriter::new(w); - w.write_all(&self.bitmap.serialize())?; - w.flush() + save_via_temp_file(path, ".tmp", |file| { + file.write_all(&self.bitmap.serialize()) })?; }