diff --git a/Sparkle/SUFileManager.h b/Sparkle/SUFileManager.h index f5cba54a..1a362808 100644 --- a/Sparkle/SUFileManager.h +++ b/Sparkle/SUFileManager.h @@ -79,6 +79,19 @@ */ - (BOOL)changeOwnerAndGroupOfItemAtRootURL:(NSURL *)targetURL toMatchURL:(NSURL *)matchURL error:(NSError **)error; +/** + * Updates the modification and access time of an item at a specified target URL to the current time + * @param targetURL A URL pointing to the target item whose modification and access time to update. 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 target item's modification and access times have been updated, otherwise NO along with a populated error object + * + * This method updates the modification and access time of an item to the current time, ideal for letting the system know we installed a new file or + * application. + * + * This is not an atomic operation. + */ +- (BOOL)updateModificationAndAccessTimeOfItemAtURL:(NSURL *)targetURL error:(NSError **)error; + /** * Releases Apple's quarantine extended attribute from the item at the specified root URL * @param rootURL A URL pointing to the item to release from Apple's quarantine. This will be applied recursively if the item is a directory. The item at this URL must exist. diff --git a/Sparkle/SUFileManager.m b/Sparkle/SUFileManager.m index cbf74c82..f2739f40 100644 --- a/Sparkle/SUFileManager.m +++ b/Sparkle/SUFileManager.m @@ -10,6 +10,7 @@ #include #include +#include #pragma clang diagnostic push // This is a private constant but it's referenced from our unit tests @@ -527,6 +528,53 @@ static BOOL AuthorizationExecuteWithPrivilegesAndWait(AuthorizationRef authoriza return success; } +// /usr/bin/touch can be used to update an application, as described in: +// https://developer.apple.com/library/mac/documentation/Carbon/Conceptual/LaunchServicesConcepts/LSCConcepts/LSCConcepts.html +// The document says LSRegisterURL() can be used as well but this hasn't worked out well for me in practice +// Anyway, updating the modification time of the application is important because the system will be aware a new version of your app is available, +// Finder will report the correct file size and other metadata for it, URL schemes your app may register will be updated, etc. +// Behind the scenes, touch calls to utimes() which is what we use here - unless we need to authenticate +- (BOOL)updateModificationAndAccessTimeOfItemAtURL:(NSURL *)targetURL error:(NSError * __autoreleasing *)error +{ + if (![self _itemExistsAtURL:targetURL]) { + if (error != NULL) { + *error = [NSError errorWithDomain:NSCocoaErrorDomain code:NSFileNoSuchFileError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed to update modification & access time because %@ does not exist.", targetURL.path.lastPathComponent] }]; + } + return NO; + } + + char path[PATH_MAX] = {0}; + if (![targetURL.path getFileSystemRepresentation:path maxLength:sizeof(path)]) { + if (error != NULL) { + *error = [NSError errorWithDomain:NSCocoaErrorDomain code:NSFileReadInvalidFileNameError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"File to update modification & access time (%@) cannot be represented as a valid file name.", targetURL.path.lastPathComponent] }]; + } + return NO; + } + + if (utimes(path, NULL) == 0) { + return YES; + } + + if (errno != EACCES) { + if (error != NULL) { + *error = [NSError errorWithDomain:NSPOSIXErrorDomain code:errno userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed to update modification & access time for %@", targetURL.path.lastPathComponent] }]; + } + return NO; + } + + if (![self _acquireAuthorizationWithError:error]) { + return NO; + } + + BOOL success = AuthorizationExecuteWithPrivilegesAndWait(_auth, "/usr/bin/touch", kAuthorizationFlagDefaults, (char *[]){ path, NULL }); + if (!success && error != NULL) { + NSString *errorMessage = [NSString stringWithFormat:@"Failed to update modification & access time on %@ with authentication.", targetURL.path.lastPathComponent]; + *error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUAuthenticationFailure userInfo:@{ NSLocalizedDescriptionKey: errorMessage }]; + } + + return success; +} + // Creates a directory at the item pointed by url // An item cannot already exist at the url, but the parent must be a directory that exists - (BOOL)_makeDirectoryAtURL:(NSURL *)url error:(NSError * __autoreleasing *)error diff --git a/Sparkle/SUPlainInstaller.m b/Sparkle/SUPlainInstaller.m index 6a9e9307..a8a90a1f 100644 --- a/Sparkle/SUPlainInstaller.m +++ b/Sparkle/SUPlainInstaller.m @@ -60,7 +60,7 @@ return NO; } - // Release our new app from quarantine and fix its owner and group IDs while it's at our temporary destination + // 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 @@ -87,6 +87,11 @@ return NO; } + if (![fileManager updateModificationAndAccessTimeOfItemAtURL:newTempURL error:error]) { + // Not a fatal error, but a pretty unfortunate one + SULog(@"Failed to update modification and access time of new app at %@", newTempURL.path); + } + // 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) { diff --git a/Tests/SUFileManagerTest.swift b/Tests/SUFileManagerTest.swift index 20722abe..76e66882 100644 --- a/Tests/SUFileManagerTest.swift +++ b/Tests/SUFileManagerTest.swift @@ -169,6 +169,27 @@ class SUFileManagerTest: XCTestCase } } + func testUpdateFileModificationTime() + { + makeTempFiles() { fileManager, rootURL, ordinaryFileURL, directoryURL, fileInDirectoryURL in + XCTAssertNil(try? fileManager.updateModificationAndAccessTimeOfItemAtURL(rootURL.URLByAppendingPathComponent("does not exist"))) + + let oldOrdinaryFileAttributes = try! NSFileManager.defaultManager().attributesOfItemAtPath(ordinaryFileURL.path!) + let oldDirectoryAttributes = try! NSFileManager.defaultManager().attributesOfItemAtPath(directoryURL.path!) + + sleep(1); // wait for clock to advance + + try! fileManager.updateModificationAndAccessTimeOfItemAtURL(ordinaryFileURL) + try! fileManager.updateModificationAndAccessTimeOfItemAtURL(directoryURL) + + let newOrdinaryFileAttributes = try! NSFileManager.defaultManager().attributesOfItemAtPath(ordinaryFileURL.path!) + XCTAssertGreaterThan((newOrdinaryFileAttributes[NSFileModificationDate] as! NSDate).timeIntervalSinceDate(oldOrdinaryFileAttributes[NSFileModificationDate] as! NSDate), 0) + + let newDirectoryAttributes = try! NSFileManager.defaultManager().attributesOfItemAtPath(directoryURL.path!) + XCTAssertGreaterThan((newDirectoryAttributes[NSFileModificationDate] as! NSDate).timeIntervalSinceDate(oldDirectoryAttributes[NSFileModificationDate] as! NSDate), 0) + } + } + func testFileExists() { makeTempFiles() { fileManager, rootURL, ordinaryFileURL, directoryURL, fileInDirectoryURL in