From 9125a57234c15580146e3677e8b710e861503b7b Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Tue, 1 Mar 2022 11:52:12 +0100 Subject: [PATCH] Add appropriate fsync() calls in write_block to ensure that data is persisted properly --- src/model/block.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/model/block.rs b/src/model/block.rs index ec1890bf..c7c178af 100644 --- a/src/model/block.rs +++ b/src/model/block.rs @@ -862,9 +862,11 @@ impl BlockManagerLocked { let data = data.inner_buffer(); let mut path = mgr.block_dir(hash); - fs::create_dir_all(&path).await?; - + let directory = path.clone(); path.push(hex::encode(hash)); + + fs::create_dir_all(&directory).await?; + let to_delete = match (mgr.is_block_compressed(hash).await, compressed) { (Ok(true), _) => return Ok(BlockRpc::Ok), (Ok(false), false) => return Ok(BlockRpc::Ok), @@ -885,6 +887,7 @@ impl BlockManagerLocked { path2.set_extension("tmp"); let mut f = fs::File::create(&path2).await?; f.write_all(data).await?; + f.sync_all().await?; drop(f); fs::rename(path2, path).await?; @@ -892,6 +895,14 @@ impl BlockManagerLocked { fs::remove_file(to_delete).await?; } + let dir = fs::OpenOptions::new() + .read(true) + .mode(0) + .open(directory) + .await?; + dir.sync_all().await?; + drop(dir); + Ok(BlockRpc::Ok) }