#3928 Update blocklist download to handle archives (including ZIP)

This commit is contained in:
Mitchell Livingston 2011-01-25 01:43:11 +00:00
parent 125d2506cd
commit 21267cd145
1 changed files with 86 additions and 3 deletions

View File

@ -27,11 +27,10 @@
#import "BlocklistScheduler.h"
#import "PrefsController.h"
#define FILE_NAME @"blocklist.temp"
@interface BlocklistDownloader (Private)
- (void) startDownload;
- (void) decompressBlocklist;
- (void) finishDownloadSuccess;
@end
@ -94,8 +93,15 @@ BlocklistDownloader * fDownloader = nil;
[self release];
}
//using the actual filename is the best bet
- (void) download: (NSURLDownload *) download decideDestinationWithSuggestedFilename: (NSString *) filename
{
[fDownload setDestination: [NSTemporaryDirectory() stringByAppendingPathComponent: filename] allowOverwrite: NO];
}
- (void) download: (NSURLDownload *) download didCreateDestination: (NSString *) path
{
[fDestination release];
fDestination = [path retain];
}
@ -132,6 +138,11 @@ BlocklistDownloader * fDownloader = nil;
[self performSelectorInBackground: @selector(finishDownloadSuccess) withObject: nil];
}
- (BOOL) download: (NSURLDownload *) download shouldDecodeSourceDataOfMIMEType: (NSString *) encodingType
{
return YES;
}
@end
@implementation BlocklistDownloader (Private)
@ -151,9 +162,78 @@ BlocklistDownloader * fDownloader = nil;
NSURLRequest * request = [NSURLRequest requestWithURL: [NSURL URLWithString: urlString]];
fDownload = [[NSURLDownload alloc] initWithRequest: request delegate: self];
[fDownload setDestination: [NSTemporaryDirectory() stringByAppendingPathComponent: FILE_NAME] allowOverwrite: NO];
}
//.gz, .tar.gz, .tgz, and .bgz will be decompressed by NSURLDownload for us. However, we have to do .zip files manually.
- (void) decompressBlocklist
{
if ([[[fDestination pathExtension] lowercaseString] isEqualToString: @"zip"]) {
BOOL success = NO;
NSString * workingDirectory = [fDestination stringByDeletingLastPathComponent];
//First, perform the actual unzipping
NSTask * unzip = [[NSTask alloc] init];
[unzip setLaunchPath: @"/usr/bin/unzip"];
[unzip setCurrentDirectoryPath: workingDirectory];
[unzip setArguments: [NSArray arrayWithObjects:
@"-o", /* overwrite */
@"-q", /* quiet! */
fDestination, /* source zip file */
@"-d", workingDirectory, /*destination*/
nil]];
@try
{
[unzip launch];
[unzip waitUntilExit];
if ([unzip terminationStatus] == 0)
success = YES;
}
@catch(id exc)
{
success = NO;
}
[unzip release];
if (success) {
//Now find out what file we actually extracted; don't just assume it matches the zipfile's name
NSTask *zipinfo;
zipinfo = [[NSTask alloc] init];
[zipinfo setLaunchPath: @"/usr/bin/zipinfo"];
[zipinfo setArguments: [NSArray arrayWithObjects:
@"-1", /* just the filename */
fDestination, /* source zip file */
nil]];
[zipinfo setStandardOutput: [NSPipe pipe]];
@try
{
NSFileHandle * zipinfoOutput = [[zipinfo standardOutput] fileHandleForReading];
[zipinfo launch];
[zipinfo waitUntilExit];
NSString * actualFilename = [[[NSString alloc] initWithData: [zipinfoOutput readDataToEndOfFile]
encoding: NSUTF8StringEncoding] autorelease];
actualFilename = [actualFilename stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString * newBlocklistPath = [workingDirectory stringByAppendingPathComponent: actualFilename];
//Finally, delete the ZIP file; we're done with it, and we'll return the unzipped blocklist
[[NSFileManager defaultManager] removeItemAtPath: fDestination error: NULL];
[fDestination release];
fDestination = [newBlocklistPath retain];
}
@catch(id exc) {}
[zipinfo release];
}
}
}
- (void) finishDownloadSuccess
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
@ -162,6 +242,9 @@ BlocklistDownloader * fDownloader = nil;
//process data
NSAssert(fDestination != nil, @"the blocklist file destination has not been specified");
[self decompressBlocklist];
const int count = tr_blocklistSetContent([PrefsController handle], [fDestination UTF8String]);
//delete downloaded file