diff --git a/SUAutomaticUpdateDriver.m b/SUAutomaticUpdateDriver.m index abba7765..66cae631 100644 --- a/SUAutomaticUpdateDriver.m +++ b/SUAutomaticUpdateDriver.m @@ -46,10 +46,12 @@ } } +- (BOOL)shouldInstallSynchronously { return postponingInstallation; } + - (void)installUpdate { showErrors = YES; - [SUInstaller installFromUpdateFolder:[downloadPath stringByDeletingLastPathComponent] overHostBundle:hostBundle delegate:self synchronously:postponingInstallation]; + [super installUpdate]; } - (void)applicationWillTerminate:(NSNotification *)note diff --git a/SUBasicUpdateDriver.h b/SUBasicUpdateDriver.h index 5699a79b..bd0297c5 100644 --- a/SUBasicUpdateDriver.h +++ b/SUBasicUpdateDriver.h @@ -19,7 +19,10 @@ NSURLDownload *download; NSString *downloadPath; + SUUnarchiver *unarchiver; + + NSString *relaunchPath; } - (void)checkForUpdatesAtURL:(NSURL *)appcastURL hostBundle:(NSBundle *)hb; diff --git a/SUBasicUpdateDriver.m b/SUBasicUpdateDriver.m index 96753799..1748e791 100644 --- a/SUBasicUpdateDriver.m +++ b/SUBasicUpdateDriver.m @@ -158,9 +158,19 @@ [self abortUpdateWithError:[NSError errorWithDomain:SUSparkleErrorDomain code:SUUnarchivingError userInfo:[NSDictionary dictionaryWithObject:SULocalizedString(@"An error occurred while extracting the archive. Please try again later.", nil) forKey:NSLocalizedDescriptionKey]]]; } +- (BOOL)shouldInstallSynchronously { return NO; } + - (void)installUpdate { - [SUInstaller installFromUpdateFolder:[downloadPath stringByDeletingLastPathComponent] overHostBundle:hostBundle delegate:self synchronously:NO]; + // Copy the relauncher into a temporary directory so we can get to it after the new version's installed. + NSString *relaunchPathToCopy = [[NSBundle bundleForClass:[self class]] pathForResource:@"relaunch" ofType:@""]; + NSString *targetPath = [NSTemporaryDirectory() stringByAppendingPathComponent:[relaunchPathToCopy lastPathComponent]]; + // Only the paranoid survive: if there's already a stray copy of relaunch there, we would have problems + [[NSFileManager defaultManager] removeFileAtPath:targetPath handler:nil]; + if ([[NSFileManager defaultManager] copyPath:relaunchPathToCopy toPath:targetPath handler:nil]) + relaunchPath = [targetPath retain]; + + [SUInstaller installFromUpdateFolder:[downloadPath stringByDeletingLastPathComponent] overHostBundle:hostBundle delegate:self synchronously:[self shouldInstallSynchronously]]; } - (void)installerFinishedForHostBundle:(NSBundle *)hb @@ -173,9 +183,12 @@ - (void)relaunchHostApp { [[NSNotificationCenter defaultCenter] postNotificationName:SUUpdaterWillRestartNotification object:self]; - NSString *relaunchPath = [[NSBundle bundleForClass:[self class]] pathForResource:@"relaunch" ofType:nil]; + @try - { + { + if(!relaunchPath || ![[NSFileManager defaultManager] fileExistsAtPath:relaunchPath]) + [NSException raise:@"SURelauncherNotFound" format:@"Couldn't find the relauncher (expected to find it at %@)", relaunchPath]; + [NSTask launchedTaskWithLaunchPath:relaunchPath arguments:[NSArray arrayWithObjects:[hostBundle bundlePath], [NSString stringWithFormat:@"%d", [[NSProcessInfo processInfo] processIdentifier]], nil]]; } @catch (NSException *e) @@ -190,6 +203,7 @@ - (void)installerForHostBundle:(NSBundle *)hb failedWithError:(NSError *)error { if (hb != hostBundle) { return; } + [[NSFileManager defaultManager] removeFileAtPath:relaunchPath handler:NULL]; // Clean up the copied relauncher. [self abortUpdateWithError:[NSError errorWithDomain:SUSparkleErrorDomain code:SUInstallationError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:SULocalizedString(@"An error occurred while installing the update. Please try again later.", nil), NSLocalizedDescriptionKey, [error localizedDescription], NSLocalizedFailureReasonErrorKey, nil]]]; } @@ -212,6 +226,7 @@ { [hostBundle release]; [downloadPath release]; + [relaunchPath release]; [unarchiver release]; [download release]; [super dealloc]; diff --git a/SUInstaller.m b/SUInstaller.m index 2955806d..54a465da 100644 --- a/SUInstaller.m +++ b/SUInstaller.m @@ -16,7 +16,7 @@ NSString *SUInstallerDelegateKey = @"SUInstallerDelegate"; @implementation SUInstaller -+ (void)installFromUpdateFolder:(NSString *)updateFolder overHostBundle:(NSBundle *)hostBundle delegate:delegate synchronously:(BOOL)synchronously; ++ (void)installFromUpdateFolder:(NSString *)updateFolder overHostBundle:(NSBundle *)hostBundle delegate:delegate synchronously:(BOOL)synchronously { // Search subdirectories for the application NSString *currentFile, *newAppDownloadPath = nil, *bundleFileName = [[hostBundle bundlePath] lastPathComponent], *alternateBundleFileName = [[hostBundle name] stringByAppendingPathExtension:[[hostBundle bundlePath] pathExtension]]; diff --git a/relaunch.m b/relaunch.m index b303b9bc..be65c539 100644 --- a/relaunch.m +++ b/relaunch.m @@ -44,7 +44,8 @@ - (void) relaunch { - [[NSWorkspace sharedWorkspace] launchApplication:[NSString stringWithUTF8String:executablePath]]; + [[NSWorkspace sharedWorkspace] launchApplication:[NSString stringWithUTF8String:executablePath]]; + [[NSFileManager defaultManager] removeFileAtPath:[NSTemporaryDirectory() stringByAppendingPathComponent:@"relaunch"] handler:nil]; exit(0); }