Move temp directory creation to locator

This commit is contained in:
Caelan
2024-09-29 12:27:23 -06:00
parent 13f35209ac
commit 2cdcbf1c22
9 changed files with 28 additions and 11 deletions

1
Cargo.lock generated
View File

@@ -2030,6 +2030,7 @@ dependencies = [
"log",
"native-tls",
"normpath",
"rand",
"regex",
"semver",
"serde",

View File

@@ -3678,6 +3678,7 @@ dependencies = [
"log",
"native-tls",
"normpath",
"rand",
"regex",
"semver",
"serde",

View File

@@ -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::<i16>();

View File

@@ -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(|_| {

View File

@@ -221,10 +221,11 @@ fn js_appbuilder_run(mut cx: FunctionContext) -> JsResult<JsUndefined> {
#[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 {

View File

@@ -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 }

View File

@@ -1,3 +1,5 @@
#![allow(dead_code)]
use std::fs::File;
use std::io::{self, Read};
use std::path::Path;

View File

@@ -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<PathBuf, Error> {
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<P: AsRef<Path>>(root_dir: P) -> VelopackLocatorConfig
pub fn create_config_from_root_dir<P: AsRef<std::path::Path>>(root_dir: P) -> VelopackLocatorConfig
{
let root_dir = root_dir.as_ref();
VelopackLocatorConfig {

View File

@@ -1,5 +1,6 @@
use std::thread;
use std::time::Duration;
use rand::distributions::{Alphanumeric, DistString};
pub fn retry_io<F, T, E>(op: F) -> Result<T, E>
where
@@ -32,3 +33,7 @@ where
op()
}
pub fn random_string(len: usize) -> String {
Alphanumeric.sample_string(&mut rand::thread_rng(), len)
}