sync_all() on leaf_set and prune_list when using temp file (#3354)

This commit is contained in:
Antioch Peverell 2020-08-19 09:37:22 +01:00 committed by GitHub
parent 29cffe9b3c
commit 78e3ec3df0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 16 deletions

View file

@ -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.

View file

@ -75,6 +75,7 @@ pub fn u64_to_key(prefix: u8, val: u64) -> Vec<u8> {
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<F, P, E>(
@ -83,7 +84,7 @@ pub fn save_via_temp_file<F, P, E>(
mut writer: F,
) -> Result<(), std::io::Error>
where
F: FnMut(Box<dyn std::io::Write>) -> Result<(), std::io::Error>,
F: FnMut(&mut File) -> Result<(), std::io::Error>,
P: AsRef<Path>,
E: AsRef<OsStr>,
{
@ -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)?;

View file

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