atomic_replace_file

Function atomic_replace_file 

Source
pub fn atomic_replace_file<P: AsRef<Path>>(
    original_path: P,
    temp_path: P,
) -> Result<(), Vec<Diagnostic>>
Expand description

Atomically replace a file using rsync-style temp files

This performs the following sequence:

  1. Write new content to temp_path (caller’s responsibility - already done)
  2. Move original_path -> .original_path.{random}.old (backup)
  3. Move temp_path -> original_path (replace)
  4. Delete .original_path.{random}.old (cleanup)

If any step fails, attempts to recover by restoring the backup.

§Arguments

  • original_path - The file to be replaced
  • temp_path - The temporary file containing the new content

§Errors

Returns diagnostics if any step of the operation fails. The function attempts automatic recovery by restoring the backup if the replacement fails.