Compare commits

...
Author SHA1 Message Date
Zorg 274634cd70 Perform Gatekeeper scan and still use new temp directory for installs 2023-09-04 20:17:26 -07:00
+58 -37
View File
@@ -41,7 +41,7 @@
return self; return self;
} }
- (void)_performInitialInstallationWithFileManager:(SUFileManager *)fileManager oldBundleURL:(NSURL *)oldBundleURL newBundleURL:(NSURL *)newBundleURL progressBlock:(nullable void(^)(double))progress SPU_OBJC_DIRECT - (void)_performInitialInstallationWithFileManager:(SUFileManager *)fileManager oldBundleURL:(NSURL *)oldBundleURL newBundleURL:(NSURL *)newBundleURL gatekeeperScan:(BOOL)gatekeeperScan progressBlock:(nullable void(^)(double))progress SPU_OBJC_DIRECT
{ {
// Release our new app from quarantine // Release our new app from quarantine
NSError *quarantineError = nil; NSError *quarantineError = nil;
@@ -88,6 +88,34 @@
if (progress) { if (progress) {
progress(8/11.0); progress(8/11.0);
} }
if (gatekeeperScan) {
// Perform a Gatekeeper scan so users do not see the 'Verifying...' dialog when the update is launched
if (@available(macOS 14, *)) {
NSURL *gktoolURL = [NSURL fileURLWithPath:@"/usr/bin/gktool" isDirectory:NO];
if ([gktoolURL checkResourceIsReachableAndReturnError:NULL]) {
NSString *newBundlePath = newBundleURL.path;
assert(newBundlePath != nil);
NSTask *gatekeeperScanTask = [[NSTask alloc] init];
gatekeeperScanTask.executableURL = gktoolURL;
gatekeeperScanTask.arguments = @[@"scan", newBundlePath];
NSError *taskError;
if (![gatekeeperScanTask launchAndReturnError:&taskError]) {
// Not a fatal error
SULog(SULogLevelError, @"Failed to perform GateKeeper scan on '%@' with error %@", newBundlePath, taskError);
} else {
[gatekeeperScanTask waitUntilExit];
int terminationStatus = gatekeeperScanTask.terminationStatus;
if (terminationStatus != 0) {
// Not a fatal error
SULog(SULogLevelError, @"gktool returned error status %d for GateKeeper scan on '%@'", terminationStatus, newBundlePath);
}
}
}
}
}
} }
@@ -143,45 +171,38 @@
progress(2/11.0); progress(2/11.0);
} }
NSURL *tempNewDirectoryURL; // Create a temporary directory for our new app that resides on our destination's volume
if (!_newAndOldBundlesOnSameVolume) { // We use oldURL here instead of installationURL because in the case of normalization, installationURL may not exist
// Create a temporary directory for our new app that resides on our destination's volume // And we don't want to use either of the URL's parent directories because the parent directory could be on a different volume
// We use oldURL here instead of installationURL because in the case of normalization, installationURL may not exist // Note it is still important to use a temporary directory for the new update even if the bundles are on the same volume
// And we don't want to use either of the URL's parent directories because the parent directory could be on a different volume // Swapping the bundles when the new bundle is not in a temporary directory created for installation can cause problems.
tempNewDirectoryURL = [fileManager makeTemporaryDirectoryAppropriateForDirectoryURL:oldURL error:error]; // For example, if we didn't do this, atomically swapping app bundles may fail after performing a Gatekeeper scan
if (tempNewDirectoryURL == nil) { NSURL *tempNewDirectoryURL = [fileManager makeTemporaryDirectoryAppropriateForDirectoryURL:oldURL error:error];
return NO; if (tempNewDirectoryURL == nil) {
} return NO;
_temporaryNewDirectory = tempNewDirectoryURL;
} else {
tempNewDirectoryURL = nil;
} }
_temporaryNewDirectory = tempNewDirectoryURL;
if (progress) { if (progress) {
progress(3/11.0); progress(3/11.0);
} }
// Move the new app to our temporary directory if needed // Move the new app to our temporary directory
NSURL *newFinalURL; NSString *newURLLastPathComponent = newURL.lastPathComponent;
if (!_newAndOldBundlesOnSameVolume) { NSURL *newTempURL = [tempNewDirectoryURL URLByAppendingPathComponent:newURLLastPathComponent];
NSString *newURLLastPathComponent = newURL.lastPathComponent; NSError *newTempMoveError = nil;
newFinalURL = [tempNewDirectoryURL URLByAppendingPathComponent:newURLLastPathComponent]; if (![fileManager moveItemAtURL:newURL toURL:newTempURL error:&newTempMoveError]) {
NSError *newTempMoveError = nil; if (error != NULL) {
if (![fileManager moveItemAtURL:newURL toURL:newFinalURL error:&newTempMoveError]) { NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithDictionary:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed to move the new app from %@ to its temp directory at %@", newURL.path, newTempURL.path] }];
if (error != NULL) {
NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithDictionary:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed to move the new app from %@ to its temp directory at %@", newURL.path, newFinalURL.path] }]; if (newTempMoveError != nil) {
userInfo[NSUnderlyingErrorKey] = newTempMoveError;
if (newTempMoveError != nil) {
userInfo[NSUnderlyingErrorKey] = newTempMoveError;
}
*error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUInstallationError userInfo:userInfo];
} }
return NO;
*error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUInstallationError userInfo:userInfo];
} }
} else { return NO;
newFinalURL = newURL;
} }
if (progress) { if (progress) {
@@ -189,7 +210,7 @@
} }
if (!_newAndOldBundlesOnSameVolume) { if (!_newAndOldBundlesOnSameVolume) {
[self _performInitialInstallationWithFileManager:fileManager oldBundleURL:oldURL newBundleURL:newFinalURL progressBlock:progress]; [self _performInitialInstallationWithFileManager:fileManager oldBundleURL:oldURL newBundleURL:newTempURL gatekeeperScan:NO progressBlock:progress];
} }
if (progress) { if (progress) {
@@ -205,7 +226,7 @@
} else { } else {
// We will be cleaning up the temporary directory later in -performCleanup: // We will be cleaning up the temporary directory later in -performCleanup:
// We don't want to clean it up now because it can take some time // We don't want to clean it up now because it can take some time
swappedApp = [fileManager swapItemAtURL:installationURL withItemAtURL:newFinalURL error:&swapError]; swappedApp = [fileManager swapItemAtURL:installationURL withItemAtURL:newTempURL error:&swapError];
} }
if (!swappedApp) { if (!swappedApp) {
@@ -259,9 +280,9 @@
// Move the new app to its final destination // Move the new app to its final destination
NSError *installMoveError = nil; NSError *installMoveError = nil;
if (![fileManager moveItemAtURL:newFinalURL toURL:installationURL error:&installMoveError]) { if (![fileManager moveItemAtURL:newTempURL toURL:installationURL error:&installMoveError]) {
if (error != NULL) { if (error != NULL) {
NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithDictionary:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed to move new app at %@ to final destination %@", newFinalURL.path, installationURL.path] }]; NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithDictionary:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed to move new app at %@ to final destination %@", newTempURL.path, installationURL.path] }];
if (installMoveError != nil) { if (installMoveError != nil) {
userInfo[NSUnderlyingErrorKey] = installMoveError; userInfo[NSUnderlyingErrorKey] = installMoveError;
@@ -312,7 +333,7 @@
// We can do a lot of the installation work ahead of time if the new app update does not need to be copied to another volume // We can do a lot of the installation work ahead of time if the new app update does not need to be copied to another volume
if (_newAndOldBundlesOnSameVolume) { if (_newAndOldBundlesOnSameVolume) {
[self _performInitialInstallationWithFileManager:fileManager oldBundleURL:_host.bundle.bundleURL newBundleURL:bundle.bundleURL progressBlock:NULL]; [self _performInitialInstallationWithFileManager:fileManager oldBundleURL:_host.bundle.bundleURL newBundleURL:bundle.bundleURL gatekeeperScan:YES progressBlock:NULL];
} }
return YES; return YES;