diff --git a/Sparkle/Autoupdate/Autoupdate.m b/Sparkle/Autoupdate/Autoupdate.m index 0849ba0d..44cf3a59 100644 --- a/Sparkle/Autoupdate/Autoupdate.m +++ b/Sparkle/Autoupdate/Autoupdate.m @@ -136,7 +136,7 @@ static const NSTimeInterval SUTerminationTimeDelay = 0.5; self.statusController = [[SUStatusController alloc] initWithHost:host]; [self.statusController setButtonTitle:SULocalizedString(@"Cancel Update", @"") target:nil action:Nil isDefault:NO]; [self.statusController beginActionWithTitle:SULocalizedString(@"Installing update...", @"") - maxProgressValue: 0 statusText: @""]; + maxProgressValue:100 statusText: @""]; [self.statusController showWindow:self]; } @@ -151,8 +151,14 @@ static const NSTimeInterval SUTerminationTimeDelay = 0.5; return; } + void(^progressBlock)(double) = ^(double progress){ + dispatch_async(dispatch_get_main_queue(), ^(){ + self.statusController.progressValue = progress * 100.0; + }); + }; + NSError *finalInstallationError = nil; - if (![installer performFinalInstallation:&finalInstallationError]) { + if (![installer performFinalInstallationProgressBlock:progressBlock error:&finalInstallationError]) { NSError *underlyingError = [finalInstallationError.userInfo objectForKey:NSUnderlyingErrorKey]; dispatch_async(dispatch_get_main_queue(), ^{ if (underlyingError == nil || underlyingError.code != SUInstallationCancelledError) { diff --git a/Sparkle/SUFileManager.h b/Sparkle/SUFileManager.h index 9434566b..817bad9d 100644 --- a/Sparkle/SUFileManager.h +++ b/Sparkle/SUFileManager.h @@ -146,6 +146,7 @@ NS_ASSUME_NONNULL_BEGIN /** * Runs an installer package (pkg) in a headless mode using /usr/sbin/installer * @param packageURL A URL pointing to the package to execute. The item at this URL must exist. + * @param progressBlock Callback that takes double value (0..1 range) notifying how far the installation has progressed * @param error If an error occurs, upon returns contains an NSError object that describes the problem. If you are not interested in possible errors, you may pass in NULL. * @return YES if the installer ran the package successfully, otherwise NO with a populated error object * @@ -153,7 +154,7 @@ NS_ASSUME_NONNULL_BEGIN * an initial authorization prompt if the calling process does not have root privileges. In other words, root privileges are required to use this method, and the file manager instance must have been created by allowing authorization. * An error can occur if the package is unable to be ran by the installer, or if the installer reports a non-zero exit status code. */ -- (BOOL)executePackageAtURL:(NSURL *)packageURL error:(NSError *_Nullable*_Nullable)error; +- (BOOL)executePackageAtURL:(NSURL *)packageURL progressBlock:(nullable void(^)(double))progressBlock error:(NSError *_Nullable*_Nullable)error; NS_ASSUME_NONNULL_END diff --git a/Sparkle/SUFileManager.m b/Sparkle/SUFileManager.m index 7b5e3e49..94696530 100644 --- a/Sparkle/SUFileManager.m +++ b/Sparkle/SUFileManager.m @@ -143,7 +143,11 @@ static BOOL SUMakeRefFromURL(NSURL *url, FSRef *ref, NSError **error) { return YES; } -- (BOOL)_authorizeAndExecuteCommand:(char *)command sourcePath:(char *)sourcePath destinationPath:(char *)destinationPath error:(NSError * __autoreleasing *)error +- (BOOL)_authorizeAndExecuteCommand:(char *)command sourcePath:(char *)sourcePath destinationPath:(char *)destinationPath error:(NSError * __autoreleasing *)error { + return [self _authorizeAndExecuteCommand:command sourcePath:sourcePath destinationPath:destinationPath lineCallback:nil error:error]; +} + +- (BOOL)_authorizeAndExecuteCommand:(char *)command sourcePath:(char *)sourcePath destinationPath:(char *)destinationPath lineCallback:(void(^)(NSString*))lineCallback error:(NSError * __autoreleasing *)error { NSError *acquireError = nil; if (![self _acquireAuthorizationWithError:&acquireError]) { @@ -184,6 +188,11 @@ static BOOL SUMakeRefFromURL(NSURL *url, FSRef *ref, NSError **error) { return NO; } + if (lineCallback) { + NSFileHandle *pipeHandle = [[NSFileHandle alloc] initWithFileDescriptor:fileno(pipe) closeOnDealloc:NO]; + [self parseLines:pipeHandle lineCallback:lineCallback]; + } + pid_t childPid = (int32_t)CFSwapInt32LittleToHost(pidData); int status = 0; @@ -204,6 +213,28 @@ static BOOL SUMakeRefFromURL(NSURL *url, FSRef *ref, NSError **error) { return YES; } +- (void)parseLines:(NSFileHandle *)pipeHandle lineCallback:(void(^)(NSString *))lineCallback { + __block NSMutableData *line = [NSMutableData new]; + pipeHandle.readabilityHandler = ^(NSFileHandle *handle){ + NSData *data = handle.availableData; + NSUInteger length = data.length; + if (!length) { + return; + } + const char *bytes = data.bytes; + for(NSUInteger i = 0; i < length; i++) { + if (bytes[i] == '\n') { + NSData *dataToEOL = [data subdataWithRange:NSMakeRange(0, i)]; + [line appendData:dataToEOL]; + NSString *fullLine = [[NSString alloc] initWithData:line encoding:NSUTF8StringEncoding]; + lineCallback(fullLine); + line = [[data subdataWithRange:NSMakeRange(i+1, length-i-1)] mutableCopy]; + break; + } + } + }; +} + - (void)dealloc { if (_auth != NULL) { @@ -924,7 +955,7 @@ static BOOL SUMakeRefFromURL(NSURL *url, FSRef *ref, NSError **error) { } // Unlike other methods, authorization is required to execute this method successfully -- (BOOL)executePackageAtURL:(NSURL *)packageURL error:(NSError * __autoreleasing *)error +- (BOOL)executePackageAtURL:(NSURL *)packageURL progressBlock:(void(^)(double))progressBlock error:(NSError * __autoreleasing *)error { if (![self _itemExistsAtURL:packageURL]) { if (error != NULL) { @@ -942,7 +973,11 @@ static BOOL SUMakeRefFromURL(NSURL *url, FSRef *ref, NSError **error) { } NSError *executeError = nil; - BOOL success = [self _authorizeAndExecuteCommand:SUFileOpInstallCommand sourcePath:path destinationPath:NULL error:&executeError]; + BOOL success = [self _authorizeAndExecuteCommand:SUFileOpInstallCommand sourcePath:path destinationPath:NULL lineCallback:^(NSString *line){ + if ([line hasPrefix:@"installer:%"]) { + progressBlock([[line substringFromIndex:11] doubleValue]/100.0); + } + } error:&executeError]; if (!success && error != NULL) { NSString* errorMessage = @"Failed to execute package installer."; diff --git a/Sparkle/SUGuidedPackageInstaller.m b/Sparkle/SUGuidedPackageInstaller.m index cea993a2..9c8e6e50 100644 --- a/Sparkle/SUGuidedPackageInstaller.m +++ b/Sparkle/SUGuidedPackageInstaller.m @@ -42,11 +42,11 @@ return YES; } -- (BOOL)performFinalInstallation:(NSError * __autoreleasing *)error +- (BOOL)performFinalInstallationProgressBlock:(nullable void(^)(double))progressBlock error:(NSError * __autoreleasing *)error { SUFileManager *fileManager = [SUFileManager fileManagerWithAuthorizationToolPath:self.fileOperationToolPath]; - return [fileManager executePackageAtURL:[NSURL fileURLWithPath:self.packagePath] error:error]; + return [fileManager executePackageAtURL:[NSURL fileURLWithPath:self.packagePath] progressBlock:progressBlock error:error]; } - (BOOL)canInstallSilently diff --git a/Sparkle/SUInstallerProtocol.h b/Sparkle/SUInstallerProtocol.h index 4e07bbd2..75312c47 100644 --- a/Sparkle/SUInstallerProtocol.h +++ b/Sparkle/SUInstallerProtocol.h @@ -21,7 +21,7 @@ NS_ASSUME_NONNULL_BEGIN // Any installation work after the user application has been terminated. This is where the final installation work can be done. // After this stage is done, the user application may be relaunched. // Should be able to be called from non-main thread -- (BOOL)performFinalInstallation:(NSError **)error; +- (BOOL)performFinalInstallationProgressBlock:(nullable void(^)(double))cb error:(NSError **)error; // Indicates whether or not this installer can install the update silently in the background, without hindering the user // Should be thread safe diff --git a/Sparkle/SUPackageInstaller.m b/Sparkle/SUPackageInstaller.m index 92654742..251a0561 100644 --- a/Sparkle/SUPackageInstaller.m +++ b/Sparkle/SUPackageInstaller.m @@ -49,7 +49,7 @@ static NSString *SUOpenUtilityPath = @"/usr/bin/open"; return YES; } -- (BOOL)performFinalInstallation:(NSError * __autoreleasing *)error +- (BOOL)performFinalInstallationProgressBlock:(nullable void(^)(double))__unused cb error:(NSError *__autoreleasing*)error { // Run installer using the "open" command to ensure it is launched in front of current application. // -W = wait until the app has quit. diff --git a/Sparkle/SUPlainInstaller.m b/Sparkle/SUPlainInstaller.m index 81ee5cfd..2f326767 100644 --- a/Sparkle/SUPlainInstaller.m +++ b/Sparkle/SUPlainInstaller.m @@ -61,7 +61,7 @@ return trimmedVersion.length > 0 ? trimmedVersion : nil; } -- (BOOL)performInstallationToURL:(NSURL *)installationURL fromUpdateAtURL:(NSURL *)newURL withHost:(SUHost *)host fileOperationToolPath:(NSString *)fileOperationToolPath error:(NSError * __autoreleasing *)error +- (BOOL)performInstallationToURL:(NSURL *)installationURL fromUpdateAtURL:(NSURL *)newURL withHost:(SUHost *)host fileOperationToolPath:(NSString *)fileOperationToolPath progressBlock:(nullable void(^)(double))progress error:(NSError * __autoreleasing *)error { if (installationURL == nil || newURL == nil) { // this really shouldn't happen but just in case @@ -71,7 +71,9 @@ } return NO; } - + + progress(1/10.0); + SUFileManager *fileManager = [SUFileManager fileManagerWithAuthorizationToolPath:fileOperationToolPath]; // Create a temporary directory for our new app that resides on our destination's volume @@ -82,7 +84,9 @@ SULog(SULogLevelError, @"Failed to make new temp directory"); return NO; } - + + progress(2/10.0); + // Move the new app to our temporary directory NSString *newURLLastPathComponent = newURL.lastPathComponent; NSURL *newTempURL = [tempNewDirectoryURL URLByAppendingPathComponent:newURLLastPathComponent]; @@ -91,7 +95,9 @@ [fileManager removeItemAtURL:tempNewDirectoryURL error:NULL]; return NO; } - + + progress(3/10.0); + // Release our new app from quarantine, fix its owner and group IDs, and update its modification time while it's at our temporary destination // We must leave moving the app to its destination as the final step in installing it, so that // it's not possible our new app can be left in an incomplete state at the final destination @@ -101,7 +107,9 @@ // Not big enough of a deal to fail the entire installation SULog(SULogLevelError, @"Failed to release quarantine at %@ with error %@", newTempURL.path, quarantineError); } - + + progress(4/10.0); + NSURL *oldURL = [NSURL fileURLWithPath:host.bundlePath]; if (oldURL == nil) { // this really shouldn't happen but just in case @@ -119,11 +127,16 @@ return NO; } + + progress(5/10.0); + if (![fileManager updateModificationAndAccessTimeOfItemAtURL:newTempURL error:error]) { // Not a fatal error, but a pretty unfortunate one SULog(SULogLevelError, @"Failed to update modification and access time of new app at %@", newTempURL.path); } - + + progress(6/10.0); + // Decide on a destination name we should use for the older app when we move it around the file system NSString *oldDestinationName = nil; if (SPARKLE_APPEND_VERSION_NUMBER) { @@ -145,7 +158,9 @@ [fileManager removeItemAtURL:tempNewDirectoryURL error:NULL]; return NO; } - + + progress(7/10.0); + // Move the old app to the temporary directory NSURL *oldTempURL = [tempOldDirectoryURL URLByAppendingPathComponent:oldDestinationNameWithPathExtension]; if (![fileManager moveItemAtURL:oldURL toURL:oldTempURL error:error]) { @@ -157,7 +172,9 @@ return NO; } - + + progress(8/10.0); + // Move the new app to its final destination if (![fileManager moveItemAtURL:newTempURL toURL:installationURL error:error]) { SULog(SULogLevelError, @"Failed to move new app at %@ to final destination %@", newTempURL.path, installationURL.path); @@ -171,14 +188,18 @@ return NO; } - + + progress(9/10.0); + // From here on out, we don't really need to bring up authorization if we haven't done so prior SUFileManager *constrainedFileManager = [fileManager fileManagerByPreservingAuthorizationRights]; // Cleanup [constrainedFileManager removeItemAtURL:tempOldDirectoryURL error:NULL]; [constrainedFileManager removeItemAtURL:tempNewDirectoryURL error:NULL]; - + + progress(10/10.0); + return YES; } @@ -207,9 +228,9 @@ return YES; } -- (BOOL)performFinalInstallation:(NSError * __autoreleasing *)error +- (BOOL)performFinalInstallationProgressBlock:(nullable void(^)(double))cb error:(NSError *__autoreleasing*)error { - return [self performInstallationToURL:[NSURL fileURLWithPath:self.installationPath] fromUpdateAtURL:[NSURL fileURLWithPath:self.bundlePath] withHost:self.host fileOperationToolPath:self.fileOperationToolPath error:error]; + return [self performInstallationToURL:[NSURL fileURLWithPath:self.installationPath] fromUpdateAtURL:[NSURL fileURLWithPath:self.bundlePath] withHost:self.host fileOperationToolPath:self.fileOperationToolPath progressBlock:cb error:error]; } - (BOOL)canInstallSilently diff --git a/Tests/SUInstallerTest.m b/Tests/SUInstallerTest.m index f5790a72..c750c042 100644 --- a/Tests/SUInstallerTest.m +++ b/Tests/SUInstallerTest.m @@ -71,7 +71,7 @@ } NSError *finalInstallationError = nil; - if (![installer performFinalInstallation:&finalInstallationError]) { + if (![installer performFinalInstallationProgressBlock:nil error:&finalInstallationError]) { XCTFail(@"Failed to perform final installation with underlying error = %@ ; error = %@", [finalInstallationError.userInfo objectForKey:NSUnderlyingErrorKey], finalInstallationError); } diff --git a/fileop/fileop.m b/fileop/fileop.m index 9bb54411..397ccb79 100644 --- a/fileop/fileop.m +++ b/fileop/fileop.m @@ -131,10 +131,12 @@ int main(int argc, const char *argv[]) NSTask *task = [[NSTask alloc] init]; task.launchPath = installerPath; - task.arguments = @[@"-pkg", filepath, @"-target", @"/"]; - // Output won't show up anyway, so we may as well ignore it + task.arguments = @[@"-verboseR", + @"-pkg", filepath, + @"-target", @"/"]; task.standardError = [NSPipe pipe]; - task.standardOutput = [NSPipe pipe]; + // Pass through verbose output from the installer, so the caller can parse it + task.standardOutput = [NSFileHandle fileHandleWithStandardOutput]; @try { [task launch];