-
Notifications
You must be signed in to change notification settings - Fork 55
/
PGArchiveAdapter.m
390 lines (326 loc) · 12.1 KB
/
PGArchiveAdapter.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/* Copyright © 2007-2009, The Sequential Project
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the the Sequential Project nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE SEQUENTIAL PROJECT ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE SEQUENTIAL PROJECT BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
#import "PGArchiveAdapter.h"
// Models
#import "PGDocument.h"
#import "PGNode.h"
#import "PGResourceIdentifier.h"
#import "PGDataProvider.h"
// Controllers
#import "PGDocumentController.h"
// Other Sources
#import "PGFoundationAdditions.h"
@interface PGDataProvider(PGArchiveDataProvider)
- (XADArchive *)archive;
- (int)entry;
@end
@interface PGArchiveDataProvider : PGDataProvider
{
@private
XADArchive *_archive;
int _entry;
}
- (id)initWithArchive:(XADArchive *)archive entry:(int)entry;
@end
@interface PGFolderDataProvider : PGDataProvider
@end
@interface PGArchiveAdapter(Private)
- (void)_threaded_setError:(NSError *)error forNode:(PGNode *)node;
- (void)_updateThumbnailsOfChildren;
@end
@interface XADArchive(PGAdditions)
- (BOOL)PG_entryIsInvisibleForName:(NSString *)name;
- (NSString *)PG_commonRootPath;
- (OSType)PG_OSTypeForEntry:(int)entry;
@end
@implementation PGArchiveAdapter
#pragma mark -PGArchiveAdapter
- (NSArray *)nodesUnderPath:(NSString *)path parentAdapter:(PGContainerAdapter *)parent remainingIndexes:(NSMutableIndexSet *)indexes
{
NSParameterAssert(path);
NSParameterAssert(parent);
NSParameterAssert(_archive);
NSMutableArray *const children = [NSMutableArray array];
NSUInteger i = [indexes firstIndex];
for(; NSNotFound != i; i = [indexes indexGreaterThanIndex:i]) {
NSString *const entryPath = [_archive nameOfEntry:i];
if(!entryPath) continue;
if([path length] && ![entryPath hasPrefix:path]) continue;
if([_archive PG_entryIsInvisibleForName:entryPath]) continue;
[indexes removeIndex:i];
NSString *const subpath = [path stringByAppendingPathComponent:[[entryPath substringFromIndex:[path length]] PG_firstPathComponent]];
if(PGEqualObjects(path, entryPath)) continue;
BOOL const isEntrylessFolder = !PGEqualObjects(subpath, entryPath);
BOOL const isFile = !isEntrylessFolder && ![_archive entryIsDirectory:i];
PGDisplayableIdentifier *const identifier = [[[[self node] identifier] subidentifierWithIndex:isEntrylessFolder ? NSNotFound : i] displayableIdentifier];
[identifier setNaturalDisplayName:[subpath lastPathComponent]];
PGNode *const node = [[[PGNode alloc] initWithParent:parent identifier:identifier] autorelease];
if(isFile) [node setDataProvider:[[[PGArchiveDataProvider alloc] initWithArchive:_archive entry:i] autorelease]];
else {
[node setDataProvider:[[[PGFolderDataProvider alloc] init] autorelease]];
if(isEntrylessFolder) [indexes addIndex:i]; // We ended up taking care of a folder in its path instead.
PGContainerAdapter *const adapter = (PGContainerAdapter *)[node resourceAdapter];
[adapter setUnsortedChildren:[self nodesUnderPath:subpath parentAdapter:adapter remainingIndexes:indexes] presortedOrder:PGUnsorted];
}
[identifier setIcon:[[[node resourceAdapter] dataProvider] icon]];
if(node) [children addObject:node];
}
return children;
}
#pragma mark -PGArchiveAdapter(Private)
- (void)_threaded_setError:(NSError *)error forNode:(PGNode *)node;
{
// TODO: Figure this out...
// [node performSelectorOnMainThread:@selector(setError:) withObject:error waitUntilDone:YES];
}
- (void)_updateThumbnailsOfChildren
{
[[self document] noteNodeThumbnailDidChange:[self node] recursively:YES];
}
#pragma mark -PGContainerAdapter
- (PGRecursionPolicy)descendantRecursionPolicy
{
return PGRecurseToAnyDepth;
}
#pragma mark -PGResourceAdapter
- (BOOL)canSaveData
{
return YES;
}
#pragma mark -
- (void)load
{
if(!_archive) {
XADError error = XADNoError;
PGDataProvider *const dataProvider = [self dataProvider];
PGResourceIdentifier *const ident = [dataProvider identifier];
if([dataProvider archive]) @synchronized([dataProvider archive]) {
_archive = [[XADArchive alloc] initWithArchive:[dataProvider archive] entry:[dataProvider entry] delegate:self error:&error];
} else if([ident isFileIdentifier]) _archive = [[XADArchive alloc] initWithFile:[[ident URL] path] delegate:self error:&error]; // -data will return data for file URLs, but it's worth using -[XADArchive initWithFile:...].
else {
NSData *const data = [self data];
if(data) _archive = [[XADArchive alloc] initWithData:data delegate:self error:&error];
}
if(!_archive || error != XADNoError || [_archive isCorrupted]) return [[self node] fallbackFromFailedAdapter:self]; // TODO: Return an appropriate error.
}
NSArray *const children = [self nodesUnderPath:[_archive PG_commonRootPath] parentAdapter:self remainingIndexes:[NSMutableIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [_archive numberOfEntries])]];
[self setUnsortedChildren:children presortedOrder:PGUnsorted];
[[self node] loadFinishedForAdapter:self];
}
#pragma mark -NSObject
- (void)dealloc
{
@synchronized(_archive) {
[_archive release];
_archive = nil;
}
[super dealloc];
}
#pragma mark -NSObject(XADArchiveDelegate)
- (void)archiveNeedsPassword:(XADArchive *)archive
{
_needsPassword = YES;
[self _threaded_setError:[NSError errorWithDomain:PGNodeErrorDomain code:PGPasswordError userInfo:nil] forNode:_currentSubnode];
}
-(NSStringEncoding)archive:(XADArchive *)archive encodingForData:(NSData *)data guess:(NSStringEncoding)guess confidence:(float)confidence
{
return guess;
}
//#pragma mark -<PGNodeDataSource>
//
//- (NSDictionary *)fileAttributesForNode:(PGNode *)node
//{
// NSUInteger const i = [[node identifier] index];
// if(NSNotFound == i) return nil;
// NSMutableDictionary *const attributes = [NSMutableDictionary dictionary];
// [attributes PG_setObject:[[_archive attributesOfEntry:i] objectForKey:XADCreationDateKey] forKey:NSFileCreationDate];
// if(![_archive entryIsDirectory:i]) [attributes setObject:[NSNumber numberWithUnsignedLongLong:[_archive representativeSizeOfEntry:i]] forKey:NSFileSize];
// return attributes;
//}
//- (void)node:(PGNode *)sender willLoadWithInfo:(NSMutableDictionary *)info
//{
// NSUInteger const i = [[sender identifier] index];
// if(NSNotFound == i) return;
// if([_archive entryIsArchive:i]) [info setObject:[NSNumber numberWithBool:YES] forKey:PGKnownToBeArchiveKey];
// if(![info objectForKey:PGOSTypeKey]) [info PG_setObject:[_archive PG_OSTypeForEntry:i standardFormat:NO] forKey:PGOSTypeKey];
// if(![info objectForKey:PGExtensionKey]) [info PG_setObject:[[_archive nameOfEntry:i] pathExtension] forKey:PGExtensionKey];
//}
//- (BOOL)node:(PGNode *)sender getData:(out NSData **)outData info:(NSDictionary *)info fast:(BOOL)flag
//{
// NSUInteger const i = [[sender identifier] index];
// if(NSNotFound == i || flag || [_archive entryIsDirectory:i]) {
// if(outData) *outData = nil;
// return YES;
// }
// NSData *data = nil;
// @synchronized(_archive) {
// NSString *const pass = [info objectForKey:PGPasswordKey];
// if(pass) [_archive setPassword:pass];
// BOOL const neededPassword = _needsPassword;
// _needsPassword = NO;
// _currentSubnode = sender;
// [_archive clearLastError];
// data = [_archive contentsOfEntry:i];
// switch([_archive lastError]) {
// case XADNoError:
// case XADPasswordError:
// if(!_needsPassword) [self archiveNeedsPassword:_archive];
// break;
// default:
// [self _threaded_setError:[NSError PG_errorWithDomain:PGNodeErrorDomain code:PGGenericError localizedDescription:[NSString stringWithFormat:NSLocalizedString(@"The error “%@” occurred while parsing the archive.", @"XADMaster error reporting. %@ is replaced with the XADMaster error."), [_archive describeLastError]] userInfo:nil] forNode:_currentSubnode];
// break;
// }
// _currentSubnode = nil;
// if(neededPassword && !_needsPassword) [[PGArchiveAdapter PG_performOn:self allowOnce:YES withStorage:PGArchiveAdapterList] performSelectorOnMainThread:@selector(_updateThumbnailsOfChildren) withObject:nil waitUntilDone:NO];
// }
// if(outData) *outData = data;
// return YES;
//}
@end
@implementation PGDataProvider(PGArchiveDataProvider)
- (XADArchive *)archive
{
return nil;
}
- (int)entry
{
return 0;
}
@end
@implementation PGArchiveDataProvider
#pragma mark -PGArchiveDataProvider
- (id)initWithArchive:(XADArchive *)archive entry:(int)entry;
{
if((self = [super init])) {
_archive = [archive retain];
_entry = entry;
}
return self;
}
#pragma mark -PGDataProvider
- (NSData *)data
{
@synchronized(_archive) {
return [_archive contentsOfEntry:_entry]; // TODO: Handle password issues.
}
return nil;
}
- (NSDate *)dateCreated
{
@synchronized(_archive) {
return [[_archive attributesOfEntry:_entry] objectForKey:XADCreationDateKey];
}
return nil;
}
#pragma mark -
- (OSType)typeCode
{
@synchronized(_archive) {
return [_archive PG_OSTypeForEntry:_entry];
}
return 0;
}
- (NSString *)extension
{
@synchronized(_archive) {
return [[[_archive nameOfEntry:_entry] pathExtension] lowercaseString];
}
return nil;
}
#pragma mark -
- (NSData *)fourCCData
{
return nil; // Too slow.
}
- (NSNumber *)dataLength
{
@synchronized(_archive) {
return [NSNumber numberWithLongLong:[_archive representativeSizeOfEntry:_entry]];
}
return nil;
}
#pragma mark -PGDataProvider(PGArchiveDataProvider)
- (XADArchive *)archive
{
return _archive;
}
- (int)entry
{
return _entry;
}
#pragma mark -PGDataProvider(PGResourceAdapterLoading)
- (NSArray *)adapterClassesForNode:(PGNode *)node
{
@synchronized(_archive) {
if([_archive entryIsArchive:_entry]) return [NSArray arrayWithObject:[PGArchiveAdapter class]];
}
return [super adapterClassesForNode:node];
}
#pragma mark -NSObject
- (void)dealloc
{
[_archive release];
[super dealloc];
}
@end
@implementation PGFolderDataProvider
#pragma mark -PGDataProvider
- (OSType)typeCode
{
return 'fold';
}
#pragma mark -PGDataProvider(PGResourceAdapterLoading)
- (NSArray *)adapterClassesForNode:(PGNode *)node
{
return [NSArray arrayWithObject:[PGContainerAdapter class]];
}
@end
@implementation XADArchive(PGAdditions)
- (BOOL)PG_entryIsInvisibleForName:(NSString *)name
{
if([name hasPrefix:@"."]) return YES;
if(NSNotFound != [name rangeOfString:@"/."].location) return YES;
return NO;
}
- (NSString *)PG_commonRootPath
{
NSInteger i;
NSString *root = nil;
for(i = 0; i < [self numberOfEntries]; i++) {
NSString *entryName = [self nameOfEntry:i];
if([self PG_entryIsInvisibleForName:entryName]) continue;
if(![self entryIsDirectory:i]) entryName = [entryName stringByDeletingLastPathComponent];
else if([entryName hasSuffix:@"/"]) entryName = [entryName substringToIndex:[entryName length] - 1];
if(!root) root = entryName;
else while(!PGEqualObjects(root, entryName)) {
if([root length] > [entryName length]) root = [root stringByDeletingLastPathComponent];
else entryName = [entryName stringByDeletingLastPathComponent];
}
}
return root ? root : @"";
}
- (OSType)PG_OSTypeForEntry:(int)entry
{
return [self entryIsDirectory:entry] ? 'fold' : [[[self attributesOfEntry:entry] objectForKey:NSFileHFSTypeCode] unsignedLongValue];
}
@end