diff --git a/Autoupdate/SUBinaryDeltaApply.m b/Autoupdate/SUBinaryDeltaApply.m index 9a7033f8..de855eef 100644 --- a/Autoupdate/SUBinaryDeltaApply.m +++ b/Autoupdate/SUBinaryDeltaApply.m @@ -16,6 +16,7 @@ #include #include #import +#include #include @@ -174,7 +175,7 @@ BOOL applyBinaryDelta(NSString *source, NSString *finalDestination, NSString *pa if (error != NULL) { *error = nil; } - + [archive enumerateItems:^(SPUDeltaArchiveItem *item, BOOL *stop) { NSString *relativePath = item.relativeFilePath; @@ -493,6 +494,31 @@ BOOL applyBinaryDelta(NSString *source, NSString *finalDestination, NSString *pa } } + // According to https://developer.apple.com/library/mac/documentation/Carbon/Conceptual/MDImporters/Concepts/Troubleshooting.html + // we should make sure mdimporter bundles have an up to date modification time in case they were delta updated. + // We used to invoke mdimport on the bundle but this is not a very good approach. + // Updating the timestamp on the mdimporter bundles is what developers have to do anyway when shipping their new update outside of Sparkle. + NSString *spotlightDirectory = [finalDestination stringByAppendingPathComponent:@"Contents/Library/Spotlight"]; + for (NSString *spotlightItem in [fileManager contentsOfDirectoryAtPath:spotlightDirectory error:NULL]) { + if ([spotlightItem.pathExtension isEqualToString:@"mdimporter"]) { + const char *mdimporterPath = [spotlightDirectory stringByAppendingPathComponent:spotlightItem].fileSystemRepresentation; + + // Using O_SYMLINK so we touch the item itself rather than what it may point to + int mdimporterFileDescriptor = open(mdimporterPath, O_RDONLY | O_SYMLINK); + if (mdimporterFileDescriptor == -1) { + fprintf(stderr, "\nWarning: failed to open file descriptor to touch %s: %s", mdimporterPath, strerror(errno)); + continue; + } + + // Using futimes() because utimes() follows symbolic links + if (futimes(mdimporterFileDescriptor, NULL) != 0) { + fprintf(stderr, "\nWarning: failed to touch %s: %s", mdimporterPath, strerror(errno)); + } + + close(mdimporterFileDescriptor); + } + } + progressCallback(6/7.0); if (verbose) { diff --git a/Autoupdate/SUBinaryDeltaUnarchiver.m b/Autoupdate/SUBinaryDeltaUnarchiver.m index ca6b701a..d38cacf7 100644 --- a/Autoupdate/SUBinaryDeltaUnarchiver.m +++ b/Autoupdate/SUBinaryDeltaUnarchiver.m @@ -10,8 +10,6 @@ #import "SUUnarchiverNotifier.h" #import "SUBinaryDeltaCommon.h" #import "SUBinaryDeltaApply.h" -#import "SULog.h" -#import "SUFileManager.h" #include "AppKitPrevention.h" @@ -33,45 +31,6 @@ return YES; } -// According to https://developer.apple.com/library/mac/documentation/Carbon/Conceptual/MDImporters/Concepts/Troubleshooting.html -// We should make sure mdimporter bundles have an up to date time in the event they were delta updated. -// We used to invoke mdimport on the bundle but this is not a very good approach. -// There's no need to do that for non-delta updates and for updates that contain no mdimporters. -// Moreover, updating the timestamp on the mdimporter bundles is what developers have to do anyway when shipping their new update outside of Sparkle -// Note: this is used from unit tests -+ (void)updateSpotlightImportersAtBundlePath:(NSString *)targetPath -#ifndef BUILDING_SPARKLE_TESTS -SPU_OBJC_DIRECT -#endif -{ - NSURL *targetURL = [NSURL fileURLWithPath:targetPath]; - // Only recurse if it's actually a directory. Don't recurse into a - // root-level symbolic link. - NSDictionary *rootAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:targetPath error:nil]; - NSString *rootType = [rootAttributes objectForKey:NSFileType]; - - if ([rootType isEqualToString:NSFileTypeDirectory]) { - // The NSDirectoryEnumerator will avoid recursing into any contained - // symbolic links, so no further type checks are needed. - NSDirectoryEnumerator *directoryEnumerator = [[NSFileManager defaultManager] enumeratorAtURL:targetURL includingPropertiesForKeys:nil options:(NSDirectoryEnumerationOptions)0 errorHandler:nil]; - - NSMutableArray *filesToUpdate = [[NSMutableArray alloc] init]; - for (NSURL *file in directoryEnumerator) { - if ([file.pathExtension isEqualToString:@"mdimporter"]) { - [filesToUpdate addObject:file]; - } - } - - SUFileManager *fileManager = [[SUFileManager alloc] init]; - for (NSURL *file in filesToUpdate) { - NSError *error = nil; - if (![fileManager updateModificationAndAccessTimeOfItemAtURL:file error:&error]) { - SULog(SULogLevelError, @"Error: During delta unarchiving, failed to touch %@", error); - } - } - } -} - - (instancetype)initWithArchivePath:(NSString *)archivePath extractionDirectory:(NSString *)extractionDirectory updateHostBundlePath:(NSString *)updateHostBundlePath { self = [super init]; @@ -110,7 +69,6 @@ SPU_OBJC_DIRECT }, &applyDiffError); if (success) { - [SUBinaryDeltaUnarchiver updateSpotlightImportersAtBundlePath:targetPath]; [notifier notifySuccess]; } else { diff --git a/Sparkle.xcodeproj/project.pbxproj b/Sparkle.xcodeproj/project.pbxproj index 598776a1..da97d527 100644 --- a/Sparkle.xcodeproj/project.pbxproj +++ b/Sparkle.xcodeproj/project.pbxproj @@ -393,7 +393,6 @@ 72B767E61C9CFD7200A07552 /* SPUAutomaticUpdateDriver.h in Headers */ = {isa = PBXBuildFile; fileRef = 72B767E41C9CFD7200A07552 /* SPUAutomaticUpdateDriver.h */; }; 72B767E71C9CFD7200A07552 /* SPUAutomaticUpdateDriver.m in Sources */ = {isa = PBXBuildFile; fileRef = 72B767E51C9CFD7200A07552 /* SPUAutomaticUpdateDriver.m */; }; 72BC6C3D275027BF0083F14B /* SparkleTestCodeSign_apfs.dmg in Resources */ = {isa = PBXBuildFile; fileRef = 72BC6C3C275027BF0083F14B /* SparkleTestCodeSign_apfs.dmg */; }; - 72BEBFEF1D7287570019146B /* SUSpotlightImporterTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 72BEBFEE1D7287560019146B /* SUSpotlightImporterTest.swift */; }; 72C56E042EFB45B10005A484 /* SUSignatureVerifier.m in Sources */ = {isa = PBXBuildFile; fileRef = 7267E59B1D3D8A5A00D1BF90 /* SUSignatureVerifier.m */; }; 72C56E052EFB45BD0005A484 /* SPUVerifierInformation.m in Sources */ = {isa = PBXBuildFile; fileRef = 72D04F3C2B094C8400A6DEAA /* SPUVerifierInformation.m */; }; 72C56E062EFB45C80005A484 /* libed25519.a in Frameworks */ = {isa = PBXBuildFile; fileRef = EA1E282D22B660BE004AA304 /* libed25519.a */; }; @@ -1342,7 +1341,6 @@ 72B767E41C9CFD7200A07552 /* SPUAutomaticUpdateDriver.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SPUAutomaticUpdateDriver.h; sourceTree = ""; }; 72B767E51C9CFD7200A07552 /* SPUAutomaticUpdateDriver.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SPUAutomaticUpdateDriver.m; sourceTree = ""; }; 72BC6C3C275027BF0083F14B /* SparkleTestCodeSign_apfs.dmg */ = {isa = PBXFileReference; lastKnownFileType = file; path = SparkleTestCodeSign_apfs.dmg; sourceTree = ""; }; - 72BEBFEE1D7287560019146B /* SUSpotlightImporterTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SUSpotlightImporterTest.swift; sourceTree = ""; }; 72C4E01F30340BE400DAF61C /* SPUInstallerProgressLocalizedStrings.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SPUInstallerProgressLocalizedStrings.h; sourceTree = ""; }; 72C4E02030340C2B00DAF61C /* generate_progress_tool_localizations.py */ = {isa = PBXFileReference; lastKnownFileType = text.script.python; path = generate_progress_tool_localizations.py; sourceTree = ""; }; 72C56E072EFDFB850005A484 /* SPUExtractSignedFeed.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SPUExtractSignedFeed.h; sourceTree = ""; }; @@ -1936,7 +1934,6 @@ 72C56E1E2F04AC890005A484 /* SUFeedSignatureVerifierTest.swift */, 72A4A23F1BB6567D00E7820D /* SUFileManagerTest.swift */, 5AF6C74C1AEA40760014A3AB /* SUInstallerTest.m */, - 72BEBFEE1D7287560019146B /* SUSpotlightImporterTest.swift */, 7210C7671B9A9A1500EB90AC /* SUUnarchiverTest.swift */, 5A5DD400249585E70045EB3E /* SUUpdateValidatorTest.swift */, 14950074195FDF5900BC5B5B /* SUUpdaterTest.m */, @@ -3390,7 +3387,6 @@ 14652F8219A9746000959E44 /* SULog.m in Sources */, 7267E5F81D3D91A800D1BF90 /* SUPipedUnarchiver.m in Sources */, 7267E5F71D3D919600D1BF90 /* SUPlainInstaller.m in Sources */, - 72BEBFEF1D7287570019146B /* SUSpotlightImporterTest.swift in Sources */, 7267E5ED1D3D912E00D1BF90 /* SUUnarchiver.m in Sources */, 72316BD41E0DB37E0039EFD9 /* SUUnarchiverNotifier.m in Sources */, 7210C7681B9A9A1500EB90AC /* SUUnarchiverTest.swift in Sources */, diff --git a/Tests/SUBinaryDeltaTest.m b/Tests/SUBinaryDeltaTest.m index f28f57ac..8fc2b011 100644 --- a/Tests/SUBinaryDeltaTest.m +++ b/Tests/SUBinaryDeltaTest.m @@ -2345,4 +2345,32 @@ typedef void (^SUDeltaHandler)(NSFileManager *fileManager, NSString *sourceDirec XCTAssertTrue(success); } +- (void)testSpotlightImporterModificationDate +{ + NSString *importerRelativePath = @"Contents/Library/Spotlight/Foo.mdimporter"; + + __block NSDate *modificationDateBeforePatch = nil; + + BOOL success = [self createAndApplyPatchWithBeforeDiffHandler:^(NSFileManager *fileManager, NSString *sourceDirectory, NSString *destinationDirectory) { + NSString *sourceImporterPath = [sourceDirectory stringByAppendingPathComponent:importerRelativePath]; + NSString *destinationImporterPath = [destinationDirectory stringByAppendingPathComponent:importerRelativePath]; + + XCTAssertTrue([fileManager createDirectoryAtPath:sourceImporterPath withIntermediateDirectories:YES attributes:nil error:nil]); + XCTAssertTrue([fileManager createDirectoryAtPath:destinationImporterPath withIntermediateDirectories:YES attributes:nil error:nil]); + + modificationDateBeforePatch = [fileManager attributesOfItemAtPath:sourceImporterPath error:nil][NSFileModificationDate]; + XCTAssertNotNil(modificationDateBeforePatch); + + sleep(1); // wait for clock to advance + } afterDiffHandler:nil afterPatchHandler:^(NSFileManager *fileManager, NSString * __unused sourceDirectory, NSString *destinationDirectory) { + NSString *patchedImporterPath = [destinationDirectory stringByAppendingPathComponent:importerRelativePath]; + + NSDate *modificationDateAfterPatch = [fileManager attributesOfItemAtPath:patchedImporterPath error:nil][NSFileModificationDate]; + XCTAssertNotNil(modificationDateAfterPatch); + + XCTAssertGreaterThan([modificationDateAfterPatch timeIntervalSinceDate:modificationDateBeforePatch], 0); + }]; + XCTAssertTrue(success); +} + @end diff --git a/Tests/SUSpotlightImporterTest.swift b/Tests/SUSpotlightImporterTest.swift deleted file mode 100644 index 46824fbf..00000000 --- a/Tests/SUSpotlightImporterTest.swift +++ /dev/null @@ -1,60 +0,0 @@ -// -// SUSpotlightImporterTest.swift -// Sparkle -// -// Created by Mayur Pawashe on 8/27/16. -// Copyright © 2016 Sparkle Project. All rights reserved. -// - -import XCTest - -class SUSpotlightImporterTest: XCTestCase -{ - func testUpdatingSpotlightBundles() - { - let fileManager = SUFileManager() - let tempDirectoryURL = try! fileManager.makeTemporaryDirectoryAppropriate(forDirectoryURL: URL(fileURLWithPath: NSHomeDirectory())) - - let bundleDirectory = tempDirectoryURL.appendingPathComponent("bundle.app") - try! fileManager.makeDirectory(at: bundleDirectory) - - let innerDirectory = bundleDirectory.appendingPathComponent("foo") - try! fileManager.makeDirectory(at: innerDirectory) - try! Data().write(to: (bundleDirectory.appendingPathComponent("bar")), options: .atomicWrite) - - let importerDirectory = innerDirectory.appendingPathComponent("baz.mdimporter") - - try! fileManager.makeDirectory(at: importerDirectory) - try! fileManager.makeDirectory(at: innerDirectory.appendingPathComponent("flag")) - - try! Data().write(to: (importerDirectory.appendingPathComponent("file")), options: .atomicWrite) - - let oldFooDirectoryAttributes = try! FileManager.default.attributesOfItem(atPath: innerDirectory.path) - let oldBarFileAttributes = try! FileManager.default.attributesOfItem(atPath: bundleDirectory.appendingPathComponent("bar").path) - let oldImporterAttributes = try! FileManager.default.attributesOfItem(atPath: importerDirectory.path) - let oldFlagAttributes = try! FileManager.default.attributesOfItem(atPath: innerDirectory.appendingPathComponent("flag").path) - let oldFileInImporterAttributes = try! FileManager.default.attributesOfItem(atPath: importerDirectory.appendingPathComponent("file").path) - - sleep(1) // wait for clock to advance - - // Update spotlight bundles - SUBinaryDeltaUnarchiver.updateSpotlightImporters(atBundlePath: bundleDirectory.path) - - let newFooDirectoryAttributes = try! FileManager.default.attributesOfItem(atPath: innerDirectory.path) - XCTAssertEqual((newFooDirectoryAttributes[FileAttributeKey.modificationDate] as! Date).timeIntervalSince(oldFooDirectoryAttributes[FileAttributeKey.modificationDate] as! Date), 0) - - let newBarFileAttributes = try! FileManager.default.attributesOfItem(atPath: bundleDirectory.appendingPathComponent("bar").path) - XCTAssertEqual((newBarFileAttributes[FileAttributeKey.modificationDate] as! Date).timeIntervalSince(oldBarFileAttributes[FileAttributeKey.modificationDate] as! Date), 0) - - let newImporterAttributes = try! FileManager.default.attributesOfItem(atPath: importerDirectory.path) - XCTAssertGreaterThan((newImporterAttributes[FileAttributeKey.modificationDate] as! Date).timeIntervalSince(oldImporterAttributes[FileAttributeKey.modificationDate] as! Date), 0) - - let newFlagAttributes = try! FileManager.default.attributesOfItem(atPath: innerDirectory.appendingPathComponent("flag").path) - XCTAssertEqual((newFlagAttributes[FileAttributeKey.modificationDate] as! Date).timeIntervalSince(oldFlagAttributes[FileAttributeKey.modificationDate] as! Date), 0) - - let newFileInImporterAttributes = try! FileManager.default.attributesOfItem(atPath: importerDirectory.appendingPathComponent("file").path) - XCTAssertEqual((newFileInImporterAttributes[FileAttributeKey.modificationDate] as! Date).timeIntervalSince(oldFileInImporterAttributes[FileAttributeKey.modificationDate] as! Date), 0) - - try! fileManager.removeItem(at: tempDirectoryURL) - } -} diff --git a/Tests/Sparkle Unit Tests-Bridging-Header.h b/Tests/Sparkle Unit Tests-Bridging-Header.h index c1508dfd..b4be4753 100644 --- a/Tests/Sparkle Unit Tests-Bridging-Header.h +++ b/Tests/Sparkle Unit Tests-Bridging-Header.h @@ -48,10 +48,4 @@ static const char *SUAppleQuarantineIdentifier = "com.apple.quarantine"; @end -@interface SUBinaryDeltaUnarchiver (Private) - -+ (void)updateSpotlightImportersAtBundlePath:(NSString *)targetPath; - -@end - NS_ASSUME_NONNULL_END