From 14e15d435dbcb0d9bcfcdab16b177d2656d37063 Mon Sep 17 00:00:00 2001 From: James Kominick Date: Tue, 23 Feb 2021 21:48:05 -0500 Subject: [PATCH] rename instead of copy when replacing the dest file --- src/lib.rs | 12 ++++++++---- src/update.rs | 1 - 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 3234d93..35742ba 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 @@ -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)); diff --git a/src/update.rs b/src/update.rs index 162543d..30046b0 100644 --- a/src/update.rs +++ b/src/update.rs @@ -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;