From 9818eff9a266570ebc44342437154bc133affc62 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sun, 24 Jan 2021 18:14:48 -0800 Subject: [PATCH 01/14] Add support for phased group updates to 2.x --- Sparkle.xcodeproj/project.pbxproj | 12 ++++++ Sparkle/SUAppcastDriver.m | 43 +++++++++++++++++++--- Sparkle/SUAppcastItem.h | 2 + Sparkle/SUAppcastItem.m | 24 ++++++++++++ Sparkle/SUConstants.h | 2 + Sparkle/SUConstants.m | 2 + Sparkle/SUPhasedUpdateGroupInfo.h | 23 ++++++++++++ Sparkle/SUPhasedUpdateGroupInfo.m | 43 ++++++++++++++++++++++ Sparkle/SUSystemUpdateInfo.h | 21 ----------- Sparkle/SUSystemUpdateInfo.m | 61 ------------------------------- Sparkle/SUUpdateAlert.m | 1 - Tests/Resources/testappcast.xml | 2 +- Tests/SUAppcastTest.swift | 1 + 13 files changed, 148 insertions(+), 89 deletions(-) create mode 100644 Sparkle/SUPhasedUpdateGroupInfo.h create mode 100644 Sparkle/SUPhasedUpdateGroupInfo.m delete mode 100644 Sparkle/SUSystemUpdateInfo.h delete mode 100644 Sparkle/SUSystemUpdateInfo.m diff --git a/Sparkle.xcodeproj/project.pbxproj b/Sparkle.xcodeproj/project.pbxproj index 7f4e30b5..eaf7c666 100644 --- a/Sparkle.xcodeproj/project.pbxproj +++ b/Sparkle.xcodeproj/project.pbxproj @@ -150,6 +150,10 @@ 721C24621CB75C68005440CB /* SULog.m in Sources */ = {isa = PBXBuildFile; fileRef = 55C14F05136EF6DB00649790 /* SULog.m */; }; 721CF1AA1AD7647000D9AC09 /* libxar.1.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 5D1AF5890FD7678C0065DB48 /* libxar.1.dylib */; }; 721CF1AB1AD764EB00D9AC09 /* libbz2.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 5D06E8FB0FD68D61005AE3F6 /* libbz2.dylib */; }; + 721D588D25BE59F900D23BEA /* SUPhasedUpdateGroupInfo.h in Headers */ = {isa = PBXBuildFile; fileRef = 721D588B25BE59F900D23BEA /* SUPhasedUpdateGroupInfo.h */; }; + 721D588E25BE59F900D23BEA /* SUPhasedUpdateGroupInfo.h in Headers */ = {isa = PBXBuildFile; fileRef = 721D588B25BE59F900D23BEA /* SUPhasedUpdateGroupInfo.h */; }; + 721D588F25BE59F900D23BEA /* SUPhasedUpdateGroupInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = 721D588C25BE59F900D23BEA /* SUPhasedUpdateGroupInfo.m */; }; + 721D589025BE59F900D23BEA /* SUPhasedUpdateGroupInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = 721D588C25BE59F900D23BEA /* SUPhasedUpdateGroupInfo.m */; }; 721D8A611D48413B0032E472 /* Security.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 61B5F8F609C4CEB300B25A18 /* Security.framework */; }; 721D8A721D4950B80032E472 /* Autoupdate in Copy Tools */ = {isa = PBXBuildFile; fileRef = 72B398D21D3D879300EE297F /* Autoupdate */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; }; 721D8A7B1D4963190032E472 /* SPULocalCacheDirectory.m in Sources */ = {isa = PBXBuildFile; fileRef = 721BC20D1D1CDE55002BC71E /* SPULocalCacheDirectory.m */; }; @@ -1028,6 +1032,8 @@ 721C24451CB753E6005440CB /* Updater.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Updater.app; sourceTree = BUILT_PRODUCTS_DIR; }; 721C24571CB754E8005440CB /* ConfigInstallerProgress.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = ConfigInstallerProgress.xcconfig; sourceTree = ""; }; 721C24581CB7567D005440CB /* InstallerProgress-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = "InstallerProgress-Info.plist"; path = "Sparkle/InstallerProgress/InstallerProgress-Info.plist"; sourceTree = SOURCE_ROOT; }; + 721D588B25BE59F900D23BEA /* SUPhasedUpdateGroupInfo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUPhasedUpdateGroupInfo.h; sourceTree = ""; }; + 721D588C25BE59F900D23BEA /* SUPhasedUpdateGroupInfo.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SUPhasedUpdateGroupInfo.m; sourceTree = ""; }; 721D8A831D498F1D0032E472 /* set-agent-signing.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = "set-agent-signing.sh"; sourceTree = ""; }; 721D8A881D4D272E0032E472 /* Installation.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = Installation.md; sourceTree = ""; }; 722194511D3BF987004C34FF /* SPUInstallerAgentProtocol.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SPUInstallerAgentProtocol.h; path = Sparkle/InstallerProgress/SPUInstallerAgentProtocol.h; sourceTree = SOURCE_ROOT; }; @@ -1862,6 +1868,8 @@ 61B5FB9509C4F04600B25A18 /* SUAppcast.m */, 61B5FC5309C5182000B25A18 /* SUAppcastItem.h */, 61B5FC5409C5182000B25A18 /* SUAppcastItem.m */, + 721D588B25BE59F900D23BEA /* SUPhasedUpdateGroupInfo.h */, + 721D588C25BE59F900D23BEA /* SUPhasedUpdateGroupInfo.m */, 61A225A20D1C4AC000430CCD /* SUStandardVersionComparator.h */, 61A225A30D1C4AC000430CCD /* SUStandardVersionComparator.m */, 61A2279A0D1CEE7600430CCD /* SUSystemProfiler.h */, @@ -2380,6 +2388,7 @@ 72A5D5DE1D692ACC0009E5AC /* SparkleCore.h in Headers */, 72B3DECE1E23479000457642 /* SPUInformationalUpdate.h in Headers */, 72A5D5EB1D692BD40009E5AC /* SPUDownloadData.h in Headers */, + 721D588E25BE59F900D23BEA /* SUPhasedUpdateGroupInfo.h in Headers */, 72A5D5F91D692F4C0009E5AC /* SPUStatusCompletionResults.h in Headers */, 72A5D5E71D692BB00009E5AC /* SPUUpdatePermissionRequest.h in Headers */, EA1E286F22B6661D004AA304 /* SUSignatures.h in Headers */, @@ -2464,6 +2473,7 @@ 72A1C2631CD6849C004CD282 /* SUUpdatePermissionPrompt.h in Headers */, 72A450531C69A68900D67EEA /* SUUpdatePermissionResponse.h in Headers */, 72F9EBE21D517E2F004AC8B6 /* SUUpdater.h in Headers */, + 721D588D25BE59F900D23BEA /* SUPhasedUpdateGroupInfo.h in Headers */, 72A6F98A1C94E2D6005F404C /* SUUpdaterDelegate.h in Headers */, 61A2259E0D1C495D00430CCD /* SUVersionComparisonProtocol.h in Headers */, 3772FEA913DE0B6B00F79537 /* SUVersionDisplayProtocol.h in Headers */, @@ -3503,6 +3513,7 @@ 72A5D5C01D6929F80009E5AC /* SUConstants.m in Sources */, 72A5D5C21D6929F80009E5AC /* SUFileManager.m in Sources */, 72A5D5C11D6929F80009E5AC /* SUHost.m in Sources */, + 721D589025BE59F900D23BEA /* SUPhasedUpdateGroupInfo.m in Sources */, 72A5D5B01D6929F80009E5AC /* SUInstallerConnection.m in Sources */, 72A5D5AE1D6929F80009E5AC /* SUInstallerLauncher.m in Sources */, 72B3DED01E23479000457642 /* SPUInformationalUpdate.m in Sources */, @@ -3597,6 +3608,7 @@ 61B5F8EE09C4CE3C00B25A18 /* SPUUpdater.m in Sources */, 720E217C1D0D00BF003A311C /* SPUUpdaterCycle.m in Sources */, 726E078E1CA891E9001A286B /* SPUUpdaterSettings.m in Sources */, + 721D588F25BE59F900D23BEA /* SUPhasedUpdateGroupInfo.m in Sources */, 72F9EC401D5E823F004AC8B6 /* SPUUpdaterTimer.m in Sources */, 72B09CE81CE6BCBD0052EF9E /* SPUURLDownload.m in Sources */, 7293A1AF1CEE933800B957A7 /* SPUURLRequest.m in Sources */, diff --git a/Sparkle/SUAppcastDriver.m b/Sparkle/SUAppcastDriver.m index 38949b1d..5e5c46fd 100644 --- a/Sparkle/SUAppcastDriver.m +++ b/Sparkle/SUAppcastDriver.m @@ -15,6 +15,7 @@ #import "SPUUpdaterDelegate.h" #import "SUHost.h" #import "SUConstants.h" +#import "SUPhasedUpdateGroupInfo.h" #include "AppKitPrevention.h" @@ -62,12 +63,12 @@ if (error != nil) { [self.delegate didFailToFetchAppcastWithError:error]; } else { - [self appcastDidFinishLoading:appcast includesSkippedUpdates:includesSkippedUpdates]; + [self appcastDidFinishLoading:appcast inBackground:background includesSkippedUpdates:includesSkippedUpdates]; } }]; } -- (void)appcastDidFinishLoading:(SUAppcast *)ac includesSkippedUpdates:(BOOL)includesSkippedUpdates +- (void)appcastDidFinishLoading:(SUAppcast *)ac inBackground:(BOOL)background includesSkippedUpdates:(BOOL)includesSkippedUpdates { [self.delegate didFinishLoadingAppcast:ac]; @@ -102,7 +103,7 @@ } } - if ([self itemContainsValidUpdate:item includesSkippedUpdates:includesSkippedUpdates]) { + if ([self itemContainsValidUpdate:item inBackground:background includesSkippedUpdates:includesSkippedUpdates]) { self.nonDeltaUpdateItem = nonDeltaUpdateItem; [self.delegate didFindValidUpdateWithAppcastItem:item preventsAutoupdate:[self itemPreventsAutoupdate:item]]; } else { @@ -189,9 +190,41 @@ return [[self versionComparator] compareVersion:[ui versionString] toVersion:skippedVersion] != NSOrderedDescending; } -- (BOOL)itemContainsValidUpdate:(SUAppcastItem *)ui includesSkippedUpdates:(BOOL)includesSkippedUpdates +- (BOOL)itemContainsValidUpdate:(SUAppcastItem *)ui inBackground:(BOOL)background includesSkippedUpdates:(BOOL)includesSkippedUpdates { - return ui && [[self class] hostSupportsItem:ui] && [self isItemNewer:ui] && (includesSkippedUpdates || ![self itemContainsSkippedVersion:ui]); + if (ui == nil) { + return NO; + } + + // Check that we have a newer appcast item than host + if (![self isItemNewer:ui]) { + return NO; + } + + // Check for skipped updates + if (!includesSkippedUpdates && [self itemContainsSkippedVersion:ui]) { + return NO; + } + + // Check phased group rollout for this appcast item + if (background && ![ui isCriticalUpdate]) { + NSNumber *phasedRolloutIntervalObject = [ui phasedRolloutInterval]; + if (phasedRolloutIntervalObject != nil) { + NSDate* itemReleaseDate = ui.date; + if (itemReleaseDate != nil) { + NSTimeInterval timeSinceRelease = [[NSDate date] timeIntervalSinceDate:itemReleaseDate]; + + NSTimeInterval phasedRolloutInterval = [phasedRolloutIntervalObject doubleValue]; + NSTimeInterval timeToWaitForGroup = phasedRolloutInterval * [SUPhasedUpdateGroupInfo updateGroupForHost:self.host]; + + if (timeSinceRelease < timeToWaitForGroup) { + return NO; + } + } + } + } + + return YES; } @end diff --git a/Sparkle/SUAppcastItem.h b/Sparkle/SUAppcastItem.h index c722e53d..d6ad8ce5 100644 --- a/Sparkle/SUAppcastItem.h +++ b/Sparkle/SUAppcastItem.h @@ -23,6 +23,7 @@ SU_EXPORT @interface SUAppcastItem : NSObject @property (copy, readonly) NSString *title; @property (copy, readonly) NSString *dateString; +@property (copy, readonly) NSDate *date; @property (copy, readonly) NSString *itemDescription; @property (strong, readonly) NSURL *releaseNotesURL; @property (strong, readonly) SUSignatures *signatures; @@ -35,6 +36,7 @@ SU_EXPORT @interface SUAppcastItem : NSObject @property (copy, readonly) NSString *displayVersionString; @property (copy, readonly) NSDictionary *deltaUpdates; @property (strong, readonly) NSURL *infoURL; +@property (copy, readonly) NSNumber* phasedRolloutInterval; @property (nonatomic, copy, readonly) NSString *installationType; @property (copy, readonly) NSString *minimumAutoupdateVersion; diff --git a/Sparkle/SUAppcastItem.m b/Sparkle/SUAppcastItem.m index 71c3f07f..241f2edf 100644 --- a/Sparkle/SUAppcastItem.m +++ b/Sparkle/SUAppcastItem.m @@ -50,6 +50,7 @@ static NSString *SUAppcastItemInstallationTypeKey = @"SUAppcastItemInstallationT @synthesize propertiesDictionary = _propertiesDictionary; @synthesize installationType = _installationType; @synthesize minimumAutoupdateVersion = _minimumAutoupdateVersion; +@synthesize phasedRolloutInterval = _phasedRolloutInterval; + (BOOL)supportsSecureCoding { @@ -83,6 +84,7 @@ static NSString *SUAppcastItemInstallationTypeKey = @"SUAppcastItemInstallationT _versionString = [(NSString *)[decoder decodeObjectOfClass:[NSString class] forKey:SUAppcastItemVersionStringKey] copy]; _osString = [(NSString *)[decoder decodeObjectOfClass:[NSString class] forKey:SUAppcastAttributeOsType] copy]; _propertiesDictionary = [decoder decodeObjectOfClasses:[NSSet setWithArray:@[[NSDictionary class], [NSString class], [NSDate class], [NSArray class]]] forKey:SUAppcastItemPropertiesKey]; + _phasedRolloutInterval = [decoder decodeObjectOfClass:[NSNumber class] forKey:SUAppcastAttributePhasedRolloutInterval]; } return self; @@ -151,6 +153,10 @@ static NSString *SUAppcastItemInstallationTypeKey = @"SUAppcastItemInstallationT if (self.installationType != nil) { [encoder encodeObject:self.installationType forKey:SUAppcastItemInstallationTypeKey]; } + + if (self.phasedRolloutInterval != nil) { + [encoder encodeObject:self.phasedRolloutInterval forKey:SUAppcastAttributePhasedRolloutInterval]; + } } - (BOOL)isDeltaUpdate @@ -169,6 +175,19 @@ static NSString *SUAppcastItemInstallationTypeKey = @"SUAppcastItemInstallationT return self.osString == nil || [self.osString isEqualToString:SUAppcastAttributeValueMacOS]; } +- (NSDate *)date +{ + if (self.dateString == nil) { + return nil; + } + + NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init]; + dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]; + dateFormatter.dateFormat = @"E, dd MMM yyyy HH:mm:ss Z"; + + return [dateFormatter dateFromString:self.dateString]; +} + - (BOOL)isInformationOnlyUpdate { return self.infoURL && !self.fileURL; @@ -298,6 +317,11 @@ static NSString *SUAppcastItemInstallationTypeKey = @"SUAppcastItemInstallationT } else if ([_installationType isEqualToString:SPUInstallationTypeInteractivePackage]) { SULog(SULogLevelDefault, @"warning: '%@' for %@ is deprecated. Use '%@' instead.", SPUInstallationTypeInteractivePackage, SUAppcastAttributeInstallationType, SPUInstallationTypeGuidedPackage); } + + NSString* enclosureRolloutIntervalString = [enclosure objectForKey:SUAppcastAttributePhasedRolloutInterval]; + if (enclosureRolloutIntervalString != nil) { + _phasedRolloutInterval = @(enclosureRolloutIntervalString.integerValue); + } // Find the appropriate release notes URL. NSString *releaseNotesString = [dict objectForKey:SUAppcastElementReleaseNotesLink]; diff --git a/Sparkle/SUConstants.h b/Sparkle/SUConstants.h index e765fc3b..1a5f398e 100644 --- a/Sparkle/SUConstants.h +++ b/Sparkle/SUConstants.h @@ -52,6 +52,7 @@ extern NSString *const SUAllowsAutomaticUpdatesKey; extern NSString *const SUEnableAutomaticChecksKey; extern NSString *const SUEnableSystemProfilingKey; extern NSString *const SUSendProfileInfoKey; +extern NSString *const SUUpdateGroupIdentifierKey; extern NSString *const SULastProfileSubmitDateKey; extern NSString *const SUPromptUserOnFirstLaunchKey; extern NSString *const SUDefaultsDomainKey; @@ -81,6 +82,7 @@ extern NSString *const SUAppcastElementMinimumSystemVersion; extern NSString *const SUAppcastElementMaximumSystemVersion; extern NSString *const SUAppcastElementReleaseNotesLink; extern NSString *const SUAppcastElementTags; +extern NSString *const SUAppcastAttributePhasedRolloutInterval; extern NSString *const SURSSAttributeURL; extern NSString *const SURSSAttributeLength; diff --git a/Sparkle/SUConstants.m b/Sparkle/SUConstants.m index 1a055784..23624c3f 100644 --- a/Sparkle/SUConstants.m +++ b/Sparkle/SUConstants.m @@ -47,6 +47,7 @@ NSString *const SUAllowsAutomaticUpdatesKey = @"SUAllowsAutomaticUpdates"; NSString *const SUEnableSystemProfilingKey = @"SUEnableSystemProfiling"; NSString *const SUEnableAutomaticChecksKey = @"SUEnableAutomaticChecks"; NSString *const SUSendProfileInfoKey = @"SUSendProfileInfo"; +NSString *const SUUpdateGroupIdentifierKey = @"SUUpdateGroupIdentifier"; NSString *const SULastProfileSubmitDateKey = @"SULastProfileSubmissionDate"; NSString *const SUPromptUserOnFirstLaunchKey = @"SUPromptUserOnFirstLaunch"; NSString *const SUEnableJavaScriptKey = @"SUEnableJavaScript"; @@ -74,6 +75,7 @@ NSString *const SUAppcastElementMinimumSystemVersion = @"sparkle:minimumSystemVe NSString *const SUAppcastElementMaximumSystemVersion = @"sparkle:maximumSystemVersion"; NSString *const SUAppcastElementReleaseNotesLink = @"sparkle:releaseNotesLink"; NSString *const SUAppcastElementTags = @"sparkle:tags"; +NSString *const SUAppcastAttributePhasedRolloutInterval = @"sparkle:phasedRolloutInterval"; NSString *const SURSSAttributeURL = @"url"; NSString *const SURSSAttributeLength = @"length"; diff --git a/Sparkle/SUPhasedUpdateGroupInfo.h b/Sparkle/SUPhasedUpdateGroupInfo.h new file mode 100644 index 00000000..b2bd183e --- /dev/null +++ b/Sparkle/SUPhasedUpdateGroupInfo.h @@ -0,0 +1,23 @@ +// +// SUPhasedUpdateGroupInfo.h +// Sparkle +// +// Created by Mayur Pawashe on 01/24/21. +// Copyright © 2016 Sparkle Project. All rights reserved. +// + +#import + +NS_ASSUME_NONNULL_BEGIN + +@class SUHost; + +@interface SUPhasedUpdateGroupInfo : NSObject + ++ (NSUInteger)updateGroupForHost:(SUHost*)host; ++ (NSNumber*)updateGroupIdentifierForHost:(SUHost*)host; ++ (NSNumber*)setNewUpdateGroupIdentifierForHost:(SUHost*)host; + +@end + +NS_ASSUME_NONNULL_END diff --git a/Sparkle/SUPhasedUpdateGroupInfo.m b/Sparkle/SUPhasedUpdateGroupInfo.m new file mode 100644 index 00000000..181c00cb --- /dev/null +++ b/Sparkle/SUPhasedUpdateGroupInfo.m @@ -0,0 +1,43 @@ +// +// SUPhasedUpdateGroupInfo.m +// Sparkle +// +// Created by Mayur Pawashe on 01/24/21. +// Copyright © 2016 Sparkle Project. All rights reserved. +// + +#import "SUPhasedUpdateGroupInfo.h" +#import "SUHost.h" +#import "SUConstants.h" +#import "SUFileManager.h" + + +#include "AppKitPrevention.h" + +@implementation SUPhasedUpdateGroupInfo + +#define NUM_UPDATE_GROUPS 7 ++ (NSUInteger)updateGroupForHost:(SUHost*)host { + NSNumber* updateGroupIdentifier = [self updateGroupIdentifierForHost:host]; + return ([updateGroupIdentifier unsignedIntValue] % NUM_UPDATE_GROUPS); +} + ++ (NSNumber*)updateGroupIdentifierForHost:(SUHost*)host { + NSNumber* updateGroupIdentifier = [host objectForInfoDictionaryKey:SUUpdateGroupIdentifierKey]; + if(updateGroupIdentifier == nil) { + updateGroupIdentifier = [self setNewUpdateGroupIdentifierForHost:host]; + } + + return updateGroupIdentifier; +} + ++ (NSNumber*)setNewUpdateGroupIdentifierForHost:(SUHost*)host { + unsigned int r = arc4random_uniform(UINT_MAX); + NSNumber* updateGroupIdentifier = @(r); + + [host setObject:updateGroupIdentifier forUserDefaultsKey:SUUpdateGroupIdentifierKey]; + + return updateGroupIdentifier; +} + +@end diff --git a/Sparkle/SUSystemUpdateInfo.h b/Sparkle/SUSystemUpdateInfo.h deleted file mode 100644 index 28f89978..00000000 --- a/Sparkle/SUSystemUpdateInfo.h +++ /dev/null @@ -1,21 +0,0 @@ -// -// SUSystemUpdateInfo.h -// Sparkle -// -// Created by Mayur Pawashe on 12/23/16. -// Copyright © 2016 Sparkle Project. All rights reserved. -// - -#import - -NS_ASSUME_NONNULL_BEGIN - -@class SUHost; - -@interface SUSystemUpdateInfo : NSObject - -+ (BOOL)systemAllowsAutomaticUpdatesForHost:(SUHost *)host; - -@end - -NS_ASSUME_NONNULL_END diff --git a/Sparkle/SUSystemUpdateInfo.m b/Sparkle/SUSystemUpdateInfo.m deleted file mode 100644 index 78325eae..00000000 --- a/Sparkle/SUSystemUpdateInfo.m +++ /dev/null @@ -1,61 +0,0 @@ -// -// SUSystemUpdateInfo.m -// Sparkle -// -// Created by Mayur Pawashe on 12/23/16. -// Copyright © 2016 Sparkle Project. All rights reserved. -// - -#import "SUSystemUpdateInfo.h" -#import "SUHost.h" -#import "SUConstants.h" -#import "SUFileManager.h" - - -#include "AppKitPrevention.h" - -@implementation SUSystemUpdateInfo - -+ (BOOL)systemAllowsAutomaticUpdatesForHost:(SUHost *)host -{ - // Does the developer want us to disable automatic updates? - NSNumber *developerAllowsAutomaticUpdates = [host objectForInfoDictionaryKey:SUAllowsAutomaticUpdatesKey]; - if (developerAllowsAutomaticUpdates != nil && !developerAllowsAutomaticUpdates.boolValue) { - return NO; - } - - // Can we automatically update in the background without bugging the user (e.g, with a administrator password prompt)? - // Note it's very well possible to have the bundle be writable but not be able to write into the parent directory - // And if the bundle isn't writable, but we can write into the parent directory, we will still need to authorize to replace it - NSString *bundlePath = [host bundlePath]; - if (![[NSFileManager defaultManager] isWritableFileAtPath:bundlePath.stringByDeletingLastPathComponent] || ![[NSFileManager defaultManager] isWritableFileAtPath:bundlePath]) { - return NO; - } - - // Just because we have writability access does not mean we can set the correct owner/group silently - // Test if we can set the owner/group on a temporarily created file - // If we can, then we can probably perform an update without authorization - // One place where this matters is if you copy and run an app from /tmp/ - - NSString *tempFilename = @"permission_test" ; - - SUFileManager *suFileManager = [SUFileManager defaultManager]; - NSURL *tempDirectoryURL = [suFileManager makeTemporaryDirectoryWithPreferredName:tempFilename appropriateForDirectoryURL:[NSURL fileURLWithPath:NSTemporaryDirectory()] error:NULL]; - - if (tempDirectoryURL == nil) { - // I don't imagine this ever happening but in case it does, requesting authorization may be the better option - return NO; - } - - NSURL *tempFileURL = [tempDirectoryURL URLByAppendingPathComponent:tempFilename]; - - BOOL changeOwnerAndGroupSuccess = - [[NSData data] writeToURL:tempFileURL atomically:NO] && - [suFileManager changeOwnerAndGroupOfItemAtRootURL:tempFileURL toMatchURL:host.bundle.bundleURL error:NULL]; - - [suFileManager removeItemAtURL:tempDirectoryURL error:NULL]; - - return changeOwnerAndGroupSuccess; -} - -@end diff --git a/Sparkle/SUUpdateAlert.m b/Sparkle/SUUpdateAlert.m index 9a3aaf05..1018636e 100644 --- a/Sparkle/SUUpdateAlert.m +++ b/Sparkle/SUUpdateAlert.m @@ -24,7 +24,6 @@ #import "SPUDownloadData.h" #import "SUApplicationInfo.h" #import "SPUUpdaterSettings.h" -#import "SUSystemUpdateInfo.h" #import "SUTouchBarForwardDeclarations.h" #import "SUTouchBarButtonGroup.h" #import "SPUXPCServiceInfo.h" diff --git a/Tests/Resources/testappcast.xml b/Tests/Resources/testappcast.xml index a7ed80a5..ba3ff109 100644 --- a/Tests/Resources/testappcast.xml +++ b/Tests/Resources/testappcast.xml @@ -12,7 +12,7 @@ Version 3.0 - + Date: Sun, 24 Jan 2021 18:18:01 -0800 Subject: [PATCH 02/14] Remove unused header import --- Sparkle/SUPhasedUpdateGroupInfo.m | 1 - 1 file changed, 1 deletion(-) diff --git a/Sparkle/SUPhasedUpdateGroupInfo.m b/Sparkle/SUPhasedUpdateGroupInfo.m index 181c00cb..d04f711c 100644 --- a/Sparkle/SUPhasedUpdateGroupInfo.m +++ b/Sparkle/SUPhasedUpdateGroupInfo.m @@ -9,7 +9,6 @@ #import "SUPhasedUpdateGroupInfo.h" #import "SUHost.h" #import "SUConstants.h" -#import "SUFileManager.h" #include "AppKitPrevention.h" From bc0e338cde523c1140443e362691f1c4fc3cd6d5 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sun, 24 Jan 2021 20:17:10 -0800 Subject: [PATCH 03/14] Use new update group for next downloaded update if applicable --- Sparkle/SPUCoreBasedUpdateDriver.h | 2 ++ Sparkle/SPUCoreBasedUpdateDriver.m | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/Sparkle/SPUCoreBasedUpdateDriver.h b/Sparkle/SPUCoreBasedUpdateDriver.h index 9014289d..d727d33c 100644 --- a/Sparkle/SPUCoreBasedUpdateDriver.h +++ b/Sparkle/SPUCoreBasedUpdateDriver.h @@ -35,6 +35,8 @@ NS_ASSUME_NONNULL_BEGIN - (void)downloadDriverDidReceiveDataOfLength:(uint64_t)length; +- (void)downloadDriverDidDownloadUpdate; + - (void)coreDriverDidStartExtractingUpdate; - (void)installerDidStartInstalling; diff --git a/Sparkle/SPUCoreBasedUpdateDriver.m b/Sparkle/SPUCoreBasedUpdateDriver.m index d1647ce4..66d2565c 100644 --- a/Sparkle/SPUCoreBasedUpdateDriver.m +++ b/Sparkle/SPUCoreBasedUpdateDriver.m @@ -20,6 +20,7 @@ #import "SUAppcastItem.h" #import "SULocalizations.h" #import "SPUInstallationType.h" +#import "SUPhasedUpdateGroupInfo.h" #include "AppKitPrevention.h" @@ -40,6 +41,7 @@ @property (nonatomic, readonly, weak) id updater; // if we didn't have legacy support, I'd remove this.. @property (nullable, nonatomic, readonly, weak) id updaterDelegate; @property (nonatomic) NSString *userAgent; +@property (nonatomic) BOOL downloadInBackground; @end @@ -58,6 +60,7 @@ @synthesize updaterDelegate = _updaterDelegate; @synthesize userAgent = _userAgent; @synthesize resumableUpdate = _resumableUpdate; +@synthesize downloadInBackground = _downloadInBackground; - (instancetype)initWithHost:(SUHost *)host applicationBundle:(NSBundle *)applicationBundle sparkleBundle:(NSBundle *)sparkleBundle updater:(id)updater updaterDelegate:(nullable id )updaterDelegate delegate:(id)delegate { @@ -166,6 +169,8 @@ - (void)downloadUpdateFromAppcastItem:(SUAppcastItem *)updateItem inBackground:(BOOL)background { + self.downloadInBackground = background; + self.downloadDriver = [[SPUDownloadDriver alloc] initWithUpdateItem:updateItem host:self.host userAgent:self.userAgent inBackground:background delegate:self]; if ([self.updaterDelegate respondsToSelector:@selector((updater:willDownloadUpdate:withRequest:))]) { @@ -200,6 +205,11 @@ - (void)downloadDriverDidDownloadUpdate:(SPUDownloadedUpdate *)downloadedUpdate { + // Use a new update group for our next downloaded update, if applicable + if (self.downloadInBackground && downloadedUpdate.updateItem.phasedRolloutInterval != nil) { + [SUPhasedUpdateGroupInfo setNewUpdateGroupIdentifierForHost:self.host]; + } + self.resumableUpdate = downloadedUpdate; [self extractUpdate:downloadedUpdate]; } From 0364e01c3acd020d865f30fab935eae968f49191 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sun, 24 Jan 2021 20:18:56 -0800 Subject: [PATCH 04/14] Remove method not used publicly in SUPhasedUpdateGroupInfo.h --- Sparkle/SUPhasedUpdateGroupInfo.h | 1 - 1 file changed, 1 deletion(-) diff --git a/Sparkle/SUPhasedUpdateGroupInfo.h b/Sparkle/SUPhasedUpdateGroupInfo.h index b2bd183e..6f54c7a3 100644 --- a/Sparkle/SUPhasedUpdateGroupInfo.h +++ b/Sparkle/SUPhasedUpdateGroupInfo.h @@ -15,7 +15,6 @@ NS_ASSUME_NONNULL_BEGIN @interface SUPhasedUpdateGroupInfo : NSObject + (NSUInteger)updateGroupForHost:(SUHost*)host; -+ (NSNumber*)updateGroupIdentifierForHost:(SUHost*)host; + (NSNumber*)setNewUpdateGroupIdentifierForHost:(SUHost*)host; @end From 03503d37b63dc575c2e6e0da65bcc220db476132 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sun, 24 Jan 2021 21:21:42 -0800 Subject: [PATCH 05/14] Fix SUUpdateGroupIdentifierKey not being read from user defaults --- Sparkle/SUPhasedUpdateGroupInfo.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sparkle/SUPhasedUpdateGroupInfo.m b/Sparkle/SUPhasedUpdateGroupInfo.m index d04f711c..48e84892 100644 --- a/Sparkle/SUPhasedUpdateGroupInfo.m +++ b/Sparkle/SUPhasedUpdateGroupInfo.m @@ -22,7 +22,7 @@ } + (NSNumber*)updateGroupIdentifierForHost:(SUHost*)host { - NSNumber* updateGroupIdentifier = [host objectForInfoDictionaryKey:SUUpdateGroupIdentifierKey]; + NSNumber* updateGroupIdentifier = [host objectForUserDefaultsKey:SUUpdateGroupIdentifierKey]; if(updateGroupIdentifier == nil) { updateGroupIdentifier = [self setNewUpdateGroupIdentifierForHost:host]; } From 27d7058e0e1b6b93517daa1299c1de0eb6a751de Mon Sep 17 00:00:00 2001 From: Zorg Date: Sun, 24 Jan 2021 21:22:24 -0800 Subject: [PATCH 06/14] Don't track if update was downloaded or checked in background Note the prior check was also wrong, it should have used the background flag from the app cast check, not the download driver. --- Sparkle/SPUCoreBasedUpdateDriver.m | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/Sparkle/SPUCoreBasedUpdateDriver.m b/Sparkle/SPUCoreBasedUpdateDriver.m index 66d2565c..83cb55b4 100644 --- a/Sparkle/SPUCoreBasedUpdateDriver.m +++ b/Sparkle/SPUCoreBasedUpdateDriver.m @@ -41,7 +41,6 @@ @property (nonatomic, readonly, weak) id updater; // if we didn't have legacy support, I'd remove this.. @property (nullable, nonatomic, readonly, weak) id updaterDelegate; @property (nonatomic) NSString *userAgent; -@property (nonatomic) BOOL downloadInBackground; @end @@ -60,7 +59,6 @@ @synthesize updaterDelegate = _updaterDelegate; @synthesize userAgent = _userAgent; @synthesize resumableUpdate = _resumableUpdate; -@synthesize downloadInBackground = _downloadInBackground; - (instancetype)initWithHost:(SUHost *)host applicationBundle:(NSBundle *)applicationBundle sparkleBundle:(NSBundle *)sparkleBundle updater:(id)updater updaterDelegate:(nullable id )updaterDelegate delegate:(id)delegate { @@ -169,8 +167,6 @@ - (void)downloadUpdateFromAppcastItem:(SUAppcastItem *)updateItem inBackground:(BOOL)background { - self.downloadInBackground = background; - self.downloadDriver = [[SPUDownloadDriver alloc] initWithUpdateItem:updateItem host:self.host userAgent:self.userAgent inBackground:background delegate:self]; if ([self.updaterDelegate respondsToSelector:@selector((updater:willDownloadUpdate:withRequest:))]) { @@ -205,8 +201,10 @@ - (void)downloadDriverDidDownloadUpdate:(SPUDownloadedUpdate *)downloadedUpdate { - // Use a new update group for our next downloaded update, if applicable - if (self.downloadInBackground && downloadedUpdate.updateItem.phasedRolloutInterval != nil) { + // Use a new update group for our next downloaded update + // We could restrict this to when the appcast was downloaded in the background, + // but it shouldn't matter. + if (downloadedUpdate.updateItem.phasedRolloutInterval != nil) { [SUPhasedUpdateGroupInfo setNewUpdateGroupIdentifierForHost:self.host]; } From f9d421f8512e0d0180ab0905e2316f54225cefcf Mon Sep 17 00:00:00 2001 From: Zorg Date: Sun, 24 Jan 2021 21:48:33 -0800 Subject: [PATCH 07/14] Update from previous version if newest item doesn't pass rollout --- Sparkle/SUAppcastDriver.m | 68 ++++++++++++++++++++++++--------------- 1 file changed, 42 insertions(+), 26 deletions(-) diff --git a/Sparkle/SUAppcastDriver.m b/Sparkle/SUAppcastDriver.m index 5e5c46fd..8c595baa 100644 --- a/Sparkle/SUAppcastDriver.m +++ b/Sparkle/SUAppcastDriver.m @@ -95,7 +95,7 @@ { // Find the best supported update SUAppcastItem *deltaUpdateItem = nil; - item = [[self class] bestItemFromAppcastItems:ac.items getDeltaItem:&deltaUpdateItem withHostVersion:self.host.version comparator:[self versionComparator]]; + item = [self bestItemFromAppcastItems:ac.items getDeltaItem:&deltaUpdateItem inBackground:background]; if (item && deltaUpdateItem) { nonDeltaUpdateItem = item; @@ -111,11 +111,16 @@ } } -+ (SUAppcastItem *)bestItemFromAppcastItems:(NSArray *)appcastItems getDeltaItem:(SUAppcastItem * __autoreleasing *)deltaItem withHostVersion:(NSString *)hostVersion comparator:(id)comparator +- (SUAppcastItem *)bestItemFromAppcastItems:(NSArray *)appcastItems getDeltaItem:(SUAppcastItem * __autoreleasing *)deltaItem inBackground:(BOOL)background { + // BUG: We should really filter out the hostSupportsItem: checks before reaching here and before + // giving the delegate an opportunity to decide the best appcast item. + + NSString *hostVersion = self.host.version; + id comparator = [self versionComparator]; SUAppcastItem *item = nil; for(SUAppcastItem *candidate in appcastItems) { - if ([[self class] hostSupportsItem:candidate]) { + if ([self hostSupportsItem:candidate inBackground:background]) { if (!item || [comparator compareVersion:item.versionString toVersion:candidate.versionString] == NSOrderedAscending) { item = candidate; } @@ -124,7 +129,7 @@ if (item && deltaItem) { SUAppcastItem *deltaUpdateItem = [item deltaUpdates][hostVersion]; - if (deltaUpdateItem && [[self class] hostSupportsItem:deltaUpdateItem]) { + if (deltaUpdateItem && [self hostSupportsItem:deltaUpdateItem inBackground:background]) { *deltaItem = deltaUpdateItem; } } @@ -132,7 +137,7 @@ return item; } -+ (BOOL)hostSupportsItem:(SUAppcastItem *)ui +- (BOOL)hostSupportsItem:(SUAppcastItem *)ui inBackground:(BOOL)background { BOOL osOK = [ui isMacOsUpdate]; if (([ui minimumSystemVersion] == nil || [[ui minimumSystemVersion] isEqualToString:@""]) && @@ -140,9 +145,10 @@ return osOK; } - BOOL minimumVersionOK = TRUE; - BOOL maximumVersionOK = TRUE; + BOOL minimumVersionOK = YES; + BOOL maximumVersionOK = YES; + // We don't want to use delegate's comparator for comparing OS versions id versionComparator = [[SUStandardVersionComparator alloc] init]; // Check minimum and maximum System Version @@ -153,7 +159,7 @@ maximumVersionOK = [versionComparator compareVersion:[ui maximumSystemVersion] toVersion:[SUOperatingSystem systemVersionString]] != NSOrderedAscending; } - return minimumVersionOK && maximumVersionOK && osOK; + return minimumVersionOK && maximumVersionOK && osOK && [self itemIsReadyForPhasedRollout:ui inBackground:background]; } - (id)versionComparator @@ -190,6 +196,34 @@ return [[self versionComparator] compareVersion:[ui versionString] toVersion:skippedVersion] != NSOrderedDescending; } +- (BOOL)itemIsReadyForPhasedRollout:(SUAppcastItem *)ui inBackground:(BOOL)background +{ + if (!background || [ui isCriticalUpdate]) { + return YES; + } + + NSNumber *phasedRolloutIntervalObject = [ui phasedRolloutInterval]; + if (phasedRolloutIntervalObject == nil) { + return YES; + } + + NSDate* itemReleaseDate = ui.date; + if (itemReleaseDate == nil) { + return YES; + } + + NSTimeInterval timeSinceRelease = [[NSDate date] timeIntervalSinceDate:itemReleaseDate]; + + NSTimeInterval phasedRolloutInterval = [phasedRolloutIntervalObject doubleValue]; + NSTimeInterval timeToWaitForGroup = phasedRolloutInterval * [SUPhasedUpdateGroupInfo updateGroupForHost:self.host]; + + if (timeSinceRelease >= timeToWaitForGroup) { + return YES; + } + + return NO; +} + - (BOOL)itemContainsValidUpdate:(SUAppcastItem *)ui inBackground:(BOOL)background includesSkippedUpdates:(BOOL)includesSkippedUpdates { if (ui == nil) { @@ -206,24 +240,6 @@ return NO; } - // Check phased group rollout for this appcast item - if (background && ![ui isCriticalUpdate]) { - NSNumber *phasedRolloutIntervalObject = [ui phasedRolloutInterval]; - if (phasedRolloutIntervalObject != nil) { - NSDate* itemReleaseDate = ui.date; - if (itemReleaseDate != nil) { - NSTimeInterval timeSinceRelease = [[NSDate date] timeIntervalSinceDate:itemReleaseDate]; - - NSTimeInterval phasedRolloutInterval = [phasedRolloutIntervalObject doubleValue]; - NSTimeInterval timeToWaitForGroup = phasedRolloutInterval * [SUPhasedUpdateGroupInfo updateGroupForHost:self.host]; - - if (timeSinceRelease < timeToWaitForGroup) { - return NO; - } - } - } - } - return YES; } From 60fcc89b7e16f7d71439a9ea72dc7d813298eb6c Mon Sep 17 00:00:00 2001 From: Zorg Date: Sun, 24 Jan 2021 23:21:18 -0800 Subject: [PATCH 08/14] Make phased rollout interval an app cast element not an enclosure one Previous behavior doesn't work properly with delta update items. They should not be treated separately. --- Sparkle/SUAppcastItem.m | 14 +++++++------- Sparkle/SUConstants.h | 2 +- Sparkle/SUConstants.m | 2 +- Tests/Resources/testappcast.xml | 3 ++- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/Sparkle/SUAppcastItem.m b/Sparkle/SUAppcastItem.m index 241f2edf..d58efc76 100644 --- a/Sparkle/SUAppcastItem.m +++ b/Sparkle/SUAppcastItem.m @@ -84,7 +84,7 @@ static NSString *SUAppcastItemInstallationTypeKey = @"SUAppcastItemInstallationT _versionString = [(NSString *)[decoder decodeObjectOfClass:[NSString class] forKey:SUAppcastItemVersionStringKey] copy]; _osString = [(NSString *)[decoder decodeObjectOfClass:[NSString class] forKey:SUAppcastAttributeOsType] copy]; _propertiesDictionary = [decoder decodeObjectOfClasses:[NSSet setWithArray:@[[NSDictionary class], [NSString class], [NSDate class], [NSArray class]]] forKey:SUAppcastItemPropertiesKey]; - _phasedRolloutInterval = [decoder decodeObjectOfClass:[NSNumber class] forKey:SUAppcastAttributePhasedRolloutInterval]; + _phasedRolloutInterval = [decoder decodeObjectOfClass:[NSNumber class] forKey:SUAppcastElementPhasedRolloutInterval]; } return self; @@ -155,7 +155,7 @@ static NSString *SUAppcastItemInstallationTypeKey = @"SUAppcastItemInstallationT } if (self.phasedRolloutInterval != nil) { - [encoder encodeObject:self.phasedRolloutInterval forKey:SUAppcastAttributePhasedRolloutInterval]; + [encoder encodeObject:self.phasedRolloutInterval forKey:SUAppcastElementPhasedRolloutInterval]; } } @@ -294,6 +294,11 @@ static NSString *SUAppcastItemInstallationTypeKey = @"SUAppcastItemInstallationT _minimumSystemVersion = [(NSString *)[dict objectForKey:SUAppcastElementMinimumSystemVersion] copy]; _maximumSystemVersion = [(NSString *)[dict objectForKey:SUAppcastElementMaximumSystemVersion] copy]; _minimumAutoupdateVersion = [(NSString *)[dict objectForKey:SUAppcastElementMinimumAutoupdateVersion] copy]; + + NSString* rolloutIntervalString = [(NSString *)[dict objectForKey:SUAppcastElementPhasedRolloutInterval] copy]; + if (rolloutIntervalString != nil) { + _phasedRolloutInterval = @(rolloutIntervalString.integerValue); + } NSString *shortVersionString = [enclosure objectForKey:SUAppcastAttributeShortVersionString]; if (nil == shortVersionString) { @@ -317,11 +322,6 @@ static NSString *SUAppcastItemInstallationTypeKey = @"SUAppcastItemInstallationT } else if ([_installationType isEqualToString:SPUInstallationTypeInteractivePackage]) { SULog(SULogLevelDefault, @"warning: '%@' for %@ is deprecated. Use '%@' instead.", SPUInstallationTypeInteractivePackage, SUAppcastAttributeInstallationType, SPUInstallationTypeGuidedPackage); } - - NSString* enclosureRolloutIntervalString = [enclosure objectForKey:SUAppcastAttributePhasedRolloutInterval]; - if (enclosureRolloutIntervalString != nil) { - _phasedRolloutInterval = @(enclosureRolloutIntervalString.integerValue); - } // Find the appropriate release notes URL. NSString *releaseNotesString = [dict objectForKey:SUAppcastElementReleaseNotesLink]; diff --git a/Sparkle/SUConstants.h b/Sparkle/SUConstants.h index 1a5f398e..7f5b8246 100644 --- a/Sparkle/SUConstants.h +++ b/Sparkle/SUConstants.h @@ -82,7 +82,7 @@ extern NSString *const SUAppcastElementMinimumSystemVersion; extern NSString *const SUAppcastElementMaximumSystemVersion; extern NSString *const SUAppcastElementReleaseNotesLink; extern NSString *const SUAppcastElementTags; -extern NSString *const SUAppcastAttributePhasedRolloutInterval; +extern NSString *const SUAppcastElementPhasedRolloutInterval; extern NSString *const SURSSAttributeURL; extern NSString *const SURSSAttributeLength; diff --git a/Sparkle/SUConstants.m b/Sparkle/SUConstants.m index 23624c3f..990244f1 100644 --- a/Sparkle/SUConstants.m +++ b/Sparkle/SUConstants.m @@ -75,7 +75,7 @@ NSString *const SUAppcastElementMinimumSystemVersion = @"sparkle:minimumSystemVe NSString *const SUAppcastElementMaximumSystemVersion = @"sparkle:maximumSystemVersion"; NSString *const SUAppcastElementReleaseNotesLink = @"sparkle:releaseNotesLink"; NSString *const SUAppcastElementTags = @"sparkle:tags"; -NSString *const SUAppcastAttributePhasedRolloutInterval = @"sparkle:phasedRolloutInterval"; +NSString *const SUAppcastElementPhasedRolloutInterval = @"sparkle:phasedRolloutInterval"; NSString *const SURSSAttributeURL = @"url"; NSString *const SURSSAttributeLength = @"length"; diff --git a/Tests/Resources/testappcast.xml b/Tests/Resources/testappcast.xml index ba3ff109..d602ba7b 100644 --- a/Tests/Resources/testappcast.xml +++ b/Tests/Resources/testappcast.xml @@ -12,7 +12,8 @@ Version 3.0 - + 86400 + Date: Sun, 24 Jan 2021 23:39:33 -0800 Subject: [PATCH 09/14] Fix hostSupportsItem: returning YES too early --- Sparkle/SUAppcastDriver.m | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Sparkle/SUAppcastDriver.m b/Sparkle/SUAppcastDriver.m index 8c595baa..2e15b3aa 100644 --- a/Sparkle/SUAppcastDriver.m +++ b/Sparkle/SUAppcastDriver.m @@ -138,6 +138,11 @@ } - (BOOL)hostSupportsItem:(SUAppcastItem *)ui inBackground:(BOOL)background +{ + return [self itemOperatingSystemIsOK:ui] && [self itemIsReadyForPhasedRollout:ui inBackground:background]; +} + +- (BOOL)itemOperatingSystemIsOK:(SUAppcastItem *)ui { BOOL osOK = [ui isMacOsUpdate]; if (([ui minimumSystemVersion] == nil || [[ui minimumSystemVersion] isEqualToString:@""]) && @@ -159,7 +164,7 @@ maximumVersionOK = [versionComparator compareVersion:[ui maximumSystemVersion] toVersion:[SUOperatingSystem systemVersionString]] != NSOrderedAscending; } - return minimumVersionOK && maximumVersionOK && osOK && [self itemIsReadyForPhasedRollout:ui inBackground:background]; + return minimumVersionOK && maximumVersionOK && osOK; } - (id)versionComparator From 98dc6314a5df3574d50ec5629ca1dbcf705a1a7f Mon Sep 17 00:00:00 2001 From: Zorg Date: Sun, 24 Jan 2021 23:44:24 -0800 Subject: [PATCH 10/14] Remove unused delegate method addition --- Sparkle/SPUCoreBasedUpdateDriver.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/Sparkle/SPUCoreBasedUpdateDriver.h b/Sparkle/SPUCoreBasedUpdateDriver.h index d727d33c..9014289d 100644 --- a/Sparkle/SPUCoreBasedUpdateDriver.h +++ b/Sparkle/SPUCoreBasedUpdateDriver.h @@ -35,8 +35,6 @@ NS_ASSUME_NONNULL_BEGIN - (void)downloadDriverDidReceiveDataOfLength:(uint64_t)length; -- (void)downloadDriverDidDownloadUpdate; - - (void)coreDriverDidStartExtractingUpdate; - (void)installerDidStartInstalling; From e089b9b43567c8837b4299f482c86f3610f4da09 Mon Sep 17 00:00:00 2001 From: Zorg Date: Fri, 29 Jan 2021 21:45:58 -0800 Subject: [PATCH 11/14] Filter supported app cast items before finding the best item This fixes scenarios where delegates may not take min/max OS version or phased update groups in account (and they shouldn't). --- Sparkle/SUAppcast.h | 1 + Sparkle/SUAppcast.m | 21 +++++++--- Sparkle/SUAppcastDriver.m | 49 ++++++++++++---------- Tests/SUAppcastTest.swift | 10 +++-- Tests/Sparkle Unit Tests-Bridging-Header.h | 2 + 5 files changed, 52 insertions(+), 31 deletions(-) diff --git a/Sparkle/SUAppcast.h b/Sparkle/SUAppcast.h index cc9fd386..5ae0f0fa 100644 --- a/Sparkle/SUAppcast.h +++ b/Sparkle/SUAppcast.h @@ -29,6 +29,7 @@ SU_EXPORT @interface SUAppcast : NSObject - (void)fetchAppcastFromURL:(NSURL *)url inBackground:(BOOL)bg completionBlock:(void (^)(NSError *_Nullable))err; - (SUAppcast *)copyWithoutDeltaUpdates; +- (SUAppcast *)copyByFilteringItems:(BOOL (^)(SUAppcastItem *))filterBlock; @property (readonly, copy, nullable) NSArray *items; @end diff --git a/Sparkle/SUAppcast.m b/Sparkle/SUAppcast.m index 8f1fb8f4..640960e8 100644 --- a/Sparkle/SUAppcast.m +++ b/Sparkle/SUAppcast.m @@ -307,14 +307,23 @@ } - (SUAppcast *)copyWithoutDeltaUpdates { + return [self copyByFilteringItems:^(SUAppcastItem *item) { + return (BOOL)![item isDeltaUpdate]; + }]; +} + +- (SUAppcast *)copyByFilteringItems:(BOOL (^)(SUAppcastItem *))filterBlock +{ SUAppcast *other = [SUAppcast new]; - NSMutableArray *nonDeltaItems = [NSMutableArray new]; - - for(SUAppcastItem *item in self.items) { - if (![item isDeltaUpdate]) [nonDeltaItems addObject:item]; + NSMutableArray *newItems = [NSMutableArray new]; + + for (SUAppcastItem *item in self.items) { + if (filterBlock(item)) { + [newItems addObject:item]; + } } - - other.items = nonDeltaItems; + + other.items = newItems; return other; } diff --git a/Sparkle/SUAppcastDriver.m b/Sparkle/SUAppcastDriver.m index 2e15b3aa..78e22554 100644 --- a/Sparkle/SUAppcastDriver.m +++ b/Sparkle/SUAppcastDriver.m @@ -68,34 +68,36 @@ }]; } -- (void)appcastDidFinishLoading:(SUAppcast *)ac inBackground:(BOOL)background includesSkippedUpdates:(BOOL)includesSkippedUpdates +- (void)appcastDidFinishLoading:(SUAppcast *)loadedAppcast inBackground:(BOOL)background includesSkippedUpdates:(BOOL)includesSkippedUpdates { - [self.delegate didFinishLoadingAppcast:ac]; + [self.delegate didFinishLoadingAppcast:loadedAppcast]; - NSDictionary *userInfo = @{ SUUpdaterAppcastNotificationKey: ac }; + NSDictionary *userInfo = @{ SUUpdaterAppcastNotificationKey: loadedAppcast }; [[NSNotificationCenter defaultCenter] postNotificationName:SUUpdaterDidFinishLoadingAppCastNotification object:self.updater userInfo:userInfo]; + SUAppcast *supportedAppcast = [[self class] filterSupportedAppcast:loadedAppcast phasedUpdateGroup:[SUPhasedUpdateGroupInfo updateGroupForHost:self.host] inBackground:background]; + SUAppcastItem *item = nil; SUAppcastItem *nonDeltaUpdateItem = nil; // Now we have to find the best valid update in the appcast. if ([self.updaterDelegate respondsToSelector:@selector((bestValidUpdateInAppcast:forUpdater:))]) { - item = [self.updaterDelegate bestValidUpdateInAppcast:ac forUpdater:(id _Nonnull)self.updater]; + item = [self.updaterDelegate bestValidUpdateInAppcast:supportedAppcast forUpdater:(id _Nonnull)self.updater]; } if (item != nil) { // Does the delegate want to handle it? if ([item isDeltaUpdate]) { - nonDeltaUpdateItem = [self.updaterDelegate bestValidUpdateInAppcast:[ac copyWithoutDeltaUpdates] forUpdater:(id _Nonnull)self.updater]; + nonDeltaUpdateItem = [self.updaterDelegate bestValidUpdateInAppcast:[supportedAppcast copyWithoutDeltaUpdates] forUpdater:(id _Nonnull)self.updater]; } } else // If not, we'll take care of it ourselves. { // Find the best supported update SUAppcastItem *deltaUpdateItem = nil; - item = [self bestItemFromAppcastItems:ac.items getDeltaItem:&deltaUpdateItem inBackground:background]; + item = [[self class] bestItemFromAppcastItems:supportedAppcast.items getDeltaItem:&deltaUpdateItem withHostVersion:self.host.version comparator:[self versionComparator]]; if (item && deltaUpdateItem) { nonDeltaUpdateItem = item; @@ -111,25 +113,28 @@ } } -- (SUAppcastItem *)bestItemFromAppcastItems:(NSArray *)appcastItems getDeltaItem:(SUAppcastItem * __autoreleasing *)deltaItem inBackground:(BOOL)background +// This method is used by unit tests ++ (SUAppcast *)filterSupportedAppcast:(SUAppcast *)appcast phasedUpdateGroup:(NSUInteger)phasedUpdateGroup inBackground:(BOOL)background +{ + return [appcast copyByFilteringItems:^(SUAppcastItem *item) { + return (BOOL)[[self class] hostSupportsItem:item phasedUpdateGroup:phasedUpdateGroup inBackground:background]; + }]; +} + +// This method is used by unit tests +// This method should not do *any* filtering, only version comparing ++ (SUAppcastItem *)bestItemFromAppcastItems:(NSArray *)appcastItems getDeltaItem:(SUAppcastItem * __autoreleasing *)deltaItem withHostVersion:(NSString *)hostVersion comparator:(id)comparator { - // BUG: We should really filter out the hostSupportsItem: checks before reaching here and before - // giving the delegate an opportunity to decide the best appcast item. - - NSString *hostVersion = self.host.version; - id comparator = [self versionComparator]; SUAppcastItem *item = nil; for(SUAppcastItem *candidate in appcastItems) { - if ([self hostSupportsItem:candidate inBackground:background]) { - if (!item || [comparator compareVersion:item.versionString toVersion:candidate.versionString] == NSOrderedAscending) { - item = candidate; - } + if (!item || [comparator compareVersion:item.versionString toVersion:candidate.versionString] == NSOrderedAscending) { + item = candidate; } } if (item && deltaItem) { SUAppcastItem *deltaUpdateItem = [item deltaUpdates][hostVersion]; - if (deltaUpdateItem && [self hostSupportsItem:deltaUpdateItem inBackground:background]) { + if (deltaUpdateItem) { *deltaItem = deltaUpdateItem; } } @@ -137,12 +142,12 @@ return item; } -- (BOOL)hostSupportsItem:(SUAppcastItem *)ui inBackground:(BOOL)background ++ (BOOL)hostSupportsItem:(SUAppcastItem *)ui phasedUpdateGroup:(NSUInteger)phasedUpdateGroup inBackground:(BOOL)background { - return [self itemOperatingSystemIsOK:ui] && [self itemIsReadyForPhasedRollout:ui inBackground:background]; + return [[self class] itemOperatingSystemIsOK:ui] && [[self class] itemIsReadyForPhasedRollout:ui phasedUpdateGroup:phasedUpdateGroup inBackground:background]; } -- (BOOL)itemOperatingSystemIsOK:(SUAppcastItem *)ui ++ (BOOL)itemOperatingSystemIsOK:(SUAppcastItem *)ui { BOOL osOK = [ui isMacOsUpdate]; if (([ui minimumSystemVersion] == nil || [[ui minimumSystemVersion] isEqualToString:@""]) && @@ -201,7 +206,7 @@ return [[self versionComparator] compareVersion:[ui versionString] toVersion:skippedVersion] != NSOrderedDescending; } -- (BOOL)itemIsReadyForPhasedRollout:(SUAppcastItem *)ui inBackground:(BOOL)background ++ (BOOL)itemIsReadyForPhasedRollout:(SUAppcastItem *)ui phasedUpdateGroup:(NSUInteger)phasedUpdateGroup inBackground:(BOOL)background { if (!background || [ui isCriticalUpdate]) { return YES; @@ -220,7 +225,7 @@ NSTimeInterval timeSinceRelease = [[NSDate date] timeIntervalSinceDate:itemReleaseDate]; NSTimeInterval phasedRolloutInterval = [phasedRolloutIntervalObject doubleValue]; - NSTimeInterval timeToWaitForGroup = phasedRolloutInterval * [SUPhasedUpdateGroupInfo updateGroupForHost:self.host]; + NSTimeInterval timeToWaitForGroup = phasedRolloutInterval * phasedUpdateGroup; if (timeSinceRelease >= timeToWaitForGroup) { return YES; diff --git a/Tests/SUAppcastTest.swift b/Tests/SUAppcastTest.swift index 9ee4073f..5c2adac9 100644 --- a/Tests/SUAppcastTest.swift +++ b/Tests/SUAppcastTest.swift @@ -44,21 +44,25 @@ class SUAppcastTest: XCTestCase { XCTAssertFalse(items[3].isCriticalUpdate) // Test best appcast item & a delta update item + let supportedAppcast = SUAppcastDriver.filterSupportedAppcast(appcast, phasedUpdateGroup: 0, inBackground: false) + + let supportedAppcastItems = supportedAppcast.items! + var deltaItem: SUAppcastItem? - let bestAppcastItem = SUAppcastDriver.bestItem(fromAppcastItems: items, getDeltaItem: &deltaItem, withHostVersion: "1.0", comparator: SUStandardVersionComparator()) + let bestAppcastItem = SUAppcastDriver.bestItem(fromAppcastItems: supportedAppcastItems, getDeltaItem: &deltaItem, withHostVersion: "1.0", comparator: SUStandardVersionComparator()) XCTAssertEqual(bestAppcastItem, items[1]) XCTAssertEqual(deltaItem!.fileURL.lastPathComponent, "3.0_from_1.0.patch") // Test latest delta update item available var latestDeltaItem: SUAppcastItem? - SUAppcastDriver.bestItem(fromAppcastItems: items, getDeltaItem: &latestDeltaItem, withHostVersion: "2.0", comparator: SUStandardVersionComparator()) + SUAppcastDriver.bestItem(fromAppcastItems: supportedAppcastItems, getDeltaItem: &latestDeltaItem, withHostVersion: "2.0", comparator: SUStandardVersionComparator()) XCTAssertEqual(latestDeltaItem!.fileURL.lastPathComponent, "3.0_from_2.0.patch") // Test a delta item that does not exist var nonexistantDeltaItem: SUAppcastItem? - SUAppcastDriver.bestItem(fromAppcastItems: items, getDeltaItem: &nonexistantDeltaItem, withHostVersion: "2.1", comparator: SUStandardVersionComparator()) + SUAppcastDriver.bestItem(fromAppcastItems: supportedAppcastItems, getDeltaItem: &nonexistantDeltaItem, withHostVersion: "2.1", comparator: SUStandardVersionComparator()) XCTAssertNil(nonexistantDeltaItem) } catch let err as NSError { diff --git a/Tests/Sparkle Unit Tests-Bridging-Header.h b/Tests/Sparkle Unit Tests-Bridging-Header.h index 9ef42341..eff5e53a 100644 --- a/Tests/Sparkle Unit Tests-Bridging-Header.h +++ b/Tests/Sparkle Unit Tests-Bridging-Header.h @@ -36,6 +36,8 @@ static const char *SUAppleQuarantineIdentifier = "com.apple.quarantine"; + (SUAppcastItem *)bestItemFromAppcastItems:(NSArray *)appcastItems getDeltaItem:(SUAppcastItem *_Nullable __autoreleasing *_Nullable)deltaItem withHostVersion:(NSString *)hostVersion comparator:(id)comparator; ++ (SUAppcast *)filterSupportedAppcast:(SUAppcast *)appcast phasedUpdateGroup:(NSUInteger)phasedUpdateGroup inBackground:(BOOL)background; + @end From 044313db594e8f3b4648aef5a890dd94bc5bf18e Mon Sep 17 00:00:00 2001 From: Zorg Date: Fri, 29 Jan 2021 22:15:50 -0800 Subject: [PATCH 12/14] Remove redundant method --- Sparkle/SUAppcastDriver.m | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/Sparkle/SUAppcastDriver.m b/Sparkle/SUAppcastDriver.m index 78e22554..5f1b0e31 100644 --- a/Sparkle/SUAppcastDriver.m +++ b/Sparkle/SUAppcastDriver.m @@ -117,7 +117,7 @@ + (SUAppcast *)filterSupportedAppcast:(SUAppcast *)appcast phasedUpdateGroup:(NSUInteger)phasedUpdateGroup inBackground:(BOOL)background { return [appcast copyByFilteringItems:^(SUAppcastItem *item) { - return (BOOL)[[self class] hostSupportsItem:item phasedUpdateGroup:phasedUpdateGroup inBackground:background]; + return (BOOL)([[self class] itemOperatingSystemIsOK:item] && [[self class] itemIsReadyForPhasedRollout:item phasedUpdateGroup:phasedUpdateGroup inBackground:background]); }]; } @@ -142,11 +142,6 @@ return item; } -+ (BOOL)hostSupportsItem:(SUAppcastItem *)ui phasedUpdateGroup:(NSUInteger)phasedUpdateGroup inBackground:(BOOL)background -{ - return [[self class] itemOperatingSystemIsOK:ui] && [[self class] itemIsReadyForPhasedRollout:ui phasedUpdateGroup:phasedUpdateGroup inBackground:background]; -} - + (BOOL)itemOperatingSystemIsOK:(SUAppcastItem *)ui { BOOL osOK = [ui isMacOsUpdate]; From 6c846d0fade9687c7600d0b8e847ea785d923b42 Mon Sep 17 00:00:00 2001 From: Zorg Date: Fri, 29 Jan 2021 22:21:26 -0800 Subject: [PATCH 13/14] Combine phasedUpdateGroup / background flags --- Sparkle/SUAppcastDriver.m | 13 +++++++------ Tests/SUAppcastTest.swift | 2 +- Tests/Sparkle Unit Tests-Bridging-Header.h | 2 +- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/Sparkle/SUAppcastDriver.m b/Sparkle/SUAppcastDriver.m index 5f1b0e31..6770f8a5 100644 --- a/Sparkle/SUAppcastDriver.m +++ b/Sparkle/SUAppcastDriver.m @@ -75,7 +75,8 @@ NSDictionary *userInfo = @{ SUUpdaterAppcastNotificationKey: loadedAppcast }; [[NSNotificationCenter defaultCenter] postNotificationName:SUUpdaterDidFinishLoadingAppCastNotification object:self.updater userInfo:userInfo]; - SUAppcast *supportedAppcast = [[self class] filterSupportedAppcast:loadedAppcast phasedUpdateGroup:[SUPhasedUpdateGroupInfo updateGroupForHost:self.host] inBackground:background]; + NSNumber *phasedUpdateGroup = background ? @([SUPhasedUpdateGroupInfo updateGroupForHost:self.host]) : nil; + SUAppcast *supportedAppcast = [[self class] filterSupportedAppcast:loadedAppcast phasedUpdateGroup:phasedUpdateGroup]; SUAppcastItem *item = nil; SUAppcastItem *nonDeltaUpdateItem = nil; @@ -114,10 +115,10 @@ } // This method is used by unit tests -+ (SUAppcast *)filterSupportedAppcast:(SUAppcast *)appcast phasedUpdateGroup:(NSUInteger)phasedUpdateGroup inBackground:(BOOL)background ++ (SUAppcast *)filterSupportedAppcast:(SUAppcast *)appcast phasedUpdateGroup:(NSNumber * _Nullable)phasedUpdateGroup { return [appcast copyByFilteringItems:^(SUAppcastItem *item) { - return (BOOL)([[self class] itemOperatingSystemIsOK:item] && [[self class] itemIsReadyForPhasedRollout:item phasedUpdateGroup:phasedUpdateGroup inBackground:background]); + return (BOOL)([[self class] itemOperatingSystemIsOK:item] && [[self class] itemIsReadyForPhasedRollout:item phasedUpdateGroup:phasedUpdateGroup]); }]; } @@ -201,9 +202,9 @@ return [[self versionComparator] compareVersion:[ui versionString] toVersion:skippedVersion] != NSOrderedDescending; } -+ (BOOL)itemIsReadyForPhasedRollout:(SUAppcastItem *)ui phasedUpdateGroup:(NSUInteger)phasedUpdateGroup inBackground:(BOOL)background ++ (BOOL)itemIsReadyForPhasedRollout:(SUAppcastItem *)ui phasedUpdateGroup:(NSNumber * _Nullable)phasedUpdateGroup { - if (!background || [ui isCriticalUpdate]) { + if (phasedUpdateGroup == nil || [ui isCriticalUpdate]) { return YES; } @@ -220,7 +221,7 @@ NSTimeInterval timeSinceRelease = [[NSDate date] timeIntervalSinceDate:itemReleaseDate]; NSTimeInterval phasedRolloutInterval = [phasedRolloutIntervalObject doubleValue]; - NSTimeInterval timeToWaitForGroup = phasedRolloutInterval * phasedUpdateGroup; + NSTimeInterval timeToWaitForGroup = phasedRolloutInterval * phasedUpdateGroup.unsignedIntegerValue; if (timeSinceRelease >= timeToWaitForGroup) { return YES; diff --git a/Tests/SUAppcastTest.swift b/Tests/SUAppcastTest.swift index 5c2adac9..9078af35 100644 --- a/Tests/SUAppcastTest.swift +++ b/Tests/SUAppcastTest.swift @@ -44,7 +44,7 @@ class SUAppcastTest: XCTestCase { XCTAssertFalse(items[3].isCriticalUpdate) // Test best appcast item & a delta update item - let supportedAppcast = SUAppcastDriver.filterSupportedAppcast(appcast, phasedUpdateGroup: 0, inBackground: false) + let supportedAppcast = SUAppcastDriver.filterSupportedAppcast(appcast, phasedUpdateGroup: nil) let supportedAppcastItems = supportedAppcast.items! diff --git a/Tests/Sparkle Unit Tests-Bridging-Header.h b/Tests/Sparkle Unit Tests-Bridging-Header.h index eff5e53a..3d588558 100644 --- a/Tests/Sparkle Unit Tests-Bridging-Header.h +++ b/Tests/Sparkle Unit Tests-Bridging-Header.h @@ -36,7 +36,7 @@ static const char *SUAppleQuarantineIdentifier = "com.apple.quarantine"; + (SUAppcastItem *)bestItemFromAppcastItems:(NSArray *)appcastItems getDeltaItem:(SUAppcastItem *_Nullable __autoreleasing *_Nullable)deltaItem withHostVersion:(NSString *)hostVersion comparator:(id)comparator; -+ (SUAppcast *)filterSupportedAppcast:(SUAppcast *)appcast phasedUpdateGroup:(NSUInteger)phasedUpdateGroup inBackground:(BOOL)background; ++ (SUAppcast *)filterSupportedAppcast:(SUAppcast *)appcast phasedUpdateGroup:(NSNumber * _Nullable)phasedUpdateGroup; @end From e8ebf2350486490e6c826f9157af0cccda0d9716 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sat, 30 Jan 2021 18:09:39 -0800 Subject: [PATCH 14/14] Compute current date only once for all app cast items --- Sparkle/SUAppcastDriver.m | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Sparkle/SUAppcastDriver.m b/Sparkle/SUAppcastDriver.m index 6770f8a5..a11c9df3 100644 --- a/Sparkle/SUAppcastDriver.m +++ b/Sparkle/SUAppcastDriver.m @@ -117,8 +117,10 @@ // This method is used by unit tests + (SUAppcast *)filterSupportedAppcast:(SUAppcast *)appcast phasedUpdateGroup:(NSNumber * _Nullable)phasedUpdateGroup { + NSDate *currentDate = [NSDate date]; + return [appcast copyByFilteringItems:^(SUAppcastItem *item) { - return (BOOL)([[self class] itemOperatingSystemIsOK:item] && [[self class] itemIsReadyForPhasedRollout:item phasedUpdateGroup:phasedUpdateGroup]); + return (BOOL)([[self class] itemOperatingSystemIsOK:item] && [[self class] itemIsReadyForPhasedRollout:item phasedUpdateGroup:phasedUpdateGroup currentDate:currentDate]); }]; } @@ -202,7 +204,7 @@ return [[self versionComparator] compareVersion:[ui versionString] toVersion:skippedVersion] != NSOrderedDescending; } -+ (BOOL)itemIsReadyForPhasedRollout:(SUAppcastItem *)ui phasedUpdateGroup:(NSNumber * _Nullable)phasedUpdateGroup ++ (BOOL)itemIsReadyForPhasedRollout:(SUAppcastItem *)ui phasedUpdateGroup:(NSNumber * _Nullable)phasedUpdateGroup currentDate:(NSDate *)currentDate { if (phasedUpdateGroup == nil || [ui isCriticalUpdate]) { return YES; @@ -218,7 +220,7 @@ return YES; } - NSTimeInterval timeSinceRelease = [[NSDate date] timeIntervalSinceDate:itemReleaseDate]; + NSTimeInterval timeSinceRelease = [currentDate timeIntervalSinceDate:itemReleaseDate]; NSTimeInterval phasedRolloutInterval = [phasedRolloutIntervalObject doubleValue]; NSTimeInterval timeToWaitForGroup = phasedRolloutInterval * phasedUpdateGroup.unsignedIntegerValue;