Skip to content

Commit

Permalink
Make sure we delete the temporary folder when the user cancels the do…
Browse files Browse the repository at this point in the history
…wnload. Also make sure we delete it on 10.4, which seems to have a bug in removeFileAtPath: that won't delete nested empty folders. As a quick fix, I just trash the folder in that case. We already put the old version in the trash anyway, and this saves me from having to write, test and debug a recursive removal function.
  • Loading branch information
uliwitness committed Dec 11, 2009
1 parent bc3fd45 commit 0ffcde4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
1 change: 1 addition & 0 deletions SUBasicUpdateDriver.h
Expand Up @@ -18,6 +18,7 @@

NSURLDownload *download;
NSString *downloadPath;
NSString *tempDir;

NSString *relaunchPath;
}
Expand Down
17 changes: 10 additions & 7 deletions SUBasicUpdateDriver.m
Expand Up @@ -143,11 +143,13 @@ - (void)download:(NSURLDownload *)d decideDestinationWithSuggestedFilename:(NSSt
// Not using a GUID here because hdiutil (for DMGs) for some reason chokes on GUIDs. Too long? I really have no idea.
NSString *prefix = [NSString stringWithFormat:@"%@ %@ Update", [host name], [host version]];
NSString *desktopFolder = [@"~/Desktop" stringByExpandingTildeInPath];
NSString *tempDir = [desktopFolder stringByAppendingPathComponent:prefix];
[tempDir release];
tempDir = [[desktopFolder stringByAppendingPathComponent:prefix] retain];
int cnt=1;
while ([[NSFileManager defaultManager] fileExistsAtPath:tempDir] && cnt <= 999)
{
tempDir = [desktopFolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@ %d", prefix, cnt++]];
[tempDir release];
tempDir = [[desktopFolder stringByAppendingPathComponent:[NSString stringWithFormat:@"%@ %d", prefix, cnt++]] retain];
}

#if MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_4
Expand Down Expand Up @@ -188,8 +190,8 @@ - (void)downloadDidFinish:(NSURLDownload *)d
- (void)download:(NSURLDownload *)download didFailWithError:(NSError *)error
{
// Get rid of what we've downloaded so far, if anything.
if (downloadPath != nil)
[[NSWorkspace sharedWorkspace] performFileOperation:NSWorkspaceRecycleOperation source:[downloadPath stringByDeletingLastPathComponent] destination:@"" files:[NSArray arrayWithObject:[downloadPath lastPathComponent]] tag:NULL];
if (tempDir != nil) // tempDir contains downloadPath, so we implicitly delete both here.
[[NSWorkspace sharedWorkspace] performFileOperation:NSWorkspaceRecycleOperation source:[tempDir stringByDeletingLastPathComponent] destination:@"" files:[NSArray arrayWithObject:[tempDir lastPathComponent]] tag:NULL];
[self abortUpdateWithError:[NSError errorWithDomain:SUSparkleErrorDomain code:SURelaunchError userInfo:[NSDictionary dictionaryWithObjectsAndKeys:SULocalizedString(@"An error occurred while downloading the update. Please try again later.", nil), NSLocalizedDescriptionKey, [error localizedDescription], NSLocalizedFailureReasonErrorKey, nil]]];
}

Expand Down Expand Up @@ -292,7 +294,7 @@ - (void)installAndRelaunchWithTool
if ([[updater delegate] respondsToSelector:@selector(pathToRelaunchForUpdater:)])
pathToRelaunch = [[updater delegate] pathToRelaunchForUpdater:updater];
NSString *relaunchToolPath = [relaunchPath stringByAppendingPathComponent: @"/Contents/MacOS/finish_installation"];
[NSTask launchedTaskWithLaunchPath: relaunchToolPath arguments:[NSArray arrayWithObjects:pathToRelaunch, [NSString stringWithFormat:@"%d", [[NSProcessInfo processInfo] processIdentifier]], [downloadPath stringByDeletingLastPathComponent], nil]];
[NSTask launchedTaskWithLaunchPath: relaunchToolPath arguments:[NSArray arrayWithObjects:pathToRelaunch, [NSString stringWithFormat:@"%d", [[NSProcessInfo processInfo] processIdentifier]], tempDir, nil]];

[NSApp terminate:self];
}
Expand All @@ -303,9 +305,9 @@ - (void)installAndRelaunchWithTool
- (void)cleanUp
{
#if MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_4
[[NSFileManager defaultManager] removeFileAtPath:[downloadPath stringByDeletingLastPathComponent] handler:nil];
[[NSFileManager defaultManager] removeFileAtPath: tempDir handler:nil];
#else
[[NSFileManager defaultManager] removeItemAtPath:[downloadPath stringByDeletingLastPathComponent] error:NULL];
[[NSFileManager defaultManager] removeItemAtPath: tempDir error:NULL];
#endif
}

Expand Down Expand Up @@ -339,6 +341,7 @@ - (void)dealloc
[updateItem release];
[download release];
[downloadPath release];
[tempDir release];
[relaunchPath release];
[super dealloc];
}
Expand Down
7 changes: 6 additions & 1 deletion SUUIBasedUpdateDriver.m
Expand Up @@ -118,11 +118,16 @@ - (void)download:(NSURLDownload *)download didReceiveDataOfLength:(NSUInteger)le
[statusController setStatusText:[NSString stringWithFormat:SULocalizedString(@"%@ downloaded", nil), [self _humanReadableSizeFromDouble:[statusController progressValue]]]];
}

- (IBAction)cancelDownload:sender
- (IBAction)cancelDownload: (id)sender
{
if (download)
[download cancel];
[self abortUpdate];
if (tempDir != nil) // tempDir contains downloadPath, so we implicitly delete both here.
{
if( ![[NSFileManager defaultManager] removeFileAtPath: tempDir handler: nil] )
[[NSWorkspace sharedWorkspace] performFileOperation:NSWorkspaceRecycleOperation source:[tempDir stringByDeletingLastPathComponent] destination:@"" files:[NSArray arrayWithObject:[tempDir lastPathComponent]] tag:NULL];
}
}

- (void)extractUpdate
Expand Down

0 comments on commit 0ffcde4

Please sign in to comment.