Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 144 lines (113 sloc) 4.044 kB
93ea93d @andymatuschak Fixes 236695
andymatuschak authored
1 //
2 // SUPipedUnarchiver.m
3 // Sparkle
4 //
5 // Created by Andy Matuschak on 6/16/08.
6 // Copyright 2008 Andy Matuschak. All rights reserved.
7 //
8
9 #import "SUPipedUnarchiver.h"
10 #import "SUUnarchiver_Private.h"
43a0a7d @uliwitness Merge of changes from SVN repository:
uliwitness authored
11 #import "SULog.h"
12
93ea93d @andymatuschak Fixes 236695
andymatuschak authored
13
14 @implementation SUPipedUnarchiver
15
dab8a36 renamed all methods that start with underscore, since that's reserved…
Sean McBride authored
16 + (SEL)selectorConformingToTypeOfPath:(NSString *)path
93ea93d @andymatuschak Fixes 236695
andymatuschak authored
17 {
18 static NSDictionary *typeSelectorDictionary;
19 if (!typeSelectorDictionary)
dab8a36 renamed all methods that start with underscore, since that's reserved…
Sean McBride authored
20 typeSelectorDictionary = [[NSDictionary dictionaryWithObjectsAndKeys:@"extractZIP", @".zip", @"extractTAR", @".tar",
21 @"extractTGZ", @".tar.gz", @"extractTGZ", @".tgz",
22 @"extractTBZ", @".tar.bz2", @"extractTBZ", @".tbz", nil] retain];
93ea93d @andymatuschak Fixes 236695
andymatuschak authored
23
58a05e2 @andymatuschak Fixes 243533 (for real this time, I hope)
andymatuschak authored
24 NSString *lastPathComponent = [path lastPathComponent];
93ea93d @andymatuschak Fixes 236695
andymatuschak authored
25 NSEnumerator *typeEnumerator = [typeSelectorDictionary keyEnumerator];
26 id currentType;
27 while ((currentType = [typeEnumerator nextObject]))
28 {
b4c7dbe @andymatuschak Added a guard to the piped unarchiver to fail more gracefully for bad…
andymatuschak authored
29 if ([currentType length] > [lastPathComponent length]) continue;
58a05e2 @andymatuschak Fixes 243533 (for real this time, I hope)
andymatuschak authored
30 if ([[lastPathComponent substringFromIndex:[lastPathComponent length] - [currentType length]] isEqualToString:currentType])
93ea93d @andymatuschak Fixes 236695
andymatuschak authored
31 return NSSelectorFromString([typeSelectorDictionary objectForKey:currentType]);
32 }
33 return NULL;
34 }
35
36 - (void)start
37 {
dab8a36 renamed all methods that start with underscore, since that's reserved…
Sean McBride authored
38 [NSThread detachNewThreadSelector:[[self class] selectorConformingToTypeOfPath:archivePath] toTarget:self withObject:nil];
93ea93d @andymatuschak Fixes 236695
andymatuschak authored
39 }
40
dab8a36 renamed all methods that start with underscore, since that's reserved…
Sean McBride authored
41 + (BOOL)canUnarchivePath:(NSString *)path
93ea93d @andymatuschak Fixes 236695
andymatuschak authored
42 {
dab8a36 renamed all methods that start with underscore, since that's reserved…
Sean McBride authored
43 return ([self selectorConformingToTypeOfPath:path] != nil);
93ea93d @andymatuschak Fixes 236695
andymatuschak authored
44 }
45
46 // This method abstracts the types that use a command line tool piping data from stdin.
dab8a36 renamed all methods that start with underscore, since that's reserved…
Sean McBride authored
47 - (void)extractArchivePipingDataToCommand:(NSString *)command
93ea93d @andymatuschak Fixes 236695
andymatuschak authored
48 {
43a0a7d @uliwitness Merge of changes from SVN repository:
uliwitness authored
49 // *** GETS CALLED ON NON-MAIN THREAD!!!
50
51
93ea93d @andymatuschak Fixes 236695
andymatuschak authored
52 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
6da2774 @andymatuschak Removed a warning, refactored the piped unarchiver code a little more.
andymatuschak authored
53 FILE *fp = NULL, *cmdFP = NULL;
93ea93d @andymatuschak Fixes 236695
andymatuschak authored
54
95398cd @andymatuschak Fixed a no-autorelease-pool leak.
andymatuschak authored
55 SULog(@"Extracting %@ using '%@'",archivePath,command);
56
93ea93d @andymatuschak Fixes 236695
andymatuschak authored
57 // Get the file size.
4fad530 Change availability checks so that they build on 10.4
Stuart Morgan authored
58 #if MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_4
d7774c0 performed a code review, specifically:
Sean McBride authored
59 NSNumber *fs = [[[NSFileManager defaultManager] fileAttributesAtPath:archivePath traverseLink:NO] objectForKey:NSFileSize];
54299d7 added #ifdef's for Tiger support
August Joki authored
60 #else
d7774c0 performed a code review, specifically:
Sean McBride authored
61 NSNumber *fs = [[[NSFileManager defaultManager] attributesOfItemAtPath:archivePath error:nil] objectForKey:NSFileSize];
54299d7 added #ifdef's for Tiger support
August Joki authored
62 #endif
93ea93d @andymatuschak Fixes 236695
andymatuschak authored
63 if (fs == nil) goto reportError;
64
65 // Thank you, Allan Odgaard!
66 // (who wrote the following extraction alg.)
6da2774 @andymatuschak Removed a warning, refactored the piped unarchiver code a little more.
andymatuschak authored
67 fp = fopen([archivePath fileSystemRepresentation], "r");
68 if (!fp) goto reportError;
93ea93d @andymatuschak Fixes 236695
andymatuschak authored
69
97c71ef @andymatuschak Fixes #170: An environment variable set by Sparkle
andymatuschak authored
70 char *oldDestinationString = getenv("DESTINATION");
eac4134 @andymatuschak Replaced UTF8String with fileSystemRepresentation and [NSString initW…
andymatuschak authored
71 setenv("DESTINATION", [[archivePath stringByDeletingLastPathComponent] fileSystemRepresentation], 1);
6da2774 @andymatuschak Removed a warning, refactored the piped unarchiver code a little more.
andymatuschak authored
72 cmdFP = popen([command fileSystemRepresentation], "w");
d7774c0 performed a code review, specifically:
Sean McBride authored
73 size_t written;
6da2774 @andymatuschak Removed a warning, refactored the piped unarchiver code a little more.
andymatuschak authored
74 if (!cmdFP) goto reportError;
75
76 char buf[32*1024];
d7774c0 performed a code review, specifically:
Sean McBride authored
77 size_t len;
6da2774 @andymatuschak Removed a warning, refactored the piped unarchiver code a little more.
andymatuschak authored
78 while((len = fread(buf, 1, 32*1024, fp)))
79 {
b571a3d @andymatuschak Fixing Bug #398921: Sparkle should detect read/write errors on archiv…
andymatuschak authored
80 written = fwrite(buf, 1, len, cmdFP);
81 if( written < len )
82 {
83 pclose(cmdFP);
84 goto reportError;
85 }
86
dab8a36 renamed all methods that start with underscore, since that's reserved…
Sean McBride authored
87 [self performSelectorOnMainThread:@selector(notifyDelegateOfExtractedLength:) withObject:[NSNumber numberWithUnsignedLong:len] waitUntilDone:NO];
93ea93d @andymatuschak Fixes 236695
andymatuschak authored
88 }
6da2774 @andymatuschak Removed a warning, refactored the piped unarchiver code a little more.
andymatuschak authored
89 pclose(cmdFP);
93ea93d @andymatuschak Fixes 236695
andymatuschak authored
90
b571a3d @andymatuschak Fixing Bug #398921: Sparkle should detect read/write errors on archiv…
andymatuschak authored
91 if( ferror( fp ) )
92 goto reportError;
93
dab8a36 renamed all methods that start with underscore, since that's reserved…
Sean McBride authored
94 [self performSelectorOnMainThread:@selector(notifyDelegateOfSuccess) withObject:nil waitUntilDone:NO];
93ea93d @andymatuschak Fixes 236695
andymatuschak authored
95 goto finally;
96
97 reportError:
dab8a36 renamed all methods that start with underscore, since that's reserved…
Sean McBride authored
98 [self performSelectorOnMainThread:@selector(notifyDelegateOfFailure) withObject:nil waitUntilDone:NO];
93ea93d @andymatuschak Fixes 236695
andymatuschak authored
99
100 finally:
101 if (fp)
102 fclose(fp);
97c71ef @andymatuschak Fixes #170: An environment variable set by Sparkle
andymatuschak authored
103 if (oldDestinationString)
104 setenv("DESTINATION", oldDestinationString, 1);
105 else
106 unsetenv("DESTINATION");
95398cd @andymatuschak Fixed a no-autorelease-pool leak.
andymatuschak authored
107 [pool release];
93ea93d @andymatuschak Fixes 236695
andymatuschak authored
108 }
109
dab8a36 renamed all methods that start with underscore, since that's reserved…
Sean McBride authored
110 - (void)extractTAR
93ea93d @andymatuschak Fixes 236695
andymatuschak authored
111 {
43a0a7d @uliwitness Merge of changes from SVN repository:
uliwitness authored
112 // *** GETS CALLED ON NON-MAIN THREAD!!!
113
dab8a36 renamed all methods that start with underscore, since that's reserved…
Sean McBride authored
114 return [self extractArchivePipingDataToCommand:@"tar -xC \"$DESTINATION\""];
93ea93d @andymatuschak Fixes 236695
andymatuschak authored
115 }
116
dab8a36 renamed all methods that start with underscore, since that's reserved…
Sean McBride authored
117 - (void)extractTGZ
93ea93d @andymatuschak Fixes 236695
andymatuschak authored
118 {
43a0a7d @uliwitness Merge of changes from SVN repository:
uliwitness authored
119 // *** GETS CALLED ON NON-MAIN THREAD!!!
120
dab8a36 renamed all methods that start with underscore, since that's reserved…
Sean McBride authored
121 return [self extractArchivePipingDataToCommand:@"tar -zxC \"$DESTINATION\""];
93ea93d @andymatuschak Fixes 236695
andymatuschak authored
122 }
123
dab8a36 renamed all methods that start with underscore, since that's reserved…
Sean McBride authored
124 - (void)extractTBZ
93ea93d @andymatuschak Fixes 236695
andymatuschak authored
125 {
43a0a7d @uliwitness Merge of changes from SVN repository:
uliwitness authored
126 // *** GETS CALLED ON NON-MAIN THREAD!!!
127
dab8a36 renamed all methods that start with underscore, since that's reserved…
Sean McBride authored
128 return [self extractArchivePipingDataToCommand:@"tar -jxC \"$DESTINATION\""];
93ea93d @andymatuschak Fixes 236695
andymatuschak authored
129 }
130
dab8a36 renamed all methods that start with underscore, since that's reserved…
Sean McBride authored
131 - (void)extractZIP
93ea93d @andymatuschak Fixes 236695
andymatuschak authored
132 {
43a0a7d @uliwitness Merge of changes from SVN repository:
uliwitness authored
133 // *** GETS CALLED ON NON-MAIN THREAD!!!
134
dab8a36 renamed all methods that start with underscore, since that's reserved…
Sean McBride authored
135 return [self extractArchivePipingDataToCommand:@"ditto -x -k - \"$DESTINATION\""];
93ea93d @andymatuschak Fixes 236695
andymatuschak authored
136 }
137
138 + (void)load
139 {
dab8a36 renamed all methods that start with underscore, since that's reserved…
Sean McBride authored
140 [self registerImplementation:self];
93ea93d @andymatuschak Fixes 236695
andymatuschak authored
141 }
142
143 @end
Something went wrong with that request. Please try again.