From 3852809c7be8a4dbddbfbc39da9b05c7f1ee6f9c Mon Sep 17 00:00:00 2001 From: David Smith Date: Wed, 14 May 2008 00:00:14 -0700 Subject: [PATCH 1/4] 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 */, From 55690853a727e7891bc58a2c7945f8dbbb0372a9 Mon Sep 17 00:00:00 2001 From: David Smith Date: Wed, 14 May 2008 00:11:28 -0700 Subject: [PATCH 2/4] OK, so aux executables don't work in frameworks. Who knew? :) --- SUBasicUpdateDriver.m | 2 +- SUInstaller.m | 2 +- Sparkle.xcodeproj/project.pbxproj | 14 ++------------ 3 files changed, 4 insertions(+), 14 deletions(-) diff --git a/SUBasicUpdateDriver.m b/SUBasicUpdateDriver.m index 750770cb..93b9d831 100644 --- a/SUBasicUpdateDriver.m +++ b/SUBasicUpdateDriver.m @@ -182,7 +182,7 @@ NSString *realRelauncherPath = relaunchPath; if(!relaunchPath || ![[NSFileManager defaultManager] fileExistsAtPath:relaunchPath]) - realRelauncherPath = [[NSBundle bundleForClass:[self class]] pathForAuxiliaryExecutable:@"relaunch"]; + realRelauncherPath = [[NSBundle bundleForClass:[self class]] pathForResource:@"relaunch" ofType:@""]; [NSTask launchedTaskWithLaunchPath:realRelauncherPath arguments:[NSArray arrayWithObjects:[hostBundle bundlePath], [NSString stringWithFormat:@"%d", [[NSProcessInfo processInfo] processIdentifier]], nil]]; diff --git a/SUInstaller.m b/SUInstaller.m index e560e681..07bb989b 100644 --- a/SUInstaller.m +++ b/SUInstaller.m @@ -56,7 +56,7 @@ NSString *SUInstallerDelegateKey = @"SUInstallerDelegate"; } else { - NSString *relaunchPathToCopy = [[NSBundle bundleForClass:[self class]] pathForAuxiliaryExecutable:@"relaunch"]; + NSString *relaunchPathToCopy = [[NSBundle bundleForClass:[self class]] pathForResource:@"relaunch" ofType:@""]; NSString *targetPath = [NSTemporaryDirectory() stringByAppendingPathComponent:[relaunchPathToCopy lastPathComponent]]; if([[NSFileManager defaultManager] copyPath:relaunchPathToCopy toPath:targetPath handler:nil]) *relaunchPath = targetPath; diff --git a/Sparkle.xcodeproj/project.pbxproj b/Sparkle.xcodeproj/project.pbxproj index 714b0128..fbb87ad2 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 CopyFiles */ = {isa = PBXBuildFile; fileRef = DAAEFC960DA571DF0051E0D0 /* relaunch */; }; + DAAEFD510DA572550051E0D0 /* relaunch in Resources */ = {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,16 +144,6 @@ ); 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 */ @@ -644,7 +634,6 @@ 8DC2EF520486A6940098B216 /* Resources */, 8DC2EF540486A6940098B216 /* Sources */, 8DC2EF560486A6940098B216 /* Frameworks */, - 63F107920DDABE9E0073D493 /* CopyFiles */, ); buildRules = ( ); @@ -749,6 +738,7 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( + DAAEFD510DA572550051E0D0 /* relaunch in Resources */, 610D5A750A1670A4004AAD9C /* SUStatus.nib in Resources */, 61AAE8280A321A7F00D8810D /* Sparkle.strings in Resources */, 61AAE8290A321A8000D8810D /* SUAutomaticUpdateAlert.nib in Resources */, From f1198d8167d6752476a673967016f591f9dd3ace Mon Sep 17 00:00:00 2001 From: David Smith Date: Wed, 14 May 2008 22:48:19 -0700 Subject: [PATCH 3/4] Actually works now, but leaves one leftover copy of relaunch in NSTemporaryDirectory() --- SUBasicUpdateDriver.m | 10 ---------- SUInstaller.m | 2 ++ 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/SUBasicUpdateDriver.m b/SUBasicUpdateDriver.m index 93b9d831..6b932a13 100644 --- a/SUBasicUpdateDriver.m +++ b/SUBasicUpdateDriver.m @@ -185,16 +185,6 @@ realRelauncherPath = [[NSBundle bundleForClass:[self class]] pathForResource:@"relaunch" ofType:@""]; [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.m b/SUInstaller.m index 07bb989b..710192b1 100644 --- a/SUInstaller.m +++ b/SUInstaller.m @@ -58,6 +58,8 @@ NSString *SUInstallerDelegateKey = @"SUInstallerDelegate"; { 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; From 93de812aeb14c11f6f27cae99942a819b9080c1b Mon Sep 17 00:00:00 2001 From: David Smith Date: Thu, 15 May 2008 00:49:54 -0700 Subject: [PATCH 4/4] At Andy's suggestion, don't bother with overly paranoid fallback behavior. If we can't copy to NSTemporaryDirectory(), something is very very wrong and we should bail. This greatly simplifies some things and lets me clean up the copy of relaunch once it's done. :) --- SUBasicUpdateDriver.m | 5 ++--- relaunch.m | 3 ++- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/SUBasicUpdateDriver.m b/SUBasicUpdateDriver.m index 6b932a13..745989a8 100644 --- a/SUBasicUpdateDriver.m +++ b/SUBasicUpdateDriver.m @@ -180,11 +180,10 @@ { //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]] pathForResource:@"relaunch" ofType:@""]; + @throw [NSException exceptionWithName:@"SURelauncherNotFound" reason:@"No relauncher available" userInfo:nil]; - [NSTask launchedTaskWithLaunchPath:realRelauncherPath arguments:[NSArray arrayWithObjects:[hostBundle bundlePath], [NSString stringWithFormat:@"%d", [[NSProcessInfo processInfo] processIdentifier]], nil]]; + [NSTask launchedTaskWithLaunchPath:relaunchPath arguments:[NSArray arrayWithObjects:[hostBundle bundlePath], [NSString stringWithFormat:@"%d", [[NSProcessInfo processInfo] processIdentifier]], nil]]; } @catch (NSException *e) { 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); }