Fix bestValidUpdateInAppcast: not handling delta updates correctly

All the top level appcast items are non-delta items. The client needs to give us back a non-delta update item. We then fetch the appropriate delta item ourselves.
This commit is contained in:
Zorg committed 2021-04-24 21:42:33 -07:00
1 parent 6d0d1a7aa8
commit e51eb32403
7 files changed
+64 -48

No files matched your search

+1 -1
View File
@@ -38,7 +38,7 @@ NS_ASSUME_NONNULL_BEGIN
- (void)resumeUpdate:(id<SPUResumableUpdate>)resumableUpdate completion:(SPUUpdateDriverCompletion)completionBlock;
@property (nullable, nonatomic, readonly) SUAppcastItem *nonDeltaUpdateItem;
@property (nullable, nonatomic, readonly) SUAppcastItem *secondaryUpdateItem;
- (void)abortUpdateAndShowNextUpdateImmediately:(BOOL)shouldSignalShowingUpdate resumableUpdate:(id<SPUResumableUpdate> _Nullable)resumableUpdate error:(nullable NSError *)error;
+2 -2
View File
@@ -112,9 +112,9 @@
[self notifyResumableUpdateItem:resumableUpdate.updateItem systemDomain:nil];
}
- (SUAppcastItem *)nonDeltaUpdateItem
- (SUAppcastItem *)secondaryUpdateItem
{
return self.appcastDriver.nonDeltaUpdateItem;
return self.appcastDriver.secondaryUpdateItem;
}
- (void)didFailToFetchAppcastWithError:(NSError *)error
+4 -4
View File
@@ -381,16 +381,16 @@
- (void)installerDidFailToApplyDeltaUpdate
{
SUAppcastItem *nonDeltaUpdateItem = self.basicDriver.nonDeltaUpdateItem;
assert(nonDeltaUpdateItem != nil);
SUAppcastItem *secondaryUpdateItem = self.basicDriver.secondaryUpdateItem;
assert(secondaryUpdateItem != nil);
BOOL backgroundDownload = self.downloadDriver.inBackground;
[self clearDownloadedUpdate];
// Fall back to the non-delta update. Note that we don't want to trigger another update was found event.
self.updateItem = nonDeltaUpdateItem;
[self downloadUpdateFromAppcastItem:nonDeltaUpdateItem inBackground:backgroundDownload];
self.updateItem = secondaryUpdateItem;
[self downloadUpdateFromAppcastItem:secondaryUpdateItem inBackground:backgroundDownload];
}
- (void)abortUpdateAndShowNextUpdateImmediately:(BOOL)shouldShowUpdateImmediately error:(nullable NSError *)error
+1 -2
View File
@@ -28,10 +28,9 @@ SU_EXPORT @interface SUAppcast : NSObject<NSURLDownloadDelegate>
@property (copy, nullable) NSDictionary<NSString *, NSString *> *httpHeaders;
- (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;
@property (readonly, copy, nullable) NSArray<SUAppcastItem *> *items;
@end
NS_ASSUME_NONNULL_END
+1 -7
View File
@@ -47,7 +47,7 @@
@interface SUAppcast () <NSURLDownloadDelegate>
@property (strong) void (^completionBlock)(NSError *);
@property (copy) NSArray *items;
@property (copy) NSArray<SUAppcastItem *> *items;
- (void)reportError:(NSError *)error;
- (NSXMLNode *)bestNodeInNodes:(NSArray *)nodes;
@end
@@ -306,12 +306,6 @@
return [nodes objectAtIndex:i];
}
- (SUAppcast *)copyWithoutDeltaUpdates {
return [self copyByFilteringItems:^(SUAppcastItem *item) {
return (BOOL)![item isDeltaUpdate];
}];
}
- (SUAppcast *)copyByFilteringItems:(BOOL (^)(SUAppcastItem *))filterBlock
{
SUAppcast *other = [SUAppcast new];
+1 -1
View File
@@ -28,7 +28,7 @@ NS_ASSUME_NONNULL_BEGIN
- (void)loadAppcastFromURL:(NSURL *)appcastURL userAgent:(NSString *)userAgent httpHeaders:(NSDictionary * _Nullable)httpHeaders inBackground:(BOOL)background includesSkippedUpdates:(BOOL)includesSkippedUpdates;
@property (nullable, nonatomic, readonly) SUAppcastItem *nonDeltaUpdateItem;
@property (nullable, nonatomic, readonly) SUAppcastItem *secondaryUpdateItem;
@end
+54 -31
View File
@@ -16,6 +16,7 @@
#import "SUHost.h"
#import "SUConstants.h"
#import "SUPhasedUpdateGroupInfo.h"
#import "SULog.h"
#include "AppKitPrevention.h"
@@ -26,7 +27,7 @@
@property (nonatomic, copy) NSString *userAgent;
@property (nullable, nonatomic, readonly, weak) id updater;
@property (nullable, nonatomic, readonly, weak) id <SPUUpdaterDelegate> updaterDelegate;
@property (nullable, nonatomic) SUAppcastItem *nonDeltaUpdateItem;
@property (nullable, nonatomic) SUAppcastItem *secondaryUpdateItem;
@property (nullable, nonatomic, readonly, weak) id <SUAppcastDriverDelegate> delegate;
@end
@@ -37,7 +38,7 @@
@synthesize userAgent = _userAgent;
@synthesize updater = _updater;
@synthesize updaterDelegate = _updaterDelegate;
@synthesize nonDeltaUpdateItem = _nonDeltaUpdateItem;
@synthesize secondaryUpdateItem = _secondaryUpdateItem;
@synthesize delegate = _delegate;
- (instancetype)initWithHost:(SUHost *)host updater:(id)updater updaterDelegate:(id <SPUUpdaterDelegate>)updaterDelegate delegate:(id <SUAppcastDriverDelegate>)delegate
@@ -68,6 +69,24 @@
}];
}
- (SUAppcastItem * _Nullable)preferredUpdateForRegularAppcastItem:(SUAppcastItem * _Nullable)regularItem secondaryUpdate:(SUAppcastItem * __autoreleasing _Nullable *)secondaryUpdate
{
if (regularItem == nil) {
return nil;
}
SUAppcastItem *deltaItem = [[self class] deltaUpdateFromAppcastItem:regularItem hostVersion:self.host.version];
if (deltaItem != nil) {
if (secondaryUpdate != NULL) {
*secondaryUpdate = regularItem;
}
return deltaItem;
} else {
return regularItem;
}
}
- (void)appcastDidFinishLoading:(SUAppcast *)loadedAppcast inBackground:(BOOL)background includesSkippedUpdates:(BOOL)includesSkippedUpdates
{
[self.delegate didFinishLoadingAppcast:loadedAppcast];
@@ -79,35 +98,33 @@
SUAppcast *supportedAppcast = [[self class] filterSupportedAppcast:loadedAppcast phasedUpdateGroup:phasedUpdateGroup];
SUAppcastItem *item = nil;
SUAppcastItem *nonDeltaUpdateItem = nil;
SUAppcastItem *secondaryItem = nil;
// Now we have to find the best valid update in the appcast.
// Find the best valid update in the appcast by asking the delegate
if ([self.updaterDelegate respondsToSelector:@selector((bestValidUpdateInAppcast:forUpdater:))])
{
item = [self.updaterDelegate bestValidUpdateInAppcast:supportedAppcast forUpdater:(id _Nonnull)self.updater];
SUAppcastItem *regularItem = [self.updaterDelegate bestValidUpdateInAppcast:supportedAppcast forUpdater:(id _Nonnull)self.updater];
assert(!regularItem.deltaUpdate);
if (regularItem.deltaUpdate) {
// Client would have to go out of their way to examine the .deltaUpdates to return one
// This is very unlikely, and we need them to give us a regular update item back
SULog(SULogLevelError, @"Error: -bestValidUpdateInAppcast:forUpdater: cannot return a delta update item");
} else {
item = [self preferredUpdateForRegularAppcastItem:regularItem secondaryUpdate:&secondaryItem];
}
}
if (item != nil)
// Take care of finding best appcast item ourselves if delegate does not
if (item == nil)
{
// Does the delegate want to handle it?
if ([item isDeltaUpdate]) {
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 class] bestItemFromAppcastItems:supportedAppcast.items getDeltaItem:&deltaUpdateItem withHostVersion:self.host.version comparator:[self versionComparator]];
SUAppcastItem *regularItem = [[self class] bestItemFromAppcastItems:supportedAppcast.items comparator:[self versionComparator]];
if (item && deltaUpdateItem) {
nonDeltaUpdateItem = item;
item = deltaUpdateItem;
}
item = [self preferredUpdateForRegularAppcastItem:regularItem secondaryUpdate:&secondaryItem];
}
if ([self itemContainsValidUpdate:item inBackground:background includesSkippedUpdates:includesSkippedUpdates]) {
self.nonDeltaUpdateItem = nonDeltaUpdateItem;
self.secondaryUpdateItem = secondaryItem;
[self.delegate didFindValidUpdateWithAppcastItem:item preventsAutoupdate:[self itemPreventsAutoupdate:item]];
} else {
NSComparisonResult hostToLatestAppcastItemComparisonResult = (item != nil) ? [[self versionComparator] compareVersion:[self.host version] toVersion:[item versionString]] : 0;
@@ -125,9 +142,12 @@
}];
}
// 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<SUVersionComparison>)comparator
+ (SUAppcastItem * _Nullable)deltaUpdateFromAppcastItem:(SUAppcastItem *)appcastItem hostVersion:(NSString *)hostVersion
{
return appcastItem.deltaUpdates[hostVersion];
}
+ (SUAppcastItem * _Nullable)bestItemFromAppcastItems:(NSArray *)appcastItems comparator:(id<SUVersionComparison>)comparator
{
SUAppcastItem *item = nil;
for(SUAppcastItem *candidate in appcastItems) {
@@ -135,14 +155,17 @@
item = candidate;
}
}
if (item && deltaItem) {
SUAppcastItem *deltaUpdateItem = [item deltaUpdates][hostVersion];
if (deltaUpdateItem) {
*deltaItem = deltaUpdateItem;
}
return item;
}
// 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<SUVersionComparison>)comparator
{
SUAppcastItem *item = [self bestItemFromAppcastItems:appcastItems comparator:comparator];
if (item != nil && deltaItem != NULL) {
*deltaItem = [self deltaUpdateFromAppcastItem:item hostVersion:hostVersion];
}
return item;
}