forked from uliwitness/Sparkle
/
SUUnarchiver.m
193 lines (161 loc) · 5.9 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//
// 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)_extractArchivePipingDataToCommand:(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);
[self performSelectorOnMainThread:@selector(notifyDelegateOfExtractedLength:) withObject:[NSNumber numberWithLong:len] waitUntilDone:NO];
}
pclose(cmdFP);
}
fclose(fp);
}
return YES;
}
- (BOOL)_extractTAR
{
return [self _extractArchivePipingDataToCommand:@"tar -xC \"$DESTINATION\""];
}
- (BOOL)_extractTGZ
{
return [self _extractArchivePipingDataToCommand:@"tar -zxC \"$DESTINATION\""];
}
- (BOOL)_extractTBZ
{
return [self _extractArchivePipingDataToCommand:@"tar -jxC \"$DESTINATION\""];
}
- (BOOL)_extractZIP
{
return [self _extractArchivePipingDataToCommand:@"ditto -x -k - \"$DESTINATION\""];
}
- (BOOL)_extractDMG
{
// 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)_unarchive
{
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 = [[archivePath 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 invokeWithTarget:self];
[invocation getReturnValue:&result];
}
else
result = NO;
if (result)
[self performSelectorOnMainThread:@selector(notifyDelegateOfSuccess) withObject:nil waitUntilDone:NO];
else
[self performSelectorOnMainThread:@selector(notifyDelegateOfFailure) withObject:nil waitUntilDone:NO];
[pool release];
}
- (void)unarchivePath:(NSString *)path
{
archivePath = [path copy];
[NSThread detachNewThreadSelector:@selector(_unarchive) toTarget:self withObject:nil];
}
- (void)setDelegate:del
{
delegate = del;
}
- (void)cleanUp
{
if ([[archivePath pathExtension] isEqualToString:@".dmg"])
{
[NSTask launchedTaskWithLaunchPath:@"/usr/bin/hdiutil" arguments:[NSArray arrayWithObjects:@"detach", [archivePath stringByDeletingLastPathComponent], @"-force", nil]];
}
[[NSFileManager defaultManager] removeFileAtPath:archivePath handler:nil];
}
- (void)dealloc
{
[archivePath release];
[super dealloc];
}
- (void)notifyDelegateOfExtractedLength:(long)length
{
if ([delegate respondsToSelector:@selector(unarchiver:extractedLength:)])
[delegate unarchiver:self extractedLength:length];
}
- (void)notifyDelegateOfSuccess
{
if ([delegate respondsToSelector:@selector(unarchiverDidFinish:)])
[delegate performSelector:@selector(unarchiverDidFinish:) withObject:self];
}
- (void)notifyDelegateOfFailure
{
if ([delegate respondsToSelector:@selector(unarchiverDidFail:)])
[delegate performSelector:@selector(unarchiverDidFail:) withObject:self];
}
@end