From 9c8fb9918432940c8de05b5b33539a3d4c31ee10 Mon Sep 17 00:00:00 2001 From: Zorg Date: Fri, 24 Jul 2015 18:47:50 -0400 Subject: [PATCH] Move deciding to abort an update into a procedure --- Sparkle/SUBasicUpdateDriver.m | 38 ++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/Sparkle/SUBasicUpdateDriver.m b/Sparkle/SUBasicUpdateDriver.m index 6371285d..8f2a6c8c 100644 --- a/Sparkle/SUBasicUpdateDriver.m +++ b/Sparkle/SUBasicUpdateDriver.m @@ -377,6 +377,31 @@ [self installWithToolAndRelaunch:relaunch displayingUserInterface:relaunch]; } +// Creates intermediate directories up until targetPath if they don't already exist, +// and removes the directory at targetPath if one already exists there +- (BOOL)preparePathForRelaunchTool:(NSString *)targetPath error:(NSError * __autoreleasing *)error +{ + NSFileManager *fileManager = [[NSFileManager alloc] init]; + if ([fileManager fileExistsAtPath:targetPath]) { + NSError *removeError = nil; + if (![fileManager removeItemAtPath:targetPath error:&removeError]) { + if (error != NULL) { + *error = removeError; + } + return NO; + } + } else { + NSError *createDirectoryError = nil; + if (![fileManager createDirectoryAtPath:[targetPath stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:@{} error:&createDirectoryError]) { + if (error != NULL) { + *error = createDirectoryError; + } + return NO; + } + } + return YES; +} + - (void)installWithToolAndRelaunch:(BOOL)relaunch displayingUserInterface:(BOOL)showUI { assert(self.updateItem); @@ -425,23 +450,12 @@ NSString *targetPath = [self.host.appCachePath stringByAppendingPathComponent:[relaunchPathToCopy lastPathComponent]]; NSFileManager *fileManager = [[NSFileManager alloc] init]; - - BOOL shouldAbortUpdate = NO; NSError *error = nil; - if ([fileManager fileExistsAtPath:targetPath]) { - if (![fileManager removeItemAtPath:targetPath error:&error]) { - shouldAbortUpdate = YES; - } - } else { - if (![fileManager createDirectoryAtPath:[targetPath stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:@{} error:&error]) { - shouldAbortUpdate = YES; - } - } // We only need to run our copy of the app by spawning a task // Since we are copying the app to a directory that is write-accessible, we don't need to muck with owner/group IDs // And since we spawn a task, we don't need to clear the quarantine bits - if (!shouldAbortUpdate && [fileManager copyItemAtPath:relaunchPathToCopy toPath:targetPath error:&error]) { + if ([self preparePathForRelaunchTool:targetPath error:&error] && [fileManager copyItemAtPath:relaunchPathToCopy toPath:targetPath error:&error]) { self.relaunchPath = targetPath; } else { [self abortUpdateWithError:[NSError errorWithDomain:SUSparkleErrorDomain code:SURelaunchError userInfo:@{