Crate self_replace

source ·
Expand description

self-replace is a crate that allows binaries to replace themselves with newer versions or to uninstall themselves. On Unix systems this is a simple feat, but on Windows a few hacks are needed which is why this crate exists.

This is a useful operation when working with single-executable utilties that want to implement a form of self updating or self uninstallation.

Self Deletion

The self_delete function schedules a binary for self deletion. On Unix the file system entry is immediately deleted, on Windows the file is deleted after the process shuts down. Note that you should not use this function to be followed up by a replacement operation, for that use self_replace as on Windows the file will still be locked.

self_replace::self_delete()?;

Self Replacing

This replaces the binary with another binary. The provided path is copied over and if the function successfully completes, you can delete the source binary.

use std::fs;

let new_binary = "/path/to/new/binary";
self_replace::self_replace(&new_binary)?;
fs::remove_file(&new_binary)?;

Functions

  • Deletes the executable in a platform independent manner.
  • Replaces the running executable with a differnet one.