From 2cfb1e6fe206c63b95bc3fa3e2d8f84d5dca97d2 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sat, 26 Jul 2025 15:46:47 -0700 Subject: [PATCH] Workaround a corner case in which the bundle path of a running application contains Contents/MacOS/Executable (#2747) --- .../InstallerProgressAppController.m | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/Sparkle/InstallerProgress/InstallerProgressAppController.m b/Sparkle/InstallerProgress/InstallerProgressAppController.m index 22a7fd85..80073155 100644 --- a/Sparkle/InstallerProgress/InstallerProgressAppController.m +++ b/Sparkle/InstallerProgress/InstallerProgressAppController.m @@ -230,11 +230,40 @@ static const NSTimeInterval SUTerminationTimeDelay = 0.3; // 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]; + BOOL needsToHandleImproperBundles; + if (@available(macOS 16, *)) { + needsToHandleImproperBundles = NO; + } else { + needsToHandleImproperBundles = YES; + } + 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) { NSArray *candidatePathComponents = candidatePath.pathComponents; + + if (needsToHandleImproperBundles) { + // Workaround cases where macOS appends Contents/MacOS/ to the bundle path which can happen in corner cases (eg: mishandled bundles or helper executables). + // This may happen with app bundles which have wrapper/tramopline executables that spawn the main executable + // https://github.com/sparkle-project/Sparkle/issues/2725 + + NSUInteger candidatePathComponentsCount = candidatePathComponents.count; + // > 3 is fine instead of >= 3 because there should be another parent path component for the bundle + if (candidatePathComponentsCount > 3 && [candidatePathComponents[candidatePathComponentsCount - 3] isEqualToString:@"Contents"] && [candidatePathComponents[candidatePathComponentsCount - 2] isEqualToString:@"MacOS"]) { + + // Lastly verify if the bundle is not actually a directory + // Note the IO check is the last check for this corner case + // This ensures it's not an ordinary helper app that happens to be inside another app bundle. + BOOL candidatePathIsDir = YES; + if ([[NSFileManager defaultManager] fileExistsAtPath:candidatePath isDirectory:&candidatePathIsDir] && !candidatePathIsDir) { + NSMutableArray *trimmedBundlePath = [candidatePathComponents mutableCopy]; + [trimmedBundlePath removeObjectsInRange:NSMakeRange(candidatePathComponentsCount - 3, 3)]; + candidatePathComponents = trimmedBundlePath; + } + } + } + if ([candidatePathComponents isEqualToArray:bundlePathComponents]) { [matchedRunningApplications addObject:runningApplication]; } else if (matchedRunningApplications.count == 0 && candidatePathComponents.count > 0 && bundlePathComponents.count > 0) {