From f79a0149e5ce9fb7af48ee4fc112d0e5578d7fb7 Mon Sep 17 00:00:00 2001 From: Xtr126 Date: Tue, 28 Nov 2023 21:47:31 +0530 Subject: [PATCH] fix: crashing on windows when extracting --- src-tauri/src/main.rs | 34 +++++++++++++++++----------------- src/main.js | 2 +- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 110a513..6e2f119 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -4,7 +4,7 @@ )] use tauri::api::dialog; -use std::{fs::{File, remove_dir_all}, path::{PathBuf, Path}, time, thread, io::{Write, Seek, SeekFrom}, process::Command}; +use std::{fs::{File, remove_dir_all}, path::{PathBuf, Path}, time, thread, io::{Write, Seek, SeekFrom}, process::Command, sync::Arc}; use compress_tools::{uncompress_archive, Ownership}; mod qemu_install; @@ -101,38 +101,38 @@ fn start_install( iso_file: String, install_dir: String, ) -> Result { + let window = Arc::new(window); let source = File::open(iso_file).map_err(|err| err.to_string())?; let filesize = source.metadata().unwrap().len(); let install_dir1 = install_dir.clone(); + let window_ = window.clone(); thread::spawn(move || { let file_path = Path::new(&install_dir1); let mut init_size = fs_extra::dir::get_size(file_path).unwrap(); - let mut progress_eq = false; - let mut progress = 0; + let mut progress_before = 1; loop { - let size = fs_extra::dir::get_size(file_path).unwrap(); - if size > init_size { - let new_progress = (size - init_size) * 100 / filesize; - - if progress == new_progress { - if progress_eq { window.emit("new-dir-size", 100).unwrap(); break; } - progress_eq = true; - } - - progress = new_progress; - window.emit("new-dir-size", progress).unwrap(); + let current_size = fs_extra::dir::get_size(file_path).unwrap(); + if current_size > init_size { + let mut new_progress = (current_size - init_size) * 100 / filesize; + // increment progress every 1 seconds if stuck + if new_progress == progress_before { new_progress = progress_before + 5 } + // 100 will be sent only from the other thread + if new_progress != 100 { window.emit("new-dir-size", new_progress).unwrap(); } + progress_before = new_progress; } else { - init_size = size; + init_size = current_size; } - thread::sleep(time::Duration::from_secs(2)); + thread::sleep(time::Duration::from_secs(1)); } }); + let window = Arc::clone(&window_); thread::spawn(move || { let dest_dir = Path::new(&install_dir); uncompress_archive(source, dest_dir, Ownership::Preserve).unwrap(); - + window.emit("new-dir-size", 100).unwrap(); + let fs_install_dir = get_fs_install_dir(install_dir.clone()); let contents = format!(r#" diff --git a/src/main.js b/src/main.js index eacb696..7ae9b40 100644 --- a/src/main.js +++ b/src/main.js @@ -67,7 +67,7 @@ async function updateProgress() { await listen('new-dir-size', (event) => { installEl.updateProgress(event.payload) }); - if (installEl.progressPercent_ >= 100) { + if (installEl.progressPercent_ == 100) { createDataImg(); installEl.bootloaderMsg_ = await invoke("create_grub_entry", {