From 3852809c7be8a4dbddbfbc39da9b05c7f1ee6f9c Mon Sep 17 00:00:00 2001 From: David Smith Date: Wed, 14 May 2008 00:00:14 -0700 Subject: [PATCH] Initial work on copying the relauncher to NSTemporaryDirectory() --- SUAutomaticUpdateDriver.m | 2 +- SUBasicUpdateDriver.h | 1 + SUBasicUpdateDriver.m | 24 +++++++++++++++++++++--- SUInstaller.h | 2 +- SUInstaller.m | 7 ++++++- Sparkle.xcodeproj/project.pbxproj | 14 ++++++++++++-- 6 files changed, 42 insertions(+), 8 deletions(-) diff --git a/SUAutomaticUpdateDriver.m b/SUAutomaticUpdateDriver.m index abba7765..3803ee76 100644 --- a/SUAutomaticUpdateDriver.m +++ b/SUAutomaticUpdateDriver.m @@ -49,7 +49,7 @@ - (void)installUpdate { showErrors = YES; - [SUInstaller installFromUpdateFolder:[downloadPath stringByDeletingLastPathComponent] overHostBundle:hostBundle delegate:self synchronously:postponingInstallation]; + [SUInstaller installFromUpdateFolder:[downloadPath stringByDeletingLastPathComponent] overHostBundle:hostBundle delegate:self synchronously:postponingInstallation relauncherPath:&relaunchPath]; } - (void)applicationWillTerminate:(NSNotification *)note diff --git a/SUBasicUpdateDriver.h b/SUBasicUpdateDriver.h index 5699a79b..251418fa 100644 --- a/SUBasicUpdateDriver.h +++ b/SUBasicUpdateDriver.h @@ -20,6 +20,7 @@ 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..750770cb 100644 --- a/SUBasicUpdateDriver.m +++ b/SUBasicUpdateDriver.m @@ -160,7 +160,9 @@ - (void)installUpdate { - [SUInstaller installFromUpdateFolder:[downloadPath stringByDeletingLastPathComponent] overHostBundle:hostBundle delegate:self synchronously:NO]; + //This will copy the relauncher into NSTemporaryDirectory() just before it overwrites the bundle + //See https://bugs.launchpad.net/sparkle/+bug/230123 + [SUInstaller installFromUpdateFolder:[downloadPath stringByDeletingLastPathComponent] overHostBundle:hostBundle delegate:self synchronously:NO relauncherPath:&relaunchPath]; } - (void)installerFinishedForHostBundle:(NSBundle *)hb @@ -173,10 +175,26 @@ - (void)relaunchHostApp { [[NSNotificationCenter defaultCenter] postNotificationName:SUUpdaterWillRestartNotification object:self]; - NSString *relaunchPath = [[NSBundle bundleForClass:[self class]] pathForResource:@"relaunch" ofType:nil]; + @try { - [NSTask launchedTaskWithLaunchPath:relaunchPath arguments:[NSArray arrayWithObjects:[hostBundle bundlePath], [NSString stringWithFormat:@"%d", [[NSProcessInfo processInfo] processIdentifier]], nil]]; + //if we failed to copy the relauncher into NSTemporaryDirectory(), use the one in the new bundle and hope for the best. + + NSString *realRelauncherPath = relaunchPath; + if(!relaunchPath || ![[NSFileManager defaultManager] fileExistsAtPath:relaunchPath]) + realRelauncherPath = [[NSBundle bundleForClass:[self class]] pathForAuxiliaryExecutable:@"relaunch"]; + + [NSTask launchedTaskWithLaunchPath:realRelauncherPath arguments:[NSArray arrayWithObjects:[hostBundle bundlePath], [NSString stringWithFormat:@"%d", [[NSProcessInfo processInfo] processIdentifier]], nil]]; + + //if there's a possibility that a copy of relauncher is in NSTemporaryDirectory(), we need to be sure to clean it up + if(relaunchPath) + { + [[NSWorkspace sharedWorkspace] performFileOperation:NSWorkspaceRecycleOperation + source:[relaunchPath stringByDeletingLastPathComponent] + destination:@"" + files:[NSArray arrayWithObject:[relaunchPath lastPathComponent]] + tag:NULL]; + } } @catch (NSException *e) { diff --git a/SUInstaller.h b/SUInstaller.h index 08bfc974..891c973e 100644 --- a/SUInstaller.h +++ b/SUInstaller.h @@ -12,7 +12,7 @@ #import @interface SUInstaller : NSObject { } -+ (void)installFromUpdateFolder:(NSString *)updateFolder overHostBundle:(NSBundle *)hostBundle delegate:delegate synchronously:(BOOL)synchronously; ++ (void)installFromUpdateFolder:(NSString *)updateFolder overHostBundle:(NSBundle *)hostBundle delegate:delegate synchronously:(BOOL)synchronously relauncherPath:(NSString **)relaunchPath; + (void)_finishInstallationWithResult:(BOOL)result hostBundle:(NSBundle *)hostBundle error:(NSError *)error delegate:delegate; @end diff --git a/SUInstaller.m b/SUInstaller.m index 2955806d..e560e681 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 relauncherPath:(NSString **)relaunchPath; { // Search subdirectories for the application NSString *currentFile, *newAppDownloadPath = nil, *bundleFileName = [[hostBundle bundlePath] lastPathComponent], *alternateBundleFileName = [[hostBundle name] stringByAppendingPathExtension:[[hostBundle bundlePath] pathExtension]]; @@ -56,6 +56,11 @@ NSString *SUInstallerDelegateKey = @"SUInstallerDelegate"; } else { + NSString *relaunchPathToCopy = [[NSBundle bundleForClass:[self class]] pathForAuxiliaryExecutable:@"relaunch"]; + NSString *targetPath = [NSTemporaryDirectory() stringByAppendingPathComponent:[relaunchPathToCopy lastPathComponent]]; + if([[NSFileManager defaultManager] copyPath:relaunchPathToCopy toPath:targetPath handler:nil]) + *relaunchPath = targetPath; + [(isPackage ? [SUPackageInstaller class] : [SUPlainInstaller class]) performInstallationWithPath:newAppDownloadPath hostBundle:hostBundle delegate:delegate synchronously:synchronously]; } } diff --git a/Sparkle.xcodeproj/project.pbxproj b/Sparkle.xcodeproj/project.pbxproj index fbb87ad2..714b0128 100644 --- a/Sparkle.xcodeproj/project.pbxproj +++ b/Sparkle.xcodeproj/project.pbxproj @@ -95,7 +95,7 @@ 61F83F740DBFE141006FDD30 /* SUBasicUpdateDriver.h in Headers */ = {isa = PBXBuildFile; fileRef = 61F83F6F0DBFE137006FDD30 /* SUBasicUpdateDriver.h */; }; DAAEFC9B0DA5722F0051E0D0 /* AppKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0867D6A5FE840307C02AAC07 /* AppKit.framework */; }; DAAEFD4E0DA572330051E0D0 /* relaunch.m in Sources */ = {isa = PBXBuildFile; fileRef = 613242130CD06CEF00106AA4 /* relaunch.m */; }; - DAAEFD510DA572550051E0D0 /* relaunch in Resources */ = {isa = PBXBuildFile; fileRef = DAAEFC960DA571DF0051E0D0 /* relaunch */; }; + DAAEFD510DA572550051E0D0 /* relaunch in CopyFiles */ = {isa = PBXBuildFile; fileRef = DAAEFC960DA571DF0051E0D0 /* relaunch */; }; FAEFA2F70D94AA7500472538 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0867D69BFE84028FC02AAC07 /* Foundation.framework */; }; FAEFA2F80D94AA7900472538 /* AppKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0867D6A5FE840307C02AAC07 /* AppKit.framework */; }; FAEFA3040D94AB3400472538 /* AppKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0867D6A5FE840307C02AAC07 /* AppKit.framework */; }; @@ -144,6 +144,16 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + 63F107920DDABE9E0073D493 /* CopyFiles */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = relaunch; + dstSubfolderSpec = 6; + files = ( + DAAEFD510DA572550051E0D0 /* relaunch in CopyFiles */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ @@ -634,6 +644,7 @@ 8DC2EF520486A6940098B216 /* Resources */, 8DC2EF540486A6940098B216 /* Sources */, 8DC2EF560486A6940098B216 /* Frameworks */, + 63F107920DDABE9E0073D493 /* CopyFiles */, ); buildRules = ( ); @@ -738,7 +749,6 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( - DAAEFD510DA572550051E0D0 /* relaunch in Resources */, 610D5A750A1670A4004AAD9C /* SUStatus.nib in Resources */, 61AAE8280A321A7F00D8810D /* Sparkle.strings in Resources */, 61AAE8290A321A8000D8810D /* SUAutomaticUpdateAlert.nib in Resources */,