mirror of
https://github.com/mimblewimble/grin.git
synced 2025-02-01 08:51:08 +03:00
sync_all() on leaf_set and prune_list when using temp file (#3354)
This commit is contained in:
parent
29cffe9b3c
commit
78e3ec3df0
3 changed files with 13 additions and 16 deletions
|
@ -171,10 +171,8 @@ impl LeafSet {
|
||||||
self.bitmap.run_optimize();
|
self.bitmap.run_optimize();
|
||||||
|
|
||||||
// Write the updated bitmap file to disk.
|
// Write the updated bitmap file to disk.
|
||||||
save_via_temp_file(&self.path, ".tmp", |w| {
|
save_via_temp_file(&self.path, ".tmp", |file| {
|
||||||
let mut w = BufWriter::new(w);
|
file.write_all(&self.bitmap.serialize())
|
||||||
w.write_all(&self.bitmap.serialize())?;
|
|
||||||
w.flush()
|
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
// Make sure our backup in memory is up to date.
|
// Make sure our backup in memory is up to date.
|
||||||
|
|
|
@ -75,6 +75,7 @@ pub fn u64_to_key(prefix: u8, val: u64) -> Vec<u8> {
|
||||||
use std::ffi::OsStr;
|
use std::ffi::OsStr;
|
||||||
use std::fs::{remove_file, rename, File};
|
use std::fs::{remove_file, rename, File};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
/// Creates temporary file with name created by adding `temp_suffix` to `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`.
|
/// Applies writer function to it and renames temporary file into original specified by `path`.
|
||||||
pub fn save_via_temp_file<F, P, E>(
|
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,
|
mut writer: F,
|
||||||
) -> Result<(), std::io::Error>
|
) -> Result<(), std::io::Error>
|
||||||
where
|
where
|
||||||
F: FnMut(Box<dyn std::io::Write>) -> Result<(), std::io::Error>,
|
F: FnMut(&mut File) -> Result<(), std::io::Error>,
|
||||||
P: AsRef<Path>,
|
P: AsRef<Path>,
|
||||||
E: AsRef<OsStr>,
|
E: AsRef<OsStr>,
|
||||||
{
|
{
|
||||||
|
@ -99,13 +100,13 @@ where
|
||||||
remove_file(&temp_path)?;
|
remove_file(&temp_path)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let file = File::create(&temp_path)?;
|
let mut temp_file = File::create(&temp_path)?;
|
||||||
writer(Box::new(file))?;
|
|
||||||
|
|
||||||
// Move temporary file into original
|
// write the new data to the temp file
|
||||||
if original.exists() {
|
writer(&mut temp_file)?;
|
||||||
remove_file(&original)?;
|
|
||||||
}
|
// force an fsync on the temp file to ensure bytes are on disk
|
||||||
|
temp_file.sync_all()?;
|
||||||
|
|
||||||
rename(&temp_path, &original)?;
|
rename(&temp_path, &original)?;
|
||||||
|
|
||||||
|
|
|
@ -21,7 +21,7 @@
|
||||||
//! must be shifted the appropriate amount when reading from the hash and data
|
//! must be shifted the appropriate amount when reading from the hash and data
|
||||||
//! files.
|
//! files.
|
||||||
|
|
||||||
use std::io::{self, BufWriter, Write};
|
use std::io::{self, Write};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
use croaring::Bitmap;
|
use croaring::Bitmap;
|
||||||
|
@ -114,10 +114,8 @@ impl PruneList {
|
||||||
|
|
||||||
// Write the updated bitmap file to disk.
|
// Write the updated bitmap file to disk.
|
||||||
if let Some(ref path) = self.path {
|
if let Some(ref path) = self.path {
|
||||||
save_via_temp_file(path, ".tmp", |w| {
|
save_via_temp_file(path, ".tmp", |file| {
|
||||||
let mut w = BufWriter::new(w);
|
file.write_all(&self.bitmap.serialize())
|
||||||
w.write_all(&self.bitmap.serialize())?;
|
|
||||||
w.flush()
|
|
||||||
})?;
|
})?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue