From b56910f455988a717b0e570add7caa74acaec01a Mon Sep 17 00:00:00 2001 From: Zorg Date: Wed, 28 Dec 2016 22:01:00 -0500 Subject: [PATCH] Remove the old app/bundle instead of moving it to the trash Fixes #827 --- Sparkle/SUFileManager.h | 15 ------ Sparkle/SUFileManager.m | 89 ----------------------------------- Sparkle/SUPlainInstaller.m | 8 +--- Tests/SUFileManagerTest.swift | 32 ------------- 4 files changed, 1 insertion(+), 143 deletions(-) diff --git a/Sparkle/SUFileManager.h b/Sparkle/SUFileManager.h index 0573bf10..9434566b 100644 --- a/Sparkle/SUFileManager.h +++ b/Sparkle/SUFileManager.h @@ -87,21 +87,6 @@ NS_ASSUME_NONNULL_BEGIN */ - (BOOL)copyItemAtURL:(NSURL *)sourceURL toURL:(NSURL *)destinationURL error:(NSError *_Nullable*_Nullable)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 *_Nullable*_Nullable)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 f08fff3c..26369b63 100644 --- a/Sparkle/SUFileManager.m +++ b/Sparkle/SUFileManager.m @@ -11,10 +11,6 @@ #import "SUOperatingSystem.h" #import "SUFileOperationConstants.h" -#if __MAC_OS_X_VERSION_MIN_REQUIRED < 1080 -#import // for NSWorkspace -#endif - #include #include #include @@ -923,91 +919,6 @@ static BOOL SUMakeRefFromURL(NSURL *url, FSRef *ref, NSError **error) { return success; } -- (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 = nil; - BOOL canUseNewTrashAPI = YES; -#if __MAC_OS_X_VERSION_MIN_REQUIRED < 1080 - canUseNewTrashAPI = [SUOperatingSystem isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion){10, 8, 0}]; - if (!canUseNewTrashAPI) { - FSRef trashRef; - if (FSFindFolder(kUserDomain, kTrashFolderType, kDontCreateFolder, &trashRef) == noErr) { - trashURL = CFBridgingRelease(CFURLCreateFromFSRef(kCFAllocatorDefault, &trashRef)); - } - } -#endif - - if (canUseNewTrashAPI) { - 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 - - BOOL success = NO; -#if __MAC_OS_X_VERSION_MIN_REQUIRED < 1080 - if (!canUseNewTrashAPI) { - NSString *tempParentPath = tempItemURL.URLByDeletingLastPathComponent.path; - success = [[NSWorkspace sharedWorkspace] performFileOperation:NSWorkspaceRecycleOperation source:tempParentPath destination:@"" files:@[tempItemURL.lastPathComponent] tag:NULL]; - if (!success && error != NULL) { - *error = [NSError errorWithDomain:NSCocoaErrorDomain code:NSFileNoSuchFileError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed to move file %@ into the trash.", tempItemURL.lastPathComponent] }]; - } - } -#endif - - if (canUseNewTrashAPI) { - NSError *trashError = nil; -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wpartial-availability" - success = [_fileManager trashItemAtURL:tempItemURL resultingItemURL:NULL error:&trashError]; -#pragma clang diagnostic pop - if (!success && error != NULL) { - *error = trashError; - } - } - - [self removeItemAtURL:tempDirectory error:NULL]; - - return success; -} - // Unlike other methods, authorization is required to execute this method successfully - (BOOL)executePackageAtURL:(NSURL *)packageURL error:(NSError * __autoreleasing *)error { diff --git a/Sparkle/SUPlainInstaller.m b/Sparkle/SUPlainInstaller.m index 41b84757..586ce47f 100644 --- a/Sparkle/SUPlainInstaller.m +++ b/Sparkle/SUPlainInstaller.m @@ -172,14 +172,8 @@ // 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: move the old app to the trash - NSError *trashError = nil; - if (![constrainedFileManager moveItemAtURLToTrash:oldTempURL error:&trashError]) { - SULog(@"Failed to move %@ to trash with error %@", oldTempURL, trashError); - } - + // Cleanup [constrainedFileManager removeItemAtURL:tempOldDirectoryURL error:NULL]; - [constrainedFileManager removeItemAtURL:tempNewDirectoryURL error:NULL]; return YES; diff --git a/Tests/SUFileManagerTest.swift b/Tests/SUFileManagerTest.swift index 1f175a7a..50aa40c9 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.moveItemAtURL(toTrash: rootURL.appendingPathComponent("does not exist"))) - - let trashURL = try! FileManager.default.url(for: .trashDirectory, in: .userDomainMask, appropriateFor: nil, create: false) - - try! fileManager.moveItemAtURL(toTrash: ordinaryFileURL) - XCTAssertFalse(fileManager._itemExists(at: ordinaryFileURL)) - - let ordinaryFileTrashURL = trashURL.appendingPathComponent(ordinaryFileURL.lastPathComponent) - XCTAssertTrue(fileManager._itemExists(at: ordinaryFileTrashURL)) - try! fileManager.removeItem(at: ordinaryFileTrashURL) - - let validSymlinkTrashURL = trashURL.appendingPathComponent(validSymlinkURL.lastPathComponent) - try! fileManager.moveItemAtURL(toTrash: validSymlinkURL) - XCTAssertTrue(fileManager._itemExists(at: validSymlinkTrashURL)) - XCTAssertTrue(fileManager._itemExists(at: directoryURL)) - try! fileManager.removeItem(at: validSymlinkTrashURL) - - try! fileManager.moveItemAtURL(toTrash: directoryURL) - XCTAssertFalse(fileManager._itemExists(at: directoryURL)) - XCTAssertFalse(fileManager._itemExists(at: fileInDirectoryURL)) - - let directoryTrashURL = trashURL.appendingPathComponent(directoryURL.lastPathComponent) - XCTAssertTrue(fileManager._itemExists(at: directoryTrashURL)) - XCTAssertTrue(fileManager._itemExists(at: directoryTrashURL.appendingPathComponent(fileInDirectoryURL.lastPathComponent))) - - try! fileManager.removeItem(at: directoryTrashURL) - } - } - func testCopyFiles() { makeTempFiles() { fileManager, rootURL, ordinaryFileURL, directoryURL, fileInDirectoryURL, validSymlinkURL, invalidSymlinkURL in