-
Notifications
You must be signed in to change notification settings - Fork 2
add UpdateMechanism for .dmg files
#92
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
Open
overheadhunter
wants to merge
6
commits into
develop
Choose a base branch
from
feature/update-api
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f954cbe
bump `integrations-api` to 1.8.0-SNAPSHOT
overheadhunter 6a43c5e
Merge branch 'develop' into feature/update-api
overheadhunter a9542ea
provides UpdateMechanism with DmgUpdateMechanism
overheadhunter 2788e54
use zsh shell
overheadhunter 20837a2
restart with `open /Applications/Cryptomator.app`
overheadhunter 7ba93ca
apply suggestions from code review
overheadhunter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
src/main/java/org/cryptomator/macos/update/DmgUpdateMechanism.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| package org.cryptomator.macos.update; | ||
|
|
||
| import org.cryptomator.integrations.common.LocalizedDisplayName; | ||
| import org.cryptomator.integrations.common.OperatingSystem; | ||
| import org.cryptomator.integrations.update.DownloadUpdateInfo; | ||
| import org.cryptomator.integrations.update.DownloadUpdateMechanism; | ||
| import org.cryptomator.integrations.update.UpdateFailedException; | ||
| import org.cryptomator.integrations.update.UpdateMechanism; | ||
| import org.cryptomator.integrations.update.UpdateStep; | ||
| import org.cryptomator.macos.common.Localization; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.InterruptedIOException; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.StandardOpenOption; | ||
| import java.util.List; | ||
| import java.util.UUID; | ||
|
|
||
| @OperatingSystem(OperatingSystem.Value.MAC) | ||
| @LocalizedDisplayName(bundle = "MacIntegrationsBundle", key = "org.cryptomator.macos.update.dmg.displayName") | ||
| public class DmgUpdateMechanism extends DownloadUpdateMechanism { | ||
|
|
||
| private static final Logger LOG = LoggerFactory.getLogger(DmgUpdateMechanism.class); | ||
|
|
||
| @Override | ||
| protected DownloadUpdateInfo checkForUpdate(String currentVersion, LatestVersionResponse response) { | ||
| String suffix = switch (System.getProperty("os.arch")) { | ||
| case "aarch64", "arm64" -> "arm64.dmg"; | ||
| default -> "x64.dmg"; | ||
| }; | ||
| var updateVersion = response.latestVersion().macVersion(); | ||
| var asset = response.assets().stream().filter(a -> a.name().endsWith(suffix)).findAny().orElse(null); | ||
| if (UpdateMechanism.isUpdateAvailable(updateVersion, currentVersion) && asset != null) { | ||
| return new DownloadUpdateInfo(this, updateVersion, asset); | ||
| } else { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public UpdateStep secondStep(Path workDir, Path assetPath, DownloadUpdateInfo updateInfo) { | ||
| return UpdateStep.of(Localization.get().getString("org.cryptomator.macos.update.dmg.unpacking"), () -> this.unpack(workDir, assetPath)); | ||
| } | ||
|
|
||
| private UpdateStep unpack(Path workDir, Path assetPath) throws IOException { | ||
| // Extract Cryptomator.app from the .dmg file | ||
| var processBuilder = new ProcessBuilder(List.of("/bin/zsh", "-s")); | ||
| processBuilder.directory(workDir.toFile()); | ||
| processBuilder.environment().put("DMG_PATH", assetPath.toString()); | ||
| processBuilder.environment().put("MOUNT_ID", UUID.randomUUID().toString()); | ||
| Process p = processBuilder.start(); | ||
| try { | ||
| try (var stdin = p.outputWriter()) { | ||
| stdin.write(""" | ||
| trap 'hdiutil detach "/Volumes/Cryptomator_${MOUNT_ID}" -quiet || true' EXIT | ||
| hdiutil attach "${DMG_PATH}" -mountpoint "/Volumes/Cryptomator_${MOUNT_ID}" -nobrowse -quiet | ||
| cp -R "/Volumes/Cryptomator_${MOUNT_ID}/Cryptomator.app" 'Cryptomator.app' | ||
| """); | ||
| } | ||
| if (p.waitFor() != 0) { | ||
| LOG.error("Failed to extract DMG, exit code: {}, output: {}", p.exitValue(), new String(p.getErrorStream().readAllBytes())); | ||
| throw new IOException("Failed to extract DMG, exit code: " + p.exitValue()); | ||
| } | ||
| LOG.debug("Unpacked app: {}", workDir.resolve("Cryptomator.app")); | ||
| } catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| throw new InterruptedIOException("Failed to extract DMG, interrupted"); | ||
| } | ||
| return UpdateStep.of(Localization.get().getString("org.cryptomator.macos.update.dmg.verifying"), () -> this.verify(workDir, assetPath)); | ||
| } | ||
|
|
||
| private UpdateStep verify(Path workDir, Path assetPath) throws IOException { | ||
| // Verify code signature of the extracted .app | ||
| var processBuilder = new ProcessBuilder(List.of("/bin/zsh", "-s")); | ||
| processBuilder.directory(workDir.toFile()); | ||
| Process p = processBuilder.start(); | ||
| try { | ||
| try (var stdin = p.outputWriter()) { | ||
| stdin.write(""" | ||
| codesign --verify --deep --strict 'Cryptomator.app' | ||
| spctl --assess --type execute 'Cryptomator.app' | ||
| """); | ||
| } | ||
| if (p.waitFor() != 0) { | ||
| LOG.error("Checking code signature failed: {}, output: {}", p.exitValue(), new String(p.getErrorStream().readAllBytes())); | ||
| throw new UpdateFailedException("Invalid Code Signature."); | ||
| } | ||
| LOG.debug("Verified app: {}", workDir.resolve("Cryptomator.app")); | ||
| } catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| throw new InterruptedIOException("Failed to extract DMG, interrupted"); | ||
| } | ||
| return UpdateStep.of(Localization.get().getString("org.cryptomator.macos.update.dmg.restarting"), () -> this.restart(workDir)); | ||
| } | ||
|
|
||
| public UpdateStep restart(Path workDir) throws IllegalStateException, IOException { | ||
| String selfPath = ProcessHandle.current().info().command().orElse(""); | ||
| String installPath; | ||
| if (selfPath.startsWith("/Applications/Cryptomator.app")) { | ||
| installPath = "/Applications/Cryptomator.app"; | ||
| } else if (selfPath.contains("/Cryptomator.app/")) { | ||
| installPath = selfPath.substring(0, selfPath.indexOf("/Cryptomator.app/")) + "/Cryptomator.app"; | ||
| } else { | ||
| throw new UpdateFailedException("Cannot determine destination path for Cryptomator.app, current path: " + selfPath); | ||
| } | ||
| LOG.info("Restarting to apply Update in {} now...", workDir); | ||
| String script = """ | ||
| while kill -0 ${CRYPTOMATOR_PID} 2> /dev/null; do sleep 0.2; done; | ||
| if [ -d "${CRYPTOMATOR_INSTALL_PATH}" ]; then | ||
| echo "Removing old installation at ${CRYPTOMATOR_INSTALL_PATH}"; | ||
| rm -rf "${CRYPTOMATOR_INSTALL_PATH}" | ||
| fi | ||
| mv 'Cryptomator.app' "${CRYPTOMATOR_INSTALL_PATH}"; | ||
| open "${CRYPTOMATOR_INSTALL_PATH}"; | ||
| """; | ||
| Files.writeString(workDir.resolve("install.sh"), script, StandardCharsets.US_ASCII, StandardOpenOption.WRITE, StandardOpenOption.CREATE_NEW); | ||
| var command = List.of("/bin/zsh", "-c", "/usr/bin/nohup zsh install.sh >install.log 2>&1 &"); | ||
| var processBuilder = new ProcessBuilder(command); | ||
| processBuilder.directory(workDir.toFile()); | ||
| processBuilder.environment().put("CRYPTOMATOR_PID", String.valueOf(ProcessHandle.current().pid())); | ||
| processBuilder.environment().put("CRYPTOMATOR_INSTALL_PATH", installPath); | ||
| processBuilder.start(); | ||
|
|
||
| return UpdateStep.EXIT; | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,6 @@ | ||
| org.cryptomator.macos.keychain.displayName=macOS Keychain | ||
| org.cryptomator.macos.keychain.touchIdDisplayName=Touch ID | ||
| org.cryptomator.macos.keychain.touchIdDisplayName=Touch ID | ||
| org.cryptomator.macos.update.dmg.displayName=Download .dmg file | ||
| org.cryptomator.macos.update.dmg.unpacking=Unpacking... | ||
| org.cryptomator.macos.update.dmg.verifying=Verifying... | ||
| org.cryptomator.macos.update.dmg.restarting=Restarting... |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix the error message for verification context.
Line 95's error message says "Failed to extract DMG, interrupted" but this is in the
verifymethod after the extraction has already succeeded. The message should reflect that verification was interrupted.Apply this diff:
} catch (InterruptedException e) { Thread.currentThread().interrupt(); - throw new InterruptedIOException("Failed to extract DMG, interrupted"); + throw new InterruptedIOException("Code signature verification interrupted"); }🤖 Prompt for AI Agents