diff --git a/Sparkle.xcodeproj/project.pbxproj b/Sparkle.xcodeproj/project.pbxproj index a47dd397..53d8c170 100644 --- a/Sparkle.xcodeproj/project.pbxproj +++ b/Sparkle.xcodeproj/project.pbxproj @@ -176,6 +176,8 @@ 721CF1AB1AD764EB00D9AC09 /* libbz2.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 5D06E8FB0FD68D61005AE3F6 /* libbz2.dylib */; }; 7223E7631AD1AEFF008E3161 /* sais.c in Sources */ = {isa = PBXBuildFile; fileRef = 7223E7611AD1AEFF008E3161 /* sais.c */; }; 722589D71E0AD86B005EA0B9 /* SUUnarchiverProtocol.h in Headers */ = {isa = PBXBuildFile; fileRef = 722589D61E0AD86B005EA0B9 /* SUUnarchiverProtocol.h */; }; + 722589DA1E0B19F4005EA0B9 /* SUUnarchiverNotifier.h in Headers */ = {isa = PBXBuildFile; fileRef = 722589D81E0B19F4005EA0B9 /* SUUnarchiverNotifier.h */; }; + 722589DB1E0B19F4005EA0B9 /* SUUnarchiverNotifier.m in Sources */ = {isa = PBXBuildFile; fileRef = 722589D91E0B19F4005EA0B9 /* SUUnarchiverNotifier.m */; }; 722954B71D04ADAF00ECF9CA /* fileop.m in Sources */ = {isa = PBXBuildFile; fileRef = 722954B61D04ADAF00ECF9CA /* fileop.m */; }; 722954BC1D04AE1100ECF9CA /* SUFileManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 7275F9C01B5F1F2900B1D19E /* SUFileManager.m */; }; 722954BD1D04AE3800ECF9CA /* SUConstants.m in Sources */ = {isa = PBXBuildFile; fileRef = 61299A5F09CA6EB100B7442F /* SUConstants.m */; }; @@ -675,6 +677,8 @@ 7223E7611AD1AEFF008E3161 /* sais.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = sais.c; sourceTree = ""; }; 7223E7621AD1AEFF008E3161 /* sais.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = sais.h; sourceTree = ""; }; 722589D61E0AD86B005EA0B9 /* SUUnarchiverProtocol.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUUnarchiverProtocol.h; sourceTree = ""; }; + 722589D81E0B19F4005EA0B9 /* SUUnarchiverNotifier.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUUnarchiverNotifier.h; sourceTree = ""; }; + 722589D91E0B19F4005EA0B9 /* SUUnarchiverNotifier.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SUUnarchiverNotifier.m; sourceTree = ""; }; 722954B41D04ADAF00ECF9CA /* fileop */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = fileop; sourceTree = BUILT_PRODUCTS_DIR; }; 722954B61D04ADAF00ECF9CA /* fileop.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = fileop.m; sourceTree = ""; }; 722954C21D04E66F00ECF9CA /* SUFileOperationConstants.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUFileOperationConstants.h; sourceTree = ""; }; @@ -1018,6 +1022,8 @@ 722589D61E0AD86B005EA0B9 /* SUUnarchiverProtocol.h */, 61299A8B09CA790200B7442F /* SUUnarchiver.h */, 61299A8C09CA790200B7442F /* SUUnarchiver.m */, + 722589D81E0B19F4005EA0B9 /* SUUnarchiverNotifier.h */, + 722589D91E0B19F4005EA0B9 /* SUUnarchiverNotifier.m */, ); name = Unarchiving; sourceTree = ""; @@ -1251,6 +1257,7 @@ 611142E910FB1BE5009810AA /* bspatch.h in Headers */, 61299B3609CB04E000B7442F /* Sparkle.h in Headers */, 61CFB3290E385186007A1735 /* Sparkle.pch in Headers */, + 722589DA1E0B19F4005EA0B9 /* SUUnarchiverNotifier.h in Headers */, 61B5FC0D09C4FC8200B25A18 /* SUAppcast.h in Headers */, 61B5FC7009C51F4A00B25A18 /* SUAppcastItem.h in Headers */, 6120721209CC5C4B007FE0F6 /* SUAutomaticUpdateAlert.h in Headers */, @@ -1813,6 +1820,7 @@ 5D06E8EC0FD68CE4005AE3F6 /* SUBinaryDeltaApply.m in Sources */, 5D06E8ED0FD68CE4005AE3F6 /* SUBinaryDeltaCommon.m in Sources */, 5D06E93A0FD69271005AE3F6 /* SUBinaryDeltaUnarchiver.m in Sources */, + 722589DB1E0B19F4005EA0B9 /* SUUnarchiverNotifier.m in Sources */, 61B078CF15A5FB6100600039 /* SUCodeSigningVerifier.m in Sources */, 61299A6009CA6EB100B7442F /* SUConstants.m in Sources */, 6102FE4B0E07803800F85D09 /* SUDiskImageUnarchiver.m in Sources */, diff --git a/Sparkle/SUAutomaticUpdateDriver.h b/Sparkle/SUAutomaticUpdateDriver.h index 8893be2d..d3fae435 100644 --- a/Sparkle/SUAutomaticUpdateDriver.h +++ b/Sparkle/SUAutomaticUpdateDriver.h @@ -14,7 +14,7 @@ #import "SUAutomaticUpdateAlert.h" #import "SUUnarchiverProtocol.h" -@interface SUAutomaticUpdateDriver : SUBasicUpdateDriver +@interface SUAutomaticUpdateDriver : SUBasicUpdateDriver @end diff --git a/Sparkle/SUBasicUpdateDriver.h b/Sparkle/SUBasicUpdateDriver.h index 7fac32f8..0e61f5f3 100644 --- a/Sparkle/SUBasicUpdateDriver.h +++ b/Sparkle/SUBasicUpdateDriver.h @@ -16,7 +16,7 @@ #import "SUUnarchiverProtocol.h" @class SUAppcastItem, SUHost; -@interface SUBasicUpdateDriver : SUUpdateDriver +@interface SUBasicUpdateDriver : SUUpdateDriver @property (strong, readonly) SUAppcastItem *updateItem; @property (strong, readonly) NSURLDownload *download; @@ -38,8 +38,6 @@ - (void)download:(NSURLDownload *)download didFailWithError:(NSError *)error; - (void)extractUpdate; -- (void)unarchiverDidFinish; -- (void)unarchiverDidFail; - (void)failedToApplyDeltaUpdate; - (void)installWithToolAndRelaunch:(BOOL)relaunch; diff --git a/Sparkle/SUBasicUpdateDriver.m b/Sparkle/SUBasicUpdateDriver.m index 73cd75e2..e4b0e39c 100644 --- a/Sparkle/SUBasicUpdateDriver.m +++ b/Sparkle/SUBasicUpdateDriver.m @@ -316,7 +316,7 @@ - (void)extractUpdate { - id unarchiver = [SUUnarchiver unarchiverForPath:self.downloadPath updatingHostBundlePath:self.host.bundlePath decryptionPassword:self.updater.decryptionPassword delegate:self]; + id unarchiver = [SUUnarchiver unarchiverForPath:self.downloadPath updatingHostBundlePath:self.host.bundlePath decryptionPassword:self.updater.decryptionPassword]; BOOL success; if (!unarchiver) { @@ -333,9 +333,18 @@ } if (!success) { - [self unarchiverDidFail]; +#warning this is bad + [self unarchiverDidFail:nil]; } else { - [unarchiver start]; + [unarchiver unarchiveWithCompletionBlock:^(NSError *err){ + if (err) { + [self unarchiverDidFail:err]; + return; + } + + assert(self.updateItem); + [self installWithToolAndRelaunch:YES]; + } progressBlock:nil]; } } @@ -355,7 +364,7 @@ [self installWithToolAndRelaunch:YES]; } -- (void)unarchiverDidFail +- (void)unarchiverDidFail:(NSError *)err { // No longer needed self.updateValidator = nil; @@ -365,7 +374,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.h b/Sparkle/SUBinaryDeltaUnarchiver.h index d7e0b7b6..ba79a047 100644 --- a/Sparkle/SUBinaryDeltaUnarchiver.h +++ b/Sparkle/SUBinaryDeltaUnarchiver.h @@ -16,7 +16,7 @@ NS_ASSUME_NONNULL_BEGIN @interface SUBinaryDeltaUnarchiver : NSObject -- (instancetype)initWithArchivePath:(NSString *)archivePath updateHostBundlePath:(NSString *)updateHostBundlePath delegate:(nullable id )delegate; +- (instancetype)initWithArchivePath:(NSString *)archivePath updateHostBundlePath:(NSString *)updateHostBundlePath; @end diff --git a/Sparkle/SUBinaryDeltaUnarchiver.m b/Sparkle/SUBinaryDeltaUnarchiver.m index 666bff83..6dc2a666 100644 --- a/Sparkle/SUBinaryDeltaUnarchiver.m +++ b/Sparkle/SUBinaryDeltaUnarchiver.m @@ -7,6 +7,7 @@ // #import "SUBinaryDeltaUnarchiver.h" +#import "SUUnarchiverNotifier.h" #import "SUBinaryDeltaCommon.h" #import "SUBinaryDeltaApply.h" #import "SULog.h" @@ -16,7 +17,6 @@ @property (nonatomic, copy, readonly) NSString *archivePath; @property (nonatomic, copy, readonly) NSString *updateHostBundlePath; -@property (nonatomic, weak, readonly) id delegate; @end @@ -24,7 +24,6 @@ @synthesize archivePath = _archivePath; @synthesize updateHostBundlePath = _updateHostBundlePath; -@synthesize delegate = _delegate; + (BOOL)canUnarchivePath:(NSString *)path { @@ -71,44 +70,46 @@ } } -- (instancetype)initWithArchivePath:(NSString *)archivePath updateHostBundlePath:(NSString *)updateHostBundlePath delegate:(id )delegate +- (instancetype)initWithArchivePath:(NSString *)archivePath updateHostBundlePath:(NSString *)updateHostBundlePath { self = [super init]; if (self != nil) { _archivePath = [archivePath copy]; _updateHostBundlePath = [updateHostBundlePath copy]; - _delegate = delegate; } return self; } -- (void)start +- (void)unarchiveWithCompletionBlock:(void (^)(NSError * _Nullable))completionBlock progressBlock:(void (^ _Nullable)(double))progressBlock { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ @autoreleasepool { - NSString *sourcePath = self.updateHostBundlePath; - NSString *targetPath = [[self.archivePath stringByDeletingLastPathComponent] stringByAppendingPathComponent:[sourcePath lastPathComponent]]; - - NSError *applyDiffError = nil; - BOOL success = applyBinaryDelta(sourcePath, targetPath, self.archivePath, NO, &applyDiffError); - - if (success) { - [[self class] updateSpotlightImportersAtBundlePath:targetPath]; - - dispatch_async(dispatch_get_main_queue(), ^{ - [self.delegate unarchiverDidFinish]; - }); - } - else { - SULog(@"Applying delta patch failed with error: %@", applyDiffError); - dispatch_async(dispatch_get_main_queue(), ^{ - [self.delegate unarchiverDidFail]; - }); - } + SUUnarchiverNotifier *notifier = [[SUUnarchiverNotifier alloc] initWithCompletionBlock:completionBlock progressBlock:progressBlock]; + [self extractDeltaWithNotifier:notifier]; } }); } +- (void)extractDeltaWithNotifier:(SUUnarchiverNotifier *)notifier +{ + NSString *sourcePath = self.updateHostBundlePath; + NSString *targetPath = [[self.archivePath stringByDeletingLastPathComponent] stringByAppendingPathComponent:[sourcePath lastPathComponent]]; + + NSError *applyDiffError = nil; + BOOL success = applyBinaryDelta(sourcePath, targetPath, self.archivePath, NO, ^(double progress){ + [notifier notifyProgress:progress]; + + }, &applyDiffError); + + if (success) { + [[self class] updateSpotlightImportersAtBundlePath:targetPath]; + [notifier notifySuccess]; + } + else { + [notifier notifyFailureWithError:applyDiffError]; + } +} + - (NSString *)description { return [NSString stringWithFormat:@"%@ <%@>", [self class], self.archivePath]; } @end diff --git a/Sparkle/SUDiskImageUnarchiver.h b/Sparkle/SUDiskImageUnarchiver.h index d39f68b3..f0de0462 100644 --- a/Sparkle/SUDiskImageUnarchiver.h +++ b/Sparkle/SUDiskImageUnarchiver.h @@ -16,7 +16,7 @@ NS_ASSUME_NONNULL_BEGIN @interface SUDiskImageUnarchiver : NSObject -- (instancetype)initWithArchivePath:(NSString *)archivePath decryptionPassword:(nullable NSString *)decryptionPassword delegate:(nullable id )delegate; +- (instancetype)initWithArchivePath:(NSString *)archivePath decryptionPassword:(nullable NSString *)decryptionPassword; @end diff --git a/Sparkle/SUDiskImageUnarchiver.m b/Sparkle/SUDiskImageUnarchiver.m index 7c8fc441..60f59780 100644 --- a/Sparkle/SUDiskImageUnarchiver.m +++ b/Sparkle/SUDiskImageUnarchiver.m @@ -7,13 +7,13 @@ // #import "SUDiskImageUnarchiver.h" +#import "SUUnarchiverNotifier.h" #import "SULog.h" @interface SUDiskImageUnarchiver () @property (nonatomic, copy, readonly) NSString *archivePath; @property (nullable, nonatomic, copy, readonly) NSString *decryptionPassword; -@property (nonatomic, weak, readonly) id delegate; @end @@ -21,7 +21,6 @@ @synthesize archivePath = _archivePath; @synthesize decryptionPassword = _decryptionPassword; -@synthesize delegate = _delegate; + (BOOL)canUnarchivePath:(NSString *)path { @@ -33,21 +32,28 @@ return NO; } -- (instancetype)initWithArchivePath:(NSString *)archivePath decryptionPassword:(nullable NSString *)decryptionPassword delegate:(nullable id )delegate +- (instancetype)initWithArchivePath:(NSString *)archivePath decryptionPassword:(nullable NSString *)decryptionPassword { self = [super init]; if (self != nil) { _archivePath = [archivePath copy]; _decryptionPassword = [decryptionPassword copy]; - _delegate = delegate; } return self; } -// Called on a non-main thread. -- (void)extractDMG +- (void)unarchiveWithCompletionBlock:(void (^)(NSError * _Nullable))completionBlock progressBlock:(void (^ _Nullable)(double))progressBlock { - @autoreleasepool { + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ + SUUnarchiverNotifier *notifier = [[SUUnarchiverNotifier alloc] initWithCompletionBlock:completionBlock progressBlock:progressBlock]; + [self extractDMGWithNotifier:notifier]; + }); +} + +// Called on a non-main thread. +- (void)extractDMGWithNotifier:(SUUnarchiverNotifier *)notifier +{ + @autoreleasepool { BOOL mountedSuccessfully = NO; // get a unique mount point path @@ -55,17 +61,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.delegate unarchiverDidFail]; - }; - dispatch_block_t delegateSuccess = ^{ - [self.delegate unarchiverDidFinish]; - }; 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. CFUUIDRef uuid = CFUUIDCreate(NULL); @@ -117,6 +114,8 @@ [task launch]; + [notifier notifyProgress:0.125]; + [inputPipe.fileHandleForWriting writeData:promptData]; [inputPipe.fileHandleForWriting closeFile]; @@ -131,8 +130,10 @@ goto reportError; } - if (taskResult != 0) - { + [notifier notifyProgress:0.5]; + + if (taskResult != 0) + { NSString *resultStr = output ? [[NSString alloc] initWithData:output encoding:NSUTF8StringEncoding] : nil; SULog(@"hdiutil failed with code: %ld data: <<%@>>", (long)taskResult, resultStr); goto reportError; @@ -147,9 +148,12 @@ SULog(@"Couldn't enumerate contents of archive mounted at %@: %@", mountPoint, error); goto reportError; } - - for (NSString *item in contents) - { + + double itemsCopied = 0; + double totalItems = [contents count]; + + for (NSString *item in contents) + { NSString *fromPath = [mountPoint stringByAppendingPathComponent:item]; NSString *toPath = [[self.archivePath stringByDeletingLastPathComponent] stringByAppendingPathComponent:item]; @@ -158,21 +162,22 @@ continue; } + itemsCopied += 1.0; + [notifier 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 : @""); + + if (![manager copyItemAtPath:fromPath toPath:toPath error:&error]) + { goto reportError; } } - dispatch_async(dispatch_get_main_queue(), delegateSuccess); + [notifier notifySuccess]; goto finally; reportError: - dispatch_async(dispatch_get_main_queue(), delegateFailure); - + [notifier notifyFailureWithError:error]; + finally: if (mountedSuccessfully) { NSTask *task = [[NSTask alloc] init]; @@ -193,13 +198,6 @@ } } -- (void)start -{ - dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ - [self extractDMG]; - }); -} - - (NSString *)description { return [NSString stringWithFormat:@"%@ <%@>", [self class], self.archivePath]; } @end diff --git a/Sparkle/SUPipedUnarchiver.h b/Sparkle/SUPipedUnarchiver.h index caec2b9e..6e9c682f 100644 --- a/Sparkle/SUPipedUnarchiver.h +++ b/Sparkle/SUPipedUnarchiver.h @@ -16,7 +16,7 @@ NS_ASSUME_NONNULL_BEGIN @interface SUPipedUnarchiver : NSObject -- (instancetype)initWithArchivePath:(NSString *)archivePath delegate:(nullable id )delegate; +- (instancetype)initWithArchivePath:(NSString *)archivePath; @end diff --git a/Sparkle/SUPipedUnarchiver.m b/Sparkle/SUPipedUnarchiver.m index 0b2a7b85..971886a4 100644 --- a/Sparkle/SUPipedUnarchiver.m +++ b/Sparkle/SUPipedUnarchiver.m @@ -7,19 +7,18 @@ // #import "SUPipedUnarchiver.h" +#import "SUUnarchiverNotifier.h" #import "SULog.h" @interface SUPipedUnarchiver () @property (nonatomic, copy, readonly) NSString *archivePath; -@property (nonatomic, weak, readonly) id delegate; @end @implementation SUPipedUnarchiver @synthesize archivePath = _archivePath; -@synthesize delegate = _delegate; + (nullable NSArray *)commandAndArgumentsConformingToTypeOfPath:(NSString *)path { @@ -64,17 +63,16 @@ return NO; } -- (instancetype)initWithArchivePath:(NSString *)archivePath delegate:(nullable id )delegate +- (instancetype)initWithArchivePath:(NSString *)archivePath { self = [super init]; if (self != nil) { _archivePath = [archivePath copy]; - _delegate = delegate; } return self; } -- (void)start +- (void)unarchiveWithCompletionBlock:(void (^)(NSError * _Nullable))completionBlock progressBlock:(void (^ _Nullable)(double))progressBlock { NSArray *commandAndArguments = [[self class] commandAndArgumentsConformingToTypeOfPath:self.archivePath]; assert(commandAndArguments != nil); @@ -85,16 +83,17 @@ NSArray *arguments = [commandAndArguments subarrayWithRange:NSMakeRange(1, commandAndArguments.count - 1)]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ - [self extractArchivePipingDataToCommand:command arguments:arguments]; + SUUnarchiverNotifier *notifier = [[SUUnarchiverNotifier alloc] initWithCompletionBlock:completionBlock progressBlock:progressBlock]; + [self extractArchivePipingDataToCommand:command arguments:arguments notifier:notifier]; }); } // This method abstracts the types that use a command line tool piping data from stdin. -- (void)extractArchivePipingDataToCommand:(NSString *)command arguments:(NSArray*)args +- (void)extractArchivePipingDataToCommand:(NSString *)command arguments:(NSArray*)args notifier:(SUUnarchiverNotifier *)notifier { // *** GETS CALLED ON NON-MAIN THREAD!!! - @autoreleasepool { - + @autoreleasepool { + NSError *error = nil; NSString *destination = [self.archivePath stringByDeletingLastPathComponent]; SULog(@"Extracting using '%@' '%@' < '%@' '%@'", command, [args componentsJoinedByString:@"' '"], self.archivePath, destination); @@ -125,11 +124,7 @@ } bytesRead += len; [archiveOutput writeData:data]; - dispatch_async(dispatch_get_main_queue(), ^{ - if ([self.delegate respondsToSelector:@selector(unarchiverExtractedProgress:)]) { - [self.delegate unarchiverExtractedProgress:(double)bytesRead / (double)expectedLength]; - } - }); + [notifier notifyProgress:(double)bytesRead / (double)expectedLength]; } while(bytesRead < expectedLength); @@ -139,23 +134,19 @@ if ([task terminationStatus] == 0) { if (bytesRead == expectedLength) { - dispatch_async(dispatch_get_main_queue(), ^{ - [self.delegate unarchiverDidFinish]; - }); + [notifier notifySuccess]; 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.delegate unarchiverDidFail]; - }); + [notifier notifyFailureWithError:error]; } } diff --git a/Sparkle/SUUIBasedUpdateDriver.h b/Sparkle/SUUIBasedUpdateDriver.h index 1018ca99..e15dc5b9 100644 --- a/Sparkle/SUUIBasedUpdateDriver.h +++ b/Sparkle/SUUIBasedUpdateDriver.h @@ -16,7 +16,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 b732c313..a52bf5a2 100644 --- a/Sparkle/SUUnarchiver.h +++ b/Sparkle/SUUnarchiver.h @@ -11,11 +11,10 @@ NS_ASSUME_NONNULL_BEGIN @protocol SUUnarchiverProtocol; -@protocol SUUnarchiverDelegate; @interface SUUnarchiver : NSObject -+ (nullable id )unarchiverForPath:(NSString *)path updatingHostBundlePath:(nullable NSString *)hostPath decryptionPassword:(nullable NSString *)decryptionPassword delegate:(nullable id )delegate; ++ (nullable id )unarchiverForPath:(NSString *)path updatingHostBundlePath:(nullable NSString *)hostPath decryptionPassword:(nullable NSString *)decryptionPassword; @end diff --git a/Sparkle/SUUnarchiver.m b/Sparkle/SUUnarchiver.m index 2d9b204c..7b808814 100644 --- a/Sparkle/SUUnarchiver.m +++ b/Sparkle/SUUnarchiver.m @@ -14,19 +14,18 @@ @implementation SUUnarchiver -+ (nullable id )unarchiverForPath:(NSString *)path updatingHostBundlePath:(nullable NSString *)hostPath decryptionPassword:(nullable NSString *)decryptionPassword delegate:(nullable id )delegate ++ (nullable id )unarchiverForPath:(NSString *)path updatingHostBundlePath:(nullable NSString *)hostPath decryptionPassword:(nullable NSString *)decryptionPassword { if ([SUPipedUnarchiver canUnarchivePath:path]) { - return [[SUPipedUnarchiver alloc] initWithArchivePath:path delegate:delegate]; + return [[SUPipedUnarchiver alloc] initWithArchivePath:path]; } else if ([SUDiskImageUnarchiver canUnarchivePath:path]) { - return [[SUDiskImageUnarchiver alloc] initWithArchivePath:path decryptionPassword:decryptionPassword delegate:delegate]; + return [[SUDiskImageUnarchiver alloc] initWithArchivePath:path decryptionPassword:decryptionPassword]; } else if ([SUBinaryDeltaUnarchiver canUnarchivePath:path]) { assert(hostPath != nil); NSString *nonNullHostPath = hostPath; - return [[SUBinaryDeltaUnarchiver alloc] initWithArchivePath:path updateHostBundlePath:nonNullHostPath delegate:delegate]; - + return [[SUBinaryDeltaUnarchiver alloc] initWithArchivePath:path updateHostBundlePath:nonNullHostPath]; } return nil; } diff --git a/Sparkle/SUUnarchiverNotifier.h b/Sparkle/SUUnarchiverNotifier.h new file mode 100644 index 00000000..9c8b65a8 --- /dev/null +++ b/Sparkle/SUUnarchiverNotifier.h @@ -0,0 +1,25 @@ +// +// SUUnarchiverNotifier.h +// Sparkle +// +// Created by Mayur Pawashe on 12/21/16. +// Copyright © 2016 Sparkle Project. All rights reserved. +// + +#import + +NS_ASSUME_NONNULL_BEGIN + +@interface SUUnarchiverNotifier : NSObject + +- (instancetype)initWithCompletionBlock:(void (^)(NSError * _Nullable))completionBlock progressBlock:(void (^ _Nullable)(double))progressBlock; + +- (void)notifySuccess; + +- (void)notifyFailureWithError:(NSError * _Nullable)reason; + +- (void)notifyProgress:(double)progress; + +@end + +NS_ASSUME_NONNULL_END diff --git a/Sparkle/SUUnarchiverNotifier.m b/Sparkle/SUUnarchiverNotifier.m new file mode 100644 index 00000000..ecd6c295 --- /dev/null +++ b/Sparkle/SUUnarchiverNotifier.m @@ -0,0 +1,63 @@ +// +// SUUnarchiverNotifier.m +// Sparkle +// +// Created by Mayur Pawashe on 12/21/16. +// Copyright © 2016 Sparkle Project. All rights reserved. +// + +#import "SUUnarchiverNotifier.h" + +@interface SUUnarchiverNotifier () + +@property (nonatomic, readonly, copy) void (^completionBlock)(NSError * _Nullable); +@property (nonatomic, readonly, copy) void (^ _Nullable progressBlock)(double); + +@end + +@implementation SUUnarchiverNotifier + +@synthesize completionBlock = _completionBlock; +@synthesize progressBlock = _progressBlock; + +- (instancetype)initWithCompletionBlock:(void (^)(NSError * _Nullable))completionBlock progressBlock:(void (^ _Nullable)(double))progressBlock +{ + self = [super init]; + if (self != nil) { + _completionBlock = [completionBlock copy]; + _progressBlock = [progressBlock copy]; + } + return self; +} + +- (void)notifySuccess +{ + dispatch_async(dispatch_get_main_queue(), ^{ + self.completionBlock(nil); + }); +} + +- (void)notifyFailureWithError:(NSError * _Nullable)reason +{ + NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithObject:SULocalizedString(@"An error occurred while extracting the archive. Please try again later.", nil) forKey:NSLocalizedDescriptionKey]; + if (reason) { + [userInfo setObject:(NSError * _Nonnull)reason forKey:NSUnderlyingErrorKey]; + } + + NSError *error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUUnarchivingError userInfo:userInfo]; + + dispatch_async(dispatch_get_main_queue(), ^{ + self.completionBlock(error); + }); +} + +- (void)notifyProgress:(double)progress +{ + if (self.progressBlock != nil) { + dispatch_async(dispatch_get_main_queue(), ^{ + self.progressBlock(progress); + }); + } +} + +@end diff --git a/Sparkle/SUUnarchiverProtocol.h b/Sparkle/SUUnarchiverProtocol.h index 5a349877..25a7797d 100644 --- a/Sparkle/SUUnarchiverProtocol.h +++ b/Sparkle/SUUnarchiverProtocol.h @@ -16,21 +16,10 @@ NS_ASSUME_NONNULL_BEGIN + (BOOL)unsafeIfArchiveIsNotValidated; -- (void)start; +- (void)unarchiveWithCompletionBlock:(void (^)(NSError * _Nullable))completionBlock progressBlock:(void (^ _Nullable)(double))progressBlock; - (NSString *)description; @end -@protocol SUUnarchiverDelegate - -- (void)unarchiverDidFinish; - -- (void)unarchiverDidFail; - -@optional -- (void)unarchiverExtractedProgress:(double)progress; - -@end - NS_ASSUME_NONNULL_END 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 9524f010..0a135e26 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() - { - self.unarchivedResult = false - self.unarchivedExpectation!.fulfill() - } - - func unarchiverDidFinish() - { - self.unarchivedResult = true - self.unarchivedExpectation!.fulfill() - } - func unarchiveTestAppWithExtension(_ archiveExtension: String) { let appName = "SparkleTestCodeSignApp" @@ -49,8 +33,12 @@ class SUUnarchiverTest: XCTestCase, SUUnarchiverDelegate self.unarchivedExpectation = super.expectation(description: "Unarchived Application (format: \(archiveExtension))") - let unarchiver = SUUnarchiver.unarchiver(forPath: tempArchiveURL.path, updatingHostBundlePath: nil, decryptionPassword: self.password, delegate: self) - unarchiver!.start() + let unarchiver = SUUnarchiver.unarchiver(forPath: tempArchiveURL.path, updatingHostBundlePath: nil, decryptionPassword: self.password)! + + unarchiver.unarchive(completionBlock: {(error: Error?) -> Void in + self.unarchivedResult = (nil == error) + self.unarchivedExpectation!.fulfill() + }, progressBlock: nil); super.waitForExpectations(timeout: 7.0, handler: nil)