-
Notifications
You must be signed in to change notification settings - Fork 1
/
DirectoryScannerBlocking.m
348 lines (299 loc) · 11.6 KB
/
DirectoryScannerBlocking.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
#import "DirectoryScannerBlocking.h"
#pragma mark Directory Scanning Junk
NSString *resolveAlias(NSString *path, Boolean *folder);
int inspectAndAddFile(NSString *filePath, NSMutableArray *fullArray, bool includeInvisible, CFStringRef type);
void scanPath(NSString*path, NSMutableArray*fullArray, BOOL follow, int depth, bool includeInvisible, CFStringRef type)
{
if(depth < 0)
return;
NSString *pname;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// force trailing slash
if([path characterAtIndex:[path length]-1] != '/')
path = [NSString stringWithFormat:@"%@/",path];
if(!includeInvisible)
{
// if the folder is invisible, don't traverse its contents.
FSRef fileRef;
CFDictionaryRef values = NULL;
CFStringRef attrs[] = { kLSItemIsInvisible };
CFArrayRef attrNames = CFArrayCreate(NULL, (const void**)attrs, 1, NULL);
if (FSPathMakeRef((const UInt8 *)[path fileSystemRepresentation], &fileRef, nil) == noErr)
if( LSCopyItemAttributes(&fileRef, kLSRolesViewer, attrNames, &values) == noErr)
if(values != NULL)
{
if(CFDictionaryGetValue(values, kLSItemIsInvisible) == kCFBooleanTrue)
{
CFRelease(values);
CFRelease(attrNames);
[pool drain];
return;
}
CFRelease(values);
}
CFRelease(attrNames);
}
NSFileManager *defaultManager = [NSFileManager defaultManager];
NSDirectoryEnumerator *direnum = [defaultManager enumeratorAtPath:path];
if(direnum == nil)
{
// this happens if the supplied path is an alias...
//NSLog(@"direnum failed for %@\n",path);
Boolean folder = FALSE;
// if file is an alias, resolve it, otherwise inspect it.
NSString *destination;
if( (destination = resolveAlias(path, &folder)) != nil )
{
//NSLog(@" * Alias resolved to %@ which %s a folder",destination,folder?"is":"is not");
if(folder && follow)
scanPath(destination, fullArray, follow, depth, includeInvisible, type);
else // not a folder, inspect file
inspectAndAddFile(destination, fullArray, includeInvisible, type);
}
else
inspectAndAddFile(path, fullArray, includeInvisible, type);
}
while(pname = [direnum nextObject])
{
//NSLog(@"%@%@",path,pname);
NSString *dirFileType = [[direnum fileAttributes] fileType];
if( dirFileType == NSFileTypeDirectory)
{
//if(recursive == FALSE)
// we trap directories here, because we need to limit depth.
[direnum skipDescendents];
scanPath([NSString stringWithFormat:@"%@/%@", path, pname], fullArray, follow, depth-1, includeInvisible, type);
inspectAndAddFile([NSString stringWithFormat:@"%@/%@",path,pname], fullArray, includeInvisible, type);
}
else if( dirFileType == NSFileTypeSymbolicLink)
{
// if symlink is a file, use it. if it's a directory, follow it if we're in recursive mode
NSDirectoryEnumerator *symenum = [defaultManager enumeratorAtPath:[NSString stringWithFormat:@"%@%@",path,pname] ];
NSString *symFileType = [[symenum directoryAttributes] fileType];
if( symFileType == NSFileTypeDirectory && follow)
{
//NSLog(@" * symlink is a directory... going to walk %@ %@\n",pname, path);
scanPath([NSString stringWithFormat:@"%@/%@",path,pname], fullArray, follow, depth-1, includeInvisible, type);
}
else if( symFileType == NSFileTypeRegular)
{
//NSLog(@" * symlink is a file... going to inspect\n");
inspectAndAddFile([NSString stringWithFormat:@"%@/%@",path,pname],fullArray, includeInvisible, type);
}
}
else if( dirFileType == NSFileTypeRegular)
{
Boolean folder = FALSE;
// if file is an alias, resolve it, otherwise inspect it.
NSString *destination;
if( (destination = resolveAlias([NSString stringWithFormat:@"%@/%@",path, pname],&folder)) != nil )
{
//NSLog(@" * Alias resolved to %@ which %s a folder",destination,folder?"is":"is not");
if(folder && follow)
scanPath(destination, fullArray, follow, depth-1, includeInvisible, type);
else // not a folder, inspect file
inspectAndAddFile(destination, fullArray, includeInvisible, type);
}
else
inspectAndAddFile([NSString stringWithFormat:@"%@/%@",path, pname], fullArray, includeInvisible, type);
}
// other types that we ignore here: Sockets, Char Specials, Block Specials, and Unknowns
}
[pool drain];
}
int inspectAndAddFile(NSString *filePath, NSMutableArray *fullArray, bool includeInvisible, CFStringRef type)
{
CFStringRef itemUTI = NULL;
FSRef fileRef;
Boolean isDirectory;
BOOL metadataMatch = FALSE; // did metadata match?
bool visible = YES;
if(filePath == nil)
return 0;
//NSLog(@" * inspectAndAddFile: %@ (%@)\n",filePath,[filePath pathExtension]);
itemUTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension,(CFStringRef)[filePath pathExtension],NULL);
NSString *lsUTI=nil, *lsType=nil, *lsCreator=nil, *lsKind=nil;
if (FSPathMakeRef((const UInt8 *)[filePath fileSystemRepresentation], &fileRef, &isDirectory) == noErr)
{
CFDictionaryRef values = NULL;
CFStringRef attrs[] = { kLSItemContentType,kLSItemFileType,kLSItemFileCreator,kLSItemDisplayKind,kLSItemIsInvisible };
CFArrayRef attrNames = CFArrayCreate(NULL, (const void**)attrs, 5, NULL);
if( LSCopyItemAttributes(&fileRef, kLSRolesViewer, attrNames, &values) == noErr)
{
if(values != NULL)
{
if(!includeInvisible)
{
if(CFDictionaryGetValue(values, kLSItemIsInvisible) == kCFBooleanTrue)
visible=NO;
}
CFTypeRef uti = CFDictionaryGetValue(values, kLSItemContentType);
if(uti != NULL)
{
lsUTI = [(NSString *)uti retain];
if(UTTypeConformsTo(uti, type))
metadataMatch = TRUE;
}
lsType = [(NSString*)CFDictionaryGetValue(values, kLSItemFileType) retain];
lsCreator = [(NSString*)CFDictionaryGetValue(values, kLSItemFileCreator) retain];
lsKind = [(NSString*)CFDictionaryGetValue(values, kLSItemDisplayKind) retain];
CFRelease(values);
}
}
CFRelease(attrNames);
}
//NSLog(@" * Obtained UTI: %@ (meta match: %i)\n",itemUTI,metadataMatch);
int fileAdded=0;
if( visible && (
metadataMatch == TRUE ||
UTTypeConformsTo(itemUTI, type) == TRUE ||
// .dv's aren't recognized as movies on Tiger (fixed on Leopard, so this check doesn't hurt)
(type == kUTTypeMovie && [[filePath pathExtension] caseInsensitiveCompare:@"dv"] == NSOrderedSame )
))
{
//NSLog(@" * Adding item (%@)...\n",filePath);
NSMutableDictionary *fileDict = [[NSMutableDictionary alloc] init];
[fileDict
setValue:[NSString stringWithFormat:@"%@",[filePath lastPathComponent]]
forKey:@"name"];
[fileDict
setValue:[NSString stringWithFormat:@"%@",filePath]
forKey:@"path"];
NSURL *fileURL = [NSURL fileURLWithPath:[fileDict objectForKey:@"path"]];
[fileDict
setObject:[fileURL absoluteString]
forKey:@"url"];
[fileDict setObject:(lsUTI?lsUTI:(NSString*)itemUTI) forKey:@"uti"];
if(lsType)
[fileDict setObject:lsType forKey:@"type"];
if(lsCreator)
[fileDict setObject:lsCreator forKey:@"creator"];
if(lsKind)
[fileDict setObject:lsKind forKey:@"kind"];
NSDictionary *attrs = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
[fileDict setObject:[attrs objectForKey:NSFileSize] forKey:@"size"];
[fileDict setObject:[NSNumber numberWithInteger:[[attrs fileModificationDate] timeIntervalSince1970]] forKey:@"modified"];
[fileDict setObject:[NSNumber numberWithInteger:[[attrs fileCreationDate] timeIntervalSince1970]] forKey:@"created"];
[fullArray addObject:fileDict];
[fileDict release];
fileAdded=1;
}
[lsUTI release];
[lsType release];
[lsCreator release];
[lsKind release];
CFRelease(itemUTI);
return fileAdded;
}
NSString *resolveAlias(NSString *filePath, Boolean *folder)
{
unsigned char pathBuffer[4096];
FSRef fsRef;
if([filePath length] == 0)
return nil;
//NSLog(@"resolveAlias: %@",filePath);
if( FSPathMakeRef((const UInt8*)[filePath UTF8String], &fsRef,NO) == noErr )
{
Boolean isAlias = FALSE;
if( FSResolveAliasFile(&fsRef, TRUE, folder, &isAlias) == noErr && isAlias)
{
FSRefMakePath(&fsRef, pathBuffer, 4096);
return [NSString stringWithUTF8String:(const char*)pathBuffer];
}
}
else
{
//NSLog(@"FSPathMakeRef failed for %@\n",filePath);
// this fails when mutliple alises are in one path, so we prune and rebuild here
return [NSString stringWithFormat:@"%@/%@",
resolveAlias([filePath stringByDeletingLastPathComponent], folder),
[filePath lastPathComponent]
];
}
return nil;
}
@implementation DirectoryScannerBlocking : QCPatch
#pragma mark QCPatch methods
+ (QCPatchExecutionMode)executionModeWithIdentifier:(id)fp8
{
return kQCPatchExecutionModeProvider;
}
+ (BOOL)allowsSubpatchesWithIdentifier:(id)fp8
{
return NO;
}
+ (QCPatchTimeMode)timeModeWithIdentifier:(id)fp8
{
return kQCPatchTimeModeNone;
}
- (id)initWithIdentifier:(id)fp8
{
if(self = [super initWithIdentifier:fp8])
{
[inputPath setStringValue: @"/Library/Desktop Pictures/"];
[inputIncludeInvisible setBooleanValue:YES];
[inputFileTypes setMaxIndexValue: 7];
[[self userInfo] setObject:@"Kineme Directory Scanner (Blocking)" forKey:@"name"];
}
return self;
}
- (BOOL)execute:(QCOpenGLContext *)context time:(double)time arguments:(NSDictionary *)arguments
{
NSString *inputPathString = KIExpandPath(self,[inputPath stringValue]);
// NSLog(@"scanning @ %f", time);
if([inputPathString length] == 0)
return YES;
if([inputPath wasUpdated] || ([inputScanSignal wasUpdated] && [inputScanSignal booleanValue]) ||
[inputFileTypes wasUpdated] || [inputFollowLinks wasUpdated] || [inputMaxDepth wasUpdated]
|| [inputIncludeInvisible wasUpdated]
|| [inputFileTypeCustom wasUpdated]
)
{
// scan!
NSMutableArray *fullArray = [[NSMutableArray alloc] init];
NSUInteger depth = [inputMaxDepth indexValue];
bool includeInvisible = [inputIncludeInvisible booleanValue];
BOOL followLinks = [inputFollowLinks booleanValue];
NSString *type = [inputFileTypeCustom stringValue];
if([type length])
scanPath(inputPathString, fullArray, followLinks, depth, includeInvisible, (CFStringRef)type);
else
switch([inputFileTypes indexValue])
{
default:
case 0: // images only
scanPath(inputPathString, fullArray, followLinks, depth, includeInvisible, kUTTypeImage);
break;
case 1: // movies only
scanPath(inputPathString, fullArray, followLinks, depth, includeInvisible, kUTTypeMovie);
break;
case 2: // movies and images
scanPath(inputPathString, fullArray, followLinks, depth, includeInvisible, kUTTypeImage);
scanPath(inputPathString, fullArray, followLinks, depth, includeInvisible, kUTTypeMovie);
break;
case 3: // Audio
scanPath(inputPathString, fullArray, followLinks, depth, includeInvisible, kUTTypeAudio);
break;
case 4: // Folders
scanPath(inputPathString, fullArray, followLinks, depth, includeInvisible, kUTTypeDirectory);
break;
case 5: // anything
scanPath(inputPathString, fullArray, followLinks, depth, includeInvisible, kUTTypeData);
break;
case 6: // Compositions
scanPath(inputPathString, fullArray, followLinks, depth, includeInvisible, (CFStringRef)@"com.apple.quartz-composer-composition");
break;
case 7: // compositions, images, and movies
scanPath(inputPathString, fullArray, followLinks, depth, includeInvisible, (CFStringRef)@"com.apple.quartz-composer-composition");
scanPath(inputPathString, fullArray, followLinks, depth, includeInvisible, kUTTypeImage);
scanPath(inputPathString, fullArray, followLinks, depth, includeInvisible, kUTTypeMovie);
break;
}
QCStructure *fullStruct = [[QCStructure alloc] initWithArray: fullArray];
[fullArray release];
[outputFileList setStructureValue: fullStruct];
[fullStruct release];
}
return YES;
}
@end