From 926c47d48e245453d7c16407ff01f654e934eb80 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sat, 20 Aug 2022 10:30:00 -0700 Subject: [PATCH] Find potential matching translocated running apps in updater agent (#2233) In the installer progress agent, when we find all potential running applications for a host bundle, we should also consider any translocated running apps that look-alike the host bundle. This matters when updating external bundles. For example if sparkle-cli tries to update an app on disk in ~/Downloads/ but a running instance of the app is translocated, sparkle-cli should terminate the translocated instance of the app before installing the update. It's possible this could generate a false positive but we err on the side of safety here. We only use running translocated apps if we find no regular matching apps. --- .../InstallerProgressAppController.m | 28 +++++++++++++++---- 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/Sparkle/InstallerProgress/InstallerProgressAppController.m b/Sparkle/InstallerProgress/InstallerProgressAppController.m index d1119308..a6078709 100644 --- a/Sparkle/InstallerProgress/InstallerProgressAppController.m +++ b/Sparkle/InstallerProgress/InstallerProgressAppController.m @@ -224,18 +224,34 @@ static const NSTimeInterval SUTerminationTimeDelay = 0.3; NSMutableArray *matchedRunningApplications = [[NSMutableArray alloc] init]; if (bundleIdentifier != nil && bundlePathComponents != nil) { - NSArray *runningApplications = - (bundleIdentifier != nil) ? - [NSRunningApplication runningApplicationsWithBundleIdentifier:bundleIdentifier] : - [[NSWorkspace sharedWorkspace] runningApplications]; + NSArray *runningApplications = [NSRunningApplication runningApplicationsWithBundleIdentifier:bundleIdentifier]; + + // If we find any running application that is translocated and looks like the bundle, we should record those too + // We will want to terminate those apps and observe their pids, but we will only do this if we don't find any regular matches + NSMutableArray *potentialMatchingTranslocatedRunningApplications = [[NSMutableArray alloc] init]; for (NSRunningApplication *runningApplication in runningApplications) { // Comparing the URLs hasn't worked well for me in practice, so I'm comparing the file paths instead NSString *candidatePath = runningApplication.bundleURL.URLByResolvingSymlinksInPath.path; - if (candidatePath != nil && [candidatePath.pathComponents isEqualToArray:bundlePathComponents]) { - [matchedRunningApplications addObject:runningApplication]; + if (candidatePath != nil) { + NSArray *candidatePathComponents = candidatePath.pathComponents; + if ([candidatePathComponents isEqualToArray:bundlePathComponents]) { + [matchedRunningApplications addObject:runningApplication]; + } else if (matchedRunningApplications.count == 0 && candidatePathComponents.count > 0 && bundlePathComponents.count > 0) { + NSString *lastBundlePathComponent = bundlePathComponents.lastObject; + NSString *lastCandidatePathComponent = candidatePathComponents.lastObject; + if (lastBundlePathComponent != nil && lastCandidatePathComponent != nil && [lastBundlePathComponent isEqualToString:lastCandidatePathComponent] && [candidatePathComponents containsObject:@"AppTranslocation"]) { + [potentialMatchingTranslocatedRunningApplications addObject:runningApplication]; + } + } } } + + // Non-translocated apps take priority first + // And we only use translocated version of apps if there are no regular apps matched + if (matchedRunningApplications.count == 0) { + [matchedRunningApplications addObjectsFromArray:potentialMatchingTranslocatedRunningApplications]; + } } return [matchedRunningApplications copy];