diff --git a/Configurations/ConfigCommon.xcconfig b/Configurations/ConfigCommon.xcconfig index 2563b492..7e3307c6 100644 --- a/Configurations/ConfigCommon.xcconfig +++ b/Configurations/ConfigCommon.xcconfig @@ -3,7 +3,7 @@ PREBINDING = NO ZERO_LINK = NO CURRENT_PROJECT_VERSION = 1.5 -SDKROOT = $(DEVELOPER_SDK_DIR)/MacOSX10.5.sdk +SDKROOT = $(DEVELOPER_SDK_DIR)/MacOSX10.4u.sdk MACOSX_DEPLOYMENT_TARGET[arch=ppc] = 10.4 MACOSX_DEPLOYMENT_TARGET[arch=ppc64] = 10.5 MACOSX_DEPLOYMENT_TARGET[arch=i386] = 10.4 @@ -14,7 +14,8 @@ GCC_DEBUGGING_SYMBOLS = full GCC_PRECOMPILE_PREFIX_HEADER = YES GCC_PREFIX_HEADER = $(SDKROOT)/System/Library/Frameworks/Cocoa.framework/Headers/Cocoa.h GCC_FAST_OBJC_DISPATCH = YES -ARCHS = ppc i386 x86_64 +GCC_VERSION = 4.0; +ARCHS = ppc i386 // Enable warnings GCC_WARN_CHECK_SWITCH_STATEMENTS = YES diff --git a/SUBasicUpdateDriver.m b/SUBasicUpdateDriver.m index 678583f2..65d06b33 100644 --- a/SUBasicUpdateDriver.m +++ b/SUBasicUpdateDriver.m @@ -141,7 +141,7 @@ // We create a temporary directory in /tmp and stick the file there. // 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 *prefix = [NSString stringWithFormat:@"%@ %@ Update", [host name], [updateItem versionString]]; NSString *desktopFolder = [@"~/Desktop" stringByExpandingTildeInPath]; [tempDir release]; tempDir = [[desktopFolder stringByAppendingPathComponent:prefix] retain]; diff --git a/SUPlainInstallerInternals.h b/SUPlainInstallerInternals.h index efbd5b68..24cd49a2 100644 --- a/SUPlainInstallerInternals.h +++ b/SUPlainInstallerInternals.h @@ -15,6 +15,8 @@ + (NSString *)temporaryNameForPath:(NSString *)path; + (BOOL)copyPathWithAuthentication:(NSString *)src overPath:(NSString *)dst temporaryName:(NSString *)tmp error:(NSError **)error; + (void)_movePathToTrash:(NSString *)path; ++ (BOOL)_removeFileAtPath:(NSString *)path error: (NSError**)error; ++ (BOOL)_removeFileAtPathWithForcedAuthentication:(NSString *)src error:(NSError **)error; @end #endif diff --git a/SUPlainInstallerInternals.m b/SUPlainInstallerInternals.m index 22a73164..37e79e97 100644 --- a/SUPlainInstallerInternals.m +++ b/SUPlainInstallerInternals.m @@ -119,7 +119,7 @@ static BOOL AuthorizationExecuteWithPrivilegesAndWait(AuthorizationRef authoriza if( err == noErr ) err = FSRefMakePath( &trashRef, trashPath, MAXPATHLEN ); if( err == noErr ) - tempDir = [NSString stringWithUTF8String: (char*) trashPath]; + tempDir = [[NSFileManager defaultManager] stringWithFileSystemRepresentation: (char*) trashPath length: strlen((char*) trashPath)]; if( outDidFindTrash ) *outDidFindTrash = (tempDir != nil); if( !tempDir ) @@ -182,6 +182,24 @@ static BOOL AuthorizationExecuteWithPrivilegesAndWait(AuthorizationRef authoriza snprintf(uidgid, sizeof(uidgid), "%d:%d", dstSB.st_uid, dstSB.st_gid); + // If the currently-running application is trusted, the new + // version should be trusted as well. Remove it from the + // quarantine to avoid a delay at launch, and to avoid + // presenting the user with a confusing trust dialog. + // + // This needs to be done after the application is moved to its + // new home with "mv" in case it's moved across filesystems: if + // that happens, "mv" actually performs a copy and may result + // in the application being quarantined. It also needs to be + // done before "chown" changes ownership, because the ownership + // change will almost certainly make it impossible to change + // attributes to release the files from the quarantine. + if (res) + { + SULog(@"releaseFromQuarantine"); + [self performSelectorOnMainThread:@selector(releaseFromQuarantine:) withObject:dst waitUntilDone:YES]; + } + if( res ) // Set permissions while it's still in source, so we have it with working and correct perms when it arrives at destination. { const char* coParams[] = { "-R", uidgid, srcPath, NULL }; @@ -213,22 +231,6 @@ static BOOL AuthorizationExecuteWithPrivilegesAndWait(AuthorizationRef authoriza // res = AuthorizationExecuteWithPrivilegesAndWait( auth, "/bin/rm", kAuthorizationFlagDefaults, rmParams2 ); // } - // If the currently-running application is trusted, the new - // version should be trusted as well. Remove it from the - // quarantine to avoid a delay at launch, and to avoid - // presenting the user with a confusing trust dialog. - // - // This needs to be done after the application is moved to its - // new home with "mv" in case it's moved across filesystems: if - // that happens, "mv" actually performs a copy and may result - // in the application being quarantined. It also needs to be - // done before "chown" changes ownership, because the ownership - // change will almost certainly make it impossible to change - // attributes to release the files from the quarantine. - if (res) { - [self performSelectorOnMainThread:@selector(releaseFromQuarantine:) withObject:dst waitUntilDone:YES]; - } - AuthorizationFree(auth, 0); if (!res) @@ -260,7 +262,8 @@ static BOOL AuthorizationExecuteWithPrivilegesAndWait(AuthorizationRef authoriza AuthorizationRef auth = NULL; OSStatus authStat = errAuthorizationDenied; - while (authStat == errAuthorizationDenied) { + while( authStat == errAuthorizationDenied ) + { authStat = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, @@ -319,6 +322,69 @@ static BOOL AuthorizationExecuteWithPrivilegesAndWait(AuthorizationRef authoriza return res; } + ++ (BOOL)_removeFileAtPathWithForcedAuthentication:(NSString *)src error:(NSError **)error +{ + // *** MUST BE SAFE TO CALL ON NON-MAIN THREAD! + + const char* srcPath = [src fileSystemRepresentation]; + + AuthorizationRef auth = NULL; + OSStatus authStat = errAuthorizationDenied; + while( authStat == errAuthorizationDenied ) + { + authStat = AuthorizationCreate(NULL, + kAuthorizationEmptyEnvironment, + kAuthorizationFlagDefaults, + &auth); + } + + BOOL res = NO; + if (authStat == errAuthorizationSuccess) + { + res = YES; + + if( res ) // If there's something at our tmp path (previous failed update or whatever) delete that first. + { + const char* rmParams[] = { "-rf", srcPath, NULL }; + res = AuthorizationExecuteWithPrivilegesAndWait( auth, "/bin/rm", kAuthorizationFlagDefaults, rmParams ); + if( !res ) + SULog(@"Can't remove destination file"); + } + + AuthorizationFree(auth, 0); + + if (!res) + { + // Something went wrong somewhere along the way, but we're not sure exactly where. + NSString *errorMessage = [NSString stringWithFormat:@"Authenticated file remove from %@ failed.", src]; + if (error != NULL) + *error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUAuthenticationFailure userInfo:[NSDictionary dictionaryWithObject:errorMessage forKey:NSLocalizedDescriptionKey]]; + } + } + else + { + if (error != NULL) + *error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUAuthenticationFailure userInfo:[NSDictionary dictionaryWithObject:@"Couldn't get permission to authenticate." forKey:NSLocalizedDescriptionKey]]; + } + return res; +} + ++ (BOOL)_removeFileAtPath:(NSString *)path error: (NSError**)error +{ + BOOL success = YES; +#if MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_4 + if( ![[NSFileManager defaultManager] removeFileAtPath: path handler: nil] ) +#else + if( ![[NSFileManager defaultManager] removeItemAtPath: path error: NULL] ) +#endif + { + success = [self _removeFileAtPathWithForcedAuthentication: path error: error]; + } + + return success; +} + + (void)_movePathToTrash:(NSString *)path { //SULog(@"Moving %@ to the trash.", path); @@ -342,17 +408,28 @@ static BOOL AuthorizationExecuteWithPrivilegesAndWait(AuthorizationRef authoriza + (BOOL)copyPathWithAuthentication:(NSString *)src overPath:(NSString *)dst temporaryName:(NSString *)tmp error:(NSError **)error { - FSRef srcRef, dstRef, dstDirRev, movedRef, tmpDirRef; + FSRef srcRef, dstRef, dstDirRef, movedRef, tmpDirRef; OSErr err; BOOL hadFileAtDest = NO, didFindTrash = NO; NSString *tmpPath = [self _temporaryCopyNameForPath: dst didFindTrash: &didFindTrash]; + // Make FSRef for destination: err = FSPathMakeRefWithOptions((UInt8 *)[dst fileSystemRepresentation], kFSPathMakeRefDoNotFollowLeafSymlink, &dstRef, NULL); hadFileAtDest = (err == noErr); // There is a file at the destination, move it aside. If we normalized the name, we might not get here, so don't error. if( hadFileAtDest ) { if (0 != access([dst fileSystemRepresentation], W_OK) || 0 != access([[dst stringByDeletingLastPathComponent] fileSystemRepresentation], W_OK)) + { return [self _copyPathWithForcedAuthentication:src toPath:dst temporaryPath:tmpPath error:error]; + } + } + else + { + if (0 != access([[dst stringByDeletingLastPathComponent] fileSystemRepresentation], W_OK) + || 0 != access([[[dst stringByDeletingLastPathComponent] stringByDeletingLastPathComponent] fileSystemRepresentation], W_OK)) + { + return [self _copyPathWithForcedAuthentication:src toPath:dst temporaryPath:tmpPath error:error]; + } } if( hadFileAtDest ) @@ -362,9 +439,12 @@ static BOOL AuthorizationExecuteWithPrivilegesAndWait(AuthorizationRef authoriza err = FSPathMakeRef((UInt8 *)[[dst stringByDeletingLastPathComponent] fileSystemRepresentation], &tmpDirRef, NULL); } - err = FSPathMakeRef((UInt8 *)[[dst stringByDeletingLastPathComponent] fileSystemRepresentation], &dstDirRev, NULL); - if (err == noErr) + err = FSPathMakeRef((UInt8 *)[[dst stringByDeletingLastPathComponent] fileSystemRepresentation], &dstDirRef, NULL); + + if (err == noErr && hadFileAtDest) + { err = FSMoveObjectSync(&dstRef, &tmpDirRef, (CFStringRef)[tmpPath lastPathComponent], &movedRef, 0); + } if (err != noErr && hadFileAtDest) { if (error != NULL) @@ -373,12 +453,12 @@ static BOOL AuthorizationExecuteWithPrivilegesAndWait(AuthorizationRef authoriza } err = FSPathMakeRef((UInt8 *)[src fileSystemRepresentation], &srcRef, NULL); if (err == noErr) - err = FSCopyObjectSync(&srcRef, &dstDirRev, (CFStringRef)[dst lastPathComponent], NULL, 0); + err = FSCopyObjectSync(&srcRef, &dstDirRef, (CFStringRef)[dst lastPathComponent], NULL, 0); if (err != noErr) { // We better move the old version back to its old location if( hadFileAtDest ) - FSMoveObjectSync(&movedRef, &dstDirRev, (CFStringRef)[dst lastPathComponent], &movedRef, 0); + FSMoveObjectSync(&movedRef, &dstDirRef, (CFStringRef)[dst lastPathComponent], &movedRef, 0); if (error != NULL) *error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUFileCopyFailure userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Couldn't copy %@ to %@.", src, dst] forKey:NSLocalizedDescriptionKey]]; return NO; diff --git a/Sparkle.xcodeproj/project.pbxproj b/Sparkle.xcodeproj/project.pbxproj index 3c21581a..7fdad7f6 100644 --- a/Sparkle.xcodeproj/project.pbxproj +++ b/Sparkle.xcodeproj/project.pbxproj @@ -1145,6 +1145,7 @@ isa = XCBuildConfiguration; buildSettings = { GCC_VERSION = 4.0; + SDKROOT = "$(DEVELOPER_SDK_DIR)/MacOSX10.4u.sdk"; WARNING_CFLAGS = "-Wundeclared-selector"; }; name = Debug; @@ -1153,6 +1154,7 @@ isa = XCBuildConfiguration; buildSettings = { GCC_VERSION = 4.0; + SDKROOT = "$(DEVELOPER_SDK_DIR)/MacOSX10.4u.sdk"; WARNING_CFLAGS = "-Wundeclared-selector"; }; name = Release; @@ -1161,10 +1163,6 @@ isa = XCBuildConfiguration; buildSettings = { ALWAYS_SEARCH_USER_PATHS = NO; - ARCHS = ( - i386, - ppc, - ); COPY_PHASE_STRIP = NO; GCC_DYNAMIC_NO_PIC = NO; GCC_ENABLE_FIX_AND_CONTINUE = YES; @@ -1189,10 +1187,6 @@ isa = XCBuildConfiguration; buildSettings = { ALWAYS_SEARCH_USER_PATHS = NO; - ARCHS = ( - i386, - ppc, - ); COPY_PHASE_STRIP = YES; DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; GCC_ENABLE_FIX_AND_CONTINUE = NO; @@ -1217,10 +1211,6 @@ isa = XCBuildConfiguration; buildSettings = { ALWAYS_SEARCH_USER_PATHS = NO; - ARCHS = ( - i386, - ppc, - ); GCC_ENABLE_FIX_AND_CONTINUE = YES; GCC_MODEL_TUNING = G5; GCC_PRECOMPILE_PREFIX_HEADER = YES; @@ -1242,6 +1232,7 @@ isa = XCBuildConfiguration; buildSettings = { GCC_VERSION = 4.0; + SDKROOT = "$(DEVELOPER_SDK_DIR)/MacOSX10.4u.sdk"; WARNING_CFLAGS = "-Wundeclared-selector"; }; name = "Release (GC dual-mode; 10.5-only)"; @@ -1265,7 +1256,6 @@ 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"; @@ -1294,7 +1284,6 @@ 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; @@ -1323,7 +1312,6 @@ 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"; diff --git a/finish_installation-Info.plist b/finish_installation-Info.plist index 875efd5b..cd1be6fa 100644 --- a/finish_installation-Info.plist +++ b/finish_installation-Info.plist @@ -26,5 +26,9 @@ MainMenu NSPrincipalClass NSApplication + LSBackgroundOnly + 1 + LSUIElement + 1 diff --git a/finish_installation.m b/finish_installation.m index d00fb1a3..08831e10 100644 --- a/finish_installation.m +++ b/finish_installation.m @@ -4,57 +4,70 @@ #import "SUHost.h" #import "SUStandardVersionComparator.h" #import "SUStatusController.h" +#import "SUPlainInstallerInternals.h" #include +#define LONG_INSTALLATION_TIME 1.2 // If the Installation takes longer than this time the Application Icon is shown in the Dock so that the user has some feedback. +#define CHECK_FOR_PARENT_TO_QUIT_TIME .5 // Time this app uses to recheck if the parent has already died. + @interface TerminationListener : NSObject { - const char *executablePath; - pid_t parentProcessId; - const char *folderPath; + const char *executablepath; + pid_t parentprocessid; + const char *folderpath; NSString *selfPath; NSTimer *watchdogTimer; + NSTimer *longInstallationTimer; SUHost *host; } -- (void) parentHasQuit; +- (void) parentHasQuit; -- (void) relaunch; -- (void) install; +- (void) relaunch; +- (void) install; -- (void) watchdog:(NSTimer *)timer; +- (void) showAppIconInDock:(NSTimer *)aTimer; +- (void) watchdog:(NSTimer *)aTimer; @end @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 selfPath: (NSString*)inSelfPath { - self = [super init]; - if (self != nil) - { - executablePath = execPath; - parentProcessId = ppid; - folderPath = inFolderPath; - selfPath = [inSelfPath retain]; - BOOL alreadyTerminated = (getppid() == 1); // ppid is launchd (1) => parent terminated already - - if( alreadyTerminated ) - [self parentHasQuit]; - else - watchdogTimer = [[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(watchdog:) userInfo:nil repeats:YES] retain]; - } + if( !(self = [super init]) ) + return nil; + + executablepath = execpath; + parentprocessid = ppid; + folderpath = infolderpath; + selfPath = [inSelfPath retain]; + + BOOL alreadyTerminated = (getppid() == 1); // ppid is launchd (1) => parent terminated already + + if( alreadyTerminated ) + [self parentHasQuit]; + else + watchdogTimer = [[NSTimer scheduledTimerWithTimeInterval:CHECK_FOR_PARENT_TO_QUIT_TIME target:self selector:@selector(watchdog:) userInfo:nil repeats:YES] retain]; + return self; } -(void) dealloc { + [longInstallationTimer invalidate]; + [longInstallationTimer release]; + longInstallationTimer = nil; + [selfPath release]; selfPath = nil; + [watchdogTimer release]; watchdogTimer = nil; + [host release]; host = nil; @@ -65,45 +78,56 @@ -(void) parentHasQuit { [watchdogTimer invalidate]; - - if( folderPath ) + longInstallationTimer = [[NSTimer scheduledTimerWithTimeInterval: LONG_INSTALLATION_TIME + target: self selector: @selector(showAppIconInDock:) + userInfo:nil repeats:NO] retain]; + + if( folderpath ) [self install]; else [self relaunch]; } - -- (void)watchdog:(NSTimer *)timer +- (void) watchdog:(NSTimer *)aTimer { ProcessSerialNumber psn; - if (GetProcessForPID(parentProcessId, &psn) == procNotFound) + if (GetProcessForPID(parentprocessid, &psn) == procNotFound) [self parentHasQuit]; } +- (void)showAppIconInDock:(NSTimer *)aTimer; +{ + ProcessSerialNumber psn = { 0, kCurrentProcess }; + TransformProcessType( &psn, kProcessTransformToForegroundApplication ); +} + + - (void) relaunch { NSString *appPath = nil; - if( !folderPath ) - appPath = [[NSFileManager defaultManager] stringWithFileSystemRepresentation:executablePath length:strlen(executablePath)]; + 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 - if( folderPath ) - [[NSFileManager defaultManager] removeFileAtPath: [SUInstaller updateFolder] handler: nil]; [[NSFileManager defaultManager] removeFileAtPath: selfPath handler: nil]; #else - if( folderPath ) - [[NSFileManager defaultManager] removeItemAtPath: [SUInstaller updateFolder] error: NULL]; [[NSFileManager defaultManager] removeItemAtPath: selfPath error: NULL]; #endif exit(EXIT_SUCCESS); } --(void) install +- (void) install { - NSBundle *theBundle = [NSBundle bundleWithPath: [NSString stringWithUTF8String: executablePath]]; + NSBundle *theBundle = [NSBundle bundleWithPath: [[NSFileManager defaultManager] stringWithFileSystemRepresentation: executablepath length:strlen(executablepath)]]; host = [[SUHost alloc] initWithBundle: theBundle]; SUStatusController* statusCtl = [[SUStatusController alloc] initWithHost: host]; // We quit anyway after we've installed, so leak this for now. @@ -112,20 +136,20 @@ maxProgressValue: 0 statusText: @""]; [statusCtl showWindow: self]; - [SUInstaller installFromUpdateFolder: [NSString stringWithUTF8String: folderPath] + [SUInstaller installFromUpdateFolder: [[NSFileManager defaultManager] stringWithFileSystemRepresentation: folderpath length: strlen(folderpath)] overHost: host delegate: self synchronously: NO versionComparator: [SUStandardVersionComparator defaultComparator]]; } -- (void)installerFinishedForHost:(SUHost *)aHost +- (void) installerFinishedForHost:(SUHost *)aHost { [self relaunch]; } -- (void)installerForHost:(SUHost *)host failedWithError:(NSError *)error +- (void) installerForHost:(SUHost *)host failedWithError:(NSError *)error { - NSRunAlertPanel( @"", @"%@", @"OK", @"", @"", error ); + NSRunAlertPanel( @"", @"%@", @"OK", @"", @"", [error localizedDescription] ); exit(EXIT_FAILURE); } @@ -138,18 +162,18 @@ int main (int argc, const char * argv[]) NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - ProcessSerialNumber psn = { 0, kCurrentProcess }; - TransformProcessType( &psn, kProcessTransformToForegroundApplication ); + //ProcessSerialNumber psn = { 0, kCurrentProcess }; + //TransformProcessType( &psn, kProcessTransformToForegroundApplication ); [[NSApplication sharedApplication] activateIgnoringOtherApps: YES]; #if 0 // Cmdline tool NSString* selfPath = nil; if( argv[0][0] == '/' ) - selfPath = [NSString stringWithUTF8String: argv[0]]; + selfPath = [[NSFileManager defaultManager] stringWithFileSystemRepresentation: argv[0] length: strlen(argv[0])]; else { selfPath = [[NSFileManager defaultManager] currentDirectoryPath]; - selfPath = [selfPath stringByAppendingPathComponent: [NSString stringWithUTF8String: argv[0]]]; + selfPath = [selfPath stringByAppendingPathComponent: [[NSFileManager defaultManager] stringWithFileSystemRepresentation: argv[0] length: strlen(argv[0])]]; } #else NSString* selfPath = [[NSBundle mainBundle] bundlePath];