/
SUUnarchiver.m
167 lines (140 loc) · 5.32 KB
/
SUUnarchiver.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//
// SUUnarchiver.m
// Sparkle
//
// Created by Andy Matuschak on 3/16/06.
// Copyright 2006 Andy Matuschak. All rights reserved.
//
#import "Sparkle.h"
#import "SUUnarchiver.h"
@implementation SUUnarchiver
// This method abstracts the types that use a command line tool piping data from stdin.
- (BOOL)_extractArchivePath:archivePath pipingDataToCommand:(NSString *)command
{
// Get the file size.
NSNumber *fs = [[[NSFileManager defaultManager] fileAttributesAtPath:archivePath traverseLink:NO] objectForKey:NSFileSize];
if (fs == nil) { return NO; }
// Thank you, Allan Odgaard!
// (who wrote the following extraction alg.)
long current = 0;
FILE *fp, *cmdFP;
if ((fp = fopen([archivePath fileSystemRepresentation], "r")))
{
setenv("DESTINATION", [[archivePath stringByDeletingLastPathComponent] UTF8String], 1);
if ((cmdFP = popen([command fileSystemRepresentation], "w")))
{
char buf[32*1024];
long len;
while((len = fread(buf, 1, 32 * 1024, fp)))
{
current += len;
NSEvent *event;
while((event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:nil inMode:NSDefaultRunLoopMode dequeue:YES]))
[NSApp sendEvent:event];
fwrite(buf, 1, len, cmdFP);
if ([delegate respondsToSelector:@selector(unarchiver:extractedLength:)])
[delegate unarchiver:self extractedLength:len];
}
pclose(cmdFP);
}
fclose(fp);
}
return YES;
}
- (BOOL)_extractTAR:(NSString *)archivePath
{
return [self _extractArchivePath:archivePath pipingDataToCommand:@"tar -xC \"$DESTINATION\""];
}
- (BOOL)_extractTGZ:(NSString *)archivePath
{
return [self _extractArchivePath:archivePath pipingDataToCommand:@"tar -zxC \"$DESTINATION\""];
}
- (BOOL)_extractTBZ:(NSString *)archivePath
{
return [self _extractArchivePath:archivePath pipingDataToCommand:@"tar -jxC \"$DESTINATION\""];
}
- (BOOL)_extractZIP:(NSString *)archivePath
{
return [self _extractArchivePath:archivePath pipingDataToCommand:@"ditto -x -k - \"$DESTINATION\""];
}
- (BOOL)_extractDMG:(NSString *)archivePath
{
// get a unique mount point path
NSString *mountPoint = [[archivePath stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"mp"];
int cnt=1;
while ([[NSFileManager defaultManager] fileExistsAtPath:mountPoint] && cnt <= 999)
mountPoint = [[archivePath stringByDeletingLastPathComponent] stringByAppendingPathComponent:[NSString stringWithFormat:@"mp%d", cnt++]];
if (![[NSFileManager defaultManager] fileExistsAtPath:mountPoint])
{
// create mount point folder
[[NSFileManager defaultManager] createDirectoryAtPath:mountPoint attributes:nil];
if ([[NSFileManager defaultManager] fileExistsAtPath:mountPoint])
{
NSArray* arguments = [NSArray arrayWithObjects:@"attach", archivePath, @"-mountpoint", mountPoint, @"-noverify", @"-nobrowse", @"-noautoopen", nil];
// set up a pipe and push "yes" (y works too), this will accept any license agreement crap
// not every .dmg needs this, but this will make sure it works with everyone
NSData* yesData = [[[NSData alloc] initWithBytes:"yes\n" length:4] autorelease];
[NTSynchronousTask task:@"/usr/bin/hdiutil" directory:@"/" withArgs:arguments input:yesData];
}
}
return YES;
}
- (void)_unarchivePath:(NSString *)path
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
// This dictionary associates names of methods responsible for extraction with file extensions.
// The methods take the path of the archive to extract. They return a BOOL indicating whether
// we should continue with the update; returns NO if an error occurred.
NSDictionary *commandDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
@"_extractTBZ:", @".tbz",
@"_extractTBZ:", @".tar.bz2",
@"_extractTGZ:", @".tgz",
@"_extractTGZ:", @".tar.gz",
@"_extractTAR:", @".tar",
@"_extractZIP:", @".zip",
@"_extractDMG:", @".dmg",
nil];
SEL command = NULL;
NSString *theLastPathComponent = [[path lastPathComponent] lowercaseString];
NSEnumerator *theEnumerator = [[commandDictionary allKeys] objectEnumerator];
NSString *theExtension = NULL;
while ((theExtension = [theEnumerator nextObject]) != NULL)
{
if ([[theLastPathComponent substringFromIndex:[theLastPathComponent length] - [theExtension length]] isEqualToString:theExtension])
{
command = NSSelectorFromString([commandDictionary objectForKey:theExtension]);
break;
}
}
BOOL result;
if (command)
{
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:command]];
[invocation setSelector:command];
[invocation setArgument:&path atIndex:2]; // 0 and 1 are private!
[invocation invokeWithTarget:self];
[invocation getReturnValue:&result];
}
else
result = NO;
if (result)
{
if ([delegate respondsToSelector:@selector(unarchiverDidFinish:)])
[delegate performSelector:@selector(unarchiverDidFinish:) withObject:self];
}
else
{
if ([delegate respondsToSelector:@selector(unarchiverDidFail:)])
[delegate performSelector:@selector(unarchiverDidFail:) withObject:self];
}
[pool release];
}
- (void)unarchivePath:(NSString *)path
{
[NSThread detachNewThreadSelector:@selector(_unarchivePath:) toTarget:self withObject:path];
}
- (void)setDelegate:del
{
delegate = del;
}
@end