Skip to content

Commit

Permalink
Merge pull request sparkle-project#797 from zorgiepoo/download-cleanup
Browse files Browse the repository at this point in the history
Download cleanup
  • Loading branch information
kornelski committed Apr 19, 2016
2 parents 7a9ff3d + a937fa0 commit 3c2f24c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 29 deletions.
39 changes: 34 additions & 5 deletions Sparkle/SUBasicUpdateDriver.m
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,36 @@ - (void)didNotFindUpdate
}]];
}

- (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:)]) {
Expand All @@ -223,13 +251,14 @@ - (void)downloadUpdate
- (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.
Expand Down Expand Up @@ -480,7 +509,7 @@ - (void)installWithToolAndRelaunch:(BOOL)relaunch displayingUserInterface:(BOOL)
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;
Expand Down
1 change: 0 additions & 1 deletion Sparkle/SUHost.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
22 changes: 0 additions & 22 deletions Sparkle/SUHost.m
Original file line number Diff line number Diff line change
Expand Up @@ -77,28 +77,6 @@ - (BOOL)allowsAutomaticUpdates
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) {
Expand Down
2 changes: 1 addition & 1 deletion TestApplication/SUTestApplicationDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ - (void)applicationDidFinishLaunching:(NSNotification * __unused)notification
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;

Expand Down

0 comments on commit 3c2f24c

Please sign in to comment.