Skip to content

Commit

Permalink
rename instead of copy when replacing the dest file
Browse files Browse the repository at this point in the history
  • Loading branch information
jaemk committed Feb 24, 2021
1 parent b13c4a2 commit 14e15d4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
12 changes: 8 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,9 @@ impl<'a> Extract<'a> {
/// `source` and `dest` must be on the same filesystem.
/// If `replace_using_temp` is specified, the destination file will be
/// replaced using the given temporary path.
/// If the existing `dest` file is a currently running long running program,
/// `replace_using_temp` may run into errors cleaning up the temp dir.
/// If that's the case for your use-case, consider not specifying a temp dir to use.
///
/// * Errors:
/// * Io - copying / renaming
Expand Down Expand Up @@ -578,10 +581,11 @@ impl<'a> Move<'a> {
}
Some(temp) => {
if dest.exists() {
// Copy the destination file rather than renaming it, so that
// if it is a running executable the temp file can be removed
// on Windows while the executable is running.
fs::copy(dest, temp)?;
// Move the existing dest to a temp location so we can move it
// back it there's an error. If the existing `dest` file is a
// long running program, this may prevent the temp dir from
// being cleaned up.
fs::rename(dest, temp)?;
if let Err(e) = fs::rename(self.source, dest) {
fs::rename(temp, dest)?;
return Err(Error::from(e));
Expand Down
1 change: 0 additions & 1 deletion src/update.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use indicatif::ProgressStyle;
use reqwest::{self, header};
use std::env;
use std::fs;
#[cfg(not(windows))]
use std::os::unix::fs::PermissionsExt;
Expand Down

0 comments on commit 14e15d4

Please sign in to comment.