From 99be33d83b37fb9dd5e93b12a5366ca6e4919dc7 Mon Sep 17 00:00:00 2001 From: Zorg Date: Tue, 19 Apr 2016 02:31:02 -0400 Subject: [PATCH 1/2] Move Test App server directory data into subdirectory inside Caches We want to wipe the caches directory we create for the test app data on launch, and want to make sure no other data is being inserted into the same directory. Before, Sparkle was inserting data into the same directory when downloading an update. --- TestApplication/SUTestApplicationDelegate.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TestApplication/SUTestApplicationDelegate.m b/TestApplication/SUTestApplicationDelegate.m index d360c607..dbf0f22a 100644 --- a/TestApplication/SUTestApplicationDelegate.m +++ b/TestApplication/SUTestApplicationDelegate.m @@ -54,7 +54,7 @@ static NSString * const UPDATED_VERSION = @"2.0"; assert(bundleIdentifier != nil); // Create a directory that'll be used for our web server listing - NSURL *serverDirectoryURL = [cacheDirectoryURL URLByAppendingPathComponent:bundleIdentifier]; + NSURL *serverDirectoryURL = [[cacheDirectoryURL URLByAppendingPathComponent:bundleIdentifier] URLByAppendingPathComponent:@"ServerData"]; if ([serverDirectoryURL checkResourceIsReachableAndReturnError:nil]) { NSError *removeServerDirectoryError = nil; From a937fa0a1738bdc5f6f4c3371fb820a5606e9b71 Mon Sep 17 00:00:00 2001 From: Zorg Date: Tue, 19 Apr 2016 02:33:33 -0400 Subject: [PATCH 2/2] Clear update caches directory before downloading new update This aims to make it impossible to have download data accumulate inside the caches directory. We still try to deal with if an intermediate file is created at the path we want. appCachePath method was also moved away from SUHost into SUBasicUpdateDriver which is the only class that needs it. This method also has very little to do with host bundles. appCachePath is also called less when checking for the existence of intermediate file paths. Lastly, we now create a directory whose name is the sparkle bundle ID instead of just "Sparkle" This is to improve potential name collision issues. --- Sparkle/SUBasicUpdateDriver.m | 39 ++++++++++++++++++++++++++++++----- Sparkle/SUHost.h | 1 - Sparkle/SUHost.m | 22 -------------------- 3 files changed, 34 insertions(+), 28 deletions(-) diff --git a/Sparkle/SUBasicUpdateDriver.m b/Sparkle/SUBasicUpdateDriver.m index 2f1230a1..e40b3893 100644 --- a/Sparkle/SUBasicUpdateDriver.m +++ b/Sparkle/SUBasicUpdateDriver.m @@ -208,8 +208,36 @@ }]]; } +- (NSString *)appCachePath +{ + NSArray *cachePaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); + NSString *cachePath = nil; + if ([cachePaths count]) { + cachePath = [cachePaths objectAtIndex:0]; + } + if (!cachePath) { + SULog(@"Failed to find user's cache directory! Using system default"); + cachePath = NSTemporaryDirectory(); + } + + NSString *name = [self.host.bundle bundleIdentifier]; + if (!name) { + name = [self.host name]; + } + + cachePath = [cachePath stringByAppendingPathComponent:name]; + cachePath = [cachePath stringByAppendingPathComponent:@SPARKLE_BUNDLE_IDENTIFIER]; + return cachePath; +} + - (void)downloadUpdate { + // Clear cache directory so that downloads can't possibly accumulate inside + NSString *appCachePath = [self appCachePath]; + if ([[NSFileManager defaultManager] fileExistsAtPath:appCachePath]) { + [[NSFileManager defaultManager] removeItemAtPath:appCachePath error:NULL]; + } + NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[self.updateItem fileURL]]; [request setValue:[self.updater userAgentString] forHTTPHeaderField:@"User-Agent"]; if ([[self.updater delegate] respondsToSelector:@selector(updater:willDownloadUpdate:withRequest:)]) { @@ -223,13 +251,14 @@ - (void)download:(NSURLDownload *)__unused d decideDestinationWithSuggestedFilename:(NSString *)name { NSString *downloadFileName = [NSString stringWithFormat:@"%@ %@", [self.host name], [self.updateItem versionString]]; - - - self.tempDir = [self.host.appCachePath stringByAppendingPathComponent:downloadFileName]; + + NSString *appCachePath = [self appCachePath]; + + self.tempDir = [appCachePath stringByAppendingPathComponent:downloadFileName]; int cnt = 1; while ([[NSFileManager defaultManager] fileExistsAtPath:self.tempDir] && cnt <= 999) { - self.tempDir = [self.host.appCachePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@ %d", downloadFileName, cnt++]]; + self.tempDir = [appCachePath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@ %d", downloadFileName, cnt++]]; } // Create the temporary directory if necessary. @@ -480,7 +509,7 @@ NSString *const relaunchToolName = @"" SPARKLE_RELAUNCH_TOOL_NAME; NSString *const relaunchPathToCopy = [sparkleBundle pathForResource:relaunchToolName ofType:@"app"]; if (relaunchPathToCopy != nil) { - NSString *targetPath = [self.host.appCachePath stringByAppendingPathComponent:[relaunchPathToCopy lastPathComponent]]; + NSString *targetPath = [[self appCachePath] stringByAppendingPathComponent:[relaunchPathToCopy lastPathComponent]]; SUFileManager *fileManager = [SUFileManager fileManagerAllowingAuthorization:NO]; NSError *error = nil; diff --git a/Sparkle/SUHost.h b/Sparkle/SUHost.h index 5f73f955..6d058b90 100644 --- a/Sparkle/SUHost.h +++ b/Sparkle/SUHost.h @@ -26,7 +26,6 @@ typedef struct { - (instancetype)initWithBundle:(NSBundle *)aBundle; @property (readonly, copy) NSString *bundlePath; @property (readonly) BOOL allowsAutomaticUpdates; -@property (readonly, copy) NSString *appCachePath; @property (readonly, copy) NSString *installationPath; @property (readonly, copy) NSString *name; @property (readonly, copy) NSString *version; diff --git a/Sparkle/SUHost.m b/Sparkle/SUHost.m index 00ba50d1..ffe8c915 100644 --- a/Sparkle/SUHost.m +++ b/Sparkle/SUHost.m @@ -74,28 +74,6 @@ return [[NSFileManager defaultManager] isWritableFileAtPath:self.bundlePath]; } -- (NSString *)appCachePath -{ - NSArray *cachePaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); - NSString *cachePath = nil; - if ([cachePaths count]) { - cachePath = [cachePaths objectAtIndex:0]; - } - if (!cachePath) { - SULog(@"Failed to find user's cache directory! Using system default"); - cachePath = NSTemporaryDirectory(); - } - - NSString *name = [self.bundle bundleIdentifier]; - if (!name) { - name = [self name]; - } - - cachePath = [cachePath stringByAppendingPathComponent:name]; - cachePath = [cachePath stringByAppendingPathComponent:@"Sparkle"]; - return cachePath; -} - - (NSString *)installationPath { if (SPARKLE_NORMALIZE_INSTALLED_APPLICATION_NAME) {