From d92d385431b3b6a14a8719898965ad9f2de9a7a2 Mon Sep 17 00:00:00 2001 From: joshaber Date: Mon, 13 Jun 2016 18:09:24 -0700 Subject: [PATCH 1/2] Don't error if our conditional GET fails. --- Squirrel/SQRLUpdater.m | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/Squirrel/SQRLUpdater.m b/Squirrel/SQRLUpdater.m index fc7bd824..fedbeba7 100644 --- a/Squirrel/SQRLUpdater.m +++ b/Squirrel/SQRLUpdater.m @@ -304,16 +304,27 @@ - (RACSignal *)downloadAndPrepareUpdate:(SQRLUpdate *)update { return [[[self uniqueTemporaryDirectoryForUpdate] flattenMap:^(NSURL *downloadDirectory) { + void (^cleanUp)(void) = ^{ + NSError *error; + if (![NSFileManager.defaultManager removeItemAtURL:downloadDirectory error:&error]) { + NSLog(@"Error removing temporary download directory at %@: %@", downloadDirectory, error.sqrl_verboseDescription); + } + }; + return [[[self downloadBundleForUpdate:update intoDirectory:downloadDirectory] flattenMap:^(NSBundle *updateBundle) { + // If the bundle is nil it means our conditional GET told us + // we already downloaded the update. So just clean up. + if (updateBundle == nil) { + cleanUp(); + return [RACSignal empty]; + } + return [self verifyAndPrepareUpdate:update fromBundle:updateBundle]; }] doError:^(id _) { - NSError *error = nil; - if (![NSFileManager.defaultManager removeItemAtURL:downloadDirectory error:&error]) { - NSLog(@"Error removing temporary download directory at %@: %@", downloadDirectory, error.sqrl_verboseDescription); - } + cleanUp(); }]; }] setNameWithFormat:@"%@ -downloadAndPrepareUpdate: %@", self, update]; @@ -339,7 +350,7 @@ - (RACSignal *)downloadBundleForUpdate:(SQRLUpdate *)update intoDirectory:(NSURL NSHTTPURLResponse *httpResponse = (id)response; if (httpResponse.statusCode == 304 /* Not Modified */) { - return [RACSignal empty]; + return [RACSignal return:nil]; } if (!(httpResponse.statusCode >= 200 && httpResponse.statusCode <= 299)) { @@ -359,6 +370,8 @@ - (RACSignal *)downloadBundleForUpdate:(SQRLUpdate *)update intoDirectory:(NSURL }] flatten] flattenMap:^(NSData *data) { + if (data == nil) return [RACSignal return:nil]; + NSURL *zipOutputURL = [downloadDirectory URLByAppendingPathComponent:zipDownloadURL.lastPathComponent]; NSError *error = nil; @@ -370,11 +383,17 @@ - (RACSignal *)downloadBundleForUpdate:(SQRLUpdate *)update intoDirectory:(NSURL }]; }] doNext:^(NSURL *zipOutputURL) { + if (zipOutputURL == nil) return; + NSLog(@"Download completed to: %@", zipOutputURL); }] flattenMap:^(NSURL *zipOutputURL) { - return [[SQRLZipArchiver + if (zipOutputURL == nil) return [RACSignal return:nil]; + + return [[[[SQRLZipArchiver unzipArchiveAtURL:zipOutputURL intoDirectoryAtURL:downloadDirectory] + ignoreValues] + concat:[RACSignal return:zipOutputURL]] doCompleted:^{ NSError *error = nil; if (![NSFileManager.defaultManager removeItemAtURL:zipOutputURL error:&error]) { @@ -382,7 +401,9 @@ - (RACSignal *)downloadBundleForUpdate:(SQRLUpdate *)update intoDirectory:(NSURL } }]; }] - then:^{ + flattenMap:^(NSURL *zipOutputURL) { + if (zipOutputURL == nil) return [RACSignal return:nil]; + return [self updateBundleMatchingCurrentApplicationInDirectory:downloadDirectory]; }] setNameWithFormat:@"%@ -downloadBundleForUpdate: %@ intoDirectory: %@", self, update, downloadDirectory]; From 413b1d2bca22e4c73d88989c40dbbe5d36c8e9c7 Mon Sep 17 00:00:00 2001 From: joshaber Date: Tue, 14 Jun 2016 15:37:24 -0700 Subject: [PATCH 2/2] Reorganize to separate out the unarchiving --- Squirrel/SQRLUpdater.m | 76 ++++++++++++++++++++---------------------- 1 file changed, 36 insertions(+), 40 deletions(-) diff --git a/Squirrel/SQRLUpdater.m b/Squirrel/SQRLUpdater.m index fedbeba7..2c860bb1 100644 --- a/Squirrel/SQRLUpdater.m +++ b/Squirrel/SQRLUpdater.m @@ -330,11 +330,43 @@ - (RACSignal *)downloadAndPrepareUpdate:(SQRLUpdate *)update { setNameWithFormat:@"%@ -downloadAndPrepareUpdate: %@", self, update]; } +- (RACSignal *)unarchiveAndPrepareData:(NSData *)data withName:(NSString *)name intoDirectory:(NSURL *)downloadDirectory { + return [[[[[RACSignal + defer:^{ + NSURL *zipOutputURL = [downloadDirectory URLByAppendingPathComponent:name]; + NSError *error = nil; + if ([data writeToURL:zipOutputURL options:NSDataWritingAtomic error:&error]) { + return [RACSignal return:zipOutputURL]; + } else { + return [RACSignal error:error]; + } + }] + doNext:^(NSURL *zipOutputURL) { + NSLog(@"Download completed to: %@", zipOutputURL); + }] + flattenMap:^(NSURL *zipOutputURL) { + return [[[[SQRLZipArchiver + unzipArchiveAtURL:zipOutputURL intoDirectoryAtURL:downloadDirectory] + ignoreValues] + concat:[RACSignal return:zipOutputURL]] + doCompleted:^{ + NSError *error = nil; + if (![NSFileManager.defaultManager removeItemAtURL:zipOutputURL error:&error]) { + NSLog(@"Error removing downloaded archive at %@: %@", zipOutputURL, error.sqrl_verboseDescription); + } + }]; + }] + flattenMap:^(NSURL *zipOutputURL) { + return [self updateBundleMatchingCurrentApplicationInDirectory:downloadDirectory]; + }] + setNameWithFormat:@"%@ -unarchiveAndPrepareData:withName: %@ intoDirectory: %@", self, name, downloadDirectory]; +} + - (RACSignal *)downloadBundleForUpdate:(SQRLUpdate *)update intoDirectory:(NSURL *)downloadDirectory { NSParameterAssert(update != nil); NSParameterAssert(downloadDirectory != nil); - return [[[[[RACSignal + return [[RACSignal defer:^{ NSURL *zipDownloadURL = update.updateURL; NSMutableURLRequest *zipDownloadRequest = [NSMutableURLRequest requestWithURL:zipDownloadURL]; @@ -343,7 +375,7 @@ - (RACSignal *)downloadBundleForUpdate:(SQRLUpdate *)update intoDirectory:(NSURL [zipDownloadRequest setValue:self.etag forHTTPHeaderField:@"If-None-Match"]; } - return [[[[NSURLConnection + return [[[NSURLConnection rac_sendAsynchronousRequest:zipDownloadRequest] reduceEach:^(NSURLResponse *response, NSData *bodyData) { if ([response isKindOfClass:NSHTTPURLResponse.class]) { @@ -366,45 +398,9 @@ - (RACSignal *)downloadBundleForUpdate:(SQRLUpdate *)update intoDirectory:(NSURL self.etag = httpResponse.allHeaderFields[@"ETag"]; } - return [RACSignal return:bodyData]; + return [self unarchiveAndPrepareData:bodyData withName:zipDownloadURL.lastPathComponent intoDirectory:downloadDirectory]; }] - flatten] - flattenMap:^(NSData *data) { - if (data == nil) return [RACSignal return:nil]; - - NSURL *zipOutputURL = [downloadDirectory URLByAppendingPathComponent:zipDownloadURL.lastPathComponent]; - - NSError *error = nil; - if ([data writeToURL:zipOutputURL options:NSDataWritingAtomic error:&error]) { - return [RACSignal return:zipOutputURL]; - } else { - return [RACSignal error:error]; - } - }]; - }] - doNext:^(NSURL *zipOutputURL) { - if (zipOutputURL == nil) return; - - NSLog(@"Download completed to: %@", zipOutputURL); - }] - flattenMap:^(NSURL *zipOutputURL) { - if (zipOutputURL == nil) return [RACSignal return:nil]; - - return [[[[SQRLZipArchiver - unzipArchiveAtURL:zipOutputURL intoDirectoryAtURL:downloadDirectory] - ignoreValues] - concat:[RACSignal return:zipOutputURL]] - doCompleted:^{ - NSError *error = nil; - if (![NSFileManager.defaultManager removeItemAtURL:zipOutputURL error:&error]) { - NSLog(@"Error removing downloaded archive at %@: %@", zipOutputURL, error.sqrl_verboseDescription); - } - }]; - }] - flattenMap:^(NSURL *zipOutputURL) { - if (zipOutputURL == nil) return [RACSignal return:nil]; - - return [self updateBundleMatchingCurrentApplicationInDirectory:downloadDirectory]; + flatten]; }] setNameWithFormat:@"%@ -downloadBundleForUpdate: %@ intoDirectory: %@", self, update, downloadDirectory]; }