Skip to content
This repository has been archived by the owner on Jun 8, 2022. It is now read-only.

Commit

Permalink
Merge pull request #87 from filecoin-project/fix-rename
Browse files Browse the repository at this point in the history
fix(sector-builder): avoid renaming between filesystems
  • Loading branch information
dignifiedquire authored Nov 14, 2019
2 parents e186e66 + 6058c83 commit c0f5a81
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions sector-builder/src/kv_store/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,28 @@ impl KeyValueStore for FileSystemKvs {
}

fn put(&self, key: &[u8], value: &[u8]) -> Result<()> {
let nt_file = tempfile::NamedTempFile::new()?;
let (mut file, oldpath) = nt_file.keep()?;

file.write_all(value)?;

let newpath = self.key_to_path(key);
let new_path = self.key_to_path(key);
let tmp_path = new_path.with_extension(".tmp");

{
let mut file = File::create(&tmp_path)?;
match file.write_all(value) {
Ok(_) => {}
Err(err) => {
fs::remove_file(&tmp_path)?;
return Err(err.into());
}
}
}

// if newpath already exists, it will be atomically replaced
std::fs::rename(oldpath, newpath).map_err(Into::into)
match std::fs::rename(&tmp_path, new_path) {
Ok(_) => Ok(()),
Err(err) => {
fs::remove_file(&tmp_path)?;
Err(err.into())
}
}
}

fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>> {
Expand Down

0 comments on commit c0f5a81

Please sign in to comment.