diff --git a/SUAutomaticUpdateDriver.m b/SUAutomaticUpdateDriver.m index fb8fcd1a..19a3c418 100644 --- a/SUAutomaticUpdateDriver.m +++ b/SUAutomaticUpdateDriver.m @@ -44,7 +44,7 @@ switch (choice) { case SUInstallNowChoice: - [self installUpdate]; + [self installWithToolAndRelaunch:YES]; break; case SUInstallLaterChoice: @@ -61,15 +61,15 @@ - (BOOL)shouldInstallSynchronously { return postponingInstallation; } -- (void)installUpdate +- (void)installWithToolAndRelaunch:(BOOL)relaunch { showErrors = YES; - [super installUpdate]; + [super installWithToolAndRelaunch:relaunch]; } - (void)applicationWillTerminate:(NSNotification *)note { - [self installUpdate]; + [self installWithToolAndRelaunch:NO]; } - (void)abortUpdateWithError:(NSError *)error diff --git a/SUBasicUpdateDriver.h b/SUBasicUpdateDriver.h index 67ed58e4..21fe7171 100644 --- a/SUBasicUpdateDriver.h +++ b/SUBasicUpdateDriver.h @@ -46,10 +46,10 @@ - (void)unarchiverDidFail:(SUUnarchiver *)ua; - (void)failedToApplyDeltaUpdate; -- (void)installUpdate; +- (void)installWithToolAndRelaunch:(BOOL)relaunch; - (void)installerForHost:(SUHost *)host failedWithError:(NSError *)error; -- (void)installAndRelaunchWithTool; +- (void)installWithToolAndRelaunch:(BOOL)relaunch; - (void)cleanUpDownload; - (void)abortUpdate; diff --git a/SUBasicUpdateDriver.m b/SUBasicUpdateDriver.m index f409d736..158d271b 100644 --- a/SUBasicUpdateDriver.m +++ b/SUBasicUpdateDriver.m @@ -237,7 +237,7 @@ - (void)unarchiverDidFinish:(SUUnarchiver *)ua { if (ua) { CFRelease(ua); } - [self installUpdate]; + [self installWithToolAndRelaunch:YES]; } - (void)unarchiverDidFail:(SUUnarchiver *)ua @@ -254,8 +254,28 @@ - (BOOL)shouldInstallSynchronously { return NO; } -- (void)installUpdate +- (void)installWithToolAndRelaunch:(BOOL)relaunch { + if (![updater mayUpdateAndRestart]) + { + [self abortUpdate]; + return; + } + + // Give the host app an opportunity to postpone the install and relaunch. + static BOOL postponedOnce = NO; + if (!postponedOnce && [[updater delegate] respondsToSelector:@selector(updater:shouldPostponeRelaunchForUpdate:untilInvoking:)]) + { + NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[[self class] instanceMethodSignatureForSelector:@selector(installWithToolAndRelaunch:)]]; + [invocation setSelector:@selector(installWithToolAndRelaunch:)]; + [invocation setArgument:&relaunch atIndex:0]; + [invocation setTarget:self]; + postponedOnce = YES; + if ([[updater delegate] updater:updater shouldPostponeRelaunchForUpdate:updateItem untilInvoking:invocation]) + return; + } + + if ([[updater delegate] respondsToSelector:@selector(updater:willInstallUpdate:)]) [[updater delegate] updater:updater willInstallUpdate:updateItem]; @@ -276,49 +296,25 @@ else [self abortUpdateWithError:[NSError errorWithDomain:SUSparkleErrorDomain code:SURelaunchError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:SULocalizedString(@"An error occurred while extracting the archive. Please try again later.", nil), NSLocalizedDescriptionKey, [NSString stringWithFormat:@"Couldn't copy relauncher (%@) to temporary path (%@)! %@", relaunchPathToCopy, targetPath, (error ? [error localizedDescription] : @"")], NSLocalizedFailureReasonErrorKey, nil]]]; - [self installAndRelaunchWithTool]; -} + [[NSNotificationCenter defaultCenter] postNotificationName:SUUpdaterWillRestartNotification object:self]; + if ([[updater delegate] respondsToSelector:@selector(updaterWillRelaunchApplication:)]) + [[updater delegate] updaterWillRelaunchApplication:updater]; -- (void)installAndRelaunchWithTool -{ - BOOL mayRelaunchAtAll = [updater mayUpdateAndRestart]; + if(!relaunchPath || ![[NSFileManager defaultManager] fileExistsAtPath:relaunchPath]) + { + // Note that we explicitly use the host app's name here, since updating plugin for Mail relaunches Mail, not just the plugin. + [self abortUpdateWithError:[NSError errorWithDomain:SUSparkleErrorDomain code:SURelaunchError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:SULocalizedString(@"An error occurred while relaunching %1$@, but the new version will be available next time you run %1$@.", nil), [host name]], NSLocalizedDescriptionKey, [NSString stringWithFormat:@"Couldn't find the relauncher (expected to find it at %@)", relaunchPath], NSLocalizedFailureReasonErrorKey, nil]]]; + // We intentionally don't abandon the update here so that the host won't initiate another. + return; + } + + NSString *pathToRelaunch = [host bundlePath]; + if ([[updater delegate] respondsToSelector:@selector(pathToRelaunchForUpdater:)]) + pathToRelaunch = [[updater delegate] pathToRelaunchForUpdater:updater]; + NSString *relaunchToolPath = [relaunchPath stringByAppendingPathComponent: @"/Contents/MacOS/finish_installation"]; + [NSTask launchedTaskWithLaunchPath: relaunchToolPath arguments:[NSArray arrayWithObjects:pathToRelaunch, [NSString stringWithFormat:@"%d", [[NSProcessInfo processInfo] processIdentifier]], tempDir, relaunch ? @"1" : @"0", nil]]; - if( mayRelaunchAtAll ) - { - // Give the host app an opportunity to postpone the relaunch. - static BOOL postponedOnce = NO; - if (!postponedOnce && [[updater delegate] respondsToSelector:@selector(updater:shouldPostponeRelaunchForUpdate:untilInvoking:)]) - { - NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[[self class] instanceMethodSignatureForSelector:@selector(installAndRelaunchWithTool)]]; - [invocation setSelector:@selector(installAndRelaunchWithTool)]; - [invocation setTarget:self]; - postponedOnce = YES; - if ([[updater delegate] updater:updater shouldPostponeRelaunchForUpdate:updateItem untilInvoking:invocation]) - return; - } - - [[NSNotificationCenter defaultCenter] postNotificationName:SUUpdaterWillRestartNotification object:self]; - if ([[updater delegate] respondsToSelector:@selector(updaterWillRelaunchApplication:)]) - [[updater delegate] updaterWillRelaunchApplication:updater]; - - if(!relaunchPath || ![[NSFileManager defaultManager] fileExistsAtPath:relaunchPath]) - { - // Note that we explicitly use the host app's name here, since updating plugin for Mail relaunches Mail, not just the plugin. - [self abortUpdateWithError:[NSError errorWithDomain:SUSparkleErrorDomain code:SURelaunchError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:SULocalizedString(@"An error occurred while relaunching %1$@, but the new version will be available next time you run %1$@.", nil), [host name]], NSLocalizedDescriptionKey, [NSString stringWithFormat:@"Couldn't find the relauncher (expected to find it at %@)", relaunchPath], NSLocalizedFailureReasonErrorKey, nil]]]; - // We intentionally don't abandon the update here so that the host won't initiate another. - return; - } - - NSString *pathToRelaunch = [host bundlePath]; - if ([[updater delegate] respondsToSelector:@selector(pathToRelaunchForUpdater:)]) - pathToRelaunch = [[updater delegate] pathToRelaunchForUpdater:updater]; - NSString *relaunchToolPath = [relaunchPath stringByAppendingPathComponent: @"/Contents/MacOS/finish_installation"]; - [NSTask launchedTaskWithLaunchPath: relaunchToolPath arguments:[NSArray arrayWithObjects:pathToRelaunch, [NSString stringWithFormat:@"%d", [[NSProcessInfo processInfo] processIdentifier]], tempDir, nil]]; - - [NSApp terminate:self]; - } - else - [self abortUpdate]; + [NSApp terminate:self]; } - (void)cleanUpDownload diff --git a/SUUIBasedUpdateDriver.m b/SUUIBasedUpdateDriver.m index 99e96297..2b209590 100644 --- a/SUUIBasedUpdateDriver.m +++ b/SUUIBasedUpdateDriver.m @@ -162,15 +162,14 @@ - (void)installAndRestart: (id)sender { - if( [updater mayUpdateAndRestart] ) - [self installUpdate]; + [self installWithToolAndRelaunch:YES]; } -- (void)installUpdate +- (void)installWithToolAndRelaunch:(BOOL)relaunch { [statusController beginActionWithTitle:SULocalizedString(@"Installing update...", @"Take care not to overflow the status window.") maxProgressValue:0.0 statusText:nil]; [statusController setButtonEnabled:NO]; - [super installUpdate]; + [super installWithToolAndRelaunch:relaunch]; // if a user chooses to NOT relaunch the app (as is the case with WebKit diff --git a/finish_installation.m b/finish_installation.m index f1ff0eec..17d3b938 100644 --- a/finish_installation.m +++ b/finish_installation.m @@ -21,6 +21,7 @@ NSTimer *watchdogTimer; NSTimer *longInstallationTimer; SUHost *host; + BOOL shouldRelaunch; } - (void) parentHasQuit; @@ -35,7 +36,7 @@ @implementation TerminationListener -- (id) initWithExecutablePath:(const char *)execpath parentProcessId:(pid_t)ppid folderPath: (const char*)infolderpath +- (id) initWithExecutablePath:(const char *)execpath parentProcessId:(pid_t)ppid folderPath: (const char*)infolderpath shouldRelaunch:(BOOL)relaunch selfPath: (NSString*)inSelfPath { if( !(self = [super init]) ) @@ -45,6 +46,7 @@ parentprocessid = ppid; folderpath = infolderpath; selfPath = [inSelfPath retain]; + shouldRelaunch = relaunch; BOOL alreadyTerminated = (getppid() == 1); // ppid is launchd (1) => parent terminated already @@ -105,23 +107,27 @@ - (void) relaunch { - NSString *appPath = nil; - if( !folderpath ) - appPath = [[NSFileManager defaultManager] stringWithFileSystemRepresentation:executablepath length:strlen(executablepath)]; - else - appPath = [host installationPath]; - [[NSWorkspace sharedWorkspace] openFile: appPath]; - if( folderpath ) - { - NSError* theError = nil; - if( ![SUPlainInstaller _removeFileAtPath: [SUInstaller updateFolder] error: &theError] ) - SULog( @"Couldn't remove update folder: %@.", theError ); - } -#if MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_4 - [[NSFileManager defaultManager] removeFileAtPath: selfPath handler: nil]; -#else - [[NSFileManager defaultManager] removeItemAtPath: selfPath error: NULL]; -#endif + if (shouldRelaunch) + { + NSString *appPath = nil; + if( !folderpath ) + appPath = [[NSFileManager defaultManager] stringWithFileSystemRepresentation:executablepath length:strlen(executablepath)]; + else + appPath = [host installationPath]; + [[NSWorkspace sharedWorkspace] openFile: appPath]; + if( folderpath ) + { + NSError* theError = nil; + if( ![SUPlainInstaller _removeFileAtPath: [SUInstaller updateFolder] error: &theError] ) + SULog( @"Couldn't remove update folder: %@.", theError ); + } + #if MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_4 + [[NSFileManager defaultManager] removeFileAtPath: selfPath handler: nil]; + #else + [[NSFileManager defaultManager] removeItemAtPath: selfPath error: NULL]; + #endif + } + exit(EXIT_SUCCESS); } @@ -158,7 +164,7 @@ int main (int argc, const char * argv[]) { - if( argc < 3 || argc > 4 ) + if( argc < 4 || argc > 5 ) return EXIT_FAILURE; NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; @@ -182,9 +188,10 @@ int main (int argc, const char * argv[]) [NSApplication sharedApplication]; [[[TerminationListener alloc] initWithExecutablePath: (argc > 1) ? argv[1] : NULL - parentProcessId: (argc > 2) ? atoi(argv[2]) : 0 - folderPath: (argc > 3) ? argv[3] : NULL - selfPath: selfPath] autorelease]; + parentProcessId: (argc > 2) ? atoi(argv[2]) : 0 + folderPath: (argc > 3) ? argv[3] : NULL + shouldRelaunch: (argc > 4) ? atoi(argv[4]) : 1 + selfPath: selfPath] autorelease]; [[NSApplication sharedApplication] run]; [pool drain];