diff --git a/Sparkle/SUAutomaticUpdateDriver.h b/Sparkle/SUAutomaticUpdateDriver.h index 4dd90e8f..4af54a11 100644 --- a/Sparkle/SUAutomaticUpdateDriver.h +++ b/Sparkle/SUAutomaticUpdateDriver.h @@ -13,7 +13,7 @@ #import "SUBasicUpdateDriver.h" #import "SUAutomaticUpdateAlert.h" -@interface SUAutomaticUpdateDriver : SUBasicUpdateDriver +@interface SUAutomaticUpdateDriver : SUBasicUpdateDriver @end diff --git a/Sparkle/SUBasicUpdateDriver.h b/Sparkle/SUBasicUpdateDriver.h index 301b4bc5..1dd8f367 100644 --- a/Sparkle/SUBasicUpdateDriver.h +++ b/Sparkle/SUBasicUpdateDriver.h @@ -15,7 +15,7 @@ #import "SUAppcast.h" @class SUAppcastItem, SUHost; -@interface SUBasicUpdateDriver : SUUpdateDriver +@interface SUBasicUpdateDriver : SUUpdateDriver @property (strong, readonly) SUAppcastItem *updateItem; @property (strong, readonly) NSURLDownload *download; @@ -37,8 +37,6 @@ - (void)download:(NSURLDownload *)download didFailWithError:(NSError *)error; - (void)extractUpdate; -- (void)unarchiverDidFinish:(SUUnarchiver *)ua; -- (void)unarchiverDidFail:(SUUnarchiver *)ua; - (void)failedToApplyDeltaUpdate; - (void)installWithToolAndRelaunch:(BOOL)relaunch; diff --git a/Sparkle/SUBasicUpdateDriver.m b/Sparkle/SUBasicUpdateDriver.m index 0c714d03..6ee0aadd 100644 --- a/Sparkle/SUBasicUpdateDriver.m +++ b/Sparkle/SUBasicUpdateDriver.m @@ -335,8 +335,17 @@ if (!success) { [self unarchiverDidFail:nil]; } else { - unarchiver.delegate = self; - [unarchiver start]; + [unarchiver unarchiveWithCompletionBlock:^(NSError *err){ + if (err) { + [self unarchiverDidFail:err]; + return; + } + + assert(self.updateItem); + [self installWithToolAndRelaunch:YES]; + } progressBlock:^(__unused double progress){ + + }]; } } @@ -349,14 +358,7 @@ [self downloadUpdate]; } -- (void)unarchiverDidFinish:(SUUnarchiver *)__unused ua -{ - assert(self.updateItem); - - [self installWithToolAndRelaunch:YES]; -} - -- (void)unarchiverDidFail:(SUUnarchiver *)__unused ua +- (void)unarchiverDidFail:(NSError *)err { // No longer needed self.updateValidator = nil; @@ -366,7 +368,7 @@ return; } - [self abortUpdateWithError:[NSError errorWithDomain:SUSparkleErrorDomain code:SUUnarchivingError userInfo:@{ NSLocalizedDescriptionKey: SULocalizedString(@"An error occurred while extracting the archive. Please try again later.", nil) }]]; + [self abortUpdateWithError:err]; } - (void)installWithToolAndRelaunch:(BOOL)relaunch diff --git a/Sparkle/SUBinaryDeltaApply.h b/Sparkle/SUBinaryDeltaApply.h index 8390d9c0..4932cd9d 100644 --- a/Sparkle/SUBinaryDeltaApply.h +++ b/Sparkle/SUBinaryDeltaApply.h @@ -10,6 +10,6 @@ #define SUBINARYDELTAAPPLY_H @class NSString; -BOOL applyBinaryDelta(NSString *source, NSString *destination, NSString *patchFile, BOOL verbose, NSError * __autoreleasing *error); +BOOL applyBinaryDelta(NSString *source, NSString *destination, NSString *patchFile, BOOL verbose, void (^progressCallback)(double), NSError * __autoreleasing *error); #endif diff --git a/Sparkle/SUBinaryDeltaApply.m b/Sparkle/SUBinaryDeltaApply.m index 63abdb4f..eac297b4 100644 --- a/Sparkle/SUBinaryDeltaApply.m +++ b/Sparkle/SUBinaryDeltaApply.m @@ -25,7 +25,7 @@ static BOOL applyBinaryDeltaToFile(xar_t x, xar_file_t file, NSString *sourceFil return success; } -BOOL applyBinaryDelta(NSString *source, NSString *destination, NSString *patchFile, BOOL verbose, NSError * __autoreleasing *error) +BOOL applyBinaryDelta(NSString *source, NSString *destination, NSString *patchFile, BOOL verbose, void (^progressCallback)(double progress), NSError * __autoreleasing *error) { xar_t x = xar_open([patchFile fileSystemRepresentation], READ); if (!x) { @@ -44,6 +44,8 @@ BOOL applyBinaryDelta(NSString *source, NSString *destination, NSString *patchFi NSString *expectedNewBeforeHash = nil; NSString *expectedNewAfterHash = nil; + progressCallback(0/6.0); + xar_subdoc_t subdoc; for (subdoc = xar_subdoc_first(x); subdoc; subdoc = xar_subdoc_next(subdoc)) { if (!strcmp(xar_subdoc_name(subdoc), BINARY_DELTA_ATTRIBUTES_KEY)) { @@ -112,6 +114,8 @@ BOOL applyBinaryDelta(NSString *source, NSString *destination, NSString *patchFi fprintf(stderr, "Verifying source..."); } + progressCallback(1/6.0); + NSString *beforeHash = hashOfTreeWithVersion(source, majorDiffVersion); if (!beforeHash) { if (verbose) { @@ -137,6 +141,8 @@ BOOL applyBinaryDelta(NSString *source, NSString *destination, NSString *patchFi fprintf(stderr, "\nCopying files..."); } + progressCallback(2/6.0); + if (!removeTree(destination)) { if (verbose) { fprintf(stderr, "\n"); @@ -146,6 +152,9 @@ BOOL applyBinaryDelta(NSString *source, NSString *destination, NSString *patchFi } return NO; } + + progressCallback(3/6.0); + if (!copyTree(source, destination)) { if (verbose) { fprintf(stderr, "\n"); @@ -156,6 +165,8 @@ BOOL applyBinaryDelta(NSString *source, NSString *destination, NSString *patchFi return NO; } + progressCallback(4/6.0); + BOOL hasExtractKeyAvailable = MAJOR_VERSION_IS_AT_LEAST(majorDiffVersion, SUBeigeMajorVersion); if (verbose) { @@ -252,6 +263,8 @@ BOOL applyBinaryDelta(NSString *source, NSString *destination, NSString *patchFi } xar_close(x); + progressCallback(5/6.0); + if (verbose) { fprintf(stderr, "\nVerifying destination..."); } @@ -277,6 +290,8 @@ BOOL applyBinaryDelta(NSString *source, NSString *destination, NSString *patchFi return NO; } + progressCallback(6/6.0); + if (verbose) { fprintf(stderr, "\nDone!\n"); } diff --git a/Sparkle/SUBinaryDeltaUnarchiver.m b/Sparkle/SUBinaryDeltaUnarchiver.m index 5ff34bd8..bd9bdcc1 100644 --- a/Sparkle/SUBinaryDeltaUnarchiver.m +++ b/Sparkle/SUBinaryDeltaUnarchiver.m @@ -68,25 +68,22 @@ NSString *targetPath = [[self.archivePath stringByDeletingLastPathComponent] stringByAppendingPathComponent:[sourcePath lastPathComponent]]; NSError *applyDiffError = nil; - BOOL success = applyBinaryDelta(sourcePath, targetPath, self.archivePath, NO, &applyDiffError); + BOOL success = applyBinaryDelta(sourcePath, targetPath, self.archivePath, NO, ^(double progress){ + [self notifyProgress:progress]; + }, &applyDiffError); if (success) { [[self class] updateSpotlightImportersAtBundlePath:targetPath]; - - dispatch_async(dispatch_get_main_queue(), ^{ - [self notifyDelegateOfSuccess]; - }); + [self unarchiverDidFinish]; } else { - SULog(@"Applying delta patch failed with error: %@", applyDiffError); - dispatch_async(dispatch_get_main_queue(), ^{ - [self notifyDelegateOfFailure]; - }); + [self unarchiverDidFailWithError:applyDiffError]; } } } -- (void)start +- (void)unarchiveWithCompletionBlock:(void (^)(NSError * _Nullable))block progressBlock:(void (^ _Nullable)(double progress))progress { + [super unarchiveWithCompletionBlock:block progressBlock:progress]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [self applyBinaryDelta]; }); diff --git a/Sparkle/SUDiskImageUnarchiver.m b/Sparkle/SUDiskImageUnarchiver.m index e6f743e7..c98725e9 100644 --- a/Sparkle/SUDiskImageUnarchiver.m +++ b/Sparkle/SUDiskImageUnarchiver.m @@ -25,14 +25,6 @@ // Called on a non-main thread. - (void)extractDMG -{ - @autoreleasepool { - [self extractDMGWithPassword:nil]; - } -} - -// Called on a non-main thread. -- (void)extractDMGWithPassword:(NSString *)__unused password { @autoreleasepool { BOOL mountedSuccessfully = NO; @@ -45,16 +37,8 @@ NSFileManager *manager; NSError *error; NSArray *contents; - // We have to declare these before a goto to prevent an error under ARC. - // No, we cannot have them in the dispatch_async calls, as the goto "jump enters - // lifetime of block which strongly captures a variable" - dispatch_block_t delegateFailure = ^{ - [self notifyDelegateOfFailure]; - }; - dispatch_block_t delegateSuccess = ^{ - [self notifyDelegateOfSuccess]; - }; - do + + do { // Using NSUUID would make creating UUIDs be done in Cocoa, // and thus managed under ARC. Sadly, the class is in 10.8 and later. @@ -106,6 +90,8 @@ [task launch]; + [self notifyProgress:0.125]; + [inputPipe.fileHandleForWriting writeData:promptData]; [inputPipe.fileHandleForWriting closeFile]; @@ -120,6 +106,8 @@ goto reportError; } + [self notifyProgress:0.5]; + if (taskResult != 0) { NSString *resultStr = output ? [[NSString alloc] initWithData:output encoding:NSUTF8StringEncoding] : nil; @@ -137,6 +125,9 @@ goto reportError; } + double itemsCopied = 0; + double totalItems = [contents count]; + for (NSString *item in contents) { NSString *fromPath = [mountPoint stringByAppendingPathComponent:item]; @@ -147,20 +138,21 @@ continue; } + itemsCopied += 1.0; + [self notifyProgress:0.5 + itemsCopied/(totalItems*2.0)]; SULog(@"copyItemAtPath:%@ toPath:%@", fromPath, toPath); if (![manager copyItemAtPath:fromPath toPath:toPath error:&error]) { - SULog(@"Couldn't copy item: %@ : %@", error, error.userInfo ? error.userInfo : @""); goto reportError; } } - dispatch_async(dispatch_get_main_queue(), delegateSuccess); + [self unarchiverDidFinish]; goto finally; reportError: - dispatch_async(dispatch_get_main_queue(), delegateFailure); + [self unarchiverDidFailWithError:error]; finally: if (mountedSuccessfully) { @@ -176,8 +168,9 @@ } } -- (void)start +- (void)unarchiveWithCompletionBlock:(void (^)(NSError * _Nullable))block progressBlock:(void (^ _Nullable)(double progress))progress { + [super unarchiveWithCompletionBlock:block progressBlock:progress]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [self extractDMG]; }); diff --git a/Sparkle/SUPipedUnarchiver.m b/Sparkle/SUPipedUnarchiver.m index 0b0157c3..40739497 100644 --- a/Sparkle/SUPipedUnarchiver.m +++ b/Sparkle/SUPipedUnarchiver.m @@ -46,8 +46,9 @@ return NO; } -- (void)start +- (void)unarchiveWithCompletionBlock:(void (^)(NSError * _Nullable))block progressBlock:(void (^ _Nullable)(double progress))progress { + [super unarchiveWithCompletionBlock:block progressBlock:progress]; [NSThread detachNewThreadSelector:[[self class] selectorConformingToTypeOfPath:self.archivePath] toTarget:self withObject:nil]; } @@ -61,7 +62,7 @@ { // *** GETS CALLED ON NON-MAIN THREAD!!! @autoreleasepool { - + NSError *error = nil; NSString *destination = [self.archivePath stringByDeletingLastPathComponent]; SULog(@"Extracting using '%@' '%@' < '%@' '%@'", command, [args componentsJoinedByString:@"' '"], self.archivePath, destination); @@ -93,7 +94,7 @@ bytesRead += len; [archiveOutput writeData:data]; dispatch_async(dispatch_get_main_queue(), ^{ - [self notifyDelegateOfProgress:(double)bytesRead / (double)expectedLength]; + [self notifyProgress:(double)bytesRead / (double)expectedLength]; }); } while(bytesRead < expectedLength); @@ -104,23 +105,20 @@ if ([task terminationStatus] == 0) { if (bytesRead == expectedLength) { - dispatch_async(dispatch_get_main_queue(), ^{ - [self notifyDelegateOfSuccess]; - }); + [self unarchiverDidFinish]; return; } else { - SULog(@"Extraction failed, command '%@' got only %ld of %ld bytes", command, (long)bytesRead, (long)expectedLength); + error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUUnarchivingError userInfo:@{ NSLocalizedDescriptionKey:[NSString stringWithFormat:@"Extraction failed, command '%@' got only %ld of %ld bytes", command, (long)bytesRead, (long)expectedLength]}]; + } } else { - SULog(@"Extraction failed, command '%@' returned %d", command, [task terminationStatus]); + error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUUnarchivingError userInfo:@{ NSLocalizedDescriptionKey:[NSString stringWithFormat:@"Extraction failed, command '%@' returned %d", command, [task terminationStatus]]}]; } } else { - SULog(@"Extraction failed, archive '%@' is empty", self.archivePath); + error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUUnarchivingError userInfo:@{ NSLocalizedDescriptionKey:[NSString stringWithFormat:@"Extraction failed, archive '%@' is empty", self.archivePath]}]; } - dispatch_async(dispatch_get_main_queue(), ^{ - [self notifyDelegateOfFailure]; - }); + [self unarchiverDidFailWithError:error]; } } diff --git a/Sparkle/SUUIBasedUpdateDriver.h b/Sparkle/SUUIBasedUpdateDriver.h index 8148912b..a511aae9 100644 --- a/Sparkle/SUUIBasedUpdateDriver.h +++ b/Sparkle/SUUIBasedUpdateDriver.h @@ -15,7 +15,7 @@ @class SUStatusController; -@interface SUUIBasedUpdateDriver : SUBasicUpdateDriver +@interface SUUIBasedUpdateDriver : SUBasicUpdateDriver - (void)showAlert:(NSAlert *)alert; - (IBAction)cancelDownload:(id)sender; diff --git a/Sparkle/SUUnarchiver.h b/Sparkle/SUUnarchiver.h index e2f79f04..6e38a728 100644 --- a/Sparkle/SUUnarchiver.h +++ b/Sparkle/SUUnarchiver.h @@ -11,29 +11,23 @@ #import +NS_ASSUME_NONNULL_BEGIN + @class SUHost; -@protocol SUUnarchiverDelegate; @interface SUUnarchiver : NSObject @property (copy, readonly) NSString *archivePath; -@property (copy, readonly) NSString *updateHostBundlePath; -@property (copy, readonly) NSString *decryptionPassword; -@property (weak) id delegate; +@property (copy, readonly) NSString *_Nullable updateHostBundlePath; +@property (copy, readonly) NSString *_Nullable decryptionPassword; -+ (SUUnarchiver *)unarchiverForPath:(NSString *)path updatingHostBundlePath:(NSString *)host withPassword:(NSString *)decryptionPassword; ++ (nullable SUUnarchiver *)unarchiverForPath:(NSString *)path updatingHostBundlePath:(nullable NSString *)host withPassword:(nullable NSString *)decryptionPassword; + (BOOL)unsafeIfArchiveIsNotValidated; -- (void)start; +- (void)unarchiveWithCompletionBlock:(void (^)(NSError *_Nullable))completion progressBlock:(void (^_Nullable)(double progress))progress; @end -@protocol SUUnarchiverDelegate -- (void)unarchiverDidFinish:(SUUnarchiver *)unarchiver; -- (void)unarchiverDidFail:(SUUnarchiver *)unarchiver; -@optional -- (void)unarchiver:(SUUnarchiver *)unarchiver extractedProgress:(double)progress; -@end - +NS_ASSUME_NONNULL_END #endif diff --git a/Sparkle/SUUnarchiver.m b/Sparkle/SUUnarchiver.m index b12d0648..1f146c3f 100644 --- a/Sparkle/SUUnarchiver.m +++ b/Sparkle/SUUnarchiver.m @@ -15,12 +15,18 @@ #import "SUUnarchiver.h" #import "SUUnarchiver_Private.h" +@interface SUUnarchiver () +@property (strong) void (^completionBlock)(NSError * _Nullable); +@property (strong) void (^progressBlock)(double progress); + +@end + @implementation SUUnarchiver @synthesize archivePath; @synthesize updateHostBundlePath; -@synthesize delegate; @synthesize decryptionPassword; +@synthesize completionBlock, progressBlock; + (SUUnarchiver *)unarchiverForPath:(NSString *)path updatingHostBundlePath:(NSString *)hostPath withPassword:(NSString *)decryptionPassword { @@ -34,9 +40,9 @@ - (NSString *)description { return [NSString stringWithFormat:@"%@ <%@>", [self class], self.archivePath]; } -- (void)start -{ - // No-op +- (void)unarchiveWithCompletionBlock:(void (^_Nonnull)(NSError * _Nullable))block progressBlock:(void (^_Nullable)(double progress))progress { + self.completionBlock = block; + self.progressBlock = progress; } - (instancetype)initWithPath:(NSString *)path hostBundlePath:(NSString *)hostPath password:(NSString *)password @@ -60,25 +66,32 @@ return NO; } -- (void)notifyDelegateOfProgress:(double)progress +- (void)notifyProgress:(double)progress { - if ([self.delegate respondsToSelector:@selector(unarchiver:extractedProgress:)]) { - [self.delegate unarchiver:self extractedProgress:progress]; + if (self.progressBlock) { + self.progressBlock(progress); } } -- (void)notifyDelegateOfSuccess +- (void)unarchiverDidFinish { - if ([self.delegate respondsToSelector:@selector(unarchiverDidFinish:)]) { - [self.delegate unarchiverDidFinish:self]; - } + dispatch_async(dispatch_get_main_queue(), ^{ + self.completionBlock(nil); + }); } -- (void)notifyDelegateOfFailure +- (void)unarchiverDidFail:(NSError *)reason { - if ([self.delegate respondsToSelector:@selector(unarchiverDidFail:)]) { - [self.delegate unarchiverDidFail:self]; + NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithObject:SULocalizedString(@"An error occurred while extracting the archive. Please try again later.", nil) forKey:NSLocalizedDescriptionKey]; + if (reason) { + [userInfo setObject:reason forKey:NSUnderlyingErrorKey]; } + + NSError *error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUUnarchivingError userInfo:userInfo]; + + dispatch_async(dispatch_get_main_queue(), ^{ + self.completionBlock(error); + }); } static NSMutableArray *gUnarchiverImplementations; diff --git a/Sparkle/SUUnarchiver_Private.h b/Sparkle/SUUnarchiver_Private.h index c29d68cf..b48792b9 100644 --- a/Sparkle/SUUnarchiver_Private.h +++ b/Sparkle/SUUnarchiver_Private.h @@ -18,9 +18,9 @@ + (BOOL)canUnarchivePath:(NSString *)path; - (instancetype)initWithPath:(NSString *)archive hostBundlePath:(NSString *)host; -- (void)notifyDelegateOfProgress:(double)progress; -- (void)notifyDelegateOfSuccess; -- (void)notifyDelegateOfFailure; +- (void)notifyProgress:(double)progress; +- (void)unarchiverDidFinish; +- (void)unarchiverDidFailWithError:(NSError *)err; @end #endif diff --git a/Tests/SUBinaryDeltaTest.m b/Tests/SUBinaryDeltaTest.m index 550f55e3..210363c6 100644 --- a/Tests/SUBinaryDeltaTest.m +++ b/Tests/SUBinaryDeltaTest.m @@ -69,7 +69,7 @@ typedef void (^SUDeltaHandler)(NSFileManager *fileManager, NSString *sourceDirec NSError *applyDiffError = nil; BOOL appliedDiff = NO; if (createdDiff) { - if (applyBinaryDelta(sourceDirectory, patchDirectory, diffFile, NO, &applyDiffError)) { + if (applyBinaryDelta(sourceDirectory, patchDirectory, diffFile, NO, ^(__unused double progress){}, &applyDiffError)) { appliedDiff = YES; } else { NSLog(@"Applying binary diff failed with error: %@", applyDiffError); diff --git a/Tests/SUUnarchiverTest.swift b/Tests/SUUnarchiverTest.swift index 4e70bba8..3e1d55ad 100644 --- a/Tests/SUUnarchiverTest.swift +++ b/Tests/SUUnarchiverTest.swift @@ -8,28 +8,12 @@ import XCTest -class SUUnarchiverTest: XCTestCase, SUUnarchiverDelegate +class SUUnarchiverTest: XCTestCase { var password: String? = nil var unarchivedExpectation: XCTestExpectation? = nil var unarchivedResult: Bool = false - func unarchiver(_ unarchiver: SUUnarchiver!, extractedProgress progress: Double) - { - } - - func unarchiverDidFail(_ unarchiver: SUUnarchiver!) - { - self.unarchivedResult = false - self.unarchivedExpectation!.fulfill() - } - - func unarchiverDidFinish(_ unarchiver: SUUnarchiver!) - { - self.unarchivedResult = true - self.unarchivedExpectation!.fulfill() - } - func unarchiveTestAppWithExtension(_ archiveExtension: String) { let appName = "SparkleTestCodeSignApp" @@ -49,10 +33,12 @@ class SUUnarchiverTest: XCTestCase, SUUnarchiverDelegate self.unarchivedExpectation = super.expectation(description: "Unarchived Application (format: \(archiveExtension))") - let unarchiver = SUUnarchiver(forPath: tempArchiveURL.path, updatingHostBundlePath: nil, withPassword: self.password) + let unarchiver = SUUnarchiver(forPath: tempArchiveURL.path, updatingHostBundlePath: nil, withPassword: self.password)! - unarchiver?.delegate = self - unarchiver?.start() + unarchiver.unarchive(completionBlock: {(error: Error?) -> Void in + self.unarchivedResult = nil == error; + self.unarchivedExpectation!.fulfill() + }, progressBlock:{(progress: Double) -> Void in }); super.waitForExpectations(timeout: 7.0, handler: nil)