diff --git a/Autoupdate/AppInstaller.m b/Autoupdate/AppInstaller.m index f6094ad3..179afe89 100644 --- a/Autoupdate/AppInstaller.m +++ b/Autoupdate/AppInstaller.m @@ -566,6 +566,10 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; return; } + // Used later for relaunching + // Compute this now before we set this installer property to nil + NSString *installationPath = [self.installer installationPath]; + NSError *thirdStageError = nil; if (![self.installer performFinalInstallation:&thirdStageError]) { SULog(@"Failed to finalize installation with error: %@", thirdStageError); @@ -590,13 +594,11 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; [self.communicator handleMessageWithIdentifier:SPUInstallationFinishedStage3 data:[NSData data]]; - NSString *installationPath = [SUInstaller installationPathForHost:self.host]; - if (self.shouldRelaunch) { NSString *pathToRelaunch = nil; - // If the installation path differs from the host path, we give higher precedence for it than - // if the desired relaunch path differs from the host path - if (![installationPath.pathComponents isEqualToArray:self.host.bundlePath.pathComponents] || [self.relaunchPath.pathComponents isEqualToArray:self.host.bundlePath.pathComponents]) { + // If the relaunch path is the same as the host bundle path, use the installation path from the installer which may be normalized + // Otherwise use the requested relaunch path in all other cases + if ([self.relaunchPath.pathComponents isEqualToArray:self.host.bundlePath.pathComponents]) { pathToRelaunch = installationPath; } else { pathToRelaunch = self.relaunchPath; diff --git a/Autoupdate/SPUInstallerProtocol.h b/Autoupdate/SPUInstallerProtocol.h index c01543e4..cb97be4a 100644 --- a/Autoupdate/SPUInstallerProtocol.h +++ b/Autoupdate/SPUInstallerProtocol.h @@ -31,6 +31,9 @@ NS_ASSUME_NONNULL_BEGIN // Should be able to be called from non-main thread - (void)cleanup; +// The destination and installation path of the bundle being updated +- (NSString *)installationPath; + @end NS_ASSUME_NONNULL_END diff --git a/Autoupdate/SUGuidedPackageInstaller.h b/Autoupdate/SUGuidedPackageInstaller.h index fd2aa5fb..be1da7ef 100644 --- a/Autoupdate/SUGuidedPackageInstaller.h +++ b/Autoupdate/SUGuidedPackageInstaller.h @@ -21,6 +21,6 @@ A guided installation can be started by applications other than the application @interface SUGuidedPackageInstaller : NSObject -- (instancetype)initWithPackagePath:(NSString *)packagePath; +- (instancetype)initWithPackagePath:(NSString *)packagePath installationPath:(NSString *)installationPath; @end diff --git a/Autoupdate/SUGuidedPackageInstaller.m b/Autoupdate/SUGuidedPackageInstaller.m index 6206aa3e..4e32c6bf 100644 --- a/Autoupdate/SUGuidedPackageInstaller.m +++ b/Autoupdate/SUGuidedPackageInstaller.m @@ -17,18 +17,21 @@ @interface SUGuidedPackageInstaller () @property (nonatomic, readonly, copy) NSString *packagePath; +@property (nonatomic, readonly, copy) NSString *installationPath; @end @implementation SUGuidedPackageInstaller @synthesize packagePath = _packagePath; +@synthesize installationPath = _installationPath; -- (instancetype)initWithPackagePath:(NSString *)packagePath +- (instancetype)initWithPackagePath:(NSString *)packagePath installationPath:(NSString *)installationPath { self = [super init]; if (self != nil) { _packagePath = [packagePath copy]; + _installationPath = [installationPath copy]; } return self; } diff --git a/Autoupdate/SUInstaller.h b/Autoupdate/SUInstaller.h index 7212df92..025b668f 100644 --- a/Autoupdate/SUInstaller.h +++ b/Autoupdate/SUInstaller.h @@ -20,8 +20,6 @@ NS_ASSUME_NONNULL_BEGIN + (NSString *)installSourcePathInUpdateFolder:(NSString *)inUpdateFolder forHost:(SUHost *)host isPackage:(BOOL *)isPackagePtr isGuided:(nullable BOOL *)isGuidedPtr; -+ (NSString *)installationPathForHost:(SUHost *)host; - @end NS_ASSUME_NONNULL_END diff --git a/Autoupdate/SUInstaller.m b/Autoupdate/SUInstaller.m index 589394f2..a796acab 100644 --- a/Autoupdate/SUInstaller.m +++ b/Autoupdate/SUInstaller.m @@ -136,7 +136,7 @@ *error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUInstallationError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Found guided package installer but '%@=%@' was probably missing in the appcast item enclosure", SUAppcastAttributeInstallationType, SPUInstallationTypeGuidedPackage] }]; } } else { - installer = [[SUGuidedPackageInstaller alloc] initWithPackagePath:newDownloadPath]; + installer = [[SUGuidedPackageInstaller alloc] initWithPackagePath:newDownloadPath installationPath:host.bundlePath]; } } else if (isPackage) { if (![expectedInstallationType isEqualToString:SPUInstallationTypeInteractivePackage]) { @@ -144,7 +144,7 @@ *error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUInstallationError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Found package installer but '%@=%@' was probably missing in the appcast item enclosure", SUAppcastAttributeInstallationType, SPUInstallationTypeInteractivePackage] }]; } } else { - installer = [[SUPackageInstaller alloc] initWithPackagePath:newDownloadPath]; + installer = [[SUPackageInstaller alloc] initWithPackagePath:newDownloadPath installationPath:host.bundlePath]; } } else { if (![expectedInstallationType isEqualToString:SPUInstallationTypeApplication]) { @@ -152,27 +152,35 @@ *error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUInstallationError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Found regular application update but expected '%@=%@' from the appcast item enclosure instead", SUAppcastAttributeInstallationType, expectedInstallationType] }]; } } else { - installer = [[SUPlainInstaller alloc] initWithHost:host applicationPath:newDownloadPath installationPath:[[self class] installationPathForHost:host] versionComparator:comparator]; + NSString *normalizedInstallationPath = nil; + if (SPARKLE_NORMALIZE_INSTALLED_APPLICATION_NAME) { + normalizedInstallationPath = [[self class] normalizedInstallationPathForHost:host]; + } + + // If we have a normalized path, we'll install to "#{CFBundleName}.app", but only if that path doesn't already exist. If we're "Foo 4.2.app," and there's a "Foo.app" in this directory, we don't want to overwrite it! But if there's no "Foo.app," we'll take that name. + // Otherwise if there's no normalized path (the more likely case), we'll just use the host bundle's path + NSString *installationPath; + if (normalizedInstallationPath != nil && ![[NSFileManager defaultManager] fileExistsAtPath:normalizedInstallationPath]) { + installationPath = normalizedInstallationPath; + } else { + installationPath = host.bundlePath; + } + + installer = [[SUPlainInstaller alloc] initWithHost:host applicationPath:newDownloadPath installationPath:installationPath versionComparator:comparator]; } } return installer; } -+ (NSString *)installationPathForHost:(SUHost *)host ++ (NSString *)normalizedInstallationPathForHost:(SUHost *)host { NSBundle *bundle = host.bundle; assert(bundle != nil); - if (SPARKLE_NORMALIZE_INSTALLED_APPLICATION_NAME) { - // We'll install to "#{CFBundleName}.app", but only if that path doesn't already exist. If we're "Foo 4.2.app," and there's a "Foo.app" in this directory, we don't want to overwrite it! But if there's no "Foo.app," we'll take that name. - NSString *normalizedAppPath = [[[bundle bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", [bundle objectForInfoDictionaryKey:(__bridge NSString *)kCFBundleNameKey], [[bundle bundlePath] pathExtension]]]; - - if (![[NSFileManager defaultManager] fileExistsAtPath:normalizedAppPath]) { - return normalizedAppPath; - } - } - return [bundle bundlePath]; + NSString *normalizedAppPath = [[[bundle bundlePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", [bundle objectForInfoDictionaryKey:(__bridge NSString *)kCFBundleNameKey], [[bundle bundlePath] pathExtension]]]; + + return normalizedAppPath; } @end diff --git a/Autoupdate/SUPackageInstaller.h b/Autoupdate/SUPackageInstaller.h index 31fd1979..d068dc17 100644 --- a/Autoupdate/SUPackageInstaller.h +++ b/Autoupdate/SUPackageInstaller.h @@ -14,6 +14,6 @@ @interface SUPackageInstaller : NSObject -- (instancetype)initWithPackagePath:(NSString *)packagePath; +- (instancetype)initWithPackagePath:(NSString *)packagePath installationPath:(NSString *)installationPath; @end diff --git a/Autoupdate/SUPackageInstaller.m b/Autoupdate/SUPackageInstaller.m index b8aad517..34b3ecb8 100644 --- a/Autoupdate/SUPackageInstaller.m +++ b/Autoupdate/SUPackageInstaller.m @@ -18,6 +18,7 @@ @interface SUPackageInstaller () @property (nonatomic, readonly, copy) NSString *packagePath; +@property (nonatomic, readonly, copy) NSString *installationPath; @end @@ -26,12 +27,14 @@ static NSString *SUOpenUtilityPath = @"/usr/bin/open"; @synthesize packagePath = _packagePath; +@synthesize installationPath = _installationPath; -- (instancetype)initWithPackagePath:(NSString *)packagePath +- (instancetype)initWithPackagePath:(NSString *)packagePath installationPath:(NSString *)installationPath { self = [super init]; if (self != nil) { _packagePath = [packagePath copy]; + _installationPath = [installationPath copy]; } return self; } diff --git a/Sparkle/InstallerProgress/InstallerProgressAppController.m b/Sparkle/InstallerProgress/InstallerProgressAppController.m index 936ee814..970b4344 100644 --- a/Sparkle/InstallerProgress/InstallerProgressAppController.m +++ b/Sparkle/InstallerProgress/InstallerProgressAppController.m @@ -243,14 +243,18 @@ static const NSTimeInterval SUTerminationTimeDelay = 0.3; }); } -// The pathToRelaunch passed to here is not necessarily the same as the one passed in -registerApplicationBundlePath:reply: if the developer uses SPARKLE_NORMALIZE_INSTALLED_APPLICATION_NAME -// Note the installer won't tell us to launch an application unless it was already running before -- (void)relaunchPath:(NSString *)pathToRelaunch +- (void)relaunchPath:(NSString *)requestedPathToRelaunch { dispatch_async(dispatch_get_main_queue(), ^{ - if (!self.willTerminate && self.applicationInitiallyAlive) { - // We only launch applications, but I'm not sure how reliable -launchApplicationAtURL:options:config: is so we're not using it - // Eg: http://www.openradar.me/10952677 + if (!self.willTerminate && self.applicationBundle != nil && self.applicationInitiallyAlive) { + // If the normalized setting is not enabled, we shouldn't trust the relaunch path input that is being passed to us + // because we already have the application path to relaunch, and we verified that it was running before + NSString *pathToRelaunch; + if (SPARKLE_NORMALIZE_INSTALLED_APPLICATION_NAME) { + pathToRelaunch = requestedPathToRelaunch; + } else { + pathToRelaunch = self.applicationBundle.bundlePath; + } // We should at least make sure we're opening a bundle however NSBundle *relaunchBundle = [NSBundle bundleWithPath:pathToRelaunch]; @@ -259,6 +263,8 @@ static const NSTimeInterval SUTerminationTimeDelay = 0.3; [self cleanupAndExitWithStatus:EXIT_FAILURE]; } + // We only launch applications, but I'm not sure how reliable -launchApplicationAtURL:options:config: is so we're not using it + // Eg: http://www.openradar.me/10952677 if (![[NSWorkspace sharedWorkspace] openFile:pathToRelaunch]) { SULog(@"Error: Failed to relaunch bundle at %@", pathToRelaunch); }