Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow self renamer to work if tempdir is on a different device #797

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ self_update_crate = { version = "~0.40", default-features = false, optional = tr
self_update_crate = { version = "~0.40", default-features = false, optional = true, package = "self_update", features = ["archive-zip", "compression-zip-deflate", "rustls"] }
winapi = "~0.3"
parselnk = "~0.1"
self-replace = "~1.4"

[profile.release]
lto = true
Expand Down
39 changes: 35 additions & 4 deletions src/self_renamer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use color_eyre::eyre::Result;
use std::{env::current_exe, fs, path::PathBuf};
use tracing::{debug, error};
use tracing::{debug, error, warn};

pub struct SelfRenamer {
exe_path: PathBuf,
Expand All @@ -10,12 +10,40 @@ pub struct SelfRenamer {
impl SelfRenamer {
pub fn create() -> Result<Self> {
let tempdir = tempfile::tempdir()?;
let temp_path = tempdir.path().join("topgrade.exe");
let mut temp_path = tempdir.path().join("topgrade.exe");
let exe_path = current_exe()?;

debug!("Current exe in {:?}. Moving it to {:?}", exe_path, temp_path);
debug!(
"Current exe in {:?}. Attempting to move it to {:?}",
exe_path, temp_path
);

fs::rename(&exe_path, &temp_path)?;
match fs::rename(&exe_path, &temp_path) {
// cross-device error
Err(e) if e.raw_os_error() == Some(17) => {
debug!("Temporary directory is on a different device. Using the binary parent directory instead");

let Some(parent_dir) = exe_path.parent() else {
return Err(color_eyre::eyre::Report::msg(
"Could not get parent directory of the current binary",
));
};

let mut builder = tempfile::Builder::new();
builder.prefix("topgrade").suffix(".exe");
let temp_file = builder.tempfile_in(parent_dir)?;
temp_path = temp_file.path().to_path_buf();

// Delete the temporary file immediately to free up the name
if let Err(e) = temp_file.close() {
warn!("Could not close temporary file: {}", e);
}

debug!("Moving current exe in {:?} to {:?}", exe_path, temp_path);
fs::rename(&exe_path, &temp_path)
}
other => other,
}?;

Ok(SelfRenamer { exe_path, temp_path })
}
Expand All @@ -25,6 +53,9 @@ impl Drop for SelfRenamer {
fn drop(&mut self) {
if self.exe_path.exists() {
debug!("{:?} exists. Topgrade was probably upgraded", self.exe_path);
if let Err(e) = self_replace::self_delete_at(&self.temp_path) {
error!("Could not clean up temporarily renamed topgrade executable: {}", e);
}
return;
}

Expand Down