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 986c983d..632cd641 100644 --- a/Sparkle/SUHost.m +++ b/Sparkle/SUHost.m @@ -77,28 +77,6 @@ return [[NSFileManager defaultManager] isWritableFileAtPath:bundlePath.stringByDeletingLastPathComponent] && [[NSFileManager defaultManager] isWritableFileAtPath: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) { 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;