From d2fd9a50e04be3fcccc06664a3a9bc7a75b0caf9 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sun, 26 Jun 2022 18:41:55 -0700 Subject: [PATCH] Use recovery suggestion key for update permission error (#2180) --- Sparkle/SPUInstallerDriver.m | 32 ++++++++++++------------------ sparkle-cli/SPUCommandLineDriver.m | 6 +++++- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/Sparkle/SPUInstallerDriver.m b/Sparkle/SPUInstallerDriver.m index b89fb54b..36bfdf12 100644 --- a/Sparkle/SPUInstallerDriver.m +++ b/Sparkle/SPUInstallerDriver.m @@ -121,28 +121,22 @@ } else if (underlyingError.code == SUInstallationError) { NSError *secondUnderlyingError = underlyingError.userInfo[NSUnderlyingErrorKey]; if (secondUnderlyingError != nil && [secondUnderlyingError.domain isEqualToString:NSCocoaErrorDomain] && secondUnderlyingError.code == NSFileWriteNoPermissionError) { - NSBundle *mainBundle = [NSBundle mainBundle]; - // macOS 13 and later introduce a policy where Gatekeeper can block app modifications if the apps have different Team IDs - BOOL warnAboutGatekeeper; - if (@available(macOS 13, *)) { - warnAboutGatekeeper = ![mainBundle isEqual:self.host.bundle]; - } else { - warnAboutGatekeeper = NO; - } - // Note: these error strings will only surface for external app updaters like sparkle-cli (i.e, updaters that update other app bundles) - NSString *errorDescription; - if (warnAboutGatekeeper) { - SUHost *mainBundleHost = [[SUHost alloc] initWithBundle:mainBundle]; - errorDescription = [NSString stringWithFormat:SULocalizedString(@"The installation failed due to not having permission to write the new update. To install the update, you may need to allow modifications from %1$@ in System Settings under Privacy & Security and App Management", nil), mainBundleHost.name]; - } else { - errorDescription = SULocalizedString(@"The installation failed due to not having permission to write the new update.", nil); - } - NSDictionary *userInfo = @{ - NSLocalizedDescriptionKey: errorDescription, + NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithDictionary:@{ + NSLocalizedDescriptionKey: SULocalizedString(@"The installation failed due to not having permission to write the new update.", nil), NSUnderlyingErrorKey: (NSError * _Nonnull)currentInstallerError - }; + }]; + + // macOS 13 and later introduce a policy where Gatekeeper can block app modifications if the apps have different Team IDs + if (@available(macOS 13, *)) { + NSBundle *mainBundle = [NSBundle mainBundle]; + if (![mainBundle isEqual:self.host.bundle]) { + SUHost *mainBundleHost = [[SUHost alloc] initWithBundle:mainBundle]; + + userInfo[NSLocalizedRecoverySuggestionErrorKey] = [NSString stringWithFormat:SULocalizedString(@"You may need to allow modifications from %1$@ in System Settings under Privacy & Security and App Management to install future updates.", nil), mainBundleHost.name]; + } + } customError = [NSError errorWithDomain:SUSparkleErrorDomain code:SUInstallationWriteNoPermissionError userInfo:userInfo]; } diff --git a/sparkle-cli/SPUCommandLineDriver.m b/sparkle-cli/SPUCommandLineDriver.m index 9fa49615..4da83648 100644 --- a/sparkle-cli/SPUCommandLineDriver.m +++ b/sparkle-cli/SPUCommandLineDriver.m @@ -248,7 +248,11 @@ typedef NS_ENUM(int, CLIErrorExitStatus) { fprintf(stderr, "%s\n", error.localizedDescription.UTF8String); exit(CLIErrorCodeCannotInstallInteractivePackageAsRoot); } else if (error.code == SUInstallationWriteNoPermissionError) { - fprintf(stderr, "Error: %s\n", error.localizedDescription.UTF8String); + fprintf(stderr, "Error: %s", error.localizedDescription.UTF8String); + if (error.localizedRecoverySuggestion != nil) { + fprintf(stderr, " %s", error.localizedRecoverySuggestion.UTF8String); + } + fprintf(stderr, "\n"); exit(CLIErrorExitStatusInstallationWriteNoPermissionError); } else { fprintf(stderr, "Error: Update has failed due to error %ld (%s). %s\n", (long)error.code, error.domain.UTF8String, error.localizedDescription.UTF8String);