From 9f4e44f919ee7fb6f06e48557d5158b0a281374e Mon Sep 17 00:00:00 2001 From: Zorg Date: Mon, 25 Jul 2016 19:07:52 -0400 Subject: [PATCH] Don't move old app to Trash Several reasons: * The user may not look at the trash anyway, and it would bloat their HD space * Other sofware updaters don't move old versions to the trash either. Can confuse the user or not be seemly transparent experience. * Moving app to trash will not work if user has to authenticate anyway * Unclear how long and expensive this operation *could* take in all scenarios * App replacements in Apple's new file system can be atomic-safe, but that may only be the case if the old app is no longer needed Cons: * Few tech savvy users may not be able to restore an app without re-downloading it. This would still be rarely done even amongst them. --- Autoupdate/SUPlainInstaller.m | 30 ++-------------- Configurations/ConfigCommon.xcconfig | 4 +-- Sparkle/SUFileManager.h | 15 -------- Sparkle/SUFileManager.m | 54 ---------------------------- Tests/SUFileManagerTest.swift | 32 ----------------- 5 files changed, 4 insertions(+), 131 deletions(-) diff --git a/Autoupdate/SUPlainInstaller.m b/Autoupdate/SUPlainInstaller.m index 0b53fd35..23a77523 100644 --- a/Autoupdate/SUPlainInstaller.m +++ b/Autoupdate/SUPlainInstaller.m @@ -30,7 +30,6 @@ @property (nonatomic) SUFileManager *fileManager; @property (nonatomic) NSURL *tempOldDirectoryURL; @property (nonatomic) NSURL *tempNewDirectoryURL; -@property (nonatomic) NSURL *oldTempURL; @end @@ -43,7 +42,6 @@ @synthesize fileManager = _fileManager; @synthesize tempOldDirectoryURL = _tempOldDirectoryURL; @synthesize tempNewDirectoryURL = _tempNewDirectoryURL; -@synthesize oldTempURL = _oldTempURL; - (instancetype)initWithHost:(SUHost *)host applicationPath:(NSString *)applicationPath installationPath:(NSString *)installationPath versionComparator:(id )comparator { @@ -148,17 +146,8 @@ } // 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) { - NSString *oldBundleVersion = [self bundleVersionAppropriateForFilenameFromHost:host]; - - oldDestinationName = [oldURL.lastPathComponent.stringByDeletingPathExtension stringByAppendingFormat:@" (%@)", oldBundleVersion != nil ? oldBundleVersion : @"old"]; - } else { - oldDestinationName = oldURL.lastPathComponent.stringByDeletingPathExtension; - } - - NSString *oldURLExtension = oldURL.pathExtension; - NSString *oldDestinationNameWithPathExtension = [oldDestinationName stringByAppendingPathExtension:oldURLExtension]; + NSString *oldDestinationName = oldURL.lastPathComponent.stringByDeletingPathExtension; + NSString *oldDestinationNameWithPathExtension = oldURL.lastPathComponent; // Create a temporary directory for our old app that resides on its volume NSURL *oldDirectoryURL = oldURL.URLByDeletingLastPathComponent; @@ -199,7 +188,6 @@ self.fileManager = fileManager; self.tempNewDirectoryURL = tempNewDirectoryURL; self.tempOldDirectoryURL = tempOldDirectoryURL; - self.oldTempURL = oldTempURL; return YES; } @@ -254,25 +242,13 @@ - (void)cleanup { SUFileManager *fileManager = self.fileManager; - NSURL *oldTempURL = self.oldTempURL; NSURL *tempOldDirectoryURL = self.tempOldDirectoryURL; NSURL *tempNewDirectoryURL = self.tempNewDirectoryURL; // Note: I'm intentionally not checking if an item at the file URLs exist since these methods already do that for us - if (oldTempURL != nil) { - // Cleanup: move the old app to the trash - // If we are the root user it's very possible that there is no trash directory to move the file into - // In that case, don't log out an error. Removing tempOldDirectoryURL later will remove the app - NSError *trashError = nil; - if (![fileManager moveItemAtURLToTrash:oldTempURL error:&trashError] && trashError.code != NSFileNoSuchFileError) { - SULog(@"Failed to move %@ to trash with error %@", oldTempURL, trashError); - } - - self.oldTempURL = nil; - } - if (tempOldDirectoryURL != nil) { + // This will remove the old app contained inside the directory as well [fileManager removeItemAtURL:tempOldDirectoryURL error:NULL]; self.tempOldDirectoryURL = nil; diff --git a/Configurations/ConfigCommon.xcconfig b/Configurations/ConfigCommon.xcconfig index b4448915..e3a25d98 100644 --- a/Configurations/ConfigCommon.xcconfig +++ b/Configurations/ConfigCommon.xcconfig @@ -4,8 +4,6 @@ SPARKLE_BUNDLE_IDENTIFIER = org.sparkle-project.Sparkle SPARKLE_RELAUNCH_TOOL_NAME = Autoupdate SPARKLE_INSTALLER_PROGRESS_TOOL_NAME = Updater -SPARKLE_APPEND_VERSION_NUMBER = 1 - // Sparkle usually doesn't allow downgrades as they're usually accidental, but // if your app has a downgrade function or URL handler, turn this on SPARKLE_AUTOMATED_DOWNGRADES = 0 @@ -45,7 +43,7 @@ INSTALLER_LAUNCHER_BUNDLE_ID = org.sparkle-project.${INSTALLER_LAUNCHER_PRODUCT_ TEMPORARY_DOWNLOADER_BUNDLE_ID = org.sparkle-project.${TEMPORARY_DOWNLOADER_PRODUCT_NAME} PERSISTENT_DOWNLOADER_BUNDLE_ID = org.sparkle-project.${PERSISTENT_DOWNLOADER_PRODUCT_NAME} -GCC_PREPROCESSOR_DEFINITIONS_COMMON = SPARKLE_NORMALIZE_INSTALLED_APPLICATION_NAME=$(SPARKLE_NORMALIZE_INSTALLED_APPLICATION_NAME) SPARKLE_AUTOMATED_DOWNGRADES=$(SPARKLE_AUTOMATED_DOWNGRADES) SPARKLE_APPEND_VERSION_NUMBER=$(SPARKLE_APPEND_VERSION_NUMBER) SPARKLE_BUNDLE_IDENTIFIER=\"$(SPARKLE_BUNDLE_IDENTIFIER)\" CURRENT_PROJECT_VERSION=\"$(CURRENT_PROJECT_VERSION)\" SPARKLE_RELAUNCH_TOOL_NAME=\"$(SPARKLE_RELAUNCH_TOOL_NAME)\" SPARKLE_INSTALLER_PROGRESS_TOOL_NAME=\"$(SPARKLE_INSTALLER_PROGRESS_TOOL_NAME)\" SPARKLE_ICON_NAME=\"$(SPARKLE_ICON_NAME)\" INSTALLER_LAUNCHER_BUNDLE_ID=\"${INSTALLER_LAUNCHER_BUNDLE_ID}\" INSTALLER_CONNECTION_BUNDLE_ID=\"${INSTALLER_CONNECTION_BUNDLE_ID}\" INSTALLER_STATUS_BUNDLE_ID=\"${INSTALLER_STATUS_BUNDLE_ID}\" TEMPORARY_DOWNLOADER_BUNDLE_ID=\"${TEMPORARY_DOWNLOADER_BUNDLE_ID}\" PERSISTENT_DOWNLOADER_BUNDLE_ID=\"${PERSISTENT_DOWNLOADER_BUNDLE_ID}\" INSTALLER_LAUNCHER_PRODUCT_NAME=\"${INSTALLER_LAUNCHER_PRODUCT_NAME}\" INSTALLER_CONNECTION_PRODUCT_NAME=\"${INSTALLER_CONNECTION_PRODUCT_NAME}\" INSTALLER_STATUS_PRODUCT_NAME=\"${INSTALLER_STATUS_PRODUCT_NAME}\" TEMPORARY_DOWNLOADER_PRODUCT_NAME=\"${TEMPORARY_DOWNLOADER_PRODUCT_NAME}\" PERSISTENT_DOWNLOADER_PRODUCT_NAME=\"${PERSISTENT_DOWNLOADER_PRODUCT_NAME}\" +GCC_PREPROCESSOR_DEFINITIONS_COMMON = SPARKLE_NORMALIZE_INSTALLED_APPLICATION_NAME=$(SPARKLE_NORMALIZE_INSTALLED_APPLICATION_NAME) SPARKLE_AUTOMATED_DOWNGRADES=$(SPARKLE_AUTOMATED_DOWNGRADES) SPARKLE_BUNDLE_IDENTIFIER=\"$(SPARKLE_BUNDLE_IDENTIFIER)\" CURRENT_PROJECT_VERSION=\"$(CURRENT_PROJECT_VERSION)\" SPARKLE_RELAUNCH_TOOL_NAME=\"$(SPARKLE_RELAUNCH_TOOL_NAME)\" SPARKLE_INSTALLER_PROGRESS_TOOL_NAME=\"$(SPARKLE_INSTALLER_PROGRESS_TOOL_NAME)\" SPARKLE_ICON_NAME=\"$(SPARKLE_ICON_NAME)\" INSTALLER_LAUNCHER_BUNDLE_ID=\"${INSTALLER_LAUNCHER_BUNDLE_ID}\" INSTALLER_CONNECTION_BUNDLE_ID=\"${INSTALLER_CONNECTION_BUNDLE_ID}\" INSTALLER_STATUS_BUNDLE_ID=\"${INSTALLER_STATUS_BUNDLE_ID}\" TEMPORARY_DOWNLOADER_BUNDLE_ID=\"${TEMPORARY_DOWNLOADER_BUNDLE_ID}\" PERSISTENT_DOWNLOADER_BUNDLE_ID=\"${PERSISTENT_DOWNLOADER_BUNDLE_ID}\" INSTALLER_LAUNCHER_PRODUCT_NAME=\"${INSTALLER_LAUNCHER_PRODUCT_NAME}\" INSTALLER_CONNECTION_PRODUCT_NAME=\"${INSTALLER_CONNECTION_PRODUCT_NAME}\" INSTALLER_STATUS_PRODUCT_NAME=\"${INSTALLER_STATUS_PRODUCT_NAME}\" TEMPORARY_DOWNLOADER_PRODUCT_NAME=\"${TEMPORARY_DOWNLOADER_PRODUCT_NAME}\" PERSISTENT_DOWNLOADER_PRODUCT_NAME=\"${PERSISTENT_DOWNLOADER_PRODUCT_NAME}\" CLANG_ENABLE_OBJC_ARC = YES GCC_ENABLE_PASCAL_STRINGS = NO diff --git a/Sparkle/SUFileManager.h b/Sparkle/SUFileManager.h index 9ab64a22..a25f6ffd 100644 --- a/Sparkle/SUFileManager.h +++ b/Sparkle/SUFileManager.h @@ -70,21 +70,6 @@ NS_ASSUME_NONNULL_BEGIN */ - (BOOL)copyItemAtURL:(NSURL *)sourceURL toURL:(NSURL *)destinationURL error:(NSError **)error; -/** - * Moves an item at a specified URL to the running user's trash directory - * @param url A URL pointing to the item to move to the trash. The item at this URL must exist. - * @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 item was moved to the trash successfully, otherwise NO along with a populated error object - * - * - * This method has to locate the trash directory and uses an intermediate temporary directory before trashing the item. - * A copy may have to be done if the url is not on the same volume as the running user's trash directory. - * If a failure occurs in the middle of this operation, the item to remove may be lost forever or stuck in a temporary location. - * - * This is not an atomic operation, nor intended to be a recoverable operation if the worst comes to worst. - */ -- (BOOL)moveItemAtURLToTrash:(NSURL *)url error:(NSError **)error; - /** * Removes an item at a URL * @param url A URL pointing to the item to remove. The item at this URL must exist. diff --git a/Sparkle/SUFileManager.m b/Sparkle/SUFileManager.m index 09907295..5b170ea3 100644 --- a/Sparkle/SUFileManager.m +++ b/Sparkle/SUFileManager.m @@ -672,60 +672,6 @@ static BOOL SUMakeRefFromURL(NSURL *url, FSRef *ref, NSError **error) { return YES; } -- (BOOL)moveItemAtURLToTrash:(NSURL *)url error:(NSError *__autoreleasing *)error -{ - if (![self _itemExistsAtURL:url]) { - if (error != NULL) { - *error = [NSError errorWithDomain:NSCocoaErrorDomain code:NSFileNoSuchFileError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed to move %@ to the trash because the file does not exist.", url.path.lastPathComponent] }]; - } - return NO; - } - - NSURL *trashURL = [_fileManager URLForDirectory:NSTrashDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil]; - - if (trashURL == nil) { - if (error != NULL) { - *error = [NSError errorWithDomain:NSCocoaErrorDomain code:NSFileNoSuchFileError userInfo:@{ NSLocalizedDescriptionKey: @"Failed to locate the user's trash folder." }]; - } - return NO; - } - - // In the rare worst case scenario, our temporary directory will be labeled incomplete and be in the user's trash directory, - // indicating that whatever inside of there is not yet completely moved. - // Regardless, we want the item to be in our Volume before we try moving it to the trash - NSString *preferredName = [url.lastPathComponent.stringByDeletingPathExtension stringByAppendingString:@" (Incomplete Files)"]; - NSURL *tempDirectory = [self makeTemporaryDirectoryWithPreferredName:preferredName appropriateForDirectoryURL:trashURL error:error]; - if (tempDirectory == nil) { - return NO; - } - - NSString *urlLastPathComponent = url.lastPathComponent; - NSURL *tempItemURL = [tempDirectory URLByAppendingPathComponent:urlLastPathComponent]; - if (![self moveItemAtURL:url toURL:tempItemURL error:error]) { - // If we can't move the item at url, just remove it completely; chances are it's not going to be missed - [self removeItemAtURL:url error:NULL]; - [self removeItemAtURL:tempDirectory error:NULL]; - return NO; - } - - if (![self changeOwnerAndGroupOfItemAtRootURL:tempItemURL toMatchURL:trashURL error:error]) { - // Removing the item inside of the temp directory is better than trying to move the item to the trash with incorrect ownership - [self removeItemAtURL:tempDirectory error:NULL]; - return NO; - } - - // If we get here, we should be able to trash the item normally without authentication - NSError *trashError = nil; - BOOL success = [_fileManager trashItemAtURL:tempItemURL resultingItemURL:NULL error:&trashError]; - if (!success && error != NULL) { - *error = trashError; - } - - [self removeItemAtURL:tempDirectory error:NULL]; - - return success; -} - @end #pragma clang diagnostic pop diff --git a/Tests/SUFileManagerTest.swift b/Tests/SUFileManagerTest.swift index 26fcf4d7..5e5e4ec3 100644 --- a/Tests/SUFileManagerTest.swift +++ b/Tests/SUFileManagerTest.swift @@ -70,38 +70,6 @@ class SUFileManagerTest: XCTestCase } } - func testMoveFilesToTrash() - { - makeTempFiles() { fileManager, rootURL, ordinaryFileURL, directoryURL, fileInDirectoryURL, validSymlinkURL, invalidSymlinkURL in - XCTAssertNil(try? fileManager.moveItemAtURLToTrash(rootURL.URLByAppendingPathComponent("does not exist"))) - - let trashURL = try! NSFileManager.defaultManager().URLForDirectory(.TrashDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: false) - - try! fileManager.moveItemAtURLToTrash(ordinaryFileURL) - XCTAssertFalse(fileManager._itemExistsAtURL(ordinaryFileURL)) - - let ordinaryFileTrashURL = trashURL.URLByAppendingPathComponent(ordinaryFileURL.lastPathComponent!) - XCTAssertTrue(fileManager._itemExistsAtURL(ordinaryFileTrashURL)) - try! fileManager.removeItemAtURL(ordinaryFileTrashURL) - - let validSymlinkTrashURL = trashURL.URLByAppendingPathComponent(validSymlinkURL.lastPathComponent!) - try! fileManager.moveItemAtURLToTrash(validSymlinkURL) - XCTAssertTrue(fileManager._itemExistsAtURL(validSymlinkTrashURL)) - XCTAssertTrue(fileManager._itemExistsAtURL(directoryURL)) - try! fileManager.removeItemAtURL(validSymlinkTrashURL) - - try! fileManager.moveItemAtURLToTrash(directoryURL) - XCTAssertFalse(fileManager._itemExistsAtURL(directoryURL)) - XCTAssertFalse(fileManager._itemExistsAtURL(fileInDirectoryURL)) - - let directoryTrashURL = trashURL.URLByAppendingPathComponent(directoryURL.lastPathComponent!) - XCTAssertTrue(fileManager._itemExistsAtURL(directoryTrashURL)) - XCTAssertTrue(fileManager._itemExistsAtURL(directoryTrashURL.URLByAppendingPathComponent(fileInDirectoryURL.lastPathComponent!))) - - try! fileManager.removeItemAtURL(directoryTrashURL) - } - } - func testCopyFiles() { makeTempFiles() { fileManager, rootURL, ordinaryFileURL, directoryURL, fileInDirectoryURL, validSymlinkURL, invalidSymlinkURL in