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

Commit

Permalink
Use an atomic rename on windows. Refs mitsuhiko#4
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed May 18, 2023
1 parent e69094e commit 50230b5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

All notable changes to `self-replace` are documented here.

## Unreleased

- Use an atomic rename on Windows in the self replacement case for the
final step to avoid accidentally leaving partial executables behind.

## 1.3.1

- Fixes a bug that caused the wrong path to be calculated internally
Expand Down
5 changes: 4 additions & 1 deletion src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ extern "C" {

static SELFDELETE_SUFFIX: &str = ".__selfdelete__.exe";
static RELOCATED_SUFFIX: &str = ".__relocated__.exe";
static TEMP_SUFFIX: &str = ".__temp__.exe";

/// Utility function that delays the deletion of a file until the process shuts down.
/// The way this works is that it marks the given executable as DELETE_ON_CLOSE, and
Expand Down Expand Up @@ -304,6 +305,8 @@ pub fn self_replace(new_executable: &Path) -> Result<(), io::Error> {
let old_exe = get_temp_executable_name(get_directory_of(&exe)?, RELOCATED_SUFFIX);
fs::rename(&exe, &old_exe)?;
schedule_self_deletion_on_shutdown(&old_exe, None)?;
fs::copy(new_executable, &exe)?;
let temp_exe = get_temp_executable_name(get_directory_of(&exe)?, TEMP_SUFFIX);
fs::copy(new_executable, &temp_exe)?;
fs::rename(&temp_exe, &exe)?;
Ok(())
}

0 comments on commit 50230b5

Please sign in to comment.