From 2cdcbf1c22c9b906180206d077af65b6887ca311 Mon Sep 17 00:00:00 2001 From: Caelan Date: Sun, 29 Sep 2024 12:27:23 -0600 Subject: [PATCH] Move temp directory creation to locator --- Cargo.lock | 1 + samples/RustIced/Cargo.lock | 1 + src/bins/src/commands/apply_windows_impl.rs | 5 ++--- src/bins/src/commands/install.rs | 2 +- src/lib-nodejs/crates/velopack_nodeffi/src/lib.rs | 9 +++++---- src/lib-rust/Cargo.toml | 1 + src/lib-rust/src/bindetect.rs | 2 ++ src/lib-rust/src/locator.rs | 13 ++++++++++--- src/lib-rust/src/util.rs | 5 +++++ 9 files changed, 28 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6755f8c4..f7e0018d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2030,6 +2030,7 @@ dependencies = [ "log", "native-tls", "normpath", + "rand", "regex", "semver", "serde", diff --git a/samples/RustIced/Cargo.lock b/samples/RustIced/Cargo.lock index c9045574..a39eba4e 100644 --- a/samples/RustIced/Cargo.lock +++ b/samples/RustIced/Cargo.lock @@ -3678,6 +3678,7 @@ dependencies = [ "log", "native-tls", "normpath", + "rand", "regex", "semver", "serde", diff --git a/src/bins/src/commands/apply_windows_impl.rs b/src/bins/src/commands/apply_windows_impl.rs index 6b6c8009..b3ce15ea 100644 --- a/src/bins/src/commands/apply_windows_impl.rs +++ b/src/bins/src/commands/apply_windows_impl.rs @@ -57,10 +57,9 @@ pub fn apply_package_impl(old_locator: &VelopackLocator, package: &PathBuf, run_ bail!("Stopping apply. Pre-requisites are missing and user cancelled."); } - let packages_dir = old_locator.get_packages_dir(); let current_dir = old_locator.get_current_bin_dir(); - let temp_path_new = packages_dir.join(format!("tmp_{}", shared::random_string(16))); - let temp_path_old = packages_dir.join(format!("tmp_{}", shared::random_string(16))); + let temp_path_new = old_locator.get_temp_dir_rand16()?; + let temp_path_old = old_locator.get_temp_dir_rand16()?; // open a dialog showing progress... let (mut tx, _) = mpsc::channel::(); diff --git a/src/bins/src/commands/install.rs b/src/bins/src/commands/install.rs index 5545e4bc..2d6d2902 100644 --- a/src/bins/src/commands/install.rs +++ b/src/bins/src/commands/install.rs @@ -101,7 +101,7 @@ pub fn install(pkg: &mut BundleZip, install_to: Option<&PathBuf>, start_args: Op anyhow!("Failed to stop application ({}), please close the application and try running the installer again.", z) })?; - root_path_renamed = format!("{}_{}", root_path_str, shared::random_string(8)); + root_path_renamed = format!("{}_{}", root_path_str, shared::random_string(16)); info!("Renaming existing directory to '{}' to allow rollback...", root_path_renamed); shared::retry_io(|| fs::rename(&root_path, &root_path_renamed)).map_err(|_| { diff --git a/src/lib-nodejs/crates/velopack_nodeffi/src/lib.rs b/src/lib-nodejs/crates/velopack_nodeffi/src/lib.rs index 78d1da70..6f558a6b 100644 --- a/src/lib-nodejs/crates/velopack_nodeffi/src/lib.rs +++ b/src/lib-nodejs/crates/velopack_nodeffi/src/lib.rs @@ -221,10 +221,11 @@ fn js_appbuilder_run(mut cx: FunctionContext) -> JsResult { #[cfg(target_os = "windows")] { - builder.on_after_install_fast_callback(|semver| hook_handler("after-install", semver)); - builder.on_before_uninstall_fast_callback(|semver| hook_handler("before-uninstall", semver)); - builder.on_before_update_fast_callback(|semver| hook_handler("before-update", semver)); - builder.on_after_update_fast_callback(|semver| hook_handler("after-update", semver)); + builder = builder + .on_after_install_fast_callback(|semver| hook_handler("after-install", semver)) + .on_before_uninstall_fast_callback(|semver| hook_handler("before-uninstall", semver)) + .on_before_update_fast_callback(|semver| hook_handler("before-update", semver)) + .on_after_update_fast_callback(|semver| hook_handler("after-update", semver)); } if let Some(locator) = locator { diff --git a/src/lib-rust/Cargo.toml b/src/lib-rust/Cargo.toml index 942282d0..fbc47dbd 100644 --- a/src/lib-rust/Cargo.toml +++ b/src/lib-rust/Cargo.toml @@ -36,6 +36,7 @@ lazy_static = "1.5" regex = "1.10" normpath = "1.3" bitflags = "2.6" +rand = "0.8" # typescript ts-rs = { version = "10.0", optional = true } diff --git a/src/lib-rust/src/bindetect.rs b/src/lib-rust/src/bindetect.rs index bb341ea2..8b3036df 100644 --- a/src/lib-rust/src/bindetect.rs +++ b/src/lib-rust/src/bindetect.rs @@ -1,3 +1,5 @@ +#![allow(dead_code)] + use std::fs::File; use std::io::{self, Read}; use std::path::Path; diff --git a/src/lib-rust/src/locator.rs b/src/lib-rust/src/locator.rs index 42337d4c..c84027a6 100644 --- a/src/lib-rust/src/locator.rs +++ b/src/lib-rust/src/locator.rs @@ -145,13 +145,20 @@ impl VelopackLocator { } /// Returns the path to the current app temporary directory. - pub fn get_temp_dir(&self) -> PathBuf { + pub fn get_temp_dir_root(&self) -> PathBuf { self.paths.PackagesDir.join("VelopackTemp") } + + /// Creates a new temporary directory inside get_temp_dir_root() with a random 16-character suffix. + pub fn get_temp_dir_rand16(&self) -> Result { + let p = self.get_temp_dir_root().join("tmp_".to_string() + &util::random_string(16)); + std::fs::create_dir_all(&p)?; + Ok(p) + } /// Returns the path to the current app temporary directory as a string. pub fn get_temp_dir_as_string(&self) -> String { - Self::path_as_string(&self.get_temp_dir()) + Self::path_as_string(&self.get_temp_dir_root()) } /// Returns the root directory of the current app. @@ -289,7 +296,7 @@ impl VelopackLocator { /// Create a paths object containing default / ideal paths for a given root directory /// Generally, this should not be used except for installing the app for the first time. #[cfg(target_os = "windows")] -pub fn create_config_from_root_dir>(root_dir: P) -> VelopackLocatorConfig +pub fn create_config_from_root_dir>(root_dir: P) -> VelopackLocatorConfig { let root_dir = root_dir.as_ref(); VelopackLocatorConfig { diff --git a/src/lib-rust/src/util.rs b/src/lib-rust/src/util.rs index c9f47d2f..15db759d 100644 --- a/src/lib-rust/src/util.rs +++ b/src/lib-rust/src/util.rs @@ -1,5 +1,6 @@ use std::thread; use std::time::Duration; +use rand::distributions::{Alphanumeric, DistString}; pub fn retry_io(op: F) -> Result where @@ -31,4 +32,8 @@ where thread::sleep(Duration::from_millis(1000)); op() +} + +pub fn random_string(len: usize) -> String { + Alphanumeric.sample_string(&mut rand::thread_rng(), len) } \ No newline at end of file