From c500487077f87fe7dce18d29fe2fa3b825fcd668 Mon Sep 17 00:00:00 2001 From: Zorg Date: Wed, 24 Nov 2021 19:21:53 -0500 Subject: [PATCH] Improve pipe usage when unarchiving files (#2027) --- Autoupdate/SUDiskImageUnarchiver.m | 80 ++++++++++++++++++++------- Autoupdate/SUGuidedPackageInstaller.m | 48 +++++++++++----- Tests/SUInstallerTest.m | 4 +- 3 files changed, 96 insertions(+), 36 deletions(-) diff --git a/Autoupdate/SUDiskImageUnarchiver.m b/Autoupdate/SUDiskImageUnarchiver.m index 4c6bb93f..a8bda84a 100644 --- a/Autoupdate/SUDiskImageUnarchiver.m +++ b/Autoupdate/SUDiskImageUnarchiver.m @@ -62,8 +62,8 @@ // get a unique mount point path NSString *mountPoint = nil; NSFileManager *manager; - NSError *error; - NSArray *contents; + NSError *error = nil; + NSArray *contents = nil; do { // Using NSUUID would make creating UUIDs be done in Cocoa, @@ -82,8 +82,7 @@ // Note: this check does not follow symbolic links, which is what we want while ([[NSURL fileURLWithPath:mountPoint] checkResourceIsReachableAndReturnError:NULL]); - NSData *promptData = nil; - promptData = [NSData dataWithBytes:"yes\n" length:4]; + NSData *promptData = [NSData dataWithBytes:"yes\n" length:4]; NSMutableArray *arguments = [@[@"attach", self.archivePath, @"-mountpoint", mountPoint, /*@"-noverify",*/ @"-nobrowse", @"-noautoopen"] mutableCopy]; @@ -102,7 +101,7 @@ NSData *output = nil; NSInteger taskResult = -1; - @try + { NSTask *task = [[NSTask alloc] init]; task.launchPath = @"/usr/bin/hdiutil"; @@ -115,23 +114,55 @@ task.standardInput = inputPipe; task.standardOutput = outputPipe; - [task launch]; + NSFileHandle *fileStdHandle = outputPipe.fileHandleForReading; + NSMutableData *currentOutput = [NSMutableData data]; + + fileStdHandle.readabilityHandler = ^(NSFileHandle *file) { + [currentOutput appendData:file.availableData]; + }; + + dispatch_semaphore_t terminationSemaphore = dispatch_semaphore_create(0); + task.terminationHandler = ^(NSTask *__unused terminatingTask) { + fileStdHandle.readabilityHandler = nil; + + dispatch_semaphore_signal(terminationSemaphore); + }; + + if (@available(macOS 10.13, *)) { + if (![task launchAndReturnError:&error]) { + goto reportError; + } + } else { + @try { + [task launch]; + } @catch (NSException *) { + goto reportError; + } + } [notifier notifyProgress:0.125]; [inputPipe.fileHandleForWriting writeData:promptData]; + + if (@available(macOS 10.15, *)) { + if (![inputPipe.fileHandleForWriting writeData:promptData error:&error]) { + goto reportError; + } + } else { + @try { + [inputPipe.fileHandleForWriting writeData:promptData]; + } @catch (NSException *) { + goto reportError; + } + } + [inputPipe.fileHandleForWriting closeFile]; - // Read data to end *before* waiting until the task ends so we don't deadlock if the stdout buffer becomes full if we haven't consumed from it - output = [outputPipe.fileHandleForReading readDataToEndOfFile]; + dispatch_semaphore_wait(terminationSemaphore, DISPATCH_TIME_FOREVER); + output = [currentOutput copy]; - [task waitUntilExit]; taskResult = task.terminationStatus; } - @catch (NSException *) - { - goto reportError; - } [notifier notifyProgress:0.5]; @@ -146,14 +177,14 @@ // Now that we've mounted it, we need to copy out its contents. manager = [[NSFileManager alloc] init]; contents = [manager contentsOfDirectoryAtPath:mountPoint error:&error]; - if (error) + if (contents == nil) { SULog(SULogLevelError, @"Couldn't enumerate contents of archive mounted at %@: %@", mountPoint, error); goto reportError; } double itemsCopied = 0; - double totalItems = [contents count]; + double totalItems = (double)[contents count]; for (NSString *item in contents) { @@ -190,11 +221,20 @@ task.standardOutput = [NSPipe pipe]; task.standardError = [NSPipe pipe]; - @try { - [task launch]; - } @catch (NSException *exception) { - SULog(SULogLevelError, @"Failed to unmount %@", mountPoint); - SULog(SULogLevelError, @"Exception: %@", exception); + + if (@available(macOS 10.13, *)) { + NSError *launchCleanupError = nil; + if (![task launchAndReturnError:&launchCleanupError]) { + SULog(SULogLevelError, @"Failed to unmount %@", mountPoint); + SULog(SULogLevelError, @"Error: %@", launchCleanupError); + } + } else { + @try { + [task launch]; + } @catch (NSException *exception) { + SULog(SULogLevelError, @"Failed to unmount %@", mountPoint); + SULog(SULogLevelError, @"Exception: %@", exception); + } } } else { SULog(SULogLevelError, @"Can't mount DMG %@", self.archivePath); diff --git a/Autoupdate/SUGuidedPackageInstaller.m b/Autoupdate/SUGuidedPackageInstaller.m index 5b6c8b60..bdbf7ee5 100644 --- a/Autoupdate/SUGuidedPackageInstaller.m +++ b/Autoupdate/SUGuidedPackageInstaller.m @@ -53,26 +53,46 @@ task.arguments = @[@"-pkg", self.packagePath, @"-target", @"/"]; // Set the $HOME and $USER variables so pre/post install scripts reference the correct user environment task.environment = @{@"HOME": self.homeDirectory, @"USER": self.userName}; - task.standardError = [NSPipe pipe]; - task.standardOutput = [NSPipe pipe]; + task.standardError = nil; + task.standardOutput = nil; - BOOL success = YES; - @try { - [task launch]; - [task waitUntilExit]; - if (task.terminationStatus != EXIT_SUCCESS) { - success = NO; + if (@available(macOS 10.13, *)) { + NSError *launchError = nil; + if (![task launchAndReturnError:&launchError]) { if (error != NULL) { - *error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUInstallationError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Guided package installer returned non-zero exit status (%d)", task.terminationStatus] }]; + NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithDictionary:@{ NSLocalizedDescriptionKey: @"Guided package installer failed to launch" }]; + + if (launchError != nil) { + userInfo[NSUnderlyingErrorKey] = launchError; + } + + *error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUInstallationError userInfo:userInfo]; } + return NO; } - } @catch (NSException *) { - success = NO; - if (error != NULL) { - *error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUInstallationError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Guided package installer task threw an exception"] }]; + } else { + @try { + [task launch]; + } @catch (NSException *) { + if (error != NULL) { + *error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUInstallationError userInfo:@{ NSLocalizedDescriptionKey: @"Guided package installer task threw an exception" }]; + } + + return NO; } } - return success; + + [task waitUntilExit]; + + if (task.terminationStatus != EXIT_SUCCESS) { + if (error != NULL) { + *error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUInstallationError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Guided package installer returned non-zero exit status (%d)", task.terminationStatus] }]; + } + + return NO; + } + + return YES; } - (void)performCleanup diff --git a/Tests/SUInstallerTest.m b/Tests/SUInstallerTest.m index dcfb18b9..deaa9c29 100644 --- a/Tests/SUInstallerTest.m +++ b/Tests/SUInstallerTest.m @@ -37,8 +37,8 @@ { uid_t uid = getuid(); - if (uid) { - NSLog(@"Test must be run as root: sudo xctest -XCTest SUInstallerTest 'Sparkle Unit Tests.xctest'"); + if (uid != 0) { + NSLog(@"Test must be run as root: sudo xcodebuild -project Sparkle.xcodeproj -scheme Sparkle '-only-testing:Sparkle Unit Tests/SUInstallerTest/testInstallIfRoot' test"); return; }