From f3f268388b75188e89c4d1851b44b9f5f963ca6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kornel=20Lesin=CC=81ski?= Date: Tue, 20 Dec 2016 21:36:31 +0100 Subject: [PATCH 1/9] Nullability declarations --- Sparkle/SUUnarchiver.h | 11 +++++++---- Tests/SUUnarchiverTest.swift | 6 +++--- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/Sparkle/SUUnarchiver.h b/Sparkle/SUUnarchiver.h index e2f79f04..17ba37da 100644 --- a/Sparkle/SUUnarchiver.h +++ b/Sparkle/SUUnarchiver.h @@ -11,17 +11,19 @@ #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; +@property (weak) _Nullable id delegate; -+ (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; @@ -36,4 +38,5 @@ - (void)unarchiver:(SUUnarchiver *)unarchiver extractedProgress:(double)progress; @end +NS_ASSUME_NONNULL_END #endif diff --git a/Tests/SUUnarchiverTest.swift b/Tests/SUUnarchiverTest.swift index 4e70bba8..0505e6c5 100644 --- a/Tests/SUUnarchiverTest.swift +++ b/Tests/SUUnarchiverTest.swift @@ -14,17 +14,17 @@ class SUUnarchiverTest: XCTestCase, SUUnarchiverDelegate var unarchivedExpectation: XCTestExpectation? = nil var unarchivedResult: Bool = false - func unarchiver(_ unarchiver: SUUnarchiver!, extractedProgress progress: Double) + func unarchiver(_ unarchiver: SUUnarchiver, extractedProgress progress: Double) { } - func unarchiverDidFail(_ unarchiver: SUUnarchiver!) + func unarchiverDidFail(_ unarchiver: SUUnarchiver) { self.unarchivedResult = false self.unarchivedExpectation!.fulfill() } - func unarchiverDidFinish(_ unarchiver: SUUnarchiver!) + func unarchiverDidFinish(_ unarchiver: SUUnarchiver) { self.unarchivedResult = true self.unarchivedExpectation!.fulfill() From 9d8b3c0c3611e8c90975e9269b5549b9cf812a31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kornel=20Lesin=CC=81ski?= Date: Tue, 20 Dec 2016 22:09:28 +0100 Subject: [PATCH 2/9] DRY async dispatch of unarchiver notifications --- Sparkle/SUBinaryDeltaUnarchiver.m | 9 ++------- Sparkle/SUDiskImageUnarchiver.m | 16 ++++------------ Sparkle/SUPipedUnarchiver.m | 8 ++------ Sparkle/SUUnarchiver.m | 16 ++++++++++------ 4 files changed, 18 insertions(+), 31 deletions(-) diff --git a/Sparkle/SUBinaryDeltaUnarchiver.m b/Sparkle/SUBinaryDeltaUnarchiver.m index 5ff34bd8..30c51f05 100644 --- a/Sparkle/SUBinaryDeltaUnarchiver.m +++ b/Sparkle/SUBinaryDeltaUnarchiver.m @@ -71,16 +71,11 @@ BOOL success = applyBinaryDelta(sourcePath, targetPath, self.archivePath, NO, &applyDiffError); if (success) { [[self class] updateSpotlightImportersAtBundlePath:targetPath]; - - dispatch_async(dispatch_get_main_queue(), ^{ - [self notifyDelegateOfSuccess]; - }); + [self notifyDelegateOfSuccess]; } else { SULog(@"Applying delta patch failed with error: %@", applyDiffError); - dispatch_async(dispatch_get_main_queue(), ^{ - [self notifyDelegateOfFailure]; - }); + [self notifyDelegateOfFailure]; } } } diff --git a/Sparkle/SUDiskImageUnarchiver.m b/Sparkle/SUDiskImageUnarchiver.m index e6f743e7..cae38bb1 100644 --- a/Sparkle/SUDiskImageUnarchiver.m +++ b/Sparkle/SUDiskImageUnarchiver.m @@ -45,16 +45,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. @@ -156,11 +148,11 @@ } } - dispatch_async(dispatch_get_main_queue(), delegateSuccess); + [self notifyDelegateOfSuccess]; goto finally; reportError: - dispatch_async(dispatch_get_main_queue(), delegateFailure); + [self notifyDelegateOfFailure]; finally: if (mountedSuccessfully) { diff --git a/Sparkle/SUPipedUnarchiver.m b/Sparkle/SUPipedUnarchiver.m index 0b0157c3..aebc5b46 100644 --- a/Sparkle/SUPipedUnarchiver.m +++ b/Sparkle/SUPipedUnarchiver.m @@ -104,9 +104,7 @@ if ([task terminationStatus] == 0) { if (bytesRead == expectedLength) { - dispatch_async(dispatch_get_main_queue(), ^{ - [self notifyDelegateOfSuccess]; - }); + [self notifyDelegateOfSuccess]; return; } else { SULog(@"Extraction failed, command '%@' got only %ld of %ld bytes", command, (long)bytesRead, (long)expectedLength); @@ -118,9 +116,7 @@ SULog(@"Extraction failed, archive '%@' is empty", self.archivePath); } - dispatch_async(dispatch_get_main_queue(), ^{ - [self notifyDelegateOfFailure]; - }); + [self notifyDelegateOfFailure]; } } diff --git a/Sparkle/SUUnarchiver.m b/Sparkle/SUUnarchiver.m index b12d0648..c65c5166 100644 --- a/Sparkle/SUUnarchiver.m +++ b/Sparkle/SUUnarchiver.m @@ -69,16 +69,20 @@ - (void)notifyDelegateOfSuccess { - if ([self.delegate respondsToSelector:@selector(unarchiverDidFinish:)]) { - [self.delegate unarchiverDidFinish:self]; - } + dispatch_async(dispatch_get_main_queue(), ^{ + if ([self.delegate respondsToSelector:@selector(unarchiverDidFinish:)]) { + [self.delegate unarchiverDidFinish:self]; + } + }); } - (void)notifyDelegateOfFailure { - if ([self.delegate respondsToSelector:@selector(unarchiverDidFail:)]) { - [self.delegate unarchiverDidFail:self]; - } + dispatch_async(dispatch_get_main_queue(), ^{ + if ([self.delegate respondsToSelector:@selector(unarchiverDidFail:)]) { + [self.delegate unarchiverDidFail:self]; + } + }); } static NSMutableArray *gUnarchiverImplementations; From 311ed493b4b0fe25fa8299967e4579cf9f5a3f7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kornel=20Lesin=CC=81ski?= Date: Tue, 20 Dec 2016 23:54:56 +0100 Subject: [PATCH 3/9] Unused --- Sparkle/SUDiskImageUnarchiver.m | 8 -------- 1 file changed, 8 deletions(-) diff --git a/Sparkle/SUDiskImageUnarchiver.m b/Sparkle/SUDiskImageUnarchiver.m index cae38bb1..68f3f2e6 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; From 23b2b49663963e97d36b22d502c29a233282a558 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kornel=20Lesin=CC=81ski?= Date: Wed, 21 Dec 2016 00:05:16 +0100 Subject: [PATCH 4/9] Ability to report specific unarchiving error --- Sparkle/SUBinaryDeltaUnarchiver.m | 3 +-- Sparkle/SUDiskImageUnarchiver.m | 3 +-- Sparkle/SUPipedUnarchiver.m | 11 ++++++----- Sparkle/SUUnarchiver.m | 2 +- Sparkle/SUUnarchiver_Private.h | 2 +- 5 files changed, 10 insertions(+), 11 deletions(-) diff --git a/Sparkle/SUBinaryDeltaUnarchiver.m b/Sparkle/SUBinaryDeltaUnarchiver.m index 30c51f05..04aba6ff 100644 --- a/Sparkle/SUBinaryDeltaUnarchiver.m +++ b/Sparkle/SUBinaryDeltaUnarchiver.m @@ -74,8 +74,7 @@ [self notifyDelegateOfSuccess]; } else { - SULog(@"Applying delta patch failed with error: %@", applyDiffError); - [self notifyDelegateOfFailure]; + [self unarchiverDidFailWithError:applyDiffError]; } } } diff --git a/Sparkle/SUDiskImageUnarchiver.m b/Sparkle/SUDiskImageUnarchiver.m index 68f3f2e6..40b69fce 100644 --- a/Sparkle/SUDiskImageUnarchiver.m +++ b/Sparkle/SUDiskImageUnarchiver.m @@ -135,7 +135,6 @@ if (![manager copyItemAtPath:fromPath toPath:toPath error:&error]) { - SULog(@"Couldn't copy item: %@ : %@", error, error.userInfo ? error.userInfo : @""); goto reportError; } } @@ -144,7 +143,7 @@ goto finally; reportError: - [self notifyDelegateOfFailure]; + [self unarchiverDidFailWithError:error]; finally: if (mountedSuccessfully) { diff --git a/Sparkle/SUPipedUnarchiver.m b/Sparkle/SUPipedUnarchiver.m index aebc5b46..6b7373e9 100644 --- a/Sparkle/SUPipedUnarchiver.m +++ b/Sparkle/SUPipedUnarchiver.m @@ -61,7 +61,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); @@ -107,16 +107,17 @@ [self notifyDelegateOfSuccess]; 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]}]; } - [self notifyDelegateOfFailure]; + [self unarchiverDidFailWithError:error]; } } diff --git a/Sparkle/SUUnarchiver.m b/Sparkle/SUUnarchiver.m index c65c5166..1b8d292a 100644 --- a/Sparkle/SUUnarchiver.m +++ b/Sparkle/SUUnarchiver.m @@ -76,7 +76,7 @@ }); } -- (void)notifyDelegateOfFailure +- (void)unarchiverDidFailWithError:(NSError *)error { dispatch_async(dispatch_get_main_queue(), ^{ if ([self.delegate respondsToSelector:@selector(unarchiverDidFail:)]) { diff --git a/Sparkle/SUUnarchiver_Private.h b/Sparkle/SUUnarchiver_Private.h index c29d68cf..f8853d1f 100644 --- a/Sparkle/SUUnarchiver_Private.h +++ b/Sparkle/SUUnarchiver_Private.h @@ -20,7 +20,7 @@ - (void)notifyDelegateOfProgress:(double)progress; - (void)notifyDelegateOfSuccess; -- (void)notifyDelegateOfFailure; +- (void)unarchiverDidFailWithError:(NSError *)error; @end #endif From 5ee02e954e46b488888492d5d6a4ed3011269a2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kornel=20Lesin=CC=81ski?= Date: Tue, 20 Dec 2016 23:40:30 +0100 Subject: [PATCH 5/9] Renamed callbacks --- Sparkle/SUBasicUpdateDriver.m | 4 ++-- Sparkle/SUBinaryDeltaUnarchiver.m | 2 +- Sparkle/SUDiskImageUnarchiver.m | 2 +- Sparkle/SUPipedUnarchiver.m | 2 +- Sparkle/SUUnarchiver.m | 4 ++-- Sparkle/SUUnarchiver_Private.h | 4 ++-- 6 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Sparkle/SUBasicUpdateDriver.m b/Sparkle/SUBasicUpdateDriver.m index 0c714d03..3735b6ba 100644 --- a/Sparkle/SUBasicUpdateDriver.m +++ b/Sparkle/SUBasicUpdateDriver.m @@ -356,7 +356,7 @@ [self installWithToolAndRelaunch:YES]; } -- (void)unarchiverDidFail:(SUUnarchiver *)__unused ua +- (void)unarchiverDidFail:(NSError *)err { // No longer needed self.updateValidator = nil; @@ -366,7 +366,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/SUBinaryDeltaUnarchiver.m b/Sparkle/SUBinaryDeltaUnarchiver.m index 04aba6ff..046dd0db 100644 --- a/Sparkle/SUBinaryDeltaUnarchiver.m +++ b/Sparkle/SUBinaryDeltaUnarchiver.m @@ -71,7 +71,7 @@ BOOL success = applyBinaryDelta(sourcePath, targetPath, self.archivePath, NO, &applyDiffError); if (success) { [[self class] updateSpotlightImportersAtBundlePath:targetPath]; - [self notifyDelegateOfSuccess]; + [self unarchiverDidFinish]; } else { [self unarchiverDidFailWithError:applyDiffError]; diff --git a/Sparkle/SUDiskImageUnarchiver.m b/Sparkle/SUDiskImageUnarchiver.m index 40b69fce..06cba163 100644 --- a/Sparkle/SUDiskImageUnarchiver.m +++ b/Sparkle/SUDiskImageUnarchiver.m @@ -139,7 +139,7 @@ } } - [self notifyDelegateOfSuccess]; + [self unarchiverDidFinish]; goto finally; reportError: diff --git a/Sparkle/SUPipedUnarchiver.m b/Sparkle/SUPipedUnarchiver.m index 6b7373e9..8b34d666 100644 --- a/Sparkle/SUPipedUnarchiver.m +++ b/Sparkle/SUPipedUnarchiver.m @@ -104,7 +104,7 @@ if ([task terminationStatus] == 0) { if (bytesRead == expectedLength) { - [self notifyDelegateOfSuccess]; + [self unarchiverDidFinish]; return; } else { error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUUnarchivingError userInfo:@{ NSLocalizedDescriptionKey:[NSString stringWithFormat:@"Extraction failed, command '%@' got only %ld of %ld bytes", command, (long)bytesRead, (long)expectedLength]}]; diff --git a/Sparkle/SUUnarchiver.m b/Sparkle/SUUnarchiver.m index 1b8d292a..9b309a70 100644 --- a/Sparkle/SUUnarchiver.m +++ b/Sparkle/SUUnarchiver.m @@ -67,7 +67,7 @@ } } -- (void)notifyDelegateOfSuccess +- (void)unarchiverDidFinish { dispatch_async(dispatch_get_main_queue(), ^{ if ([self.delegate respondsToSelector:@selector(unarchiverDidFinish:)]) { @@ -76,7 +76,7 @@ }); } -- (void)unarchiverDidFailWithError:(NSError *)error +- (void)unarchiverDidFail:(NSError *)reason { dispatch_async(dispatch_get_main_queue(), ^{ if ([self.delegate respondsToSelector:@selector(unarchiverDidFail:)]) { diff --git a/Sparkle/SUUnarchiver_Private.h b/Sparkle/SUUnarchiver_Private.h index f8853d1f..c7718e9f 100644 --- a/Sparkle/SUUnarchiver_Private.h +++ b/Sparkle/SUUnarchiver_Private.h @@ -19,8 +19,8 @@ - (instancetype)initWithPath:(NSString *)archive hostBundlePath:(NSString *)host; - (void)notifyDelegateOfProgress:(double)progress; -- (void)notifyDelegateOfSuccess; -- (void)unarchiverDidFailWithError:(NSError *)error; +- (void)unarchiverDidFinish; +- (void)unarchiverDidFailWithError:(NSError *)err; @end #endif From 70f56817974e034bcfbd7990d2fed8d5f68221fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kornel=20Lesin=CC=81ski?= Date: Wed, 21 Dec 2016 00:16:24 +0100 Subject: [PATCH 6/9] Use completion block for unarchiving --- Sparkle/SUBasicUpdateDriver.h | 2 -- Sparkle/SUBasicUpdateDriver.m | 17 +++++++++-------- Sparkle/SUBinaryDeltaUnarchiver.m | 3 ++- Sparkle/SUDiskImageUnarchiver.m | 3 ++- Sparkle/SUPipedUnarchiver.m | 3 ++- Sparkle/SUUnarchiver.h | 4 +--- Sparkle/SUUnarchiver.m | 25 ++++++++++++++++--------- Tests/SUUnarchiverTest.swift | 21 ++++++--------------- 8 files changed, 38 insertions(+), 40 deletions(-) diff --git a/Sparkle/SUBasicUpdateDriver.h b/Sparkle/SUBasicUpdateDriver.h index 301b4bc5..002d82e2 100644 --- a/Sparkle/SUBasicUpdateDriver.h +++ b/Sparkle/SUBasicUpdateDriver.h @@ -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 3735b6ba..76ea355c 100644 --- a/Sparkle/SUBasicUpdateDriver.m +++ b/Sparkle/SUBasicUpdateDriver.m @@ -336,7 +336,15 @@ [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]; + }]; } } @@ -349,13 +357,6 @@ [self downloadUpdate]; } -- (void)unarchiverDidFinish:(SUUnarchiver *)__unused ua -{ - assert(self.updateItem); - - [self installWithToolAndRelaunch:YES]; -} - - (void)unarchiverDidFail:(NSError *)err { // No longer needed diff --git a/Sparkle/SUBinaryDeltaUnarchiver.m b/Sparkle/SUBinaryDeltaUnarchiver.m index 046dd0db..f45cd26d 100644 --- a/Sparkle/SUBinaryDeltaUnarchiver.m +++ b/Sparkle/SUBinaryDeltaUnarchiver.m @@ -79,8 +79,9 @@ } } -- (void)start +- (void)unarchiveWithCompletionBlock:(void (^)(NSError * _Nullable))block { + [super unarchiveWithCompletionBlock:block]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [self applyBinaryDelta]; }); diff --git a/Sparkle/SUDiskImageUnarchiver.m b/Sparkle/SUDiskImageUnarchiver.m index 06cba163..e731e9f3 100644 --- a/Sparkle/SUDiskImageUnarchiver.m +++ b/Sparkle/SUDiskImageUnarchiver.m @@ -159,8 +159,9 @@ } } -- (void)start +- (void)unarchiveWithCompletionBlock:(void (^)(NSError * _Nullable))block { + [super unarchiveWithCompletionBlock:block]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [self extractDMG]; }); diff --git a/Sparkle/SUPipedUnarchiver.m b/Sparkle/SUPipedUnarchiver.m index 8b34d666..320d150d 100644 --- a/Sparkle/SUPipedUnarchiver.m +++ b/Sparkle/SUPipedUnarchiver.m @@ -46,8 +46,9 @@ return NO; } -- (void)start +- (void)unarchiveWithCompletionBlock:(void (^)(NSError * _Nullable))block { + [super unarchiveWithCompletionBlock:block]; [NSThread detachNewThreadSelector:[[self class] selectorConformingToTypeOfPath:self.archivePath] toTarget:self withObject:nil]; } diff --git a/Sparkle/SUUnarchiver.h b/Sparkle/SUUnarchiver.h index 17ba37da..486a3604 100644 --- a/Sparkle/SUUnarchiver.h +++ b/Sparkle/SUUnarchiver.h @@ -27,13 +27,11 @@ NS_ASSUME_NONNULL_BEGIN + (BOOL)unsafeIfArchiveIsNotValidated; -- (void)start; +- (void)unarchiveWithCompletionBlock:(void (^_Nonnull)(NSError *_Nullable))block; @end @protocol SUUnarchiverDelegate -- (void)unarchiverDidFinish:(SUUnarchiver *)unarchiver; -- (void)unarchiverDidFail:(SUUnarchiver *)unarchiver; @optional - (void)unarchiver:(SUUnarchiver *)unarchiver extractedProgress:(double)progress; @end diff --git a/Sparkle/SUUnarchiver.m b/Sparkle/SUUnarchiver.m index 9b309a70..d719a8bd 100644 --- a/Sparkle/SUUnarchiver.m +++ b/Sparkle/SUUnarchiver.m @@ -15,12 +15,17 @@ #import "SUUnarchiver.h" #import "SUUnarchiver_Private.h" +@interface SUUnarchiver () +@property (strong) void (^completionBlock)(NSError * _Nullable); +@end + @implementation SUUnarchiver @synthesize archivePath; @synthesize updateHostBundlePath; @synthesize delegate; @synthesize decryptionPassword; +@synthesize completionBlock; + (SUUnarchiver *)unarchiverForPath:(NSString *)path updatingHostBundlePath:(NSString *)hostPath withPassword:(NSString *)decryptionPassword { @@ -34,9 +39,8 @@ - (NSString *)description { return [NSString stringWithFormat:@"%@ <%@>", [self class], self.archivePath]; } -- (void)start -{ - // No-op +- (void)unarchiveWithCompletionBlock:(void (^_Nonnull)(NSError * _Nullable))block { + self.completionBlock = block; } - (instancetype)initWithPath:(NSString *)path hostBundlePath:(NSString *)hostPath password:(NSString *)password @@ -70,18 +74,21 @@ - (void)unarchiverDidFinish { dispatch_async(dispatch_get_main_queue(), ^{ - if ([self.delegate respondsToSelector:@selector(unarchiverDidFinish:)]) { - [self.delegate unarchiverDidFinish:self]; - } + self.completionBlock(nil); }); } - (void)unarchiverDidFail:(NSError *)reason { + 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(), ^{ - if ([self.delegate respondsToSelector:@selector(unarchiverDidFail:)]) { - [self.delegate unarchiverDidFail:self]; - } + self.completionBlock(error); }); } diff --git a/Tests/SUUnarchiverTest.swift b/Tests/SUUnarchiverTest.swift index 0505e6c5..c2261077 100644 --- a/Tests/SUUnarchiverTest.swift +++ b/Tests/SUUnarchiverTest.swift @@ -18,18 +18,6 @@ class SUUnarchiverTest: XCTestCase, SUUnarchiverDelegate { } - 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 +37,13 @@ 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.delegate = self + unarchiver.unarchive {(error: Error?) -> Void in + self.unarchivedResult = nil == error; + self.unarchivedExpectation!.fulfill() + } super.waitForExpectations(timeout: 7.0, handler: nil) From 8103f5fd4339a6295a6dbec7e77b1f712ef58254 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kornel=20Lesin=CC=81ski?= Date: Wed, 21 Dec 2016 00:36:04 +0100 Subject: [PATCH 7/9] Replaced progress delegate with a block --- Sparkle/SUAutomaticUpdateDriver.h | 2 +- Sparkle/SUBasicUpdateDriver.h | 2 +- Sparkle/SUBasicUpdateDriver.m | 3 ++- Sparkle/SUBinaryDeltaUnarchiver.m | 4 ++-- Sparkle/SUDiskImageUnarchiver.m | 4 ++-- Sparkle/SUPipedUnarchiver.m | 6 +++--- Sparkle/SUUIBasedUpdateDriver.h | 2 +- Sparkle/SUUnarchiver.h | 9 +-------- Sparkle/SUUnarchiver.m | 14 ++++++++------ Sparkle/SUUnarchiver_Private.h | 2 +- Tests/SUUnarchiverTest.swift | 11 +++-------- 11 files changed, 25 insertions(+), 34 deletions(-) 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 002d82e2..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; diff --git a/Sparkle/SUBasicUpdateDriver.m b/Sparkle/SUBasicUpdateDriver.m index 76ea355c..6ee0aadd 100644 --- a/Sparkle/SUBasicUpdateDriver.m +++ b/Sparkle/SUBasicUpdateDriver.m @@ -335,7 +335,6 @@ if (!success) { [self unarchiverDidFail:nil]; } else { - unarchiver.delegate = self; [unarchiver unarchiveWithCompletionBlock:^(NSError *err){ if (err) { [self unarchiverDidFail:err]; @@ -344,6 +343,8 @@ assert(self.updateItem); [self installWithToolAndRelaunch:YES]; + } progressBlock:^(__unused double progress){ + }]; } } diff --git a/Sparkle/SUBinaryDeltaUnarchiver.m b/Sparkle/SUBinaryDeltaUnarchiver.m index f45cd26d..415c9033 100644 --- a/Sparkle/SUBinaryDeltaUnarchiver.m +++ b/Sparkle/SUBinaryDeltaUnarchiver.m @@ -79,9 +79,9 @@ } } -- (void)unarchiveWithCompletionBlock:(void (^)(NSError * _Nullable))block +- (void)unarchiveWithCompletionBlock:(void (^)(NSError * _Nullable))block progressBlock:(void (^ _Nullable)(double progress))progress { - [super unarchiveWithCompletionBlock:block]; + [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 e731e9f3..ae1987ef 100644 --- a/Sparkle/SUDiskImageUnarchiver.m +++ b/Sparkle/SUDiskImageUnarchiver.m @@ -159,9 +159,9 @@ } } -- (void)unarchiveWithCompletionBlock:(void (^)(NSError * _Nullable))block +- (void)unarchiveWithCompletionBlock:(void (^)(NSError * _Nullable))block progressBlock:(void (^ _Nullable)(double progress))progress { - [super unarchiveWithCompletionBlock:block]; + [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 320d150d..40739497 100644 --- a/Sparkle/SUPipedUnarchiver.m +++ b/Sparkle/SUPipedUnarchiver.m @@ -46,9 +46,9 @@ return NO; } -- (void)unarchiveWithCompletionBlock:(void (^)(NSError * _Nullable))block +- (void)unarchiveWithCompletionBlock:(void (^)(NSError * _Nullable))block progressBlock:(void (^ _Nullable)(double progress))progress { - [super unarchiveWithCompletionBlock:block]; + [super unarchiveWithCompletionBlock:block progressBlock:progress]; [NSThread detachNewThreadSelector:[[self class] selectorConformingToTypeOfPath:self.archivePath] toTarget:self withObject:nil]; } @@ -94,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); 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 486a3604..6e38a728 100644 --- a/Sparkle/SUUnarchiver.h +++ b/Sparkle/SUUnarchiver.h @@ -14,27 +14,20 @@ NS_ASSUME_NONNULL_BEGIN @class SUHost; -@protocol SUUnarchiverDelegate; @interface SUUnarchiver : NSObject @property (copy, readonly) NSString *archivePath; @property (copy, readonly) NSString *_Nullable updateHostBundlePath; @property (copy, readonly) NSString *_Nullable decryptionPassword; -@property (weak) _Nullable id delegate; + (nullable SUUnarchiver *)unarchiverForPath:(NSString *)path updatingHostBundlePath:(nullable NSString *)host withPassword:(nullable NSString *)decryptionPassword; + (BOOL)unsafeIfArchiveIsNotValidated; -- (void)unarchiveWithCompletionBlock:(void (^_Nonnull)(NSError *_Nullable))block; +- (void)unarchiveWithCompletionBlock:(void (^)(NSError *_Nullable))completion progressBlock:(void (^_Nullable)(double progress))progress; @end -@protocol SUUnarchiverDelegate -@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 d719a8bd..1f146c3f 100644 --- a/Sparkle/SUUnarchiver.m +++ b/Sparkle/SUUnarchiver.m @@ -17,15 +17,16 @@ @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; +@synthesize completionBlock, progressBlock; + (SUUnarchiver *)unarchiverForPath:(NSString *)path updatingHostBundlePath:(NSString *)hostPath withPassword:(NSString *)decryptionPassword { @@ -39,8 +40,9 @@ - (NSString *)description { return [NSString stringWithFormat:@"%@ <%@>", [self class], self.archivePath]; } -- (void)unarchiveWithCompletionBlock:(void (^_Nonnull)(NSError * _Nullable))block { +- (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 @@ -64,10 +66,10 @@ 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); } } diff --git a/Sparkle/SUUnarchiver_Private.h b/Sparkle/SUUnarchiver_Private.h index c7718e9f..b48792b9 100644 --- a/Sparkle/SUUnarchiver_Private.h +++ b/Sparkle/SUUnarchiver_Private.h @@ -18,7 +18,7 @@ + (BOOL)canUnarchivePath:(NSString *)path; - (instancetype)initWithPath:(NSString *)archive hostBundlePath:(NSString *)host; -- (void)notifyDelegateOfProgress:(double)progress; +- (void)notifyProgress:(double)progress; - (void)unarchiverDidFinish; - (void)unarchiverDidFailWithError:(NSError *)err; @end diff --git a/Tests/SUUnarchiverTest.swift b/Tests/SUUnarchiverTest.swift index c2261077..3e1d55ad 100644 --- a/Tests/SUUnarchiverTest.swift +++ b/Tests/SUUnarchiverTest.swift @@ -8,16 +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 unarchiveTestAppWithExtension(_ archiveExtension: String) { let appName = "SparkleTestCodeSignApp" @@ -39,11 +35,10 @@ class SUUnarchiverTest: XCTestCase, SUUnarchiverDelegate let unarchiver = SUUnarchiver(forPath: tempArchiveURL.path, updatingHostBundlePath: nil, withPassword: self.password)! - unarchiver.delegate = self - unarchiver.unarchive {(error: Error?) -> Void in + 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) From e3fb6bb1a83cd5baf3b16e584c6272b9b2a924d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kornel=20Lesin=CC=81ski?= Date: Wed, 21 Dec 2016 00:49:12 +0100 Subject: [PATCH 8/9] Report DMG extraction progress --- Sparkle/SUDiskImageUnarchiver.m | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Sparkle/SUDiskImageUnarchiver.m b/Sparkle/SUDiskImageUnarchiver.m index ae1987ef..c98725e9 100644 --- a/Sparkle/SUDiskImageUnarchiver.m +++ b/Sparkle/SUDiskImageUnarchiver.m @@ -90,6 +90,8 @@ [task launch]; + [self notifyProgress:0.125]; + [inputPipe.fileHandleForWriting writeData:promptData]; [inputPipe.fileHandleForWriting closeFile]; @@ -104,6 +106,8 @@ goto reportError; } + [self notifyProgress:0.5]; + if (taskResult != 0) { NSString *resultStr = output ? [[NSString alloc] initWithData:output encoding:NSUTF8StringEncoding] : nil; @@ -121,6 +125,9 @@ goto reportError; } + double itemsCopied = 0; + double totalItems = [contents count]; + for (NSString *item in contents) { NSString *fromPath = [mountPoint stringByAppendingPathComponent:item]; @@ -131,6 +138,8 @@ 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]) From 59d90e0bb1ce7ead7494ff3d99ddb9f74d395538 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kornel=20Lesin=CC=81ski?= Date: Wed, 21 Dec 2016 00:56:39 +0100 Subject: [PATCH 9/9] Basic binary delta progress --- Sparkle/SUBinaryDeltaApply.h | 2 +- Sparkle/SUBinaryDeltaApply.m | 17 ++++++++++++++++- Sparkle/SUBinaryDeltaUnarchiver.m | 4 +++- Tests/SUBinaryDeltaTest.m | 2 +- 4 files changed, 21 insertions(+), 4 deletions(-) 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 415c9033..bd9bdcc1 100644 --- a/Sparkle/SUBinaryDeltaUnarchiver.m +++ b/Sparkle/SUBinaryDeltaUnarchiver.m @@ -68,7 +68,9 @@ 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]; [self unarchiverDidFinish]; 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);