From 0ffcde4fbbc8c13401fb279d73fbb503ada25802 Mon Sep 17 00:00:00 2001 From: Uli Kusterer Date: Fri, 11 Dec 2009 15:13:30 +0100 Subject: [PATCH 1/5] Make sure we delete the temporary folder when the user cancels the download. Also make sure we delete it on 10.4, which seems to have a bug in removeFileAtPath: that won't delete nested empty folders. As a quick fix, I just trash the folder in that case. We already put the old version in the trash anyway, and this saves me from having to write, test and debug a recursive removal function. --- SUBasicUpdateDriver.h | 1 + SUBasicUpdateDriver.m | 17 ++++++++++------- SUUIBasedUpdateDriver.m | 7 ++++++- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/SUBasicUpdateDriver.h b/SUBasicUpdateDriver.h index ecf64279..21317496 100644 --- a/SUBasicUpdateDriver.h +++ b/SUBasicUpdateDriver.h @@ -18,6 +18,7 @@ NSURLDownload *download; NSString *downloadPath; + NSString *tempDir; NSString *relaunchPath; } diff --git a/SUBasicUpdateDriver.m b/SUBasicUpdateDriver.m index 86b623fc..678583f2 100644 --- a/SUBasicUpdateDriver.m +++ b/SUBasicUpdateDriver.m @@ -143,11 +143,13 @@ // Not using a GUID here because hdiutil (for DMGs) for some reason chokes on GUIDs. Too long? I really have no idea. NSString *prefix = [NSString stringWithFormat:@"%@ %@ Update", [host name], [host version]]; NSString *desktopFolder = [@"~/Desktop" stringByExpandingTildeInPath]; - NSString *tempDir = [desktopFolder stringByAppendingPathComponent:prefix]; + [tempDir release]; + tempDir = [[desktopFolder stringByAppendingPathComponent:prefix] retain]; int cnt=1; while ([[NSFileManager defaultManager] fileExistsAtPath:tempDir] && cnt <= 999) { - tempDir = [desktopFolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@ %d", prefix, cnt++]]; + [tempDir release]; + tempDir = [[desktopFolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@ %d", prefix, cnt++]] retain]; } #if MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_4 @@ -188,8 +190,8 @@ - (void)download:(NSURLDownload *)download didFailWithError:(NSError *)error { // Get rid of what we've downloaded so far, if anything. - if (downloadPath != nil) - [[NSWorkspace sharedWorkspace] performFileOperation:NSWorkspaceRecycleOperation source:[downloadPath stringByDeletingLastPathComponent] destination:@"" files:[NSArray arrayWithObject:[downloadPath lastPathComponent]] tag:NULL]; + if (tempDir != nil) // tempDir contains downloadPath, so we implicitly delete both here. + [[NSWorkspace sharedWorkspace] performFileOperation:NSWorkspaceRecycleOperation source:[tempDir stringByDeletingLastPathComponent] destination:@"" files:[NSArray arrayWithObject:[tempDir lastPathComponent]] tag:NULL]; [self abortUpdateWithError:[NSError errorWithDomain:SUSparkleErrorDomain code:SURelaunchError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:SULocalizedString(@"An error occurred while downloading the update. Please try again later.", nil), NSLocalizedDescriptionKey, [error localizedDescription], NSLocalizedFailureReasonErrorKey, nil]]]; } @@ -292,7 +294,7 @@ 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]], [downloadPath stringByDeletingLastPathComponent], nil]]; + [NSTask launchedTaskWithLaunchPath: relaunchToolPath arguments:[NSArray arrayWithObjects:pathToRelaunch, [NSString stringWithFormat:@"%d", [[NSProcessInfo processInfo] processIdentifier]], tempDir, nil]]; [NSApp terminate:self]; } @@ -303,9 +305,9 @@ - (void)cleanUp { #if MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_4 - [[NSFileManager defaultManager] removeFileAtPath:[downloadPath stringByDeletingLastPathComponent] handler:nil]; + [[NSFileManager defaultManager] removeFileAtPath: tempDir handler:nil]; #else - [[NSFileManager defaultManager] removeItemAtPath:[downloadPath stringByDeletingLastPathComponent] error:NULL]; + [[NSFileManager defaultManager] removeItemAtPath: tempDir error:NULL]; #endif } @@ -339,6 +341,7 @@ [updateItem release]; [download release]; [downloadPath release]; + [tempDir release]; [relaunchPath release]; [super dealloc]; } diff --git a/SUUIBasedUpdateDriver.m b/SUUIBasedUpdateDriver.m index b5552546..f85cb371 100644 --- a/SUUIBasedUpdateDriver.m +++ b/SUUIBasedUpdateDriver.m @@ -118,11 +118,16 @@ [statusController setStatusText:[NSString stringWithFormat:SULocalizedString(@"%@ downloaded", nil), [self _humanReadableSizeFromDouble:[statusController progressValue]]]]; } -- (IBAction)cancelDownload:sender +- (IBAction)cancelDownload: (id)sender { if (download) [download cancel]; [self abortUpdate]; + if (tempDir != nil) // tempDir contains downloadPath, so we implicitly delete both here. + { + if( ![[NSFileManager defaultManager] removeFileAtPath: tempDir handler: nil] ) + [[NSWorkspace sharedWorkspace] performFileOperation:NSWorkspaceRecycleOperation source:[tempDir stringByDeletingLastPathComponent] destination:@"" files:[NSArray arrayWithObject:[tempDir lastPathComponent]] tag:NULL]; + } } - (void)extractUpdate From 4797446f7c448b19dfb650fb02d32e5bb96b3450 Mon Sep 17 00:00:00 2001 From: Uli Kusterer Date: Fri, 11 Dec 2009 15:17:59 +0100 Subject: [PATCH 2/5] Be paranoid and wait for the mdimport to finish before we launch the app. Might make spurious "no executable" errors a bit less likely. --- SUInstaller.m | 1 + 1 file changed, 1 insertion(+) diff --git a/SUInstaller.m b/SUInstaller.m index 64c50318..a2037bf4 100644 --- a/SUInstaller.m +++ b/SUInstaller.m @@ -129,6 +129,7 @@ static NSString* sUpdateFolder = nil; @try { [mdimport launch]; + [mdimport waitUntilExit]; } @catch (NSException * launchException) { From d593ab38adcc4d938bf5834b66560a994ca902d5 Mon Sep 17 00:00:00 2001 From: Uli Kusterer Date: Tue, 15 Dec 2009 16:49:35 +0100 Subject: [PATCH 3/5] Fix configuration for finish_installation.app target: Otherwise it won't build correctly on 10.4 and gives us "unsupported required load command" errors instead of starting up. --- Sparkle.xcodeproj/project.pbxproj | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Sparkle.xcodeproj/project.pbxproj b/Sparkle.xcodeproj/project.pbxproj index 7fdad7f6..1a7aa170 100644 --- a/Sparkle.xcodeproj/project.pbxproj +++ b/Sparkle.xcodeproj/project.pbxproj @@ -1161,6 +1161,7 @@ }; 55F9B5E810C68F8A0099848A /* Debug */ = { isa = XCBuildConfiguration; + baseConfigurationReference = FA1941D30D94A70100DD942E /* ConfigRelaunchDebug.xcconfig */; buildSettings = { ALWAYS_SEARCH_USER_PATHS = NO; COPY_PHASE_STRIP = NO; @@ -1185,6 +1186,7 @@ }; 55F9B5E910C68F8A0099848A /* Release */ = { isa = XCBuildConfiguration; + baseConfigurationReference = FA1941D40D94A70100DD942E /* ConfigRelaunchRelease.xcconfig */; buildSettings = { ALWAYS_SEARCH_USER_PATHS = NO; COPY_PHASE_STRIP = YES; @@ -1209,6 +1211,7 @@ }; 55F9B5EA10C68F8A0099848A /* Release (GC dual-mode; 10.5-only) */ = { isa = XCBuildConfiguration; + baseConfigurationReference = FA1941D40D94A70100DD942E /* ConfigRelaunchRelease.xcconfig */; buildSettings = { ALWAYS_SEARCH_USER_PATHS = NO; GCC_ENABLE_FIX_AND_CONTINUE = YES; @@ -1256,6 +1259,7 @@ isa = XCBuildConfiguration; buildSettings = { ALWAYS_SEARCH_USER_PATHS = NO; + ARCHS = "$(NATIVE_ARCH)"; COPY_PHASE_STRIP = YES; DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; FRAMEWORK_SEARCH_PATHS = "$(DEVELOPER_LIBRARY_DIR)/Frameworks"; @@ -1284,6 +1288,7 @@ isa = XCBuildConfiguration; buildSettings = { ALWAYS_SEARCH_USER_PATHS = NO; + ARCHS = "$(NATIVE_ARCH_ACTUAL)"; COPY_PHASE_STRIP = NO; FRAMEWORK_SEARCH_PATHS = "$(DEVELOPER_LIBRARY_DIR)/Frameworks"; GCC_DYNAMIC_NO_PIC = NO; @@ -1312,6 +1317,7 @@ isa = XCBuildConfiguration; buildSettings = { ALWAYS_SEARCH_USER_PATHS = NO; + ARCHS = "$(NATIVE_ARCH)"; COPY_PHASE_STRIP = YES; DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; FRAMEWORK_SEARCH_PATHS = "$(DEVELOPER_LIBRARY_DIR)/Frameworks"; From 4f76b42bb570ffb5a82da6664a318027bafbe12a Mon Sep 17 00:00:00 2001 From: Uli Kusterer Date: Tue, 15 Dec 2009 16:50:43 +0100 Subject: [PATCH 4/5] If we install under a normalized name, make sure we take owner/group from the target directory, not from a nonexistent destination application we haven't installed yet. --- SUPlainInstallerInternals.m | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/SUPlainInstallerInternals.m b/SUPlainInstallerInternals.m index 37e79e97..a20fb74b 100644 --- a/SUPlainInstallerInternals.m +++ b/SUPlainInstallerInternals.m @@ -163,7 +163,17 @@ static BOOL AuthorizationExecuteWithPrivilegesAndWait(AuthorizationRef authoriza const char* dstPath = [dst fileSystemRepresentation]; struct stat dstSB; - stat(dstPath, &dstSB); + if( stat(dstPath, &dstSB) != 0 ) // Doesn't exist yet, try containing folder. + { + const char* dstDirPath = [[dst stringByDeletingLastPathComponent] fileSystemRepresentation]; + if( stat(dstDirPath, &dstSB) != 0 ) + { + NSString *errorMessage = [NSString stringWithFormat:@"Stat on %@ during authenticated file copy failed.", dst]; + if (error != NULL) + *error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUFileCopyFailure userInfo:[NSDictionary dictionaryWithObject:errorMessage forKey:NSLocalizedDescriptionKey]]; + return NO; + } + } AuthorizationRef auth = NULL; OSStatus authStat = errAuthorizationDenied; @@ -179,7 +189,7 @@ static BOOL AuthorizationExecuteWithPrivilegesAndWait(AuthorizationRef authoriza res = YES; char uidgid[42]; - snprintf(uidgid, sizeof(uidgid), "%d:%d", + snprintf(uidgid, sizeof(uidgid), "%u:%u", dstSB.st_uid, dstSB.st_gid); // If the currently-running application is trusted, the new @@ -204,6 +214,8 @@ static BOOL AuthorizationExecuteWithPrivilegesAndWait(AuthorizationRef authoriza { const char* coParams[] = { "-R", uidgid, srcPath, NULL }; res = AuthorizationExecuteWithPrivilegesAndWait( auth, "/usr/sbin/chown", kAuthorizationFlagDefaults, coParams ); + if( !res ) + SULog( @"chown -R %s %s failed.", uidgid, srcPath ); } BOOL haveDst = [[NSFileManager defaultManager] fileExistsAtPath: dst]; @@ -211,18 +223,24 @@ static BOOL AuthorizationExecuteWithPrivilegesAndWait(AuthorizationRef authoriza { const char* rmParams[] = { "-rf", tmpPath, NULL }; res = AuthorizationExecuteWithPrivilegesAndWait( auth, "/bin/rm", kAuthorizationFlagDefaults, rmParams ); + if( !res ) + SULog( @"rm failed" ); } if( res && haveDst ) // Move old exe to tmp path. { const char* mvParams[] = { "-f", dstPath, tmpPath, NULL }; res = AuthorizationExecuteWithPrivilegesAndWait( auth, "/bin/mv", kAuthorizationFlagDefaults, mvParams ); + if( !res ) + SULog( @"mv 1 failed" ); } if( res ) // Move new exe to old exe's path. { const char* mvParams2[] = { "-f", srcPath, dstPath, NULL }; res = AuthorizationExecuteWithPrivilegesAndWait( auth, "/bin/mv", kAuthorizationFlagDefaults, mvParams2 ); + if( !res ) + SULog( @"mv 2 failed" ); } // if( res && haveDst /*&& !foundTrash*/ ) // If we managed to put the old exe in the trash, leave it there for the user to delete or recover. From 50b76d59f7ea4d8880be77055c22611c96e435db Mon Sep 17 00:00:00 2001 From: Uli Kusterer Date: Tue, 15 Dec 2009 16:51:01 +0100 Subject: [PATCH 5/5] Add missing include. --- finish_installation.m | 1 + 1 file changed, 1 insertion(+) diff --git a/finish_installation.m b/finish_installation.m index 08831e10..f1ff0eec 100644 --- a/finish_installation.m +++ b/finish_installation.m @@ -5,6 +5,7 @@ #import "SUStandardVersionComparator.h" #import "SUStatusController.h" #import "SUPlainInstallerInternals.h" +#import "SULog.h" #include