From 52fd21e1193e52b2fdcd32efbd4bf1b4a919fc72 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sun, 31 Jul 2016 20:38:48 -0400 Subject: [PATCH] Reload NSBundle cache on its info plist dictionary We reload the info dictionary before we start off an update driver. This is so that we have the latest up to date information for the bundle. --- Sparkle/SUHost.h | 5 +++++ Sparkle/SUHost.m | 23 +++++++++++++++++------ Sparkle/SUUpdater.m | 4 ++++ 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/Sparkle/SUHost.h b/Sparkle/SUHost.h index b3e5aef6..498eea1e 100644 --- a/Sparkle/SUHost.h +++ b/Sparkle/SUHost.h @@ -12,6 +12,11 @@ @property (strong, readonly) NSBundle *bundle; - (instancetype)initWithBundle:(NSBundle *)aBundle; + +// Invalidates the info dictionary cache +// This is not thread safe +- (void)reloadInfoDictionary; + @property (readonly, copy) NSString *bundlePath; @property (readonly, copy) NSString *name; @property (readonly, copy) NSString *version; diff --git a/Sparkle/SUHost.m b/Sparkle/SUHost.m index 9de48e4b..5df111d5 100644 --- a/Sparkle/SUHost.m +++ b/Sparkle/SUHost.m @@ -28,6 +28,7 @@ @property (strong, readwrite) NSBundle *bundle; @property (copy) NSString *defaultsDomain; @property (assign) BOOL usesStandardUserDefaults; +@property (nonatomic) NSDictionary *infoDictionary; @end @@ -36,6 +37,7 @@ @synthesize bundle; @synthesize defaultsDomain; @synthesize usesStandardUserDefaults; +@synthesize infoDictionary = _infoDictionary; - (instancetype)initWithBundle:(NSBundle *)aBundle { @@ -46,8 +48,10 @@ if (![self.bundle bundleIdentifier]) { SULog(@"Error: the bundle being updated at %@ has no %@! This will cause preference read/write to not work properly.", self.bundle, kCFBundleIdentifierKey); } + + _infoDictionary = aBundle.infoDictionary; - self.defaultsDomain = [self.bundle objectForInfoDictionaryKey:SUDefaultsDomainKey]; + self.defaultsDomain = [self objectForInfoDictionaryKey:SUDefaultsDomainKey]; if (!self.defaultsDomain) { self.defaultsDomain = [self.bundle bundleIdentifier]; } @@ -59,6 +63,13 @@ return self; } +// NSBundles always cache the info dictionary, even if you create a new NSBundle instance, but we sometimes want to reload it +// in case the bundle changes or is updated +- (void)reloadInfoDictionary +{ + CFDictionaryRef infoDictionary = CFBundleCopyInfoDictionaryInDirectory((CFURLRef)self.bundle.bundleURL); + self.infoDictionary = CFBridgingRelease(infoDictionary); +} - (NSString *)description { return [NSString stringWithFormat:@"%@ <%@>", [self class], [self bundlePath]]; } @@ -75,7 +86,7 @@ name = [self objectForInfoDictionaryKey:@"SUBundleName"]; if (name && name.length > 0) return name; - name = [self.bundle objectForInfoDictionaryKey:@"CFBundleDisplayName"]; + name = [self objectForInfoDictionaryKey:@"CFBundleDisplayName"]; if (name && name.length > 0) return name; name = [self objectForInfoDictionaryKey:(__bridge NSString *)kCFBundleNameKey]; @@ -96,7 +107,7 @@ - (NSString *)_version { - NSString *version = [self.bundle objectForInfoDictionaryKey:(__bridge NSString *)kCFBundleVersionKey]; + NSString *version = [self objectForInfoDictionaryKey:(__bridge NSString *)kCFBundleVersionKey]; return ([self isValidVersion:version] ? version : nil); } @@ -112,7 +123,7 @@ - (NSString * _Nonnull)displayVersion { - NSString *shortVersionString = [self.bundle objectForInfoDictionaryKey:@"CFBundleShortVersionString"]; + NSString *shortVersionString = [self objectForInfoDictionaryKey:@"CFBundleShortVersionString"]; if (shortVersionString) return shortVersionString; else @@ -129,7 +140,7 @@ - (NSString *_Nullable)publicDSAKey { // Maybe the key is just a string in the Info.plist. - NSString *key = [self.bundle objectForInfoDictionaryKey:SUPublicDSAKeyKey]; + NSString *key = [self objectForInfoDictionaryKey:SUPublicDSAKeyKey]; if (key) { return key; } @@ -149,7 +160,7 @@ - (id)objectForInfoDictionaryKey:(NSString *)key { - return [self.bundle objectForInfoDictionaryKey:key]; + return [self.infoDictionary objectForKey:key]; } - (BOOL)boolForInfoDictionaryKey:(NSString *)key diff --git a/Sparkle/SUUpdater.m b/Sparkle/SUUpdater.m index 6da27258..cf4f1487 100644 --- a/Sparkle/SUUpdater.m +++ b/Sparkle/SUUpdater.m @@ -475,6 +475,10 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification [self scheduleNextUpdateCheck]; return; } + + // Reload our host bundle information in case its current cache is out of date + // We could have updated the bundle before for example without terminating this updater process + [self.host reloadInfoDictionary]; // Because an application can change the configuration (eg: the feed url) at any point, we should always check if it's valid NSError *configurationError = nil;