Change Database::compare_and_swap to atomically replace the target

- Currently, the file system code writes the data to a temp file,
    removes the target file, and then renames the temp file to the
    target file.

  - This has two problems.  1.) If the rename fails, then the old
    version is also gone.  2.) A reader (e.g., NGINX) will return a
    404 after the old file has been removed, but before the file has
    been renamed.

  - Since tmp.persist() atomically replaces the target file, there is
    no need to remove the file.
This commit is contained in:
Neal H. Walfield 2019-02-22 12:39:27 +01:00
parent c8666f2ab6
commit e240a0bac6
1 changed files with 0 additions and 9 deletions

View File

@ -171,15 +171,6 @@ impl Database for Filesystem {
.tempfile_in(dir)?;
tmp.write_all(new)?;
if target.is_file() {
if old.is_some() {
remove_file(target.clone())?;
} else {
return Err(
format!("stray file {}", target.display()).into()
);
}
}
let _ = tmp.persist(ensure_parent(&target)?)?;
// fix permissions to 640