pub fn write_using_tmp_file<P, F>(dest: P, action: F) -> io::Result<()>
where
    P: AsRef<Path>,
    F: FnOnce(&mut io::BufWriter<&fs::File>) -> io::Result<()>,
{
    let dest_tmp = get_tmp_for_path(&dest);

    {
        let file = File::create(dest_tmp.as_path())?;
        let mut w = io::BufWriter::new(&file);
        action(&mut w)?;
        w.flush()?;
        file.sync_all()?;
    }

    let dest = dest.as_ref();
    fs::rename(dest_tmp.as_path(), dest)?;

    sync_path(dest.parent().unwrap_or_else(|| Path::new("/")))?;
    Ok(())
}
/// Write to file `dest` using `action` in a crash-safe manner
///
/// A new temporary file `dest.tmp` will be created (and truncated if it
/// already exists). The file will then be opened and `action` executed with
/// the BufWriter to that file.
///
/// The buffer and file will then be fsynced followed by renaming the
/// `dest.tmp` to `dest`. Target file `dest` will be overwritten in that
/// process if it already exists.
///
/// After renaming, the parent directory of `dest` will be fsynced.
///
/// The function will fail if `dest` exists but is a directory.


The buffer and file will then be fsynced followed by renaming the
`dest.tmp` to `dest`. Target file `dest` will be overwritten in that
process if it already exists.

After renaming, the parent directory of `dest` will be fsynced.

The function will fail if `dest` exists but is a directory.
The buffer and file will then be fsynced followed by renaming the ` dest . tmp ` to ` dest ` . Target file ` dest ` will be overwritten in that process if it already exists . After renaming the parent directory of ` dest ` will be fsynced . The function will fail if ` dest ` exists but is a directory .
pub fn write_using_tmp_file<P, F>(dest: P, action: F) -> io::Result<()>
where
    P: AsRef<Path>,
    F: FnOnce(&mut io::BufWriter<&fs::File>) -> io::Result<()>,
{
    let dest_tmp = get_tmp_for_path(&dest);
    {
        let file = File::create(dest_tmp.as_path())?;
        let mut w = io::BufWriter::new(&file);
        action(&mut w)?;
        w.flush()?;
        file.sync_all()?;
    }
    let dest = dest.as_ref();
    fs::rename(dest_tmp.as_path(), dest)?;
    sync_path(dest.parent().unwrap_or_else(|| Path::new("/")))?;
    Ok(())
}
pub fn write_using_tmp_file < P , F > ( dest : P , action : F ) -> io :: Result < ( ) > where P : AsRef < Path > , F : FnOnce ( & mut io :: BufWriter < & fs :: File > ) -> io :: Result < ( ) > , { let dest_tmp = get_tmp_for_path ( & dest ) ; { let file = File :: create ( dest_tmp . as_path ( ) ) ? ; let mut w = io :: BufWriter :: new ( & file ) ; action ( & mut w ) ? ; w . flush ( ) ? ; file . sync_all ( ) ? ; } let dest = dest . as_ref ( ) ; fs :: rename ( dest_tmp . as_path ( ) , dest ) ? ; sync_path ( dest . parent ( ) . unwrap_or_else ( | | Path :: new ( " " ) ) ) ? ; Ok ( ( ) ) }
Write to file `dest` using `action` in a crash-safe manner
A new temporary file `dest.tmp` will be created (and truncated if it
already exists).
Write to file ` dest ` using ` action ` in a crash - safe manner A new temporary file ` dest . tmp ` will be created ( and truncated if it already exists ) .
dest
P
action
F
dest
P
action
F