From d9ec4ec1c19942b8f4da7b00059d7d6b2bc43bae Mon Sep 17 00:00:00 2001 From: amr-crabnebula Date: Thu, 30 Nov 2023 19:32:12 +0200 Subject: [PATCH] does this fix macOS ? --- crates/packager/src/util.rs | 16 +++++++++++++++ crates/updater/src/lib.rs | 41 +++++++++++++++---------------------- 2 files changed, 33 insertions(+), 24 deletions(-) diff --git a/crates/packager/src/util.rs b/crates/packager/src/util.rs index 28791674..d2efbb18 100644 --- a/crates/packager/src/util.rs +++ b/crates/packager/src/util.rs @@ -383,6 +383,22 @@ fn make_icns_image(img: image::DynamicImage) -> std::io::Result { } /// Writes a tar file to the given writer containing the given directory. +/// +/// The generated tar contains the `src_dir` as a whole and not just its files, +/// so if we are creating a tar for: +/// ```text +/// dir/ +/// |_ file1 +/// |_ file2 +/// |_ file3 +/// ``` +/// the generated tar will contain the following entries: +/// ```text +/// - dir +/// - dir/file1 +/// - dir/file2 +/// - dir/file3 +/// ``` pub fn create_tar_from_dir, W: Write>(src_dir: P, dest_file: W) -> crate::Result { let src_dir = src_dir.as_ref(); let filename = src_dir diff --git a/crates/updater/src/lib.rs b/crates/updater/src/lib.rs index 0b4c6580..ccddc8e7 100644 --- a/crates/updater/src/lib.rs +++ b/crates/updater/src/lib.rs @@ -755,7 +755,6 @@ impl Update { use flate2::read::GzDecoder; let cursor = Cursor::new(bytes); - let mut extracted_files: Vec = Vec::new(); // the first file in the tar.gz will always be // /Contents @@ -767,33 +766,27 @@ impl Update { let decoder = GzDecoder::new(cursor); let mut archive = tar::Archive::new(decoder); - std::fs::create_dir(&self.extract_path)?; - - for entry in archive.entries()? { - let mut entry = entry?; - - let extraction_path = &self.extract_path.join(entry.path()?); - - // if something went wrong during the extraction, we should restore previous app - if let Err(err) = entry.unpack(extraction_path) { - for file in extracted_files.iter().rev() { - // delete all the files we extracted - if file.is_dir() { - std::fs::remove_dir(file)?; - } else { - std::fs::remove_file(file)?; - } - } - std::fs::rename(tmp_dir.path(), &self.extract_path)?; - return Err(err.into()); + fn extract_archive(archive: tar::Archive, extract_path: &Path) -> Result<()> { + std::fs::create_dir(extract_path)?; + for entry in archive.entries()? { + let mut entry = entry?; + let entry_path: PathBuf = entry.path()?.components().skip(1).collect(); + entry.unpack(extract_path.join(entry_path))?; } - extracted_files.push(extraction_path.to_path_buf()); + let _ = std::process::Command::new("touch") + .arg(extract_path) + .status(); + + Ok(()) } - let _ = std::process::Command::new("touch") - .arg(&self.extract_path) - .status(); + // if something went wrong during the extraction, we should restore previous app + if let Err(e) = extract_archive(archive, self.extract_path) { + std::fs::remove_dir(extract_path)?; + std::fs::rename(tmp_dir.path(), &self.extract_path)?; + return Err(e); + } Ok(()) }