Merge pull request #938 from sparkle-project/unarch

Unarchiving blocks
This commit is contained in:
Kornel authored and GitHub committed 2016-12-21 17:07:19 +01:00
commit 301af71228
14 files changed
+109 -113

No files matched your search

+1 -1
View File
@@ -13,7 +13,7 @@
#import "SUBasicUpdateDriver.h"
#import "SUAutomaticUpdateAlert.h"
@interface SUAutomaticUpdateDriver : SUBasicUpdateDriver <SUUnarchiverDelegate>
@interface SUAutomaticUpdateDriver : SUBasicUpdateDriver
@end
+1 -3
View File
@@ -15,7 +15,7 @@
#import "SUAppcast.h"
@class SUAppcastItem, SUHost;
@interface SUBasicUpdateDriver : SUUpdateDriver <NSURLDownloadDelegate, SUUnarchiverDelegate>
@interface SUBasicUpdateDriver : SUUpdateDriver <NSURLDownloadDelegate>
@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;
+13 -11
View File
@@ -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
+1 -1
View File
@@ -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
+16 -1
View File
@@ -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");
}
+7 -10
View File
@@ -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];
});
+15 -22
View File
@@ -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];
});
+10 -12
View File
@@ -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];
}
}
+1 -1
View File
@@ -15,7 +15,7 @@
@class SUStatusController;
@interface SUUIBasedUpdateDriver : SUBasicUpdateDriver <SUUnarchiverDelegate>
@interface SUUIBasedUpdateDriver : SUBasicUpdateDriver
- (void)showAlert:(NSAlert *)alert;
- (IBAction)cancelDownload:(id)sender;
+7 -13
View File
@@ -11,29 +11,23 @@
#import <Foundation/Foundation.h>
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<SUUnarchiverDelegate> 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 <NSObject>
- (void)unarchiverDidFinish:(SUUnarchiver *)unarchiver;
- (void)unarchiverDidFail:(SUUnarchiver *)unarchiver;
@optional
- (void)unarchiver:(SUUnarchiver *)unarchiver extractedProgress:(double)progress;
@end
NS_ASSUME_NONNULL_END
#endif
+27 -14
View File
@@ -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;
+3 -3
View File
@@ -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
+1 -1
View File
@@ -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);
+6 -20
View File
@@ -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)