Call fsync before close

This commit is contained in:
Ajeet D'Souza 2025-05-12 09:37:04 -07:00
parent 8e14038811
commit 4807518c4b
1 changed files with 6 additions and 0 deletions

View File

@ -179,6 +179,12 @@ pub fn write(path: impl AsRef<Path>, contents: impl AsRef<[u8]>) -> Result<()> {
}
// Close and rename the tmpfile.
// In some cases, errors from the last write() are reported only on close().
// Rust ignores errors from close(), since it occurs inside `Drop`. To
// catch these errors, we manually call `File::sync_all()` first.
tmp_file
.sync_all()
.with_context(|| format!("could not sync writes to file: {}", tmp_path.display()))?;
mem::drop(tmp_file);
rename(&tmp_path, path)
})();