forked from sparkle-project/Sparkle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SUPipedUnarchiver.m
146 lines (118 loc) · 3.57 KB
/
SUPipedUnarchiver.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
//
// SUPipedUnarchiver.m
// Sparkle
//
// Created by Andy Matuschak on 6/16/08.
// Copyright 2008 Andy Matuschak. All rights reserved.
//
#import "SUPipedUnarchiver.h"
#import "SUUnarchiver_Private.h"
#import "SULog.h"
@implementation SUPipedUnarchiver
+ (SEL)selectorConformingToTypeOfPath:(NSString *)path
{
static NSDictionary *typeSelectorDictionary;
if (!typeSelectorDictionary)
typeSelectorDictionary = [@{@".zip": @"extractZIP",
@".tar": @"extractTAR",
@".tar.gz": @"extractTGZ",
@".tgz": @"extractTGZ",
@".tar.bz2": @"extractTBZ",
@".tbz": @"extractTBZ"} retain];
NSString *lastPathComponent = [path lastPathComponent];
for (NSString *currentType in typeSelectorDictionary)
{
if ([currentType length] > [lastPathComponent length]) continue;
if ([[lastPathComponent substringFromIndex:[lastPathComponent length] - [currentType length]] isEqualToString:currentType])
return NSSelectorFromString(typeSelectorDictionary[currentType]);
}
return NULL;
}
- (void)start
{
[NSThread detachNewThreadSelector:[[self class] selectorConformingToTypeOfPath:archivePath] toTarget:self withObject:nil];
}
+ (BOOL)canUnarchivePath:(NSString *)path
{
return ([self selectorConformingToTypeOfPath:path] != nil);
}
// This method abstracts the types that use a command line tool piping data from stdin.
- (void)extractArchivePipingDataToCommand:(NSString *)command
{
// *** GETS CALLED ON NON-MAIN THREAD!!!
@autoreleasepool {
FILE *fp = NULL, *cmdFP = NULL;
char *oldDestinationString = NULL;
SULog(@"Extracting %@ using '%@'",archivePath,command);
// Get the file size.
NSNumber *fs = [[NSFileManager defaultManager] attributesOfItemAtPath:archivePath error:nil][NSFileSize];
if (fs == nil) goto reportError;
// Thank you, Allan Odgaard!
// (who wrote the following extraction alg.)
fp = fopen([archivePath fileSystemRepresentation], "r");
if (!fp) goto reportError;
oldDestinationString = getenv("DESTINATION");
setenv("DESTINATION", [[archivePath stringByDeletingLastPathComponent] fileSystemRepresentation], 1);
cmdFP = popen([command fileSystemRepresentation], "w");
size_t written;
if (!cmdFP) goto reportError;
char buf[32*1024];
size_t len;
while((len = fread(buf, 1, 32*1024, fp)))
{
written = fwrite(buf, 1, len, cmdFP);
if( written < len )
{
pclose(cmdFP);
goto reportError;
}
dispatch_async(dispatch_get_main_queue(), ^{
[self notifyDelegateOfExtractedLength:len];
});
}
pclose(cmdFP);
if (ferror(fp)) {
goto reportError;
}
dispatch_async(dispatch_get_main_queue(), ^{
[self notifyDelegateOfSuccess];
});
goto finally;
reportError:
dispatch_async(dispatch_get_main_queue(), ^{
[self notifyDelegateOfFailure];
});
finally:
if (fp)
fclose(fp);
if (oldDestinationString)
setenv("DESTINATION", oldDestinationString, 1);
else
unsetenv("DESTINATION");
}
}
- (void)extractTAR
{
// *** GETS CALLED ON NON-MAIN THREAD!!!
[self extractArchivePipingDataToCommand:@"tar -xC \"$DESTINATION\""];
}
- (void)extractTGZ
{
// *** GETS CALLED ON NON-MAIN THREAD!!!
[self extractArchivePipingDataToCommand:@"tar -zxC \"$DESTINATION\""];
}
- (void)extractTBZ
{
// *** GETS CALLED ON NON-MAIN THREAD!!!
[self extractArchivePipingDataToCommand:@"tar -jxC \"$DESTINATION\""];
}
- (void)extractZIP
{
// *** GETS CALLED ON NON-MAIN THREAD!!!
[self extractArchivePipingDataToCommand:@"ditto -x -k - \"$DESTINATION\""];
}
+ (void)load
{
[self registerImplementation:self];
}
@end