diff --git a/Sparkle/SPUDownloader.m b/Sparkle/SPUDownloader.m index 787438c2..ef6e9067 100755 --- a/Sparkle/SPUDownloader.m +++ b/Sparkle/SPUDownloader.m @@ -97,4 +97,22 @@ } } +-(NSString*)getAndCleanTempDirectory +{ + NSString *rootPersistentDownloadCachePath = [[SPULocalCacheDirectory cachePathForBundleIdentifier:self.bundleIdentifier] stringByAppendingPathComponent:@"PersistentDownloads"]; + + [SPULocalCacheDirectory removeOldItemsInDirectory:rootPersistentDownloadCachePath]; + + NSString *tempDir = [SPULocalCacheDirectory createUniqueDirectoryInDirectory:rootPersistentDownloadCachePath]; + if (tempDir == nil) { + // Okay, something's really broken with this user's file structure. + NSError *error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUTemporaryDirectoryError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Can't make a temporary directory for the update download at %@.", tempDir] }]; + + [self.delegate downloaderDidFailWithError:error]; + + [self cancel]; + } + return tempDir; +} + @end diff --git a/Sparkle/SPUDownloaderDeprecated.m b/Sparkle/SPUDownloaderDeprecated.m index 24204764..ae2a065c 100644 --- a/Sparkle/SPUDownloaderDeprecated.m +++ b/Sparkle/SPUDownloaderDeprecated.m @@ -60,36 +60,19 @@ - (void)download:(NSURLDownload *)__unused d decideDestinationWithSuggestedFilename:(NSString *)name { - if (self.mode == SPUDownloadModeTemporary) - { + if (self.mode == SPUDownloadModeTemporary) { // Files downloaded in temporary mode should not last for very long, // so it's ideal to place them in a system temporary directory NSString *destinationFilename = NSTemporaryDirectory(); - if (destinationFilename) - { + if (destinationFilename) { destinationFilename = [destinationFilename stringByAppendingPathComponent:name]; [self.download setDestination:destinationFilename allowOverwrite:NO]; } - } - else - { - // Remove our old caches path so we don't start accumulating files in there - NSString *rootPersistentDownloadCachePath = [[SPULocalCacheDirectory cachePathForBundleIdentifier:self.bundleIdentifier] stringByAppendingPathComponent:@"PersistentDownloads"]; + } else { + NSString *tempDir = [super getAndCleanTempDirectory]; - [SPULocalCacheDirectory removeOldItemsInDirectory:rootPersistentDownloadCachePath]; - - NSString *tempDir = [SPULocalCacheDirectory createUniqueDirectoryInDirectory:rootPersistentDownloadCachePath]; - if (tempDir == nil) - { - // Okay, something's really broken with this user's file structure. - [self.download cancel]; - self.download = nil; - - NSError *error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUTemporaryDirectoryError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Can't make a temporary directory for the update download at %@.", tempDir] }]; - - [self.delegate downloaderDidFailWithError:error]; - } else { + if (tempDir != nil) { NSString *downloadFileName = self.desiredFilename; NSString *downloadFileNameDirectory = [tempDir stringByAppendingPathComponent:downloadFileName]; diff --git a/Sparkle/SPUDownloaderSession.m b/Sparkle/SPUDownloaderSession.m index 60f18fb9..88470ce5 100644 --- a/Sparkle/SPUDownloaderSession.m +++ b/Sparkle/SPUDownloaderSession.m @@ -70,29 +70,13 @@ - (void)URLSession:(NSURLSession *)__unused session downloadTask:(NSURLSessionDownloadTask *)__unused downloadTask didFinishDownloadingToURL:(NSURL *)location { - if (self.mode == SPUDownloadModeTemporary) - { + if (self.mode == SPUDownloadModeTemporary) { self.downloadFilename = location.path; [self downloadDidFinish]; // file is already in a system temp dir - } - else - { - // Remove our old caches path so we don't start accumulating files in there - NSString *rootPersistentDownloadCachePath = [[SPULocalCacheDirectory cachePathForBundleIdentifier:self.bundleIdentifier] stringByAppendingPathComponent:@"PersistentDownloads"]; + } else { + NSString *tempDir = [super getAndCleanTempDirectory]; - [SPULocalCacheDirectory removeOldItemsInDirectory:rootPersistentDownloadCachePath]; - - NSString *tempDir = [SPULocalCacheDirectory createUniqueDirectoryInDirectory:rootPersistentDownloadCachePath]; - if (tempDir == nil) - { - // Okay, something's really broken with this user's file structure. - [self.download cancel]; - self.download = nil; - - NSError *error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUTemporaryDirectoryError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Can't make a temporary directory for the update download at %@.", tempDir] }]; - - [self.delegate downloaderDidFailWithError:error]; - } else { + if (tempDir != nil) { NSString *downloadFileName = self.desiredFilename; NSString *downloadFileNameDirectory = [tempDir stringByAppendingPathComponent:downloadFileName]; diff --git a/Sparkle/SPUDownloader_Private.h b/Sparkle/SPUDownloader_Private.h index c679c881..af0464c8 100644 --- a/Sparkle/SPUDownloader_Private.h +++ b/Sparkle/SPUDownloader_Private.h @@ -30,6 +30,10 @@ static NSString *SUDownloadingReason = @"Downloading update related file"; @property (nonatomic) SPUDownloadMode mode; @property (nonatomic) BOOL receivedExpectedBytes; +// returns temp directory location if successful; if not successful, +// returns nil after sending error message to client and canceling download +-(NSString*)getAndCleanTempDirectory; + -(void)downloadDidFinishWithData:(SPUDownloadData*)data; @end